1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #ifndef UTHASH_H
36 # define UTHASH_H
37
38 # include <string.h>
39 # include <signal.h>
40 # include <stddef.h>
41 # include <stdlib.h>
42
43
44
45
46
47
48
49
50 # ifdef _MSC_VER
51 # if _MSC_VER >= 1600 && defined(__cplusplus)
52 # define DECLTYPE(x) (decltype(x))
53 # else
54 # define NO_DECLTYPE
55 # define DECLTYPE(x)
56 # endif
57 # else
58 # define DECLTYPE(x) (__typeof(x))
59 # endif
60
61 # ifdef NO_DECLTYPE
62 # define DECLTYPE_ASSIGN(dst,src) \
63 do { \
64 char **_da_dst = (char**)(&(dst)); \
65 *_da_dst = (char*)(src); \
66 } while(0)
67 # else
68 # define DECLTYPE_ASSIGN(dst,src) \
69 do { \
70 (dst) = DECLTYPE(dst)(src); \
71 } while(0)
72 # endif
73
74
75
76
77
78
79 # ifdef _MSC_VER
80 typedef unsigned int uint32_t;
81 typedef unsigned char uint8_t;
82 # else
83 # include <inttypes.h>
84 # endif
85
86 # define UTHASH_VERSION 21.9.8
87
88 # undef FREE
89 # ifdef TESTING
90 # define FREE(p) free(p)
91 # else
92 # define FREE(p) do \
93 { \
94 free((p)); \
95 (p) = NULL; \
96 } while(0)
97 # endif
98
99 # ifndef uthash_fatal
100 # define uthash_fatal(msg) abort()
101 # endif
102 # ifndef uthash_malloc
103 # define uthash_malloc(sz) malloc(sz)
104 # endif
105 # ifndef uthash_free
106 # define uthash_free(ptr,sz) FREE(ptr)
107 # endif
108
109 # ifndef uthash_noexpand_fyi
110 # define uthash_noexpand_fyi(tbl)
111 # endif
112 # ifndef uthash_expand_fyi
113 # define uthash_expand_fyi(tbl)
114 # endif
115
116
117 # define HASH_INITIAL_NUM_BUCKETS 32
118 # define HASH_INITIAL_NUM_BUCKETS_LOG2 5
119 # define HASH_BKT_CAPACITY_THRESH 10
120
121
122 # define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho)))
123
124 # define HASH_FIND(hh,head,keyptr,keylen,out) \
125 do { \
126 unsigned _hf_bkt,_hf_hashv; \
127 out=NULL; \
128 if (head) { \
129 HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \
130 if (HASH_BLOOM_TEST((head)->hh.tbl, _hf_hashv)) { \
131 HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], \
132 keyptr,keylen,out); \
133 } \
134 } \
135 } while (0)
136
137 # ifdef HASH_BLOOM
138 # define HASH_BLOOM_BITLEN (1ULL << HASH_BLOOM)
139 # define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8) + ((HASH_BLOOM_BITLEN%8) ? 1:0)
140 # define HASH_BLOOM_MAKE(tbl) \
141 do { \
142 (tbl)->bloom_nbits = HASH_BLOOM; \
143 (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \
144 if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \
145 memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \
146 (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \
147 } while (0)
148
149 # define HASH_BLOOM_FREE(tbl) \
150 do { \
151 uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \
152 } while (0)
153
154 # define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8] |= (1U << ((idx)%8)))
155 # define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8] & (1U << ((idx)%8)))
156
157 # define HASH_BLOOM_ADD(tbl,hashv) \
158 HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1)))
159
160 # define HASH_BLOOM_TEST(tbl,hashv) \
161 HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1)))
162
163 # else
164 # define HASH_BLOOM_MAKE(tbl)
165 # define HASH_BLOOM_FREE(tbl)
166 # define HASH_BLOOM_ADD(tbl,hashv)
167 # define HASH_BLOOM_TEST(tbl,hashv) (1)
168 # define HASH_BLOOM_BYTELEN 0
169 # endif
170
171 # define HASH_MAKE_TABLE(hh,head) \
172 do { \
173 (head)->hh.tbl = (UT_hash_table*)uthash_malloc( \
174 sizeof(UT_hash_table)); \
175 if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \
176 memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \
177 (head)->hh.tbl->tail = &((head)->hh); \
178 (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
179 (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \
180 (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \
181 (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \
182 HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
183 if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \
184 memset((head)->hh.tbl->buckets, 0, \
185 HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
186 HASH_BLOOM_MAKE((head)->hh.tbl); \
187 (head)->hh.tbl->signature = HASH_SIGNATURE; \
188 } while(0)
189
190 # define HASH_ADD(hh,head,fieldname,keylen_in,add) \
191 HASH_ADD_KEYPTR(hh,head,&((add)->fieldname),keylen_in,add)
192
193 # define HASH_REPLACE(hh,head,fieldname,keylen_in,add,replaced) \
194 do { \
195 replaced=NULL; \
196 HASH_FIND(hh,head,&((add)->fieldname),keylen_in,replaced); \
197 if (replaced!=NULL) { \
198 HASH_DELETE(hh,head,replaced); \
199 }; \
200 HASH_ADD(hh,head,fieldname,keylen_in,add); \
201 } while(0)
202
203 # define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \
204 do { \
205 unsigned _ha_bkt; \
206 (add)->hh.next = NULL; \
207 (add)->hh.key = (const void*)keyptr; \
208 (add)->hh.keylen = (unsigned)keylen_in; \
209 if (!(head)) { \
210 head = (add); \
211 (head)->hh.prev = NULL; \
212 HASH_MAKE_TABLE(hh,head); \
213 } else { \
214 (head)->hh.tbl->tail->next = (add); \
215 (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \
216 (head)->hh.tbl->tail = &((add)->hh); \
217 } \
218 (head)->hh.tbl->num_items++; \
219 (add)->hh.tbl = (head)->hh.tbl; \
220 HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets, \
221 (add)->hh.hashv, _ha_bkt); \
222 HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt],&(add)->hh); \
223 HASH_BLOOM_ADD((head)->hh.tbl,(add)->hh.hashv); \
224 HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \
225 HASH_FSCK(hh,head); \
226 } while(0)
227
228 # define HASH_TO_BKT( hashv, num_bkts, bkt ) \
229 do { \
230 bkt = ((hashv) & ((num_bkts) - 1)); \
231 } while(0)
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 # define HASH_DELETE(hh,head,delptr) \
248 do { \
249 unsigned _hd_bkt; \
250 struct UT_hash_handle *_hd_hh_del; \
251 if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \
252 uthash_free((head)->hh.tbl->buckets, \
253 (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \
254 HASH_BLOOM_FREE((head)->hh.tbl); \
255 uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
256 head = NULL; \
257 } else { \
258 _hd_hh_del = &((delptr)->hh); \
259 if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \
260 (head)->hh.tbl->tail = \
261 (UT_hash_handle*)((ptrdiff_t)((delptr)->hh.prev) + \
262 (head)->hh.tbl->hho); \
263 } \
264 if ((delptr)->hh.prev) { \
265 ((UT_hash_handle*)((ptrdiff_t)((delptr)->hh.prev) + \
266 (head)->hh.tbl->hho))->next = (delptr)->hh.next; \
267 } else { \
268 DECLTYPE_ASSIGN(head,(delptr)->hh.next); \
269 } \
270 if (_hd_hh_del->next) { \
271 ((UT_hash_handle*)((ptrdiff_t)_hd_hh_del->next + \
272 (head)->hh.tbl->hho))->prev = \
273 _hd_hh_del->prev; \
274 } \
275 HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
276 HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \
277 (head)->hh.tbl->num_items--; \
278 } \
279 HASH_FSCK(hh,head); \
280 } while (0)
281
282
283
284
285
286
287 # define HASH_FIND_STR(head,findstr,out) \
288 HASH_FIND(hh,head,findstr,strlen(findstr),out)
289 # define HASH_ADD_STR(head,strfield,add) \
290 HASH_ADD(hh,head,strfield,strlen(add->strfield),add)
291 # define HASH_REPLACE_STR(head,strfield,add,replaced) \
292 HASH_REPLACE(hh,head,strfield,strlen(add->strfield),add,replaced)
293 # define HASH_FIND_INT(head,findint,out) \
294 HASH_FIND(hh,head,findint,sizeof(int),out)
295 # define HASH_ADD_INT(head,intfield,add) \
296 HASH_ADD(hh,head,intfield,sizeof(int),add)
297 # define HASH_REPLACE_INT(head,intfield,add,replaced) \
298 HASH_REPLACE(hh,head,intfield,sizeof(int),add,replaced)
299 # define HASH_FIND_PTR(head,findptr,out) \
300 HASH_FIND(hh,head,findptr,sizeof(void *),out)
301 # define HASH_ADD_PTR(head,ptrfield,add) \
302 HASH_ADD(hh,head,ptrfield,sizeof(void *),add)
303 # define HASH_REPLACE_PTR(head,ptrfield,add) \
304 HASH_REPLACE(hh,head,ptrfield,sizeof(void *),add,replaced)
305 # define HASH_DEL(head,delptr) \
306 HASH_DELETE(hh,head,delptr)
307
308
309
310
311
312
313
314
315
316 # ifdef HASH_DEBUG
317 # define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); abort(); } while (0)
318 # define HASH_FSCK(hh,head) \
319 do { \
320 unsigned _bkt_i; \
321 unsigned _count, _bkt_count; \
322 char *_prev; \
323 struct UT_hash_handle *_thh; \
324 if (head) { \
325 _count = 0; \
326 for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \
327 _bkt_count = 0; \
328 _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \
329 _prev = NULL; \
330 while (_thh) { \
331 if (_prev != (char*)(_thh->hh_prev)) { \
332 HASH_OOPS("invalid hh_prev %p, actual %p\n", \
333 _thh->hh_prev, _prev ); \
334 } \
335 _bkt_count++; \
336 _prev = (char*)(_thh); \
337 _thh = _thh->hh_next; \
338 } \
339 _count += _bkt_count; \
340 if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \
341 HASH_OOPS("invalid bucket count %d, actual %d\n", \
342 (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \
343 } \
344 } \
345 if (_count != (head)->hh.tbl->num_items) { \
346 HASH_OOPS("invalid hh item count %d, actual %d\n", \
347 (head)->hh.tbl->num_items, _count ); \
348 } \
349 \
350 _count = 0; \
351 _prev = NULL; \
352 _thh = &(head)->hh; \
353 while (_thh) { \
354 _count++; \
355 if (_prev !=(char*)(_thh->prev)) { \
356 HASH_OOPS("invalid prev %p, actual %p\n", \
357 _thh->prev, _prev ); \
358 } \
359 _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \
360 _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \
361 (head)->hh.tbl->hho) : NULL ); \
362 } \
363 if (_count != (head)->hh.tbl->num_items) { \
364 HASH_OOPS("invalid app item count %d, actual %d\n", \
365 (head)->hh.tbl->num_items, _count ); \
366 } \
367 } \
368 } while (0)
369 # else
370 # define HASH_FSCK(hh,head)
371 # endif
372
373
374
375
376
377
378
379
380
381
382 # ifdef HASH_EMIT_KEYS
383 # define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \
384 do { \
385 unsigned _klen = fieldlen; \
386 write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \
387 write(HASH_EMIT_KEYS, keyptr, fieldlen); \
388 } while (0)
389 # else
390 # define HASH_EMIT_KEY(hh,head,keyptr,fieldlen)
391 # endif
392
393
394
395
396
397
398 # ifdef HASH_FUNCTION
399 # define HASH_FCN HASH_FUNCTION
400 # else
401 # define HASH_FCN HASH_JEN
402 # endif
403
404
405
406
407
408
409 # define HASH_BER(key,keylen,num_bkts,hashv,bkt) \
410 do { \
411 unsigned _hb_keylen=keylen; \
412 char *_hb_key=(char*)(key); \
413 (hashv) = 0; \
414 while (_hb_keylen--) { (hashv) = ((hashv) * 33) + *_hb_key++; } \
415 bkt = (hashv) & (num_bkts-1); \
416 } while (0)
417
418
419
420
421
422
423 # define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \
424 do { \
425 unsigned _sx_i; \
426 char *_hs_key=(char*)(key); \
427 hashv = 0; \
428 for(_sx_i=0; _sx_i < keylen; _sx_i++) \
429 hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \
430 bkt = hashv & (num_bkts-1); \
431 } while (0)
432
433 # define HASH_FNV(key,keylen,num_bkts,hashv,bkt) \
434 do { \
435 unsigned _fn_i; \
436 char *_hf_key=(char*)(key); \
437 hashv = 2166136261UL; \
438 for(_fn_i=0; _fn_i < keylen; _fn_i++) \
439 hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \
440 bkt = hashv & (num_bkts-1); \
441 } while(0)
442
443 # define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \
444 do { \
445 unsigned _ho_i; \
446 char *_ho_key=(char*)(key); \
447 hashv = 0; \
448 for(_ho_i=0; _ho_i < keylen; _ho_i++) { \
449 hashv += _ho_key[_ho_i]; \
450 hashv += (hashv << 10); \
451 hashv ^= (hashv >> 6); \
452 } \
453 hashv += (hashv << 3); \
454 hashv ^= (hashv >> 11); \
455 hashv += (hashv << 15); \
456 bkt = hashv & (num_bkts-1); \
457 } while(0)
458
459 # define HASH_JEN_MIX(a,b,c) \
460 do { \
461 a -= b; a -= c; a ^= ( c >> 13 ); \
462 b -= c; b -= a; b ^= ( a << 8 ); \
463 c -= a; c -= b; c ^= ( b >> 13 ); \
464 a -= b; a -= c; a ^= ( c >> 12 ); \
465 b -= c; b -= a; b ^= ( a << 16 ); \
466 c -= a; c -= b; c ^= ( b >> 5 ); \
467 a -= b; a -= c; a ^= ( c >> 3 ); \
468 b -= c; b -= a; b ^= ( a << 10 ); \
469 c -= a; c -= b; c ^= ( b >> 15 ); \
470 } while (0)
471
472 # define HASH_JEN(key,keylen,num_bkts,hashv,bkt) \
473 do { \
474 unsigned _hj_i,_hj_j,_hj_k; \
475 unsigned char *_hj_key=(unsigned char*)(key); \
476 hashv = 0xfeedbeef; \
477 _hj_i = _hj_j = 0x9e3779b9; \
478 _hj_k = (unsigned)keylen; \
479 while (_hj_k >= 12) { \
480 _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \
481 + ( (unsigned)_hj_key[2] << 16 ) \
482 + ( (unsigned)_hj_key[3] << 24 ) ); \
483 _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \
484 + ( (unsigned)_hj_key[6] << 16 ) \
485 + ( (unsigned)_hj_key[7] << 24 ) ); \
486 hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \
487 + ( (unsigned)_hj_key[10] << 16 ) \
488 + ( (unsigned)_hj_key[11] << 24 ) ); \
489 \
490 HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
491 \
492 _hj_key += 12; \
493 _hj_k -= 12; \
494 } \
495 hashv += keylen; \
496 switch ( _hj_k ) { \
497 case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \
498 case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \
499 case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \
500 case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \
501 case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \
502 case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \
503 case 5: _hj_j += _hj_key[4]; \
504 case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \
505 case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \
506 case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \
507 case 1: _hj_i += _hj_key[0]; \
508 } \
509 HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
510 bkt = hashv & (num_bkts-1); \
511 } while(0)
512
513
514
515
516
517
518 # undef get16bits
519 # if ( defined (__GNUC__) && defined (__i386__) )
520 # define get16bits(d) (*((const uint16_t *) (d)))
521 # endif
522
523 # if !defined (get16bits)
524 # define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \
525 +(uint32_t)(((const uint8_t *)(d))[0]) )
526 # endif
527 # define HASH_SFH(key,keylen,num_bkts,hashv,bkt) \
528 do { \
529 unsigned char *_sfh_key=(unsigned char*)(key); \
530 uint32_t _sfh_tmp, _sfh_len = keylen; \
531 \
532 int _sfh_rem = _sfh_len & 3; \
533 _sfh_len >>= 2; \
534 hashv = 0xcafebabe; \
535 \
536 \
537 for (;_sfh_len > 0; _sfh_len--) { \
538 hashv += get16bits (_sfh_key); \
539 _sfh_tmp = (uint32_t)(get16bits (_sfh_key+2)) << 11 ^ hashv; \
540 hashv = (hashv << 16) ^ _sfh_tmp; \
541 _sfh_key += 2*sizeof (uint16_t); \
542 hashv += hashv >> 11; \
543 } \
544 \
545 \
546 switch (_sfh_rem) { \
547 case 3: hashv += get16bits (_sfh_key); \
548 hashv ^= hashv << 16; \
549 hashv ^= (uint32_t)(_sfh_key[sizeof (uint16_t)] << 18); \
550 hashv += hashv >> 11; \
551 break; \
552 case 2: hashv += get16bits (_sfh_key); \
553 hashv ^= hashv << 11; \
554 hashv += hashv >> 17; \
555 break; \
556 case 1: hashv += *_sfh_key; \
557 hashv ^= hashv << 10; \
558 hashv += hashv >> 1; \
559 } \
560 \
561 \
562 hashv ^= hashv << 3; \
563 hashv += hashv >> 5; \
564 hashv ^= hashv << 4; \
565 hashv += hashv >> 17; \
566 hashv ^= hashv << 25; \
567 hashv += hashv >> 6; \
568 bkt = hashv & (num_bkts-1); \
569 } while(0)
570
571 # ifdef HASH_USING_NO_STRICT_ALIASING
572
573
574
575
576
577
578
579
580
581
582
583
584 # if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86))
585 # define MUR_GETBLOCK(p,i) p[i]
586 # else
587 # define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 0x3) == 0)
588 # define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 0x3) == 1)
589 # define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 0x3) == 2)
590 # define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 0x3) == 3)
591 # define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL))
592 # if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__))
593 # define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24))
594 # define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16))
595 # define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8))
596 # else
597 # define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24))
598 # define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16))
599 # define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8))
600 # endif
601 # define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \
602 (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \
603 (MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \
604 MUR_ONE_THREE(p))))
605 # endif
606 # define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
607 # define MUR_FMIX(_h) \
608 do { \
609 _h ^= _h >> 16; \
610 _h *= 0x85ebca6b; \
611 _h ^= _h >> 13; \
612 _h *= 0xc2b2ae35l; \
613 _h ^= _h >> 16; \
614 } while(0)
615
616 # define HASH_MUR(key,keylen,num_bkts,hashv,bkt) \
617 do { \
618 const uint8_t *_mur_data = (const uint8_t*)(key); \
619 const int _mur_nblocks = (keylen) / 4; \
620 uint32_t _mur_h1 = 0xf88D5353; \
621 uint32_t _mur_c1 = 0xcc9e2d51; \
622 uint32_t _mur_c2 = 0x1b873593; \
623 uint32_t _mur_k1 = 0; \
624 const uint8_t *_mur_tail; \
625 const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+_mur_nblocks*4); \
626 int _mur_i; \
627 for(_mur_i = -_mur_nblocks; _mur_i; _mur_i++) { \
628 _mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \
629 _mur_k1 *= _mur_c1; \
630 _mur_k1 = MUR_ROTL32(_mur_k1,15); \
631 _mur_k1 *= _mur_c2; \
632 \
633 _mur_h1 ^= _mur_k1; \
634 _mur_h1 = MUR_ROTL32(_mur_h1,13); \
635 _mur_h1 = _mur_h1*5+0xe6546b64; \
636 } \
637 _mur_tail = (const uint8_t*)(_mur_data + _mur_nblocks*4); \
638 _mur_k1=0; \
639 switch((keylen) & 3) { \
640 case 3: _mur_k1 ^= _mur_tail[2] << 16; \
641 case 2: _mur_k1 ^= _mur_tail[1] << 8; \
642 case 1: _mur_k1 ^= _mur_tail[0]; \
643 _mur_k1 *= _mur_c1; \
644 _mur_k1 = MUR_ROTL32(_mur_k1,15); \
645 _mur_k1 *= _mur_c2; \
646 _mur_h1 ^= _mur_k1; \
647 } \
648 _mur_h1 ^= (keylen); \
649 MUR_FMIX(_mur_h1); \
650 hashv = _mur_h1; \
651 bkt = hashv & (num_bkts-1); \
652 } while(0)
653 # endif
654
655
656
657
658
659
660 # define HASH_KEYCMP(a,b,len) memcmp(a,b,len)
661
662
663
664
665
666
667 # define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \
668 do { \
669 if (head.hh_head) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,head.hh_head)); \
670 else out=NULL; \
671 while (out) { \
672 if ((out)->hh.keylen == keylen_in) { \
673 if ((HASH_KEYCMP((out)->hh.key,keyptr,keylen_in)) == 0) break; \
674 } \
675 if ((out)->hh.hh_next) \
676 DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,(out)->hh.hh_next)); \
677 else out = NULL; \
678 } \
679 } while(0)
680
681
682
683
684
685
686 # define HASH_ADD_TO_BKT(head,addhh) \
687 do { \
688 head.count++; \
689 (addhh)->hh_next = head.hh_head; \
690 (addhh)->hh_prev = NULL; \
691 if (head.hh_head) { (head).hh_head->hh_prev = (addhh); } \
692 (head).hh_head=addhh; \
693 if (head.count >= ((head.expand_mult+1) * HASH_BKT_CAPACITY_THRESH) \
694 && (addhh)->tbl->noexpand != 1) { \
695 HASH_EXPAND_BUCKETS((addhh)->tbl); \
696 } \
697 } while(0)
698
699
700
701
702
703
704 # define HASH_DEL_IN_BKT(hh,head,hh_del) \
705 (head).count--; \
706 if ((head).hh_head == hh_del) { \
707 (head).hh_head = hh_del->hh_next; \
708 } \
709 if (hh_del->hh_prev) { \
710 hh_del->hh_prev->hh_next = hh_del->hh_next; \
711 } \
712 if (hh_del->hh_next) { \
713 hh_del->hh_next->hh_prev = hh_del->hh_prev; \
714 }
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746 # define HASH_EXPAND_BUCKETS(tbl) \
747 do { \
748 unsigned _he_bkt; \
749 unsigned _he_bkt_i; \
750 struct UT_hash_handle *_he_thh, *_he_hh_nxt; \
751 UT_hash_bucket *_he_new_buckets, *_he_newbkt; \
752 _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \
753 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
754 if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \
755 memset(_he_new_buckets, 0, \
756 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \
757 tbl->ideal_chain_maxlen = \
758 (tbl->num_items >> (tbl->log2_num_buckets+1)) + \
759 ((tbl->num_items & ((tbl->num_buckets*2)-1)) ? 1 : 0); \
760 tbl->nonideal_items = 0; \
761 for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \
762 { \
763 _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \
764 while (_he_thh) { \
765 _he_hh_nxt = _he_thh->hh_next; \
766 HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2, _he_bkt); \
767 _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \
768 if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \
769 tbl->nonideal_items++; \
770 _he_newbkt->expand_mult = _he_newbkt->count / \
771 tbl->ideal_chain_maxlen; \
772 } \
773 _he_thh->hh_prev = NULL; \
774 _he_thh->hh_next = _he_newbkt->hh_head; \
775 if (_he_newbkt->hh_head) _he_newbkt->hh_head->hh_prev = \
776 _he_thh; \
777 _he_newbkt->hh_head = _he_thh; \
778 _he_thh = _he_hh_nxt; \
779 } \
780 } \
781 uthash_free( tbl->buckets, tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \
782 tbl->num_buckets *= 2; \
783 tbl->log2_num_buckets++; \
784 tbl->buckets = _he_new_buckets; \
785 tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \
786 (tbl->ineff_expands+1) : 0; \
787 if (tbl->ineff_expands > 1) { \
788 tbl->noexpand=1; \
789 uthash_noexpand_fyi(tbl); \
790 } \
791 uthash_expand_fyi(tbl); \
792 } while(0)
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807 # define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn)
808 # define HASH_SRT(hh,head,cmpfcn) \
809 do { \
810 unsigned _hs_i; \
811 unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \
812 struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \
813 if (head) { \
814 _hs_insize = 1; \
815 _hs_looping = 1; \
816 _hs_list = &((head)->hh); \
817 while (_hs_looping) { \
818 _hs_p = _hs_list; \
819 _hs_list = NULL; \
820 _hs_tail = NULL; \
821 _hs_nmerges = 0; \
822 while (_hs_p) { \
823 _hs_nmerges++; \
824 _hs_q = _hs_p; \
825 _hs_psize = 0; \
826 for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \
827 _hs_psize++; \
828 _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
829 ((void*)((char*)(_hs_q->next) + \
830 (head)->hh.tbl->hho)) : NULL); \
831 if (! (_hs_q) ) break; \
832 } \
833 _hs_qsize = _hs_insize; \
834 while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q )) { \
835 if (_hs_psize == 0) { \
836 _hs_e = _hs_q; \
837 _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
838 ((void*)((char*)(_hs_q->next) + \
839 (head)->hh.tbl->hho)) : NULL); \
840 _hs_qsize--; \
841 } else if ( (_hs_qsize == 0) || !(_hs_q) ) { \
842 _hs_e = _hs_p; \
843 _hs_p = (UT_hash_handle*)((_hs_p->next) ? \
844 ((void*)((char*)(_hs_p->next) + \
845 (head)->hh.tbl->hho)) : NULL); \
846 _hs_psize--; \
847 } else if (( \
848 cmpfcn(DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \
849 DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \
850 ) <= 0) { \
851 _hs_e = _hs_p; \
852 _hs_p = (UT_hash_handle*)((_hs_p->next) ? \
853 ((void*)((char*)(_hs_p->next) + \
854 (head)->hh.tbl->hho)) : NULL); \
855 _hs_psize--; \
856 } else { \
857 _hs_e = _hs_q; \
858 _hs_q = (UT_hash_handle*)((_hs_q->next) ? \
859 ((void*)((char*)(_hs_q->next) + \
860 (head)->hh.tbl->hho)) : NULL); \
861 _hs_qsize--; \
862 } \
863 if ( _hs_tail ) { \
864 _hs_tail->next = ((_hs_e) ? \
865 ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \
866 } else { \
867 _hs_list = _hs_e; \
868 } \
869 _hs_e->prev = ((_hs_tail) ? \
870 ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \
871 _hs_tail = _hs_e; \
872 } \
873 _hs_p = _hs_q; \
874 } \
875 _hs_tail->next = NULL; \
876 if ( _hs_nmerges <= 1 ) { \
877 _hs_looping=0; \
878 (head)->hh.tbl->tail = _hs_tail; \
879 DECLTYPE_ASSIGN(head,ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \
880 } \
881 _hs_insize *= 2; \
882 } \
883 HASH_FSCK(hh,head); \
884 } \
885 } while (0)
886
887
888
889
890
891
892
893
894
895 # define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \
896 do { \
897 unsigned _src_bkt, _dst_bkt; \
898 void *_last_elt=NULL, *_elt; \
899 UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \
900 ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \
901 if (src) { \
902 for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \
903 for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \
904 _src_hh; \
905 _src_hh = _src_hh->hh_next) { \
906 _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \
907 if (cond(_elt)) { \
908 _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \
909 _dst_hh->key = _src_hh->key; \
910 _dst_hh->keylen = _src_hh->keylen; \
911 _dst_hh->hashv = _src_hh->hashv; \
912 _dst_hh->prev = _last_elt; \
913 _dst_hh->next = NULL; \
914 if (_last_elt_hh) { _last_elt_hh->next = _elt; } \
915 if (!dst) { \
916 DECLTYPE_ASSIGN(dst,_elt); \
917 HASH_MAKE_TABLE(hh_dst,dst); \
918 } else { \
919 _dst_hh->tbl = (dst)->hh_dst.tbl; \
920 } \
921 HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \
922 HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \
923 (dst)->hh_dst.tbl->num_items++; \
924 _last_elt = _elt; \
925 _last_elt_hh = _dst_hh; \
926 } \
927 } \
928 } \
929 } \
930 HASH_FSCK(hh_dst,dst); \
931 } while (0)
932
933 # define HASH_CLEAR(hh,head) \
934 do { \
935 if (head) { \
936 uthash_free((head)->hh.tbl->buckets, \
937 (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \
938 HASH_BLOOM_FREE((head)->hh.tbl); \
939 uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
940 (head)=NULL; \
941 } \
942 } while(0)
943
944 # define HASH_OVERHEAD(hh,head) \
945 (size_t)((((head)->hh.tbl->num_items * sizeof(UT_hash_handle)) + \
946 ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket)) + \
947 (sizeof(UT_hash_table)) + \
948 (HASH_BLOOM_BYTELEN)))
949
950 # ifdef NO_DECLTYPE
951 # define HASH_ITER(hh,head,el,tmp) \
952 for((el)=(head), (*(char**)(&(tmp)))=(char*)((head)?(head)->hh.next:NULL); \
953 el; (el)=(tmp),(*(char**)(&(tmp)))=(char*)((tmp)?(tmp)->hh.next:NULL))
954 # else
955 # define HASH_ITER(hh,head,el,tmp) \
956 for((el)=(head),(tmp)=DECLTYPE(el)((head)?(head)->hh.next:NULL); \
957 el; (el)=(tmp),(tmp)=DECLTYPE(el)((tmp)?(tmp)->hh.next:NULL))
958 # endif
959
960
961 # define HASH_COUNT(head) HASH_CNT(hh,head)
962 # define HASH_CNT(hh,head) ((head)?((head)->hh.tbl->num_items):0)
963
964 typedef struct UT_hash_bucket {
965 struct UT_hash_handle *hh_head;
966 unsigned count;
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982 unsigned expand_mult;
983
984 } UT_hash_bucket;
985
986
987 # define HASH_SIGNATURE 0xa0111fe1
988 # define HASH_BLOOM_SIGNATURE 0xb12220f2
989
990 typedef struct UT_hash_table {
991 UT_hash_bucket *buckets;
992 unsigned num_buckets, log2_num_buckets;
993 unsigned num_items;
994 struct UT_hash_handle *tail;
995 ptrdiff_t hho;
996
997
998
999
1000
1001
1002 unsigned ideal_chain_maxlen;
1003
1004
1005
1006
1007
1008
1009
1010 unsigned nonideal_items;
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021 unsigned ineff_expands, noexpand;
1022
1023 uint32_t signature;
1024 # ifdef HASH_BLOOM
1025 uint32_t bloom_sig;
1026 uint8_t *bloom_bv;
1027 char bloom_nbits;
1028 # endif
1029
1030 } UT_hash_table;
1031
1032 typedef struct UT_hash_handle {
1033 struct UT_hash_table *tbl;
1034 void *prev;
1035 void *next;
1036 struct UT_hash_handle *hh_prev;
1037 struct UT_hash_handle *hh_next;
1038 const void *key;
1039 unsigned keylen;
1040 unsigned hashv;
1041 } UT_hash_handle;
1042
1043 #endif