PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/rt/uthash/uthash.h

http://github.com/jruderman/rust
C Header | 766 lines | 599 code | 52 blank | 115 comment | 99 complexity | f2b3e43b1c3a339950b5b2f7dab0ef35 MD5 | raw file
  1. /*
  2. Copyright (c) 2003-2009, Troy D. Hanson http://uthash.sourceforge.net
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  10. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  11. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  12. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  16. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. */
  20. #ifndef UTHASH_H
  21. #define UTHASH_H
  22. #include <string.h> /* memcmp,strlen */
  23. #include <stddef.h> /* ptrdiff_t */
  24. #include <inttypes.h> /* uint32_t etc */
  25. #define UTHASH_VERSION 1.6
  26. /* C++ requires extra stringent casting */
  27. #if defined __cplusplus
  28. #define TYPEOF(x) (typeof(x))
  29. #else
  30. #define TYPEOF(x)
  31. #endif
  32. #define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */
  33. #define uthash_bkt_malloc(sz) malloc(sz) /* malloc fcn for UT_hash_bucket's */
  34. #define uthash_bkt_free(ptr) free(ptr) /* free fcn for UT_hash_bucket's */
  35. #define uthash_tbl_malloc(sz) malloc(sz) /* malloc fcn for UT_hash_table */
  36. #define uthash_tbl_free(ptr) free(ptr) /* free fcn for UT_hash_table */
  37. #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */
  38. #define uthash_expand_fyi(tbl) /* can be defined to log expands */
  39. /* initial number of buckets */
  40. #define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */
  41. #define HASH_INITIAL_NUM_BUCKETS_LOG2 5 /* lg2 of initial number of buckets */
  42. #define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches */
  43. /* calculate the element whose hash handle address is hhe */
  44. #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)hhp) - (tbl)->hho))
  45. #define HASH_FIND(hh,head,keyptr,keylen,out) \
  46. do { \
  47. unsigned _hf_bkt,_hf_hashv; \
  48. out=TYPEOF(out)head; \
  49. if (head) { \
  50. HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \
  51. HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], \
  52. keyptr,keylen,out); \
  53. } \
  54. } while (0)
  55. #define HASH_MAKE_TABLE(hh,head) \
  56. do { \
  57. (head)->hh.tbl = (UT_hash_table*)uthash_tbl_malloc( \
  58. sizeof(UT_hash_table)); \
  59. if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \
  60. memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \
  61. (head)->hh.tbl->tail = &((head)->hh); \
  62. (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
  63. (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \
  64. (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \
  65. (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_bkt_malloc( \
  66. HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
  67. if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \
  68. memset((head)->hh.tbl->buckets, 0, \
  69. HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
  70. } while(0)
  71. #define HASH_ADD(hh,head,fieldname,keylen_in,add) \
  72. HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add)
  73. #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \
  74. do { \
  75. unsigned _ha_bkt; \
  76. (add)->hh.next = NULL; \
  77. (add)->hh.key = (char*)keyptr; \
  78. (add)->hh.keylen = keylen_in; \
  79. if (!(head)) { \
  80. head = (add); \
  81. (head)->hh.prev = NULL; \
  82. HASH_MAKE_TABLE(hh,head); \
  83. } else { \
  84. (head)->hh.tbl->tail->next = (add); \
  85. (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \
  86. (head)->hh.tbl->tail = &((add)->hh); \
  87. } \
  88. (head)->hh.tbl->num_items++; \
  89. (add)->hh.tbl = (head)->hh.tbl; \
  90. HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets, \
  91. (add)->hh.hashv, _ha_bkt); \
  92. HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt],&(add)->hh); \
  93. HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \
  94. HASH_FSCK(hh,head); \
  95. } while(0)
  96. #define HASH_TO_BKT( hashv, num_bkts, bkt ) \
  97. do { \
  98. bkt = ((hashv) & ((num_bkts) - 1)); \
  99. } while(0)
  100. /* delete "delptr" from the hash table.
  101. * "the usual" patch-up process for the app-order doubly-linked-list.
  102. * The use of _hd_hh_del below deserves special explanation.
  103. * These used to be expressed using (delptr) but that led to a bug
  104. * if someone used the same symbol for the head and deletee, like
  105. * HASH_DELETE(hh,users,users);
  106. * We want that to work, but by changing the head (users) below
  107. * we were forfeiting our ability to further refer to the deletee (users)
  108. * in the patch-up process. Solution: use scratch space in the table to
  109. * copy the deletee pointer, then the latter references are via that
  110. * scratch pointer rather than through the repointed (users) symbol.
  111. */
  112. #define HASH_DELETE(hh,head,delptr) \
  113. do { \
  114. unsigned _hd_bkt; \
  115. struct UT_hash_handle *_hd_hh_del; \
  116. if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \
  117. uthash_bkt_free((head)->hh.tbl->buckets ); \
  118. uthash_tbl_free((head)->hh.tbl); \
  119. head = NULL; \
  120. } else { \
  121. _hd_hh_del = &((delptr)->hh); \
  122. if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \
  123. (head)->hh.tbl->tail = \
  124. (UT_hash_handle*)((char*)((delptr)->hh.prev) + \
  125. (head)->hh.tbl->hho); \
  126. } \
  127. if ((delptr)->hh.prev) { \
  128. ((UT_hash_handle*)((char*)((delptr)->hh.prev) + \
  129. (head)->hh.tbl->hho))->next = (delptr)->hh.next; \
  130. } else { \
  131. head = TYPEOF(head)((delptr)->hh.next); \
  132. } \
  133. if (_hd_hh_del->next) { \
  134. ((UT_hash_handle*)((char*)_hd_hh_del->next + \
  135. (head)->hh.tbl->hho))->prev = \
  136. _hd_hh_del->prev; \
  137. } \
  138. HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
  139. HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \
  140. (head)->hh.tbl->num_items--; \
  141. } \
  142. HASH_FSCK(hh,head); \
  143. } while (0)
  144. /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */
  145. #define HASH_FIND_STR(head,findstr,out) \
  146. HASH_FIND(hh,head,findstr,strlen(findstr),out)
  147. #define HASH_ADD_STR(head,strfield,add) \
  148. HASH_ADD(hh,head,strfield,strlen(add->strfield),add)
  149. #define HASH_FIND_INT(head,findint,out) \
  150. HASH_FIND(hh,head,findint,sizeof(int),out)
  151. #define HASH_ADD_INT(head,intfield,add) \
  152. HASH_ADD(hh,head,intfield,sizeof(int),add)
  153. #define HASH_DEL(head,delptr) \
  154. HASH_DELETE(hh,head,delptr)
  155. /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined.
  156. * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined.
  157. */
  158. #ifdef HASH_DEBUG
  159. #define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0)
  160. #define HASH_FSCK(hh,head) \
  161. do { \
  162. unsigned _bkt_i; \
  163. unsigned _count, _bkt_count; \
  164. char *_prev; \
  165. struct UT_hash_handle *_thh; \
  166. if (head) { \
  167. _count = 0; \
  168. for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \
  169. _bkt_count = 0; \
  170. _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \
  171. _prev = NULL; \
  172. while (_thh) { \
  173. if (_prev != (char*)(_thh->hh_prev)) { \
  174. HASH_OOPS("invalid hh_prev %p, actual %p\n", \
  175. _thh->hh_prev, _prev ); \
  176. } \
  177. _bkt_count++; \
  178. _prev = (char*)(_thh); \
  179. _thh = _thh->hh_next; \
  180. } \
  181. _count += _bkt_count; \
  182. if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \
  183. HASH_OOPS("invalid bucket count %d, actual %d\n", \
  184. (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \
  185. } \
  186. } \
  187. if (_count != (head)->hh.tbl->num_items) { \
  188. HASH_OOPS("invalid hh item count %d, actual %d\n", \
  189. (head)->hh.tbl->num_items, _count ); \
  190. } \
  191. /* traverse hh in app order; check next/prev integrity, count */ \
  192. _count = 0; \
  193. _prev = NULL; \
  194. _thh = &(head)->hh; \
  195. while (_thh) { \
  196. _count++; \
  197. if (_prev !=(char*)(_thh->prev)) { \
  198. HASH_OOPS("invalid prev %p, actual %p\n", \
  199. _thh->prev, _prev ); \
  200. } \
  201. _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \
  202. _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \
  203. (head)->hh.tbl->hho) : NULL ); \
  204. } \
  205. if (_count != (head)->hh.tbl->num_items) { \
  206. HASH_OOPS("invalid app item count %d, actual %d\n", \
  207. (head)->hh.tbl->num_items, _count ); \
  208. } \
  209. } \
  210. } while (0)
  211. #else
  212. #define HASH_FSCK(hh,head)
  213. #endif
  214. /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to
  215. * the descriptor to which this macro is defined for tuning the hash function.
  216. * The app can #include <unistd.h> to get the prototype for write(2). */
  217. #ifdef HASH_EMIT_KEYS
  218. #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \
  219. do { \
  220. unsigned _klen = fieldlen; \
  221. write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \
  222. write(HASH_EMIT_KEYS, keyptr, fieldlen); \
  223. } while (0)
  224. #else
  225. #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen)
  226. #endif
  227. /* default to MurmurHash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */
  228. #ifdef HASH_FUNCTION
  229. #define HASH_FCN HASH_FUNCTION
  230. #else
  231. #define HASH_FCN HASH_MUR
  232. #endif
  233. /* The Bernstein hash function, used in Perl prior to v5.6 */
  234. #define HASH_BER(key,keylen,num_bkts,hashv,bkt) \
  235. do { \
  236. unsigned _hb_keylen=keylen; \
  237. char *_hb_key=(char*)key; \
  238. (hashv) = 0; \
  239. while (_hb_keylen--) { (hashv) = ((hashv) * 33) + *_hb_key++; } \
  240. bkt = (hashv) & (num_bkts-1); \
  241. } while (0)
  242. /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at
  243. * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */
  244. #define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \
  245. do { \
  246. unsigned _sx_i; \
  247. char *_hs_key=(char*)key; \
  248. hashv = 0; \
  249. for(_sx_i=0; _sx_i < keylen; _sx_i++) \
  250. hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \
  251. bkt = hashv & (num_bkts-1); \
  252. } while (0)
  253. #define HASH_FNV(key,keylen,num_bkts,hashv,bkt) \
  254. do { \
  255. unsigned _fn_i; \
  256. char *_hf_key=(char*)key; \
  257. hashv = 2166136261UL; \
  258. for(_fn_i=0; _fn_i < keylen; _fn_i++) \
  259. hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \
  260. bkt = hashv & (num_bkts-1); \
  261. } while(0);
  262. #define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \
  263. do { \
  264. unsigned _ho_i; \
  265. char *_ho_key=(char*)key; \
  266. hashv = 0; \
  267. for(_ho_i=0; _ho_i < keylen; _ho_i++) { \
  268. hashv += _ho_key[_ho_i]; \
  269. hashv += (hashv << 10); \
  270. hashv ^= (hashv >> 6); \
  271. } \
  272. hashv += (hashv << 3); \
  273. hashv ^= (hashv >> 11); \
  274. hashv += (hashv << 15); \
  275. bkt = hashv & (num_bkts-1); \
  276. } while(0)
  277. #define HASH_JEN_MIX(a,b,c) \
  278. do { \
  279. a -= b; a -= c; a ^= ( c >> 13 ); \
  280. b -= c; b -= a; b ^= ( a << 8 ); \
  281. c -= a; c -= b; c ^= ( b >> 13 ); \
  282. a -= b; a -= c; a ^= ( c >> 12 ); \
  283. b -= c; b -= a; b ^= ( a << 16 ); \
  284. c -= a; c -= b; c ^= ( b >> 5 ); \
  285. a -= b; a -= c; a ^= ( c >> 3 ); \
  286. b -= c; b -= a; b ^= ( a << 10 ); \
  287. c -= a; c -= b; c ^= ( b >> 15 ); \
  288. } while (0)
  289. #define HASH_JEN(key,keylen,num_bkts,hashv,bkt) \
  290. do { \
  291. unsigned _hj_i,_hj_j,_hj_k; \
  292. char *_hj_key=(char*)key; \
  293. hashv = 0xfeedbeef; \
  294. _hj_i = _hj_j = 0x9e3779b9; \
  295. _hj_k = keylen; \
  296. while (_hj_k >= 12) { \
  297. _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \
  298. + ( (unsigned)_hj_key[2] << 16 ) \
  299. + ( (unsigned)_hj_key[3] << 24 ) ); \
  300. _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \
  301. + ( (unsigned)_hj_key[6] << 16 ) \
  302. + ( (unsigned)_hj_key[7] << 24 ) ); \
  303. hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \
  304. + ( (unsigned)_hj_key[10] << 16 ) \
  305. + ( (unsigned)_hj_key[11] << 24 ) ); \
  306. \
  307. HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
  308. \
  309. _hj_key += 12; \
  310. _hj_k -= 12; \
  311. } \
  312. hashv += keylen; \
  313. switch ( _hj_k ) { \
  314. case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \
  315. case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \
  316. case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \
  317. case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \
  318. case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \
  319. case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \
  320. case 5: _hj_j += _hj_key[4]; \
  321. case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \
  322. case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \
  323. case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \
  324. case 1: _hj_i += _hj_key[0]; \
  325. } \
  326. HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
  327. bkt = hashv & (num_bkts-1); \
  328. } while(0)
  329. /* The Paul Hsieh hash function */
  330. #undef get16bits
  331. #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
  332. || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
  333. #define get16bits(d) (*((const uint16_t *) (d)))
  334. #endif
  335. #if !defined (get16bits)
  336. #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
  337. +(uint32_t)(((const uint8_t *)(d))[0]) )
  338. #endif
  339. #define HASH_SFH(key,keylen,num_bkts,hashv,bkt) \
  340. do { \
  341. char *_sfh_key=(char*)key; \
  342. hashv = 0xcafebabe; \
  343. uint32_t _sfh_tmp, _sfh_len = keylen; \
  344. \
  345. int _sfh_rem = _sfh_len & 3; \
  346. _sfh_len >>= 2; \
  347. \
  348. /* Main loop */ \
  349. for (;_sfh_len > 0; _sfh_len--) { \
  350. hashv += get16bits (_sfh_key); \
  351. _sfh_tmp = (get16bits (_sfh_key+2) << 11) ^ hashv; \
  352. hashv = (hashv << 16) ^ _sfh_tmp; \
  353. _sfh_key += 2*sizeof (uint16_t); \
  354. hashv += hashv >> 11; \
  355. } \
  356. \
  357. /* Handle end cases */ \
  358. switch (_sfh_rem) { \
  359. case 3: hashv += get16bits (_sfh_key); \
  360. hashv ^= hashv << 16; \
  361. hashv ^= _sfh_key[sizeof (uint16_t)] << 18; \
  362. hashv += hashv >> 11; \
  363. break; \
  364. case 2: hashv += get16bits (_sfh_key); \
  365. hashv ^= hashv << 11; \
  366. hashv += hashv >> 17; \
  367. break; \
  368. case 1: hashv += *_sfh_key; \
  369. hashv ^= hashv << 10; \
  370. hashv += hashv >> 1; \
  371. } \
  372. \
  373. /* Force "avalanching" of final 127 bits */ \
  374. hashv ^= hashv << 3; \
  375. hashv += hashv >> 5; \
  376. hashv ^= hashv << 4; \
  377. hashv += hashv >> 17; \
  378. hashv ^= hashv << 25; \
  379. hashv += hashv >> 6; \
  380. bkt = hashv & (num_bkts-1); \
  381. } while(0);
  382. /* Austin Appleby's MurmurHash */
  383. #define HASH_MUR(key,keylen,num_bkts,hashv,bkt) \
  384. do { \
  385. const unsigned int _mur_m = 0x5bd1e995; \
  386. const int _mur_r = 24; \
  387. hashv = 0xcafebabe ^ keylen; \
  388. char *_mur_key = (char *)key; \
  389. uint32_t _mur_tmp, _mur_len = keylen; \
  390. \
  391. for (;_mur_len >= 4; _mur_len-=4) { \
  392. _mur_tmp = *(uint32_t *)_mur_key; \
  393. _mur_tmp *= _mur_m; \
  394. _mur_tmp ^= _mur_tmp >> _mur_r; \
  395. _mur_tmp *= _mur_m; \
  396. hashv *= _mur_m; \
  397. hashv ^= _mur_tmp; \
  398. _mur_key += 4; \
  399. } \
  400. \
  401. switch(_mur_len) \
  402. { \
  403. case 3: hashv ^= _mur_key[2] << 16; \
  404. case 2: hashv ^= _mur_key[1] << 8; \
  405. case 1: hashv ^= _mur_key[0]; \
  406. hashv *= _mur_m; \
  407. }; \
  408. \
  409. hashv ^= hashv >> 13; \
  410. hashv *= _mur_m; \
  411. hashv ^= hashv >> 15; \
  412. \
  413. bkt = hashv & (num_bkts-1); \
  414. } while(0)
  415. /* key comparison function; return 0 if keys equal */
  416. #define HASH_KEYCMP(a,b,len) memcmp(a,b,len)
  417. /* iterate over items in a known bucket to find desired item */
  418. #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \
  419. out = TYPEOF(out)((head.hh_head) ? ELMT_FROM_HH(tbl,head.hh_head) : NULL); \
  420. while (out) { \
  421. if (out->hh.keylen == keylen_in) { \
  422. if ((HASH_KEYCMP(out->hh.key,keyptr,keylen_in)) == 0) break; \
  423. } \
  424. out= TYPEOF(out)((out->hh.hh_next) ? \
  425. ELMT_FROM_HH(tbl,out->hh.hh_next) : NULL); \
  426. }
  427. /* add an item to a bucket */
  428. #define HASH_ADD_TO_BKT(head,addhh) \
  429. do { \
  430. head.count++; \
  431. (addhh)->hh_next = head.hh_head; \
  432. (addhh)->hh_prev = NULL; \
  433. if (head.hh_head) { (head).hh_head->hh_prev = (addhh); } \
  434. (head).hh_head=addhh; \
  435. if (head.count >= ((head.expand_mult+1) * HASH_BKT_CAPACITY_THRESH) \
  436. && (addhh)->tbl->noexpand != 1) { \
  437. HASH_EXPAND_BUCKETS((addhh)->tbl); \
  438. } \
  439. } while(0)
  440. /* remove an item from a given bucket */
  441. #define HASH_DEL_IN_BKT(hh,head,hh_del) \
  442. (head).count--; \
  443. if ((head).hh_head == hh_del) { \
  444. (head).hh_head = hh_del->hh_next; \
  445. } \
  446. if (hh_del->hh_prev) { \
  447. hh_del->hh_prev->hh_next = hh_del->hh_next; \
  448. } \
  449. if (hh_del->hh_next) { \
  450. hh_del->hh_next->hh_prev = hh_del->hh_prev; \
  451. }
  452. /* Bucket expansion has the effect of doubling the number of buckets
  453. * and redistributing the items into the new buckets. Ideally the
  454. * items will distribute more or less evenly into the new buckets
  455. * (the extent to which this is true is a measure of the quality of
  456. * the hash function as it applies to the key domain).
  457. *
  458. * With the items distributed into more buckets, the chain length
  459. * (item count) in each bucket is reduced. Thus by expanding buckets
  460. * the hash keeps a bound on the chain length. This bounded chain
  461. * length is the essence of how a hash provides constant time lookup.
  462. *
  463. * The calculation of tbl->ideal_chain_maxlen below deserves some
  464. * explanation. First, keep in mind that we're calculating the ideal
  465. * maximum chain length based on the *new* (doubled) bucket count.
  466. * In fractions this is just n/b (n=number of items,b=new num buckets).
  467. * Since the ideal chain length is an integer, we want to calculate
  468. * ceil(n/b). We don't depend on floating point arithmetic in this
  469. * hash, so to calculate ceil(n/b) with integers we could write
  470. *
  471. * ceil(n/b) = (n/b) + ((n%b)?1:0)
  472. *
  473. * and in fact a previous version of this hash did just that.
  474. * But now we have improved things a bit by recognizing that b is
  475. * always a power of two. We keep its base 2 log handy (call it lb),
  476. * so now we can write this with a bit shift and logical AND:
  477. *
  478. * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0)
  479. *
  480. */
  481. #define HASH_EXPAND_BUCKETS(tbl) \
  482. do { \
  483. unsigned _he_bkt; \
  484. unsigned _he_bkt_i; \
  485. struct UT_hash_handle *_he_thh, *_he_hh_nxt; \
  486. UT_hash_bucket *_he_new_buckets, *_he_newbkt; \
  487. _he_new_buckets = (UT_hash_bucket*)uthash_bkt_malloc( \
  488. 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
  489. if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \
  490. memset(_he_new_buckets, 0, \
  491. 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
  492. tbl->ideal_chain_maxlen = \
  493. (tbl->num_items >> (tbl->log2_num_buckets+1)) + \
  494. ((tbl->num_items & ((tbl->num_buckets*2)-1)) ? 1 : 0); \
  495. tbl->nonideal_items = 0; \
  496. for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \
  497. { \
  498. _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \
  499. while (_he_thh) { \
  500. _he_hh_nxt = _he_thh->hh_next; \
  501. HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2, _he_bkt); \
  502. _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \
  503. if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \
  504. tbl->nonideal_items++; \
  505. _he_newbkt->expand_mult = _he_newbkt->count / \
  506. tbl->ideal_chain_maxlen; \
  507. } \
  508. _he_thh->hh_prev = NULL; \
  509. _he_thh->hh_next = _he_newbkt->hh_head; \
  510. if (_he_newbkt->hh_head) _he_newbkt->hh_head->hh_prev = \
  511. _he_thh; \
  512. _he_newbkt->hh_head = _he_thh; \
  513. _he_thh = _he_hh_nxt; \
  514. } \
  515. } \
  516. tbl->num_buckets *= 2; \
  517. tbl->log2_num_buckets++; \
  518. uthash_bkt_free( tbl->buckets ); \
  519. tbl->buckets = _he_new_buckets; \
  520. tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \
  521. (tbl->ineff_expands+1) : 0; \
  522. if (tbl->ineff_expands > 1) { \
  523. tbl->noexpand=1; \
  524. uthash_noexpand_fyi(tbl); \
  525. } \
  526. uthash_expand_fyi(tbl); \
  527. } while(0)
  528. /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */
  529. /* Note that HASH_SORT assumes the hash handle name to be hh.
  530. * HASH_SRT was added to allow the hash handle name to be passed in. */
  531. #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn)
  532. #define HASH_SRT(hh,head,cmpfcn) \
  533. do { \
  534. unsigned _hs_i; \
  535. unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \
  536. struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \
  537. if (head) { \
  538. _hs_insize = 1; \
  539. _hs_looping = 1; \
  540. _hs_list = &((head)->hh); \
  541. while (_hs_looping) { \
  542. _hs_p = _hs_list; \
  543. _hs_list = NULL; \
  544. _hs_tail = NULL; \
  545. _hs_nmerges = 0; \
  546. while (_hs_p) { \
  547. _hs_nmerges++; \
  548. _hs_q = _hs_p; \
  549. _hs_psize = 0; \
  550. for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \
  551. _hs_psize++; \
  552. _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
  553. ((void*)((char*)(_hs_q->next) + \
  554. (head)->hh.tbl->hho)) : NULL); \
  555. if (! (_hs_q) ) break; \
  556. } \
  557. _hs_qsize = _hs_insize; \
  558. while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q )) { \
  559. if (_hs_psize == 0) { \
  560. _hs_e = _hs_q; \
  561. _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
  562. ((void*)((char*)(_hs_q->next) + \
  563. (head)->hh.tbl->hho)) : NULL); \
  564. _hs_qsize--; \
  565. } else if ( (_hs_qsize == 0) || !(_hs_q) ) { \
  566. _hs_e = _hs_p; \
  567. _hs_p = (UT_hash_handle*)((_hs_p->next) ? \
  568. ((void*)((char*)(_hs_p->next) + \
  569. (head)->hh.tbl->hho)) : NULL); \
  570. _hs_psize--; \
  571. } else if (( \
  572. cmpfcn(TYPEOF(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \
  573. TYPEOF(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \
  574. ) <= 0) { \
  575. _hs_e = _hs_p; \
  576. _hs_p = (UT_hash_handle*)((_hs_p->next) ? \
  577. ((void*)((char*)(_hs_p->next) + \
  578. (head)->hh.tbl->hho)) : NULL); \
  579. _hs_psize--; \
  580. } else { \
  581. _hs_e = _hs_q; \
  582. _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
  583. ((void*)((char*)(_hs_q->next) + \
  584. (head)->hh.tbl->hho)) : NULL); \
  585. _hs_qsize--; \
  586. } \
  587. if ( _hs_tail ) { \
  588. _hs_tail->next = ((_hs_e) ? \
  589. ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \
  590. } else { \
  591. _hs_list = _hs_e; \
  592. } \
  593. _hs_e->prev = ((_hs_tail) ? \
  594. ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \
  595. _hs_tail = _hs_e; \
  596. } \
  597. _hs_p = _hs_q; \
  598. } \
  599. _hs_tail->next = NULL; \
  600. if ( _hs_nmerges <= 1 ) { \
  601. _hs_looping=0; \
  602. (head)->hh.tbl->tail = _hs_tail; \
  603. (head) = TYPEOF(head)ELMT_FROM_HH((head)->hh.tbl, _hs_list); \
  604. } \
  605. _hs_insize *= 2; \
  606. } \
  607. HASH_FSCK(hh,head); \
  608. } \
  609. } while (0)
  610. /* This function selects items from one hash into another hash.
  611. * The end result is that the selected items have dual presence
  612. * in both hashes. There is no copy of the items made; rather
  613. * they are added into the new hash through a secondary hash
  614. * hash handle that must be present in the structure. */
  615. #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \
  616. do { \
  617. unsigned _src_bkt, _dst_bkt; \
  618. void *_last_elt=NULL, *_elt; \
  619. UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \
  620. ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \
  621. if (src) { \
  622. for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \
  623. for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \
  624. _src_hh; \
  625. _src_hh = _src_hh->hh_next) { \
  626. _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \
  627. if (cond(_elt)) { \
  628. _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \
  629. _dst_hh->key = _src_hh->key; \
  630. _dst_hh->keylen = _src_hh->keylen; \
  631. _dst_hh->hashv = _src_hh->hashv; \
  632. _dst_hh->prev = _last_elt; \
  633. _dst_hh->next = NULL; \
  634. if (_last_elt_hh) { _last_elt_hh->next = _elt; } \
  635. if (!dst) { \
  636. dst = TYPEOF(dst)_elt; \
  637. HASH_MAKE_TABLE(hh_dst,dst); \
  638. } else { \
  639. _dst_hh->tbl = (dst)->hh_dst.tbl; \
  640. } \
  641. HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \
  642. HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \
  643. (dst)->hh_dst.tbl->num_items++; \
  644. _last_elt = _elt; \
  645. _last_elt_hh = _dst_hh; \
  646. } \
  647. } \
  648. } \
  649. } \
  650. HASH_FSCK(hh_dst,dst); \
  651. } while (0)
  652. #define HASH_CLEAR(hh,head) \
  653. do { \
  654. if (head) { \
  655. uthash_bkt_free((head)->hh.tbl->buckets ); \
  656. uthash_tbl_free((head)->hh.tbl); \
  657. (head)=NULL; \
  658. } \
  659. } while(0)
  660. /* obtain a count of items in the hash */
  661. #define HASH_COUNT(head) HASH_CNT(hh,head)
  662. #define HASH_CNT(hh,head) (head?(head->hh.tbl->num_items):0)
  663. typedef struct UT_hash_bucket {
  664. struct UT_hash_handle *hh_head;
  665. unsigned count;
  666. /* expand_mult is normally set to 0. In this situation, the max chain length
  667. * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If
  668. * the bucket's chain exceeds this length, bucket expansion is triggered).
  669. * However, setting expand_mult to a non-zero value delays bucket expansion
  670. * (that would be triggered by additions to this particular bucket)
  671. * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH.
  672. * (The multiplier is simply expand_mult+1). The whole idea of this
  673. * multiplier is to reduce bucket expansions, since they are expensive, in
  674. * situations where we know that a particular bucket tends to be overused.
  675. * It is better to let its chain length grow to a longer yet-still-bounded
  676. * value, than to do an O(n) bucket expansion too often.
  677. */
  678. unsigned expand_mult;
  679. } UT_hash_bucket;
  680. typedef struct UT_hash_table {
  681. UT_hash_bucket *buckets;
  682. unsigned num_buckets, log2_num_buckets;
  683. unsigned num_items;
  684. struct UT_hash_handle *tail; /* tail hh in app order, for fast append */
  685. ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */
  686. /* in an ideal situation (all buckets used equally), no bucket would have
  687. * more than ceil(#items/#buckets) items. that's the ideal chain length. */
  688. unsigned ideal_chain_maxlen;
  689. /* nonideal_items is the number of items in the hash whose chain position
  690. * exceeds the ideal chain maxlen. these items pay the penalty for an uneven
  691. * hash distribution; reaching them in a chain traversal takes >ideal steps */
  692. unsigned nonideal_items;
  693. /* ineffective expands occur when a bucket doubling was performed, but
  694. * afterward, more than half the items in the hash had nonideal chain
  695. * positions. If this happens on two consecutive expansions we inhibit any
  696. * further expansion, as it's not helping; this happens when the hash
  697. * function isn't a good fit for the key domain. When expansion is inhibited
  698. * the hash will still work, albeit no longer in constant time. */
  699. unsigned ineff_expands, noexpand;
  700. } UT_hash_table;
  701. typedef struct UT_hash_handle {
  702. struct UT_hash_table *tbl;
  703. void *prev; /* prev element in app order */
  704. void *next; /* next element in app order */
  705. struct UT_hash_handle *hh_prev; /* previous hh in bucket order */
  706. struct UT_hash_handle *hh_next; /* next hh in bucket order */
  707. void *key; /* ptr to enclosing struct's key */
  708. unsigned keylen; /* enclosing struct's key len */
  709. unsigned hashv; /* result of hash-fcn(key) */
  710. } UT_hash_handle;
  711. #endif /* UTHASH_H */