1 /* $NetBSD: ht-internal.h,v 1.2 2013/04/11 16:56:41 christos Exp $ */
2 /* Based on work Copyright 2002 Christopher Clark */
3 /* Copyright 2005-2012 Nick Mathewson */
4 /* Copyright 2009-2012 Niels Provos and Nick Mathewson */
5 /* See license at end. */
7 /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
12 #define HT_HEAD(name, type) \
14 /* The hash table itself. */ \
15 struct type **hth_table; \
16 /* How long is the hash table? */ \
17 unsigned hth_table_length; \
18 /* How many elements does the table contain? */ \
19 unsigned hth_n_entries; \
20 /* How many elements will we allow in the table before resizing it? */ \
21 unsigned hth_load_limit; \
22 /* Position of hth_table_length in the primes table. */ \
26 #define HT_INITIALIZER() \
29 #ifdef HT_CACHE_HASH_VALUES
30 #define HT_ENTRY(type) \
32 struct type *hte_next; \
36 #define HT_ENTRY(type) \
38 struct type *hte_next; \
42 #define HT_EMPTY(head) \
43 ((head)->hth_n_entries == 0)
45 /* How many elements in 'head'? */
46 #define HT_SIZE(head) \
47 ((head)->hth_n_entries)
49 #define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
50 #define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
51 #define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
52 #define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
53 #define HT_START(name, head) name##_HT_START(head)
54 #define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
55 #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
56 #define HT_CLEAR(name, head) name##_HT_CLEAR(head)
57 #define HT_INIT(name, head) name##_HT_INIT(head)
59 static inline unsigned
60 ht_improve_hash(unsigned h
)
62 /* Aim to protect against poor hash functions by adding logic here
63 * - logic taken from java 1.4 hashtable source */
65 h
^= ((h
>> 14) | (h
<< 18)); /* >>> */
67 h
^= ((h
>> 10) | (h
<< 22)); /* >>> */
72 /** Basic string hash function, from Java standard String.hashCode(). */
73 static inline unsigned
74 ht_string_hash(const char *s
)
79 h
+= ((signed char)*s
++)*m
;
80 m
= (m
<<5)-1; /* m *= 31 */
86 /** Basic string hash function, from Python's str.__hash__() */
87 static inline unsigned
88 ht_string_hash(const char *s
)
91 const unsigned char *cp
= (const unsigned char *)s
;
94 h
= (1000003*h
) ^ *cp
++;
96 /* This conversion truncates the length of the string, but that's ok. */
97 h
^= (unsigned)(cp
-(const unsigned char*)s
);
101 #ifdef HT_CACHE_HASH_VALUES
102 #define _HT_SET_HASH(elm, field, hashfn) \
103 do { (elm)->field.hte_hash = hashfn(elm); } while (/*CONSTCOND*/0)
104 #define _HT_SET_HASHVAL(elm, field, val) \
105 do { (elm)->field.hte_hash = (val); } while (/*CONSTCOND*/0)
106 #define _HT_ELT_HASH(elm, field, hashfn) \
107 ((elm)->field.hte_hash)
109 #define _HT_SET_HASH(elm, field, hashfn) \
111 #define _HT_ELT_HASH(elm, field, hashfn) \
113 #define _HT_SET_HASHVAL(elm, field, val) \
117 /* Helper: alias for the bucket containing 'elm'. */
118 #define _HT_BUCKET(head, field, elm, hashfn) \
119 ((head)->hth_table[_HT_ELT_HASH(elm,field,hashfn) % head->hth_table_length])
121 #define HT_FOREACH(x, name, head) \
122 for ((x) = HT_START(name, head); \
124 (x) = HT_NEXT(name, head, x))
126 #define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
127 int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
128 void name##_HT_CLEAR(struct name *ht); \
129 int _##name##_HT_REP_IS_BAD(const struct name *ht); \
131 name##_HT_INIT(struct name *head) { \
132 head->hth_table_length = 0; \
133 head->hth_table = NULL; \
134 head->hth_n_entries = 0; \
135 head->hth_load_limit = 0; \
136 head->hth_prime_idx = -1; \
138 /* Helper: returns a pointer to the right location in the table \
139 * 'head' to find or insert the element 'elm'. */ \
140 static inline struct type ** \
141 _##name##_HT_FIND_P(struct name *head, struct type *elm) \
144 if (!head->hth_table) \
146 p = &_HT_BUCKET(head, field, elm, hashfn); \
150 p = &(*p)->field.hte_next; \
154 /* Return a pointer to the element in the table 'head' matching 'elm', \
155 * or NULL if no such element exists */ \
156 static inline struct type * \
157 name##_HT_FIND(const struct name *head, struct type *elm) \
160 struct name *h = __UNCONST(head); \
161 _HT_SET_HASH(elm, field, hashfn); \
162 p = _##name##_HT_FIND_P(h, elm); \
163 return p ? *p : NULL; \
165 /* Insert the element 'elm' into the table 'head'. Do not call this \
166 * function if the table might already contain a matching element. */ \
168 name##_HT_INSERT(struct name *head, struct type *elm) \
171 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
172 name##_HT_GROW(head, head->hth_n_entries+1); \
173 ++head->hth_n_entries; \
174 _HT_SET_HASH(elm, field, hashfn); \
175 p = &_HT_BUCKET(head, field, elm, hashfn); \
176 elm->field.hte_next = *p; \
179 /* Insert the element 'elm' into the table 'head'. If there already \
180 * a matching element in the table, replace that element and return \
182 static inline struct type * \
183 name##_HT_REPLACE(struct name *head, struct type *elm) \
185 struct type **p, *r; \
186 if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
187 name##_HT_GROW(head, head->hth_n_entries+1); \
188 _HT_SET_HASH(elm, field, hashfn); \
189 p = _##name##_HT_FIND_P(head, elm); \
192 if (r && (r!=elm)) { \
193 elm->field.hte_next = r->field.hte_next; \
194 r->field.hte_next = NULL; \
197 ++head->hth_n_entries; \
201 /* Remove any element matching 'elm' from the table 'head'. If such \
202 * an element is found, return it; otherwise return NULL. */ \
203 static inline struct type * \
204 name##_HT_REMOVE(struct name *head, struct type *elm) \
206 struct type **p, *r; \
207 _HT_SET_HASH(elm, field, hashfn); \
208 p = _##name##_HT_FIND_P(head,elm); \
212 *p = r->field.hte_next; \
213 r->field.hte_next = NULL; \
214 --head->hth_n_entries; \
217 /* Invoke the function 'fn' on every element of the table 'head', \
218 * using 'data' as its second argument. If the function returns \
219 * nonzero, remove the most recently examined element before invoking \
220 * the function again. */ \
222 name##_HT_FOREACH_FN(struct name *head, \
223 int (*fn)(struct type *, void *), \
227 struct type **p, **nextp, *next; \
228 if (!head->hth_table) \
230 for (idx=0; idx < head->hth_table_length; ++idx) { \
231 p = &head->hth_table[idx]; \
233 nextp = &(*p)->field.hte_next; \
235 if (fn(*p, data)) { \
236 --head->hth_n_entries; \
244 /* Return a pointer to the first element in the table 'head', under \
245 * an arbitrary order. This order is stable under remove operations, \
246 * but not under others. If the table is empty, return NULL. */ \
247 static inline struct type ** \
248 name##_HT_START(struct name *head) \
251 while (b < head->hth_table_length) { \
252 if (head->hth_table[b]) \
253 return &head->hth_table[b]; \
258 /* Return the next element in 'head' after 'elm', under the arbitrary \
259 * order used by HT_START. If there are no more elements, return \
260 * NULL. If 'elm' is to be removed from the table, you must call \
261 * this function for the next value before you remove it. \
263 static inline struct type ** \
264 name##_HT_NEXT(struct name *head, struct type **elm) \
266 if ((*elm)->field.hte_next) { \
267 return &(*elm)->field.hte_next; \
269 unsigned b = (_HT_ELT_HASH(*elm, field, hashfn) % head->hth_table_length)+1; \
270 while (b < head->hth_table_length) { \
271 if (head->hth_table[b]) \
272 return &head->hth_table[b]; \
278 static inline struct type ** \
279 name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
281 unsigned h = _HT_ELT_HASH(*elm, field, hashfn); \
282 *elm = (*elm)->field.hte_next; \
283 --head->hth_n_entries; \
287 unsigned b = (h % head->hth_table_length)+1; \
288 while (b < head->hth_table_length) { \
289 if (head->hth_table[b]) \
290 return &head->hth_table[b]; \
297 #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, \
299 static unsigned name##_PRIMES[] = { \
301 769, 1543, 3079, 6151, \
302 12289, 24593, 49157, 98317, \
303 196613, 393241, 786433, 1572869, \
304 3145739, 6291469, 12582917, 25165843, \
305 50331653, 100663319, 201326611, 402653189, \
306 805306457, 1610612741 \
308 static unsigned name##_N_PRIMES = \
309 (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \
310 /* Expand the internal table of 'head' until it is large enough to \
311 * hold 'size' elements. Return 0 on success, -1 on allocation \
314 name##_HT_GROW(struct name *head, unsigned size) \
316 unsigned new_len, new_load_limit; \
318 struct type **new_table; \
319 if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
321 if (head->hth_load_limit > size) \
323 prime_idx = head->hth_prime_idx; \
325 new_len = name##_PRIMES[++prime_idx]; \
326 new_load_limit = (unsigned)(load*new_len); \
327 } while (new_load_limit <= size && \
328 prime_idx < (int)name##_N_PRIMES); \
329 if ((new_table = mallocfn(new_len*sizeof(struct type*)))) { \
331 memset(new_table, 0, new_len*sizeof(struct type*)); \
332 for (b = 0; b < head->hth_table_length; ++b) { \
333 struct type *elm, *next; \
335 elm = head->hth_table[b]; \
337 next = elm->field.hte_next; \
338 b2 = _HT_ELT_HASH(elm, field, hashfn) % new_len; \
339 elm->field.hte_next = new_table[b2]; \
340 new_table[b2] = elm; \
344 if (head->hth_table) \
345 freefn(head->hth_table); \
346 head->hth_table = new_table; \
349 new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
350 if (!new_table) return -1; \
351 memset(new_table + head->hth_table_length, 0, \
352 (new_len - head->hth_table_length)*sizeof(struct type*)); \
353 for (b=0; b < head->hth_table_length; ++b) { \
354 struct type *e, **pE; \
355 for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
356 b2 = _HT_ELT_HASH(e, field, hashfn) % new_len; \
358 pE = &e->field.hte_next; \
360 *pE = e->field.hte_next; \
361 e->field.hte_next = new_table[b2]; \
366 head->hth_table = new_table; \
368 head->hth_table_length = new_len; \
369 head->hth_prime_idx = prime_idx; \
370 head->hth_load_limit = new_load_limit; \
373 /* Free all storage held by 'head'. Does not free 'head' itself, or \
374 * individual elements. */ \
376 name##_HT_CLEAR(struct name *head) \
378 if (head->hth_table) \
379 freefn(head->hth_table); \
380 head->hth_table_length = 0; \
381 name##_HT_INIT(head); \
383 /* Debugging helper: return false iff the representation of 'head' is \
384 * internally consistent. */ \
386 _##name##_HT_REP_IS_BAD(const struct name *head) \
390 if (!head->hth_table_length) { \
391 if (!head->hth_table && !head->hth_n_entries && \
392 !head->hth_load_limit && head->hth_prime_idx == -1) \
397 if (!head->hth_table || head->hth_prime_idx < 0 || \
398 !head->hth_load_limit) \
400 if (head->hth_n_entries > head->hth_load_limit) \
402 if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
404 if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
406 for (n = i = 0; i < head->hth_table_length; ++i) { \
407 for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
408 if (_HT_ELT_HASH(elm, field, hashfn) != hashfn(elm)) \
410 if ((_HT_ELT_HASH(elm, field, hashfn) % head->hth_table_length) != i) \
415 if (n != head->hth_n_entries) \
420 /** Implements an over-optimized "find and insert if absent" block;
421 * not meant for direct usage by typical code, or usage outside the critical
423 #define _HT_FIND_OR_INSERT(name, field, hashfn, head, eltype, elm, var, y, n) \
425 struct name *_##var##_head = head; \
426 struct eltype **var; \
427 if (!_##var##_head->hth_table || \
428 _##var##_head->hth_n_entries >= _##var##_head->hth_load_limit) \
429 name##_HT_GROW(_##var##_head, _##var##_head->hth_n_entries+1); \
430 _HT_SET_HASH((elm), field, hashfn); \
431 var = _##name##_HT_FIND_P(_##var##_head, (elm)); \
438 #define _HT_FOI_INSERT(field, head, elm, newent, var) \
440 _HT_SET_HASHVAL(newent, field, (elm)->field.hte_hash); \
441 newent->field.hte_next = NULL; \
443 ++((head)->hth_n_entries); \
447 * Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
448 * by Cristopher Clark, retrofit to allow drop-in memory management, and to
449 * use the same interface as Niels Provos's tree.h. This is probably still
450 * a derived work, so the original license below still applies.
452 * Copyright (c) 2002, Christopher Clark
453 * All rights reserved.
455 * Redistribution and use in source and binary forms, with or without
456 * modification, are permitted provided that the following conditions
459 * * Redistributions of source code must retain the above copyright
460 * notice, this list of conditions and the following disclaimer.
462 * * Redistributions in binary form must reproduce the above copyright
463 * notice, this list of conditions and the following disclaimer in the
464 * documentation and/or other materials provided with the distribution.
466 * * Neither the name of the original author; nor the names of any contributors
467 * may be used to endorse or promote products derived from this software
468 * without specific prior written permission.
471 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
472 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
473 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
474 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
475 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
476 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
477 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
478 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
479 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
480 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
481 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.