27 #define UTHASH_VERSION 2.0.2
37 #if !defined(DECLTYPE) && !defined(NO_DECLTYPE)
39 #if _MSC_VER >= 1600 && defined(__cplusplus)
40 #define DECLTYPE(x) (decltype(x))
44 #elif defined(__BORLANDC__) || defined(__ICCARM__) || defined(__LCC__) || defined(__WATCOMC__)
47 #define DECLTYPE(x) (__typeof(x))
53 #define DECLTYPE_ASSIGN(dst,src) \
55 char **_da_dst = (char**)(&(dst)); \
56 *_da_dst = (char*)(src); \
59 #define DECLTYPE_ASSIGN(dst,src) \
61 (dst) = DECLTYPE(dst)(src); \
67 #if defined(_MSC_VER) && _MSC_VER >= 1600
69 #elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__)
75 #elif defined(__GNUC__) && !defined(__VXWORKS__)
83 #define uthash_fatal(msg) exit(-1)
86 #define uthash_malloc(sz) malloc(sz)
89 #define uthash_free(ptr,sz) free(ptr)
92 #define uthash_bzero(a,n) memset(a,'\0',n)
95 #define uthash_memcmp(a,b,n) memcmp(a,b,n)
98 #define uthash_strlen(s) strlen(s)
101 #ifndef uthash_noexpand_fyi
102 #define uthash_noexpand_fyi(tbl)
104 #ifndef uthash_expand_fyi
105 #define uthash_expand_fyi(tbl)
109 #define HASH_INITIAL_NUM_BUCKETS 32U
110 #define HASH_INITIAL_NUM_BUCKETS_LOG2 5U
111 #define HASH_BKT_CAPACITY_THRESH 10U
114 #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho)))
116 #define HH_FROM_ELMT(tbl,elp) ((UT_hash_handle *)(((char*)(elp)) + ((tbl)->hho)))
118 #define HASH_VALUE(keyptr,keylen,hashv) \
120 HASH_FCN(keyptr, keylen, hashv); \
123 #define HASH_FIND_BYHASHVALUE(hh,head,keyptr,keylen,hashval,out) \
128 HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _hf_bkt); \
129 if (HASH_BLOOM_TEST((head)->hh.tbl, hashval) != 0) { \
130 HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], keyptr, keylen, hashval, out); \
135 #define HASH_FIND(hh,head,keyptr,keylen,out) \
137 unsigned _hf_hashv; \
138 HASH_VALUE(keyptr, keylen, _hf_hashv); \
139 HASH_FIND_BYHASHVALUE(hh, head, keyptr, keylen, _hf_hashv, out); \
143 #define HASH_BLOOM_BITLEN (1UL << HASH_BLOOM)
144 #define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8UL) + (((HASH_BLOOM_BITLEN%8UL)!=0UL) ? 1UL : 0UL)
145 #define HASH_BLOOM_MAKE(tbl) \
147 (tbl)->bloom_nbits = HASH_BLOOM; \
148 (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \
149 if (!(tbl)->bloom_bv) { \
150 uthash_fatal("out of memory"); \
152 uthash_bzero((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \
153 (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \
156 #define HASH_BLOOM_FREE(tbl) \
158 uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \
161 #define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8U] |= (1U << ((idx)%8U)))
162 #define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8U] & (1U << ((idx)%8U)))
164 #define HASH_BLOOM_ADD(tbl,hashv) \
165 HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
167 #define HASH_BLOOM_TEST(tbl,hashv) \
168 HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
171 #define HASH_BLOOM_MAKE(tbl)
172 #define HASH_BLOOM_FREE(tbl)
173 #define HASH_BLOOM_ADD(tbl,hashv)
174 #define HASH_BLOOM_TEST(tbl,hashv) (1)
175 #define HASH_BLOOM_BYTELEN 0U
178 #define HASH_MAKE_TABLE(hh,head) \
180 (head)->hh.tbl = (UT_hash_table*)uthash_malloc(sizeof(UT_hash_table)); \
181 if (!(head)->hh.tbl) { \
182 uthash_fatal("out of memory"); \
184 uthash_bzero((head)->hh.tbl, sizeof(UT_hash_table)); \
185 (head)->hh.tbl->tail = &((head)->hh); \
186 (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
187 (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \
188 (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \
189 (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \
190 HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
191 if (!(head)->hh.tbl->buckets) { \
192 uthash_fatal("out of memory"); \
194 uthash_bzero((head)->hh.tbl->buckets, \
195 HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
196 HASH_BLOOM_MAKE((head)->hh.tbl); \
197 (head)->hh.tbl->signature = HASH_SIGNATURE; \
200 #define HASH_REPLACE_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,replaced,cmpfcn) \
203 HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \
205 HASH_DELETE(hh, head, replaced); \
207 HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn); \
210 #define HASH_REPLACE_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add,replaced) \
213 HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \
215 HASH_DELETE(hh, head, replaced); \
217 HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add); \
220 #define HASH_REPLACE(hh,head,fieldname,keylen_in,add,replaced) \
222 unsigned _hr_hashv; \
223 HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \
224 HASH_REPLACE_BYHASHVALUE(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced); \
227 #define HASH_REPLACE_INORDER(hh,head,fieldname,keylen_in,add,replaced,cmpfcn) \
229 unsigned _hr_hashv; \
230 HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \
231 HASH_REPLACE_BYHASHVALUE_INORDER(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced, cmpfcn); \
234 #define HASH_APPEND_LIST(hh, head, add) \
236 (add)->hh.next = NULL; \
237 (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \
238 (head)->hh.tbl->tail->next = (add); \
239 (head)->hh.tbl->tail = &((add)->hh); \
242 #define HASH_AKBI_INNER_LOOP(hh,head,add,cmpfcn) \
245 if (cmpfcn(DECLTYPE(head)(_hs_iter), add) > 0) { \
248 } while ((_hs_iter = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->next)); \
252 #undef HASH_AKBI_INNER_LOOP
253 #define HASH_AKBI_INNER_LOOP(hh,head,add,cmpfcn) \
255 char *_hs_saved_head = (char*)(head); \
257 DECLTYPE_ASSIGN(head, _hs_iter); \
258 if (cmpfcn(head, add) > 0) { \
259 DECLTYPE_ASSIGN(head, _hs_saved_head); \
262 DECLTYPE_ASSIGN(head, _hs_saved_head); \
263 } while ((_hs_iter = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->next)); \
267 #define HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh,head,keyptr,keylen_in,hashval,add,cmpfcn) \
270 (add)->hh.hashv = (hashval); \
271 (add)->hh.key = (char*) (keyptr); \
272 (add)->hh.keylen = (unsigned) (keylen_in); \
274 (add)->hh.next = NULL; \
275 (add)->hh.prev = NULL; \
277 HASH_MAKE_TABLE(hh, head); \
279 void *_hs_iter = (head); \
280 (add)->hh.tbl = (head)->hh.tbl; \
281 HASH_AKBI_INNER_LOOP(hh, head, add, cmpfcn); \
283 (add)->hh.next = _hs_iter; \
284 if (((add)->hh.prev = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev)) { \
285 HH_FROM_ELMT((head)->hh.tbl, (add)->hh.prev)->next = (add); \
289 HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev = (add); \
291 HASH_APPEND_LIST(hh, head, add); \
294 (head)->hh.tbl->num_items++; \
295 HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
296 HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], &(add)->hh); \
297 HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
298 HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
299 HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE_INORDER"); \
302 #define HASH_ADD_KEYPTR_INORDER(hh,head,keyptr,keylen_in,add,cmpfcn) \
304 unsigned _hs_hashv; \
305 HASH_VALUE(keyptr, keylen_in, _hs_hashv); \
306 HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, keyptr, keylen_in, _hs_hashv, add, cmpfcn); \
309 #define HASH_ADD_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,cmpfcn) \
310 HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn)
312 #define HASH_ADD_INORDER(hh,head,fieldname,keylen_in,add,cmpfcn) \
313 HASH_ADD_KEYPTR_INORDER(hh, head, &((add)->fieldname), keylen_in, add, cmpfcn)
315 #define HASH_ADD_KEYPTR_BYHASHVALUE(hh,head,keyptr,keylen_in,hashval,add) \
318 (add)->hh.hashv = (hashval); \
319 (add)->hh.key = (const void *) (keyptr); \
320 (add)->hh.keylen = (unsigned) (keylen_in); \
322 (add)->hh.next = NULL; \
323 (add)->hh.prev = NULL; \
325 HASH_MAKE_TABLE(hh, head); \
327 (add)->hh.tbl = (head)->hh.tbl; \
328 HASH_APPEND_LIST(hh, head, add); \
330 (head)->hh.tbl->num_items++; \
331 HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
332 HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], &(add)->hh); \
333 HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
334 HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
335 HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE"); \
338 #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \
340 unsigned _ha_hashv; \
341 HASH_VALUE(keyptr, keylen_in, _ha_hashv); \
342 HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, keyptr, keylen_in, _ha_hashv, add); \
345 #define HASH_ADD_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add) \
346 HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add)
348 #define HASH_ADD(hh,head,fieldname,keylen_in,add) \
349 HASH_ADD_KEYPTR(hh, head, &((add)->fieldname), keylen_in, add)
351 #define HASH_TO_BKT(hashv,num_bkts,bkt) \
353 bkt = ((hashv) & ((num_bkts) - 1U)); \
368 #define HASH_DELETE(hh,head,delptr) \
369 HASH_DELETE_HH(hh, head, &(delptr)->hh)
371 #define HASH_DELETE_HH(hh,head,delptrhh) \
373 struct UT_hash_handle *_hd_hh_del = (delptrhh); \
374 if ((_hd_hh_del->prev == NULL) && (_hd_hh_del->next == NULL)) { \
375 HASH_BLOOM_FREE((head)->hh.tbl); \
376 uthash_free((head)->hh.tbl->buckets, \
377 (head)->hh.tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
378 uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
382 if (_hd_hh_del == (head)->hh.tbl->tail) { \
383 (head)->hh.tbl->tail = HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->prev); \
385 if (_hd_hh_del->prev != NULL) { \
386 HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->prev)->next = _hd_hh_del->next; \
388 DECLTYPE_ASSIGN(head, _hd_hh_del->next); \
390 if (_hd_hh_del->next != NULL) { \
391 HH_FROM_ELMT((head)->hh.tbl, _hd_hh_del->next)->prev = _hd_hh_del->prev; \
393 HASH_TO_BKT(_hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
394 HASH_DEL_IN_BKT((head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \
395 (head)->hh.tbl->num_items--; \
397 HASH_FSCK(hh, head, "HASH_DELETE"); \
402 #define HASH_FIND_STR(head,findstr,out) \
403 HASH_FIND(hh,head,findstr,(unsigned)uthash_strlen(findstr),out)
404 #define HASH_ADD_STR(head,strfield,add) \
405 HASH_ADD(hh,head,strfield[0],(unsigned)uthash_strlen(add->strfield),add)
406 #define HASH_REPLACE_STR(head,strfield,add,replaced) \
407 HASH_REPLACE(hh,head,strfield[0],(unsigned)uthash_strlen(add->strfield),add,replaced)
408 #define HASH_FIND_INT(head,findint,out) \
409 HASH_FIND(hh,head,findint,sizeof(int),out)
410 #define HASH_ADD_INT(head,intfield,add) \
411 HASH_ADD(hh,head,intfield,sizeof(int),add)
412 #define HASH_REPLACE_INT(head,intfield,add,replaced) \
413 HASH_REPLACE(hh,head,intfield,sizeof(int),add,replaced)
414 #define HASH_FIND_PTR(head,findptr,out) \
415 HASH_FIND(hh,head,findptr,sizeof(void *),out)
416 #define HASH_ADD_PTR(head,ptrfield,add) \
417 HASH_ADD(hh,head,ptrfield,sizeof(void *),add)
418 #define HASH_REPLACE_PTR(head,ptrfield,add,replaced) \
419 HASH_REPLACE(hh,head,ptrfield,sizeof(void *),add,replaced)
420 #define HASH_DEL(head,delptr) \
421 HASH_DELETE(hh,head,delptr)
427 #define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0)
428 #define HASH_FSCK(hh,head,where) \
430 struct UT_hash_handle *_thh; \
433 unsigned _count = 0; \
435 for (_bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; ++_bkt_i) { \
436 unsigned _bkt_count = 0; \
437 _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \
440 if (_prev != (char*)(_thh->hh_prev)) { \
441 HASH_OOPS("%s: invalid hh_prev %p, actual %p\n", \
442 (where), (void*)_thh->hh_prev, (void*)_prev); \
445 _prev = (char*)(_thh); \
446 _thh = _thh->hh_next; \
448 _count += _bkt_count; \
449 if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \
450 HASH_OOPS("%s: invalid bucket count %u, actual %u\n", \
451 (where), (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \
454 if (_count != (head)->hh.tbl->num_items) { \
455 HASH_OOPS("%s: invalid hh item count %u, actual %u\n", \
456 (where), (head)->hh.tbl->num_items, _count); \
460 _thh = &(head)->hh; \
463 if (_prev != (char*)_thh->prev) { \
464 HASH_OOPS("%s: invalid prev %p, actual %p\n", \
465 (where), (void*)_thh->prev, (void*)_prev); \
467 _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \
468 _thh = (_thh->next ? HH_FROM_ELMT((head)->hh.tbl, _thh->next) : NULL); \
470 if (_count != (head)->hh.tbl->num_items) { \
471 HASH_OOPS("%s: invalid app item count %u, actual %u\n", \
472 (where), (head)->hh.tbl->num_items, _count); \
477 #define HASH_FSCK(hh,head,where)
483 #ifdef HASH_EMIT_KEYS
484 #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \
486 unsigned _klen = fieldlen; \
487 write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \
488 write(HASH_EMIT_KEYS, keyptr, (unsigned long)fieldlen); \
491 #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen)
496 #define HASH_FCN HASH_FUNCTION
498 #define HASH_FCN HASH_JEN
502 #define HASH_BER(key,keylen,hashv) \
504 unsigned _hb_keylen = (unsigned)keylen; \
505 const unsigned char *_hb_key = (const unsigned char*)(key); \
507 while (_hb_keylen-- != 0U) { \
508 (hashv) = (((hashv) << 5) + (hashv)) + *_hb_key++; \
515 #define HASH_SAX(key,keylen,hashv) \
518 const unsigned char *_hs_key = (const unsigned char*)(key); \
520 for (_sx_i=0; _sx_i < keylen; _sx_i++) { \
521 hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \
525 #define HASH_FNV(key,keylen,hashv) \
528 const unsigned char *_hf_key = (const unsigned char*)(key); \
529 (hashv) = 2166136261U; \
530 for (_fn_i=0; _fn_i < keylen; _fn_i++) { \
531 hashv = hashv ^ _hf_key[_fn_i]; \
532 hashv = hashv * 16777619U; \
536 #define HASH_OAT(key,keylen,hashv) \
539 const unsigned char *_ho_key=(const unsigned char*)(key); \
541 for(_ho_i=0; _ho_i < keylen; _ho_i++) { \
542 hashv += _ho_key[_ho_i]; \
543 hashv += (hashv << 10); \
544 hashv ^= (hashv >> 6); \
546 hashv += (hashv << 3); \
547 hashv ^= (hashv >> 11); \
548 hashv += (hashv << 15); \
551 #define HASH_JEN_MIX(a,b,c) \
553 a -= b; a -= c; a ^= ( c >> 13 ); \
554 b -= c; b -= a; b ^= ( a << 8 ); \
555 c -= a; c -= b; c ^= ( b >> 13 ); \
556 a -= b; a -= c; a ^= ( c >> 12 ); \
557 b -= c; b -= a; b ^= ( a << 16 ); \
558 c -= a; c -= b; c ^= ( b >> 5 ); \
559 a -= b; a -= c; a ^= ( c >> 3 ); \
560 b -= c; b -= a; b ^= ( a << 10 ); \
561 c -= a; c -= b; c ^= ( b >> 15 ); \
564 #define HASH_JEN(key,keylen,hashv) \
566 unsigned _hj_i,_hj_j,_hj_k; \
567 unsigned const char *_hj_key=(unsigned const char*)(key); \
568 hashv = 0xfeedbeefu; \
569 _hj_i = _hj_j = 0x9e3779b9u; \
570 _hj_k = (unsigned)(keylen); \
571 while (_hj_k >= 12U) { \
572 _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \
573 + ( (unsigned)_hj_key[2] << 16 ) \
574 + ( (unsigned)_hj_key[3] << 24 ) ); \
575 _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \
576 + ( (unsigned)_hj_key[6] << 16 ) \
577 + ( (unsigned)_hj_key[7] << 24 ) ); \
578 hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \
579 + ( (unsigned)_hj_key[10] << 16 ) \
580 + ( (unsigned)_hj_key[11] << 24 ) ); \
582 HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
587 hashv += (unsigned)(keylen); \
589 case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \
590 case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \
591 case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \
592 case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \
593 case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \
594 case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \
595 case 5: _hj_j += _hj_key[4]; \
596 case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \
597 case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \
598 case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \
599 case 1: _hj_i += _hj_key[0]; \
602 HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
607 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
608 || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
609 #define get16bits(d) (*((const uint16_t *) (d)))
612 #if !defined (get16bits)
613 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \
614 +(uint32_t)(((const uint8_t *)(d))[0]) )
616 #define HASH_SFH(key,keylen,hashv) \
618 unsigned const char *_sfh_key=(unsigned const char*)(key); \
619 uint32_t _sfh_tmp, _sfh_len = (uint32_t)keylen; \
621 unsigned _sfh_rem = _sfh_len & 3U; \
623 hashv = 0xcafebabeu; \
626 for (;_sfh_len > 0U; _sfh_len--) { \
627 hashv += get16bits (_sfh_key); \
628 _sfh_tmp = ((uint32_t)(get16bits (_sfh_key+2)) << 11) ^ hashv; \
629 hashv = (hashv << 16) ^ _sfh_tmp; \
630 _sfh_key += 2U*sizeof (uint16_t); \
631 hashv += hashv >> 11; \
635 switch (_sfh_rem) { \
636 case 3: hashv += get16bits (_sfh_key); \
637 hashv ^= hashv << 16; \
638 hashv ^= (uint32_t)(_sfh_key[sizeof (uint16_t)]) << 18; \
639 hashv += hashv >> 11; \
641 case 2: hashv += get16bits (_sfh_key); \
642 hashv ^= hashv << 11; \
643 hashv += hashv >> 17; \
645 case 1: hashv += *_sfh_key; \
646 hashv ^= hashv << 10; \
647 hashv += hashv >> 1; \
651 hashv ^= hashv << 3; \
652 hashv += hashv >> 5; \
653 hashv ^= hashv << 4; \
654 hashv += hashv >> 17; \
655 hashv ^= hashv << 25; \
656 hashv += hashv >> 6; \
659 #ifdef HASH_USING_NO_STRICT_ALIASING
669 #if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86))
670 #define MUR_GETBLOCK(p,i) p[i]
672 #define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 3UL) == 0UL)
673 #define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 3UL) == 1UL)
674 #define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 3UL) == 2UL)
675 #define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 3UL) == 3UL)
676 #define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL))
677 #if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__))
678 #define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24))
679 #define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16))
680 #define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8))
682 #define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24))
683 #define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16))
684 #define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8))
686 #define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \
687 (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \
688 (MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \
691 #define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
692 #define MUR_FMIX(_h) \
701 #define HASH_MUR(key,keylen,hashv) \
703 const uint8_t *_mur_data = (const uint8_t*)(key); \
704 const int _mur_nblocks = (int)(keylen) / 4; \
705 uint32_t _mur_h1 = 0xf88D5353u; \
706 uint32_t _mur_c1 = 0xcc9e2d51u; \
707 uint32_t _mur_c2 = 0x1b873593u; \
708 uint32_t _mur_k1 = 0; \
709 const uint8_t *_mur_tail; \
710 const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+(_mur_nblocks*4)); \
712 for (_mur_i = -_mur_nblocks; _mur_i != 0; _mur_i++) { \
713 _mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \
714 _mur_k1 *= _mur_c1; \
715 _mur_k1 = MUR_ROTL32(_mur_k1,15); \
716 _mur_k1 *= _mur_c2; \
718 _mur_h1 ^= _mur_k1; \
719 _mur_h1 = MUR_ROTL32(_mur_h1,13); \
720 _mur_h1 = (_mur_h1*5U) + 0xe6546b64u; \
722 _mur_tail = (const uint8_t*)(_mur_data + (_mur_nblocks*4)); \
724 switch ((keylen) & 3U) { \
726 case 3: _mur_k1 ^= (uint32_t)_mur_tail[2] << 16; \
727 case 2: _mur_k1 ^= (uint32_t)_mur_tail[1] << 8; \
728 case 1: _mur_k1 ^= (uint32_t)_mur_tail[0]; \
729 _mur_k1 *= _mur_c1; \
730 _mur_k1 = MUR_ROTL32(_mur_k1,15); \
731 _mur_k1 *= _mur_c2; \
732 _mur_h1 ^= _mur_k1; \
734 _mur_h1 ^= (uint32_t)(keylen); \
741 #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,hashval,out) \
743 if ((head).hh_head != NULL) { \
744 DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (head).hh_head)); \
748 while ((out) != NULL) { \
749 if ((out)->hh.hashv == (hashval) && (out)->hh.keylen == (keylen_in)) { \
750 if (uthash_memcmp((out)->hh.key, keyptr, keylen_in) == 0) { \
754 if ((out)->hh.hh_next != NULL) { \
755 DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (out)->hh.hh_next)); \
763 #define HASH_ADD_TO_BKT(head,addhh) \
765 UT_hash_bucket *_ha_head = &(head); \
767 (addhh)->hh_next = _ha_head->hh_head; \
768 (addhh)->hh_prev = NULL; \
769 if (_ha_head->hh_head != NULL) { \
770 _ha_head->hh_head->hh_prev = (addhh); \
772 _ha_head->hh_head = (addhh); \
773 if ((_ha_head->count >= ((_ha_head->expand_mult + 1U) * HASH_BKT_CAPACITY_THRESH)) \
774 && !(addhh)->tbl->noexpand) { \
775 HASH_EXPAND_BUCKETS((addhh)->tbl); \
780 #define HASH_DEL_IN_BKT(head,delhh) \
782 UT_hash_bucket *_hd_head = &(head); \
784 if (_hd_head->hh_head == (delhh)) { \
785 _hd_head->hh_head = (delhh)->hh_next; \
787 if ((delhh)->hh_prev) { \
788 (delhh)->hh_prev->hh_next = (delhh)->hh_next; \
790 if ((delhh)->hh_next) { \
791 (delhh)->hh_next->hh_prev = (delhh)->hh_prev; \
824 #define HASH_EXPAND_BUCKETS(tbl) \
827 unsigned _he_bkt_i; \
828 struct UT_hash_handle *_he_thh, *_he_hh_nxt; \
829 UT_hash_bucket *_he_new_buckets, *_he_newbkt; \
830 _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \
831 2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
832 if (!_he_new_buckets) { \
833 uthash_fatal("out of memory"); \
835 uthash_bzero(_he_new_buckets, \
836 2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
837 (tbl)->ideal_chain_maxlen = \
838 ((tbl)->num_items >> ((tbl)->log2_num_buckets+1U)) + \
839 ((((tbl)->num_items & (((tbl)->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \
840 (tbl)->nonideal_items = 0; \
841 for (_he_bkt_i = 0; _he_bkt_i < (tbl)->num_buckets; _he_bkt_i++) { \
842 _he_thh = (tbl)->buckets[ _he_bkt_i ].hh_head; \
843 while (_he_thh != NULL) { \
844 _he_hh_nxt = _he_thh->hh_next; \
845 HASH_TO_BKT(_he_thh->hashv, (tbl)->num_buckets * 2U, _he_bkt); \
846 _he_newbkt = &(_he_new_buckets[_he_bkt]); \
847 if (++(_he_newbkt->count) > (tbl)->ideal_chain_maxlen) { \
848 (tbl)->nonideal_items++; \
849 _he_newbkt->expand_mult = _he_newbkt->count / (tbl)->ideal_chain_maxlen; \
851 _he_thh->hh_prev = NULL; \
852 _he_thh->hh_next = _he_newbkt->hh_head; \
853 if (_he_newbkt->hh_head != NULL) { \
854 _he_newbkt->hh_head->hh_prev = _he_thh; \
856 _he_newbkt->hh_head = _he_thh; \
857 _he_thh = _he_hh_nxt; \
860 uthash_free((tbl)->buckets, (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
861 (tbl)->num_buckets *= 2U; \
862 (tbl)->log2_num_buckets++; \
863 (tbl)->buckets = _he_new_buckets; \
864 (tbl)->ineff_expands = ((tbl)->nonideal_items > ((tbl)->num_items >> 1)) ? \
865 ((tbl)->ineff_expands+1U) : 0U; \
866 if ((tbl)->ineff_expands > 1U) { \
867 (tbl)->noexpand = 1; \
868 uthash_noexpand_fyi(tbl); \
870 uthash_expand_fyi(tbl); \
877 #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn)
878 #define HASH_SRT(hh,head,cmpfcn) \
881 unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \
882 struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \
883 if (head != NULL) { \
886 _hs_list = &((head)->hh); \
887 while (_hs_looping != 0U) { \
892 while (_hs_p != NULL) { \
896 for (_hs_i = 0; _hs_i < _hs_insize; ++_hs_i) { \
898 _hs_q = ((_hs_q->next != NULL) ? \
899 HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \
900 if (_hs_q == NULL) { \
904 _hs_qsize = _hs_insize; \
905 while ((_hs_psize != 0U) || ((_hs_qsize != 0U) && (_hs_q != NULL))) { \
906 if (_hs_psize == 0U) { \
908 _hs_q = ((_hs_q->next != NULL) ? \
909 HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \
911 } else if ((_hs_qsize == 0U) || (_hs_q == NULL)) { \
913 if (_hs_p != NULL) { \
914 _hs_p = ((_hs_p->next != NULL) ? \
915 HH_FROM_ELMT((head)->hh.tbl, _hs_p->next) : NULL); \
918 } else if ((cmpfcn( \
919 DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl, _hs_p)), \
920 DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl, _hs_q)) \
923 if (_hs_p != NULL) { \
924 _hs_p = ((_hs_p->next != NULL) ? \
925 HH_FROM_ELMT((head)->hh.tbl, _hs_p->next) : NULL); \
930 _hs_q = ((_hs_q->next != NULL) ? \
931 HH_FROM_ELMT((head)->hh.tbl, _hs_q->next) : NULL); \
934 if ( _hs_tail != NULL ) { \
935 _hs_tail->next = ((_hs_e != NULL) ? \
936 ELMT_FROM_HH((head)->hh.tbl, _hs_e) : NULL); \
940 if (_hs_e != NULL) { \
941 _hs_e->prev = ((_hs_tail != NULL) ? \
942 ELMT_FROM_HH((head)->hh.tbl, _hs_tail) : NULL); \
948 if (_hs_tail != NULL) { \
949 _hs_tail->next = NULL; \
951 if (_hs_nmerges <= 1U) { \
953 (head)->hh.tbl->tail = _hs_tail; \
954 DECLTYPE_ASSIGN(head, ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \
958 HASH_FSCK(hh, head, "HASH_SRT"); \
967 #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \
969 unsigned _src_bkt, _dst_bkt; \
970 void *_last_elt = NULL, *_elt; \
971 UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \
972 ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \
973 if ((src) != NULL) { \
974 for (_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \
975 for (_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \
977 _src_hh = _src_hh->hh_next) { \
978 _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \
980 _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \
981 _dst_hh->key = _src_hh->key; \
982 _dst_hh->keylen = _src_hh->keylen; \
983 _dst_hh->hashv = _src_hh->hashv; \
984 _dst_hh->prev = _last_elt; \
985 _dst_hh->next = NULL; \
986 if (_last_elt_hh != NULL) { \
987 _last_elt_hh->next = _elt; \
989 if ((dst) == NULL) { \
990 DECLTYPE_ASSIGN(dst, _elt); \
991 HASH_MAKE_TABLE(hh_dst, dst); \
993 _dst_hh->tbl = (dst)->hh_dst.tbl; \
995 HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \
996 HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt], _dst_hh); \
997 HASH_BLOOM_ADD(_dst_hh->tbl, _dst_hh->hashv); \
998 (dst)->hh_dst.tbl->num_items++; \
1000 _last_elt_hh = _dst_hh; \
1005 HASH_FSCK(hh_dst, dst, "HASH_SELECT"); \
1008 #define HASH_CLEAR(hh,head) \
1010 if ((head) != NULL) { \
1011 HASH_BLOOM_FREE((head)->hh.tbl); \
1012 uthash_free((head)->hh.tbl->buckets, \
1013 (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \
1014 uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
1019 #define HASH_OVERHEAD(hh,head) \
1020 (((head) != NULL) ? ( \
1021 (size_t)(((head)->hh.tbl->num_items * sizeof(UT_hash_handle)) + \
1022 ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket)) + \
1023 sizeof(UT_hash_table) + \
1024 (HASH_BLOOM_BYTELEN))) : 0U)
1027 #define HASH_ITER(hh,head,el,tmp) \
1028 for(((el)=(head)), ((*(char**)(&(tmp)))=(char*)((head!=NULL)?(head)->hh.next:NULL)); \
1029 (el) != NULL; ((el)=(tmp)), ((*(char**)(&(tmp)))=(char*)((tmp!=NULL)?(tmp)->hh.next:NULL)))
1031 #define HASH_ITER(hh,head,el,tmp) \
1032 for(((el)=(head)), ((tmp)=DECLTYPE(el)((head!=NULL)?(head)->hh.next:NULL)); \
1033 (el) != NULL; ((el)=(tmp)), ((tmp)=DECLTYPE(el)((tmp!=NULL)?(tmp)->hh.next:NULL)))
1037 #define HASH_COUNT(head) HASH_CNT(hh,head)
1038 #define HASH_CNT(hh,head) ((head != NULL)?((head)->hh.tbl->num_items):0U)
1061 #define HASH_SIGNATURE 0xa0111fe1u
1062 #define HASH_BLOOM_SIGNATURE 0xb12220f2u
struct UT_hash_handle * hh_head
struct UT_hash_handle * hh_prev
struct UT_hash_handle * hh_next
struct UT_hash_table * tbl
struct UT_hash_handle * tail
unsigned ideal_chain_maxlen
unsigned log2_num_buckets
struct UT_hash_bucket UT_hash_bucket
struct UT_hash_handle UT_hash_handle
struct UT_hash_table UT_hash_table