1 | /*␊ |
2 | Copyright (c) 2003-2011, Troy D. Hanson http://uthash.sourceforge.net␊ |
3 | All rights reserved.␊ |
4 | ␊ |
5 | Redistribution and use in source and binary forms, with or without␊ |
6 | modification, are permitted provided that the following conditions are met:␊ |
7 | ␊ |
8 | * Redistributions of source code must retain the above copyright␊ |
9 | notice, this list of conditions and the following disclaimer.␊ |
10 | ␊ |
11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS␊ |
12 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED␊ |
13 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A␊ |
14 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER␊ |
15 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,␊ |
16 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,␊ |
17 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR␊ |
18 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF␊ |
19 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING␊ |
20 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS␊ |
21 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.␊ |
22 | */␊ |
23 | ␊ |
24 | #ifndef UTHASH_H␊ |
25 | #define UTHASH_H ␊ |
26 | ␊ |
27 | #include "libsaio.h"␊ |
28 | ␊ |
29 | /* These macros use decltype or the earlier __typeof GNU extension.␊ |
30 | As decltype is only available in newer compilers (VS2010 or gcc 4.3+␊ |
31 | when compiling c++ source) this code uses whatever method is needed␊ |
32 | or, for VS2008 where neither is available, uses casting workarounds. */␊ |
33 | #ifdef _MSC_VER /* MS compiler */␊ |
34 | #if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */␊ |
35 | #define DECLTYPE(x) (decltype(x))␊ |
36 | #else /* VS2008 or older (or VS2010 in C mode) */␊ |
37 | #define NO_DECLTYPE␊ |
38 | #define DECLTYPE(x)␊ |
39 | #endif␊ |
40 | #else /* GNU, Sun and other compilers */␊ |
41 | #define DECLTYPE(x) (__typeof(x))␊ |
42 | #endif␊ |
43 | ␊ |
44 | #ifdef NO_DECLTYPE␊ |
45 | #define DECLTYPE_ASSIGN(dst,src) \␊ |
46 | do { \␊ |
47 | char **_da_dst = (char**)(&(dst)); \␊ |
48 | *_da_dst = (char*)(src); \␊ |
49 | } while(0)␊ |
50 | #else ␊ |
51 | #define DECLTYPE_ASSIGN(dst,src) \␊ |
52 | do { \␊ |
53 | (dst) = DECLTYPE(dst)(src); \␊ |
54 | } while(0)␊ |
55 | #endif␊ |
56 | ␊ |
57 | #define UTHASH_VERSION 1.9.4␊ |
58 | ␊ |
59 | #define uthash_fatal(msg) longjmp(h_buf_error,-1) /* fatal error (out of memory,etc) */␊ |
60 | #define uthash_malloc(sz) malloc(sz) /* malloc fcn */␊ |
61 | #define uthash_free(ptr,sz) free(ptr) /* free fcn */␊ |
62 | ␊ |
63 | #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */␊ |
64 | #define uthash_expand_fyi(tbl) /* can be defined to log expands */␊ |
65 | ␊ |
66 | /* initial number of buckets */␊ |
67 | #define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */␊ |
68 | #define HASH_INITIAL_NUM_BUCKETS_LOG2 5 /* lg2 of initial number of buckets */␊ |
69 | #define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches */␊ |
70 | ␊ |
71 | /* calculate the element whose hash handle address is hhe */␊ |
72 | #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho)))␊ |
73 | ␊ |
74 | #define HASH_FIND(hh,head,keyptr,keylen,out) \␊ |
75 | do { \␊ |
76 | unsigned _hf_bkt,_hf_hashv; \␊ |
77 | out=NULL; \␊ |
78 | if (head) { \␊ |
79 | HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \␊ |
80 | if (HASH_BLOOM_TEST((head)->hh.tbl, _hf_hashv)) { \␊ |
81 | HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], \␊ |
82 | keyptr,keylen,out); \␊ |
83 | } \␊ |
84 | } \␊ |
85 | } while (0)␊ |
86 | ␊ |
87 | #ifdef HASH_BLOOM␊ |
88 | #define HASH_BLOOM_BITLEN (1ULL << HASH_BLOOM)␊ |
89 | #define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8) + ((HASH_BLOOM_BITLEN%8) ? 1:0)␊ |
90 | #define HASH_BLOOM_MAKE(tbl) \␊ |
91 | do { \␊ |
92 | (tbl)->bloom_nbits = HASH_BLOOM; \␊ |
93 | (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \␊ |
94 | if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \␊ |
95 | memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \␊ |
96 | (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \␊ |
97 | } while (0);␊ |
98 | ␊ |
99 | #define HASH_BLOOM_FREE(tbl) \␊ |
100 | do { \␊ |
101 | uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \␊ |
102 | } while (0);␊ |
103 | ␊ |
104 | #define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8] |= (1U << ((idx)%8)))␊ |
105 | #define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8] & (1U << ((idx)%8)))␊ |
106 | ␊ |
107 | #define HASH_BLOOM_ADD(tbl,hashv) \␊ |
108 | HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1)))␊ |
109 | ␊ |
110 | #define HASH_BLOOM_TEST(tbl,hashv) \␊ |
111 | HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1)))␊ |
112 | ␊ |
113 | #else␊ |
114 | #define HASH_BLOOM_MAKE(tbl) ␊ |
115 | #define HASH_BLOOM_FREE(tbl) ␊ |
116 | #define HASH_BLOOM_ADD(tbl,hashv) ␊ |
117 | #define HASH_BLOOM_TEST(tbl,hashv) (1)␊ |
118 | #endif␊ |
119 | ␊ |
120 | #define HASH_MAKE_TABLE(hh,head) \␊ |
121 | do { \␊ |
122 | (head)->hh.tbl = (UT_hash_table*)uthash_malloc( \␊ |
123 | sizeof(UT_hash_table)); \␊ |
124 | if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \␊ |
125 | memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \␊ |
126 | (head)->hh.tbl->tail = &((head)->hh); \␊ |
127 | (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \␊ |
128 | (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \␊ |
129 | (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \␊ |
130 | (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \␊ |
131 | HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \␊ |
132 | if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \␊ |
133 | memset((head)->hh.tbl->buckets, 0, \␊ |
134 | HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \␊ |
135 | HASH_BLOOM_MAKE((head)->hh.tbl); \␊ |
136 | (head)->hh.tbl->signature = HASH_SIGNATURE; \␊ |
137 | } while(0)␊ |
138 | ␊ |
139 | #define HASH_ADD(hh,head,fieldname,keylen_in,add) \␊ |
140 | HASH_ADD_KEYPTR(hh,head,&((add)->fieldname),keylen_in,add)␊ |
141 | ␊ |
142 | #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \␊ |
143 | do { \␊ |
144 | unsigned _ha_bkt; \␊ |
145 | (add)->hh.next = NULL; \␊ |
146 | (add)->hh.key = (char*)keyptr; \␊ |
147 | (add)->hh.keylen = keylen_in; \␊ |
148 | if (!(head)) { \␊ |
149 | head = (add); \␊ |
150 | (head)->hh.prev = NULL; \␊ |
151 | HASH_MAKE_TABLE(hh,head); \␊ |
152 | } else { \␊ |
153 | (head)->hh.tbl->tail->next = (add); \␊ |
154 | (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \␊ |
155 | (head)->hh.tbl->tail = &((add)->hh); \␊ |
156 | } \␊ |
157 | (head)->hh.tbl->num_items++; \␊ |
158 | (add)->hh.tbl = (head)->hh.tbl; \␊ |
159 | HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets, \␊ |
160 | (add)->hh.hashv, _ha_bkt); \␊ |
161 | HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt],&(add)->hh); \␊ |
162 | HASH_BLOOM_ADD((head)->hh.tbl,(add)->hh.hashv); \␊ |
163 | HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \␊ |
164 | HASH_FSCK(hh,head); \␊ |
165 | } while(0)␊ |
166 | ␊ |
167 | #define HASH_TO_BKT( hashv, num_bkts, bkt ) \␊ |
168 | do { \␊ |
169 | bkt = ((hashv) & ((num_bkts) - 1)); \␊ |
170 | } while(0)␊ |
171 | ␊ |
172 | /* delete "delptr" from the hash table.␊ |
173 | * "the usual" patch-up process for the app-order doubly-linked-list.␊ |
174 | * The use of _hd_hh_del below deserves special explanation.␊ |
175 | * These used to be expressed using (delptr) but that led to a bug␊ |
176 | * if someone used the same symbol for the head and deletee, like␊ |
177 | * HASH_DELETE(hh,users,users);␊ |
178 | * We want that to work, but by changing the head (users) below␊ |
179 | * we were forfeiting our ability to further refer to the deletee (users)␊ |
180 | * in the patch-up process. Solution: use scratch space to␊ |
181 | * copy the deletee pointer, then the latter references are via that␊ |
182 | * scratch pointer rather than through the repointed (users) symbol.␊ |
183 | */␊ |
184 | #define HASH_DELETE(hh,head,delptr) \␊ |
185 | do { \␊ |
186 | unsigned _hd_bkt; \␊ |
187 | struct UT_hash_handle *_hd_hh_del; \␊ |
188 | if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \␊ |
189 | uthash_free((head)->hh.tbl->buckets, \␊ |
190 | (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \␊ |
191 | HASH_BLOOM_FREE((head)->hh.tbl); \␊ |
192 | uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \␊ |
193 | head = NULL; \␊ |
194 | } else { \␊ |
195 | _hd_hh_del = &((delptr)->hh); \␊ |
196 | if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \␊ |
197 | (head)->hh.tbl->tail = \␊ |
198 | (UT_hash_handle*)((char*)((delptr)->hh.prev) + \␊ |
199 | (head)->hh.tbl->hho); \␊ |
200 | } \␊ |
201 | if ((delptr)->hh.prev) { \␊ |
202 | ((UT_hash_handle*)((char*)((delptr)->hh.prev) + \␊ |
203 | (head)->hh.tbl->hho))->next = (delptr)->hh.next; \␊ |
204 | } else { \␊ |
205 | DECLTYPE_ASSIGN(head,(delptr)->hh.next); \␊ |
206 | } \␊ |
207 | if (_hd_hh_del->next) { \␊ |
208 | ((UT_hash_handle*)((char*)_hd_hh_del->next + \␊ |
209 | (head)->hh.tbl->hho))->prev = \␊ |
210 | _hd_hh_del->prev; \␊ |
211 | } \␊ |
212 | HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \␊ |
213 | HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \␊ |
214 | (head)->hh.tbl->num_items--; \␊ |
215 | } \␊ |
216 | HASH_FSCK(hh,head); \␊ |
217 | } while (0)␊ |
218 | ␊ |
219 | ␊ |
220 | /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */␊ |
221 | #define HASH_FIND_STR(head,findstr,out) \␊ |
222 | HASH_FIND(hh,head,findstr,strlen(findstr),out)␊ |
223 | #define HASH_ADD_STR(head,strfield,add) \␊ |
224 | HASH_ADD(hh,head,strfield,strlen(add->strfield),add)␊ |
225 | #define HASH_FIND_INT(head,findint,out) \␊ |
226 | HASH_FIND(hh,head,findint,sizeof(int),out)␊ |
227 | #define HASH_ADD_INT(head,intfield,add) \␊ |
228 | HASH_ADD(hh,head,intfield,sizeof(int),add)␊ |
229 | #define HASH_FIND_PTR(head,findptr,out) \␊ |
230 | HASH_FIND(hh,head,findptr,sizeof(void *),out)␊ |
231 | #define HASH_ADD_PTR(head,ptrfield,add) \␊ |
232 | HASH_ADD(hh,head,ptrfield,sizeof(void *),add)␊ |
233 | #define HASH_DEL(head,delptr) \␊ |
234 | HASH_DELETE(hh,head,delptr)␊ |
235 | ␊ |
236 | /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined.␊ |
237 | * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined.␊ |
238 | */␊ |
239 | #ifdef HASH_DEBUG␊ |
240 | #define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); longjmp(h_buf_error,-1); } while (0)␊ |
241 | #define HASH_FSCK(hh,head) \␊ |
242 | do { \␊ |
243 | unsigned _bkt_i; \␊ |
244 | unsigned _count, _bkt_count; \␊ |
245 | char *_prev; \␊ |
246 | struct UT_hash_handle *_thh; \␊ |
247 | if (head) { \␊ |
248 | _count = 0; \␊ |
249 | for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \␊ |
250 | _bkt_count = 0; \␊ |
251 | _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \␊ |
252 | _prev = NULL; \␊ |
253 | while (_thh) { \␊ |
254 | if (_prev != (char*)(_thh->hh_prev)) { \␊ |
255 | HASH_OOPS("invalid hh_prev %p, actual %p\n", \␊ |
256 | _thh->hh_prev, _prev ); \␊ |
257 | } \␊ |
258 | _bkt_count++; \␊ |
259 | _prev = (char*)(_thh); \␊ |
260 | _thh = _thh->hh_next; \␊ |
261 | } \␊ |
262 | _count += _bkt_count; \␊ |
263 | if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \␊ |
264 | HASH_OOPS("invalid bucket count %d, actual %d\n", \␊ |
265 | (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \␊ |
266 | } \␊ |
267 | } \␊ |
268 | if (_count != (head)->hh.tbl->num_items) { \␊ |
269 | HASH_OOPS("invalid hh item count %d, actual %d\n", \␊ |
270 | (head)->hh.tbl->num_items, _count ); \␊ |
271 | } \␊ |
272 | /* traverse hh in app order; check next/prev integrity, count */ \␊ |
273 | _count = 0; \␊ |
274 | _prev = NULL; \␊ |
275 | _thh = &(head)->hh; \␊ |
276 | while (_thh) { \␊ |
277 | _count++; \␊ |
278 | if (_prev !=(char*)(_thh->prev)) { \␊ |
279 | HASH_OOPS("invalid prev %p, actual %p\n", \␊ |
280 | _thh->prev, _prev ); \␊ |
281 | } \␊ |
282 | _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \␊ |
283 | _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \␊ |
284 | (head)->hh.tbl->hho) : NULL ); \␊ |
285 | } \␊ |
286 | if (_count != (head)->hh.tbl->num_items) { \␊ |
287 | HASH_OOPS("invalid app item count %d, actual %d\n", \␊ |
288 | (head)->hh.tbl->num_items, _count ); \␊ |
289 | } \␊ |
290 | } \␊ |
291 | } while (0)␊ |
292 | #else␊ |
293 | #define HASH_FSCK(hh,head) ␊ |
294 | #endif␊ |
295 | ␊ |
296 | /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to ␊ |
297 | * the descriptor to which this macro is defined for tuning the hash function.␊ |
298 | * The app can #include <unistd.h> to get the prototype for write(2). */␊ |
299 | #ifdef HASH_EMIT_KEYS␊ |
300 | #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \␊ |
301 | do { \␊ |
302 | unsigned _klen = fieldlen; \␊ |
303 | write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \␊ |
304 | write(HASH_EMIT_KEYS, keyptr, fieldlen); \␊ |
305 | } while (0)␊ |
306 | #else ␊ |
307 | #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) ␊ |
308 | #endif␊ |
309 | ␊ |
310 | /* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */␊ |
311 | #ifdef HASH_FUNCTION ␊ |
312 | #define HASH_FCN HASH_FUNCTION␊ |
313 | #else␊ |
314 | #define HASH_FCN HASH_JEN␊ |
315 | #endif␊ |
316 | ␊ |
317 | /* The Bernstein hash function, used in Perl prior to v5.6 */␊ |
318 | #define HASH_BER(key,keylen,num_bkts,hashv,bkt) \␊ |
319 | do { \␊ |
320 | unsigned _hb_keylen=keylen; \␊ |
321 | char *_hb_key=(char*)(key); \␊ |
322 | (hashv) = 0; \␊ |
323 | while (_hb_keylen--) { (hashv) = ((hashv) * 33) + *_hb_key++; } \␊ |
324 | bkt = (hashv) & (num_bkts-1); \␊ |
325 | } while (0)␊ |
326 | ␊ |
327 | ␊ |
328 | /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at ␊ |
329 | * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */␊ |
330 | #define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \␊ |
331 | do { \␊ |
332 | unsigned _sx_i; \␊ |
333 | char *_hs_key=(char*)(key); \␊ |
334 | hashv = 0; \␊ |
335 | for(_sx_i=0; _sx_i < keylen; _sx_i++) \␊ |
336 | hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \␊ |
337 | bkt = hashv & (num_bkts-1); \␊ |
338 | } while (0)␊ |
339 | ␊ |
340 | #define HASH_FNV(key,keylen,num_bkts,hashv,bkt) \␊ |
341 | do { \␊ |
342 | unsigned _fn_i; \␊ |
343 | char *_hf_key=(char*)(key); \␊ |
344 | hashv = 2166136261UL; \␊ |
345 | for(_fn_i=0; _fn_i < keylen; _fn_i++) \␊ |
346 | hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \␊ |
347 | bkt = hashv & (num_bkts-1); \␊ |
348 | } while(0);␊ |
349 | ␊ |
350 | #define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \␊ |
351 | do { \␊ |
352 | unsigned _ho_i; \␊ |
353 | char *_ho_key=(char*)(key); \␊ |
354 | hashv = 0; \␊ |
355 | for(_ho_i=0; _ho_i < keylen; _ho_i++) { \␊ |
356 | hashv += _ho_key[_ho_i]; \␊ |
357 | hashv += (hashv << 10); \␊ |
358 | hashv ^= (hashv >> 6); \␊ |
359 | } \␊ |
360 | hashv += (hashv << 3); \␊ |
361 | hashv ^= (hashv >> 11); \␊ |
362 | hashv += (hashv << 15); \␊ |
363 | bkt = hashv & (num_bkts-1); \␊ |
364 | } while(0)␊ |
365 | ␊ |
366 | #define HASH_JEN_MIX(a,b,c) \␊ |
367 | do { \␊ |
368 | a -= b; a -= c; a ^= ( c >> 13 ); \␊ |
369 | b -= c; b -= a; b ^= ( a << 8 ); \␊ |
370 | c -= a; c -= b; c ^= ( b >> 13 ); \␊ |
371 | a -= b; a -= c; a ^= ( c >> 12 ); \␊ |
372 | b -= c; b -= a; b ^= ( a << 16 ); \␊ |
373 | c -= a; c -= b; c ^= ( b >> 5 ); \␊ |
374 | a -= b; a -= c; a ^= ( c >> 3 ); \␊ |
375 | b -= c; b -= a; b ^= ( a << 10 ); \␊ |
376 | c -= a; c -= b; c ^= ( b >> 15 ); \␊ |
377 | } while (0)␊ |
378 | ␊ |
379 | #define HASH_JEN(key,keylen,num_bkts,hashv,bkt) \␊ |
380 | do { \␊ |
381 | unsigned _hj_i,_hj_j,_hj_k; \␊ |
382 | char *_hj_key=(char*)(key); \␊ |
383 | hashv = 0xfeedbeef; \␊ |
384 | _hj_i = _hj_j = 0x9e3779b9; \␊ |
385 | _hj_k = keylen; \␊ |
386 | while (_hj_k >= 12) { \␊ |
387 | _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \␊ |
388 | + ( (unsigned)_hj_key[2] << 16 ) \␊ |
389 | + ( (unsigned)_hj_key[3] << 24 ) ); \␊ |
390 | _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \␊ |
391 | + ( (unsigned)_hj_key[6] << 16 ) \␊ |
392 | + ( (unsigned)_hj_key[7] << 24 ) ); \␊ |
393 | hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \␊ |
394 | + ( (unsigned)_hj_key[10] << 16 ) \␊ |
395 | + ( (unsigned)_hj_key[11] << 24 ) ); \␊ |
396 | \␊ |
397 | HASH_JEN_MIX(_hj_i, _hj_j, hashv); \␊ |
398 | \␊ |
399 | _hj_key += 12; \␊ |
400 | _hj_k -= 12; \␊ |
401 | } \␊ |
402 | hashv += keylen; \␊ |
403 | switch ( _hj_k ) { \␊ |
404 | case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \␊ |
405 | case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \␊ |
406 | case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \␊ |
407 | case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \␊ |
408 | case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \␊ |
409 | case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \␊ |
410 | case 5: _hj_j += _hj_key[4]; \␊ |
411 | case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \␊ |
412 | case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \␊ |
413 | case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \␊ |
414 | case 1: _hj_i += _hj_key[0]; \␊ |
415 | } \␊ |
416 | HASH_JEN_MIX(_hj_i, _hj_j, hashv); \␊ |
417 | bkt = hashv & (num_bkts-1); \␊ |
418 | } while(0)␊ |
419 | ␊ |
420 | /* The Paul Hsieh hash function */␊ |
421 | #undef get16bits␊ |
422 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \␊ |
423 | || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)␊ |
424 | #define get16bits(d) (*((const uint16_t *) (d)))␊ |
425 | #endif␊ |
426 | ␊ |
427 | #if !defined (get16bits)␊ |
428 | #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \␊ |
429 | +(uint32_t)(((const uint8_t *)(d))[0]) )␊ |
430 | #endif␊ |
431 | #define HASH_SFH(key,keylen,num_bkts,hashv,bkt) \␊ |
432 | do { \␊ |
433 | char *_sfh_key=(char*)(key); \␊ |
434 | uint32_t _sfh_tmp, _sfh_len = keylen; \␊ |
435 | \␊ |
436 | int _sfh_rem = _sfh_len & 3; \␊ |
437 | _sfh_len >>= 2; \␊ |
438 | hashv = 0xcafebabe; \␊ |
439 | \␊ |
440 | /* Main loop */ \␊ |
441 | for (;_sfh_len > 0; _sfh_len--) { \␊ |
442 | hashv += get16bits (_sfh_key); \␊ |
443 | _sfh_tmp = (get16bits (_sfh_key+2) << 11) ^ hashv; \␊ |
444 | hashv = (hashv << 16) ^ _sfh_tmp; \␊ |
445 | _sfh_key += 2*sizeof (uint16_t); \␊ |
446 | hashv += hashv >> 11; \␊ |
447 | } \␊ |
448 | \␊ |
449 | /* Handle end cases */ \␊ |
450 | switch (_sfh_rem) { \␊ |
451 | case 3: hashv += get16bits (_sfh_key); \␊ |
452 | hashv ^= hashv << 16; \␊ |
453 | hashv ^= _sfh_key[sizeof (uint16_t)] << 18; \␊ |
454 | hashv += hashv >> 11; \␊ |
455 | break; \␊ |
456 | case 2: hashv += get16bits (_sfh_key); \␊ |
457 | hashv ^= hashv << 11; \␊ |
458 | hashv += hashv >> 17; \␊ |
459 | break; \␊ |
460 | case 1: hashv += *_sfh_key; \␊ |
461 | hashv ^= hashv << 10; \␊ |
462 | hashv += hashv >> 1; \␊ |
463 | } \␊ |
464 | \␊ |
465 | /* Force "avalanching" of final 127 bits */ \␊ |
466 | hashv ^= hashv << 3; \␊ |
467 | hashv += hashv >> 5; \␊ |
468 | hashv ^= hashv << 4; \␊ |
469 | hashv += hashv >> 17; \␊ |
470 | hashv ^= hashv << 25; \␊ |
471 | hashv += hashv >> 6; \␊ |
472 | bkt = hashv & (num_bkts-1); \␊ |
473 | } while(0);␊ |
474 | ␊ |
475 | #ifdef HASH_USING_NO_STRICT_ALIASING␊ |
476 | /* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads.␊ |
477 | * For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error.␊ |
478 | * MurmurHash uses the faster approach only on CPU's where we know it's safe. ␊ |
479 | *␊ |
480 | * Note the preprocessor built-in defines can be emitted using:␊ |
481 | *␊ |
482 | * gcc -m64 -dM -E - < /dev/null (on gcc)␊ |
483 | * cc -## a.c (where a.c is a simple test file) (Sun Studio)␊ |
484 | */␊ |
485 | #if (defined(__i386__) || defined(__x86_64__)) ␊ |
486 | #define MUR_GETBLOCK(p,i) p[i]␊ |
487 | #else /* non intel */␊ |
488 | #define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 0x3) == 0)␊ |
489 | #define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 0x3) == 1)␊ |
490 | #define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 0x3) == 2)␊ |
491 | #define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 0x3) == 3)␊ |
492 | #define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL))␊ |
493 | #if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__))␊ |
494 | #define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24))␊ |
495 | #define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16))␊ |
496 | #define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8))␊ |
497 | #else /* assume little endian non-intel */␊ |
498 | #define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24))␊ |
499 | #define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16))␊ |
500 | #define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8))␊ |
501 | #endif␊ |
502 | #define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \␊ |
503 | (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \␊ |
504 | (MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \␊ |
505 | MUR_ONE_THREE(p))))␊ |
506 | #endif␊ |
507 | #define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))␊ |
508 | #define MUR_FMIX(_h) \␊ |
509 | do { \␊ |
510 | _h ^= _h >> 16; \␊ |
511 | _h *= 0x85ebca6b; \␊ |
512 | _h ^= _h >> 13; \␊ |
513 | _h *= 0xc2b2ae35l; \␊ |
514 | _h ^= _h >> 16; \␊ |
515 | } while(0)␊ |
516 | ␊ |
517 | #define HASH_MUR(key,keylen,num_bkts,hashv,bkt) \␊ |
518 | do { \␊ |
519 | const uint8_t *_mur_data = (const uint8_t*)(key); \␊ |
520 | const int _mur_nblocks = (keylen) / 4; \␊ |
521 | uint32_t _mur_h1 = 0xf88D5353; \␊ |
522 | uint32_t _mur_c1 = 0xcc9e2d51; \␊ |
523 | uint32_t _mur_c2 = 0x1b873593; \␊ |
524 | const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+_mur_nblocks*4); \␊ |
525 | int _mur_i; \␊ |
526 | for(_mur_i = -_mur_nblocks; _mur_i; _mur_i++) { \␊ |
527 | uint32_t _mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \␊ |
528 | _mur_k1 *= _mur_c1; \␊ |
529 | _mur_k1 = MUR_ROTL32(_mur_k1,15); \␊ |
530 | _mur_k1 *= _mur_c2; \␊ |
531 | \␊ |
532 | _mur_h1 ^= _mur_k1; \␊ |
533 | _mur_h1 = MUR_ROTL32(_mur_h1,13); \␊ |
534 | _mur_h1 = _mur_h1*5+0xe6546b64; \␊ |
535 | } \␊ |
536 | const uint8_t *_mur_tail = (const uint8_t*)(_mur_data + _mur_nblocks*4); \␊ |
537 | uint32_t _mur_k1=0; \␊ |
538 | switch((keylen) & 3) { \␊ |
539 | case 3: _mur_k1 ^= _mur_tail[2] << 16; \␊ |
540 | case 2: _mur_k1 ^= _mur_tail[1] << 8; \␊ |
541 | case 1: _mur_k1 ^= _mur_tail[0]; \␊ |
542 | _mur_k1 *= _mur_c1; \␊ |
543 | _mur_k1 = MUR_ROTL32(_mur_k1,15); \␊ |
544 | _mur_k1 *= _mur_c2; \␊ |
545 | _mur_h1 ^= _mur_k1; \␊ |
546 | } \␊ |
547 | _mur_h1 ^= (keylen); \␊ |
548 | MUR_FMIX(_mur_h1); \␊ |
549 | hashv = _mur_h1; \␊ |
550 | bkt = hashv & (num_bkts-1); \␊ |
551 | } while(0)␊ |
552 | #endif /* HASH_USING_NO_STRICT_ALIASING */␊ |
553 | ␊ |
554 | /* key comparison function; return 0 if keys equal */␊ |
555 | #define HASH_KEYCMP(a,b,len) memcmp(a,b,len) ␊ |
556 | ␊ |
557 | /* iterate over items in a known bucket to find desired item */␊ |
558 | #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \␊ |
559 | do { \␊ |
560 | if (head.hh_head) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,head.hh_head)); \␊ |
561 | else out=NULL; \␊ |
562 | while (out) { \␊ |
563 | if (out->hh.keylen == keylen_in) { \␊ |
564 | if ((HASH_KEYCMP(out->hh.key,keyptr,keylen_in)) == 0) break; \␊ |
565 | } \␊ |
566 | if (out->hh.hh_next) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,out->hh.hh_next)); \␊ |
567 | else out = NULL; \␊ |
568 | } \␊ |
569 | } while(0)␊ |
570 | ␊ |
571 | /* add an item to a bucket */␊ |
572 | #define HASH_ADD_TO_BKT(head,addhh) \␊ |
573 | do { \␊ |
574 | head.count++; \␊ |
575 | (addhh)->hh_next = head.hh_head; \␊ |
576 | (addhh)->hh_prev = NULL; \␊ |
577 | if (head.hh_head) { (head).hh_head->hh_prev = (addhh); } \␊ |
578 | (head).hh_head=addhh; \␊ |
579 | if (head.count >= ((head.expand_mult+1) * HASH_BKT_CAPACITY_THRESH) \␊ |
580 | && (addhh)->tbl->noexpand != 1) { \␊ |
581 | HASH_EXPAND_BUCKETS((addhh)->tbl); \␊ |
582 | } \␊ |
583 | } while(0)␊ |
584 | ␊ |
585 | /* remove an item from a given bucket */␊ |
586 | #define HASH_DEL_IN_BKT(hh,head,hh_del) \␊ |
587 | (head).count--; \␊ |
588 | if ((head).hh_head == hh_del) { \␊ |
589 | (head).hh_head = hh_del->hh_next; \␊ |
590 | } \␊ |
591 | if (hh_del->hh_prev) { \␊ |
592 | hh_del->hh_prev->hh_next = hh_del->hh_next; \␊ |
593 | } \␊ |
594 | if (hh_del->hh_next) { \␊ |
595 | hh_del->hh_next->hh_prev = hh_del->hh_prev; \␊ |
596 | } ␊ |
597 | ␊ |
598 | /* Bucket expansion has the effect of doubling the number of buckets␊ |
599 | * and redistributing the items into the new buckets. Ideally the␊ |
600 | * items will distribute more or less evenly into the new buckets␊ |
601 | * (the extent to which this is true is a measure of the quality of␊ |
602 | * the hash function as it applies to the key domain). ␊ |
603 | * ␊ |
604 | * With the items distributed into more buckets, the chain length␊ |
605 | * (item count) in each bucket is reduced. Thus by expanding buckets␊ |
606 | * the hash keeps a bound on the chain length. This bounded chain ␊ |
607 | * length is the essence of how a hash provides constant time lookup.␊ |
608 | * ␊ |
609 | * The calculation of tbl->ideal_chain_maxlen below deserves some␊ |
610 | * explanation. First, keep in mind that we're calculating the ideal␊ |
611 | * maximum chain length based on the *new* (doubled) bucket count.␊ |
612 | * In fractions this is just n/b (n=number of items,b=new num buckets).␊ |
613 | * Since the ideal chain length is an integer, we want to calculate ␊ |
614 | * ceil(n/b). We don't depend on floating point arithmetic in this␊ |
615 | * hash, so to calculate ceil(n/b) with integers we could write␊ |
616 | * ␊ |
617 | * ceil(n/b) = (n/b) + ((n%b)?1:0)␊ |
618 | * ␊ |
619 | * and in fact a previous version of this hash did just that.␊ |
620 | * But now we have improved things a bit by recognizing that b is␊ |
621 | * always a power of two. We keep its base 2 log handy (call it lb),␊ |
622 | * so now we can write this with a bit shift and logical AND:␊ |
623 | * ␊ |
624 | * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0)␊ |
625 | * ␊ |
626 | */␊ |
627 | #define HASH_EXPAND_BUCKETS(tbl) \␊ |
628 | do { \␊ |
629 | unsigned _he_bkt; \␊ |
630 | unsigned _he_bkt_i; \␊ |
631 | struct UT_hash_handle *_he_thh, *_he_hh_nxt; \␊ |
632 | UT_hash_bucket *_he_new_buckets, *_he_newbkt; \␊ |
633 | _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \␊ |
634 | 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \␊ |
635 | if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \␊ |
636 | memset(_he_new_buckets, 0, \␊ |
637 | 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \␊ |
638 | tbl->ideal_chain_maxlen = \␊ |
639 | (tbl->num_items >> (tbl->log2_num_buckets+1)) + \␊ |
640 | ((tbl->num_items & ((tbl->num_buckets*2)-1)) ? 1 : 0); \␊ |
641 | tbl->nonideal_items = 0; \␊ |
642 | for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \␊ |
643 | { \␊ |
644 | _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \␊ |
645 | while (_he_thh) { \␊ |
646 | _he_hh_nxt = _he_thh->hh_next; \␊ |
647 | HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2, _he_bkt); \␊ |
648 | _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \␊ |
649 | if (!_he_newbkt) { uthash_fatal( "out of memory"); } \␊ |
650 | if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \␊ |
651 | tbl->nonideal_items++; \␊ |
652 | _he_newbkt->expand_mult = _he_newbkt->count / \␊ |
653 | tbl->ideal_chain_maxlen; \␊ |
654 | } \␊ |
655 | _he_thh->hh_prev = NULL; \␊ |
656 | _he_thh->hh_next = _he_newbkt->hh_head; \␊ |
657 | if (_he_newbkt->hh_head) _he_newbkt->hh_head->hh_prev = \␊ |
658 | _he_thh; \␊ |
659 | _he_newbkt->hh_head = _he_thh; \␊ |
660 | _he_thh = _he_hh_nxt; \␊ |
661 | } \␊ |
662 | } \␊ |
663 | uthash_free( tbl->buckets, tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \␊ |
664 | tbl->num_buckets *= 2; \␊ |
665 | tbl->log2_num_buckets++; \␊ |
666 | tbl->buckets = _he_new_buckets; \␊ |
667 | tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \␊ |
668 | (tbl->ineff_expands+1) : 0; \␊ |
669 | if (tbl->ineff_expands > 1) { \␊ |
670 | tbl->noexpand=1; \␊ |
671 | uthash_noexpand_fyi(tbl); \␊ |
672 | } \␊ |
673 | uthash_expand_fyi(tbl); \␊ |
674 | } while(0)␊ |
675 | ␊ |
676 | ␊ |
677 | /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */␊ |
678 | /* Note that HASH_SORT assumes the hash handle name to be hh. ␊ |
679 | * HASH_SRT was added to allow the hash handle name to be passed in. */␊ |
680 | #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn)␊ |
681 | #define HASH_SRT(hh,head,cmpfcn) \␊ |
682 | do { \␊ |
683 | unsigned _hs_i; \␊ |
684 | unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \␊ |
685 | struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \␊ |
686 | if (head) { \␊ |
687 | _hs_insize = 1; \␊ |
688 | _hs_looping = 1; \␊ |
689 | _hs_list = &((head)->hh); \␊ |
690 | while (_hs_looping) { \␊ |
691 | _hs_p = _hs_list; \␊ |
692 | _hs_list = NULL; \␊ |
693 | _hs_tail = NULL; \␊ |
694 | _hs_nmerges = 0; \␊ |
695 | while (_hs_p) { \␊ |
696 | _hs_nmerges++; \␊ |
697 | _hs_q = _hs_p; \␊ |
698 | _hs_psize = 0; \␊ |
699 | for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \␊ |
700 | _hs_psize++; \␊ |
701 | _hs_q = (UT_hash_handle*)((_hs_q->next) ? \␊ |
702 | ((void*)((char*)(_hs_q->next) + \␊ |
703 | (head)->hh.tbl->hho)) : NULL); \␊ |
704 | if (! (_hs_q) ) break; \␊ |
705 | } \␊ |
706 | _hs_qsize = _hs_insize; \␊ |
707 | while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q )) { \␊ |
708 | if (_hs_psize == 0) { \␊ |
709 | _hs_e = _hs_q; \␊ |
710 | _hs_q = (UT_hash_handle*)((_hs_q->next) ? \␊ |
711 | ((void*)((char*)(_hs_q->next) + \␊ |
712 | (head)->hh.tbl->hho)) : NULL); \␊ |
713 | _hs_qsize--; \␊ |
714 | } else if ( (_hs_qsize == 0) || !(_hs_q) ) { \␊ |
715 | _hs_e = _hs_p; \␊ |
716 | _hs_p = (UT_hash_handle*)((_hs_p->next) ? \␊ |
717 | ((void*)((char*)(_hs_p->next) + \␊ |
718 | (head)->hh.tbl->hho)) : NULL); \␊ |
719 | _hs_psize--; \␊ |
720 | } else if (( \␊ |
721 | cmpfcn(DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \␊ |
722 | DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \␊ |
723 | ) <= 0) { \␊ |
724 | _hs_e = _hs_p; \␊ |
725 | _hs_p = (UT_hash_handle*)((_hs_p->next) ? \␊ |
726 | ((void*)((char*)(_hs_p->next) + \␊ |
727 | (head)->hh.tbl->hho)) : NULL); \␊ |
728 | _hs_psize--; \␊ |
729 | } else { \␊ |
730 | _hs_e = _hs_q; \␊ |
731 | _hs_q = (UT_hash_handle*)((_hs_q->next) ? \␊ |
732 | ((void*)((char*)(_hs_q->next) + \␊ |
733 | (head)->hh.tbl->hho)) : NULL); \␊ |
734 | _hs_qsize--; \␊ |
735 | } \␊ |
736 | if ( _hs_tail ) { \␊ |
737 | _hs_tail->next = ((_hs_e) ? \␊ |
738 | ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \␊ |
739 | } else { \␊ |
740 | _hs_list = _hs_e; \␊ |
741 | } \␊ |
742 | _hs_e->prev = ((_hs_tail) ? \␊ |
743 | ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \␊ |
744 | _hs_tail = _hs_e; \␊ |
745 | } \␊ |
746 | _hs_p = _hs_q; \␊ |
747 | } \␊ |
748 | _hs_tail->next = NULL; \␊ |
749 | if ( _hs_nmerges <= 1 ) { \␊ |
750 | _hs_looping=0; \␊ |
751 | (head)->hh.tbl->tail = _hs_tail; \␊ |
752 | DECLTYPE_ASSIGN(head,ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \␊ |
753 | } \␊ |
754 | _hs_insize *= 2; \␊ |
755 | } \␊ |
756 | HASH_FSCK(hh,head); \␊ |
757 | } \␊ |
758 | } while (0)␊ |
759 | ␊ |
760 | /* This function selects items from one hash into another hash. ␊ |
761 | * The end result is that the selected items have dual presence ␊ |
762 | * in both hashes. There is no copy of the items made; rather ␊ |
763 | * they are added into the new hash through a secondary hash ␊ |
764 | * hash handle that must be present in the structure. */␊ |
765 | #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \␊ |
766 | do { \␊ |
767 | unsigned _src_bkt, _dst_bkt; \␊ |
768 | void *_last_elt=NULL, *_elt; \␊ |
769 | UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \␊ |
770 | ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \␊ |
771 | if (src) { \␊ |
772 | for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \␊ |
773 | for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \␊ |
774 | _src_hh; \␊ |
775 | _src_hh = _src_hh->hh_next) { \␊ |
776 | _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \␊ |
777 | if (cond(_elt)) { \␊ |
778 | _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \␊ |
779 | _dst_hh->key = _src_hh->key; \␊ |
780 | _dst_hh->keylen = _src_hh->keylen; \␊ |
781 | _dst_hh->hashv = _src_hh->hashv; \␊ |
782 | _dst_hh->prev = _last_elt; \␊ |
783 | _dst_hh->next = NULL; \␊ |
784 | if (_last_elt_hh) { _last_elt_hh->next = _elt; } \␊ |
785 | if (!dst) { \␊ |
786 | DECLTYPE_ASSIGN(dst,_elt); \␊ |
787 | HASH_MAKE_TABLE(hh_dst,dst); \␊ |
788 | } else { \␊ |
789 | _dst_hh->tbl = (dst)->hh_dst.tbl; \␊ |
790 | } \␊ |
791 | HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \␊ |
792 | HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \␊ |
793 | (dst)->hh_dst.tbl->num_items++; \␊ |
794 | _last_elt = _elt; \␊ |
795 | _last_elt_hh = _dst_hh; \␊ |
796 | } \␊ |
797 | } \␊ |
798 | } \␊ |
799 | } \␊ |
800 | HASH_FSCK(hh_dst,dst); \␊ |
801 | } while (0)␊ |
802 | ␊ |
803 | #define HASH_CLEAR(hh,head) \␊ |
804 | do { \␊ |
805 | if (head) { \␊ |
806 | uthash_free((head)->hh.tbl->buckets, \␊ |
807 | (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \␊ |
808 | HASH_BLOOM_FREE((head)->hh.tbl); \␊ |
809 | uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \␊ |
810 | (head)=NULL; \␊ |
811 | } \␊ |
812 | } while(0)␊ |
813 | ␊ |
814 | #ifdef NO_DECLTYPE␊ |
815 | #define HASH_ITER(hh,head,el,tmp) \␊ |
816 | for((el)=(head), (*(char**)(&(tmp)))=(char*)((head)?(head)->hh.next:NULL); \␊ |
817 | el; (el)=(tmp),(*(char**)(&(tmp)))=(char*)((tmp)?(tmp)->hh.next:NULL)) ␊ |
818 | #else␊ |
819 | #define HASH_ITER(hh,head,el,tmp) \␊ |
820 | for((el)=(head),(tmp)=DECLTYPE(el)((head)?(head)->hh.next:NULL); \␊ |
821 | el; (el)=(tmp),(tmp)=DECLTYPE(el)((tmp)?(tmp)->hh.next:NULL))␊ |
822 | #endif␊ |
823 | ␊ |
824 | /* obtain a count of items in the hash */␊ |
825 | #define HASH_COUNT(head) HASH_CNT(hh,head) ␊ |
826 | #define HASH_CNT(hh,head) ((head)?((head)->hh.tbl->num_items):0)␊ |
827 | ␊ |
828 | typedef struct UT_hash_bucket {␊ |
829 | struct UT_hash_handle *hh_head;␊ |
830 | unsigned count;␊ |
831 | ␊ |
832 | /* expand_mult is normally set to 0. In this situation, the max chain length␊ |
833 | * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If␊ |
834 | * the bucket's chain exceeds this length, bucket expansion is triggered). ␊ |
835 | * However, setting expand_mult to a non-zero value delays bucket expansion␊ |
836 | * (that would be triggered by additions to this particular bucket)␊ |
837 | * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH.␊ |
838 | * (The multiplier is simply expand_mult+1). The whole idea of this␊ |
839 | * multiplier is to reduce bucket expansions, since they are expensive, in␊ |
840 | * situations where we know that a particular bucket tends to be overused.␊ |
841 | * It is better to let its chain length grow to a longer yet-still-bounded␊ |
842 | * value, than to do an O(n) bucket expansion too often. ␊ |
843 | */␊ |
844 | unsigned expand_mult;␊ |
845 | ␊ |
846 | } UT_hash_bucket;␊ |
847 | ␊ |
848 | /* random signature used only to find hash tables in external analysis */␊ |
849 | #define HASH_SIGNATURE 0xa0111fe1␊ |
850 | #define HASH_BLOOM_SIGNATURE 0xb12220f2␊ |
851 | ␊ |
852 | typedef struct UT_hash_table {␊ |
853 | UT_hash_bucket *buckets;␊ |
854 | unsigned num_buckets, log2_num_buckets;␊ |
855 | unsigned num_items;␊ |
856 | struct UT_hash_handle *tail; /* tail hh in app order, for fast append */␊ |
857 | ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */␊ |
858 | ␊ |
859 | /* in an ideal situation (all buckets used equally), no bucket would have␊ |
860 | * more than ceil(#items/#buckets) items. that's the ideal chain length. */␊ |
861 | unsigned ideal_chain_maxlen;␊ |
862 | ␊ |
863 | /* nonideal_items is the number of items in the hash whose chain position␊ |
864 | * exceeds the ideal chain maxlen. these items pay the penalty for an uneven␊ |
865 | * hash distribution; reaching them in a chain traversal takes >ideal steps */␊ |
866 | unsigned nonideal_items;␊ |
867 | ␊ |
868 | /* ineffective expands occur when a bucket doubling was performed, but ␊ |
869 | * afterward, more than half the items in the hash had nonideal chain␊ |
870 | * positions. If this happens on two consecutive expansions we inhibit any␊ |
871 | * further expansion, as it's not helping; this happens when the hash␊ |
872 | * function isn't a good fit for the key domain. When expansion is inhibited␊ |
873 | * the hash will still work, albeit no longer in constant time. */␊ |
874 | unsigned ineff_expands, noexpand;␊ |
875 | ␊ |
876 | uint32_t signature; /* used only to find hash tables in external analysis */␊ |
877 | #ifdef HASH_BLOOM␊ |
878 | uint32_t bloom_sig; /* used only to test bloom exists in external analysis */␊ |
879 | uint8_t *bloom_bv;␊ |
880 | char bloom_nbits;␊ |
881 | #endif␊ |
882 | ␊ |
883 | } UT_hash_table;␊ |
884 | ␊ |
885 | typedef struct UT_hash_handle {␊ |
886 | struct UT_hash_table *tbl;␊ |
887 | void *prev; /* prev element in app order */␊ |
888 | void *next; /* next element in app order */␊ |
889 | struct UT_hash_handle *hh_prev; /* previous hh in bucket order */␊ |
890 | struct UT_hash_handle *hh_next; /* next hh in bucket order */␊ |
891 | void *key; /* ptr to enclosing struct's key */␊ |
892 | unsigned keylen; /* enclosing struct's key len */␊ |
893 | unsigned hashv; /* result of hash-fcn(key) */␊ |
894 | } UT_hash_handle;␊ |
895 | ␊ |
896 | #endif /* UTHASH_H */␊ |
897 | |