2 * Sherlock Library -- Universal Hash Table
4 * (c) 2002--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2002 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 * This is not a normal header file, it's a generator of hash tables.
13 * Each time you include it with parameters set in the corresponding
14 * preprocessor macros, it generates a hash table with the parameters
17 * You need to specify:
19 * HASH_NODE data type where a node dwells (usually a struct).
20 * HASH_PREFIX(x) macro to add a name prefix (used on all global names
21 * defined by the hash table generator).
23 * Then decide on type of keys:
25 * HASH_KEY_ATOMIC=f use node->f as a key of an atomic type (i.e.,
26 * a type which can be compared using `==')
27 * HASH_ATOMIC_TYPE (defaults to int).
28 * | HASH_KEY_STRING=f use node->f as a string key, allocated
29 * separately from the rest of the node.
30 * | HASH_KEY_ENDSTRING=f use node->f as a string key, allocated
31 * automatically at the end of the node struct
32 * (to be declared as "char f[1]" at the end).
33 * | HASH_KEY_COMPLEX use a multi-component key; as the name suggests,
34 * the passing of parameters is a bit complex then.
35 * The HASH_KEY_COMPLEX(x) macro should expand to
36 * `x k1, x k2, ... x kn' and you should also define:
37 * HASH_KEY_DECL declaration of function parameters in which key
38 * should be passed to all hash table operations.
39 * That is, `type1 k1, type2 k2, ... typen kn'.
40 * With complex keys, HASH_GIVE_HASHFN and HASH_GIVE_EQ
43 * Then specify what operations you request (all names are automatically
44 * prefixed by calling HASH_PREFIX):
46 * <always defined> init() -- initialize the hash table.
47 * HASH_WANT_CLEANUP cleanup() -- deallocate the hash table.
48 * HASH_WANT_FIND node *find(key) -- find first node with the specified
49 * key, return NULL if no such node exists.
50 * HASH_WANT_FIND_NEXT node *find(node *start) -- find next node with the
51 * specified key, return NULL if no such node exists.
52 * HASH_WANT_NEW node *new(key) -- create new node with given key.
53 * Doesn't check whether it already exists.
54 * HASH_WANT_LOOKUP node *lookup(key) -- find node with given key,
55 * if it doesn't exist, create it. Defining
56 * HASH_GIVE_INIT_DATA is strongly recommended.
57 * HASH_WANT_DELETE int delete(key) -- delete and deallocate node
58 * with given key. Returns success.
59 * HASH_WANT_REMOVE remove(node *) -- delete and deallocate given node.
61 * You can also supply several functions:
63 * HASH_GIVE_HASHFN unsigned int hash(key) -- calculate hash value of key.
64 * We have sensible default hash functions for strings
66 * HASH_GIVE_EQ int eq(key1, key2) -- return whether keys are equal.
67 * By default, we use == for atomic types and either
68 * strcmp or strcasecmp for strings.
69 * HASH_GIVE_EXTRA_SIZE int extra_size(key) -- returns how many bytes after the
70 * node should be allocated for dynamic data. Default=0
71 * or length of the string with HASH_KEY_ENDSTRING.
72 * HASH_GIVE_INIT_KEY void init_key(node *,key) -- initialize key in a newly
73 * created node. Defaults: assignment for atomic keys
74 * and static strings, strcpy for end-allocated strings.
75 * HASH_GIVE_INIT_DATA void init_data(node *) -- initialize data fields in a
76 * newly created node. Very useful for lookup operations.
77 * HASH_GIVE_ALLOC void *alloc(unsigned int size) -- allocate space for
78 * a node. Default is either normal or pooled allocation
79 * depending on whether we want deletions.
80 * void free(void *) -- the converse.
82 * ... and a couple of extra parameters:
84 * HASH_NOCASE string comparisons should be case-insensitive.
85 * HASH_DEFAULT_SIZE=n initially, use hash table of approx. `n' entries.
86 * HASH_CONSERVE_SPACE use as little space as possible.
87 * HASH_FN_BITS=n The hash function gives only `n' significant bits.
88 * HASH_ATOMIC_TYPE=t Atomic values are of type `t' instead of int.
89 * HASH_USE_POOL=pool Allocate all nodes from given mempool.
90 * Collides with delete/remove functions.
91 * HASH_AUTO_POOL=size Create a pool of the given block size automatically.
93 * You also get a iterator macro at no extra charge:
95 * HASH_FOR_ALL(hash_prefix, variable)
97 * // node *variable gets declared automatically
98 * do_something_with_node(variable);
99 * // use HASH_BREAK and HASH_CONTINUE instead of break and continue
100 * // you must not alter contents of the hash table here
104 * Then include "lib/hashtable.h" and voila, you have a hash table
105 * suiting all your needs (at least those which you've revealed :) ).
107 * After including this file, all parameter macros are automatically
111 #ifndef _SHERLOCK_HASHFUNC_H
112 #include "lib/hashfunc.h"
117 #if !defined(HASH_NODE) || !defined(HASH_PREFIX)
118 #error Some of the mandatory configuration macros are missing.
121 #define P(x) HASH_PREFIX(x)
123 /* Declare buckets and the hash table */
125 typedef HASH_NODE P(node);
127 typedef struct P(bucket) {
128 struct P(bucket) *next;
129 #ifndef HASH_CONSERVE_SPACE
137 uns hash_count, hash_max, hash_min, hash_hard_max;
143 /* Preset parameters */
145 #if defined(HASH_KEY_ATOMIC)
147 #define HASH_KEY(x) x HASH_KEY_ATOMIC
149 #ifndef HASH_ATOMIC_TYPE
150 # define HASH_ATOMIC_TYPE int
152 #define HASH_KEY_DECL HASH_ATOMIC_TYPE HASH_KEY( )
154 #ifndef HASH_GIVE_HASHFN
155 # define HASH_GIVE_HASHFN
156 static inline int P(hash) (HASH_ATOMIC_TYPE x)
157 { return hash_int(x); }
161 # define HASH_GIVE_EQ
162 static inline int P(eq) (HASH_ATOMIC_TYPE x, HASH_ATOMIC_TYPE y)
166 #ifndef HASH_GIVE_INIT_KEY
167 # define HASH_GIVE_INIT_KEY
168 static inline void P(init_key) (P(node) *n, HASH_ATOMIC_TYPE k)
169 { HASH_KEY(n->) = k; }
172 #ifndef HASH_CONSERVE_SPACE
173 #define HASH_CONSERVE_SPACE
176 #elif defined(HASH_KEY_STRING) || defined(HASH_KEY_ENDSTRING)
178 #ifdef HASH_KEY_STRING
179 # define HASH_KEY(x) x HASH_KEY_STRING
180 # ifndef HASH_GIVE_INIT_KEY
181 # define HASH_GIVE_INIT_KEY
182 static inline void P(init_key) (P(node) *n, char *k)
183 { HASH_KEY(n->) = k; }
186 # define HASH_KEY(x) x HASH_KEY_ENDSTRING
187 # define HASH_GIVE_EXTRA_SIZE
188 static inline int P(extra_size) (char *k)
189 { return strlen(k); }
190 # ifndef HASH_GIVE_INIT_KEY
191 # define HASH_GIVE_INIT_KEY
192 static inline void P(init_key) (P(node) *n, char *k)
193 { strcpy(HASH_KEY(n->), k); }
196 #define HASH_KEY_DECL char *HASH_KEY( )
198 #ifndef HASH_GIVE_HASHFN
199 #define HASH_GIVE_HASHFN
200 static inline uns P(hash) (char *k)
203 return hash_string_nocase(k);
205 return hash_string(k);
211 # define HASH_GIVE_EQ
212 static inline int P(eq) (char *x, char *y)
215 return !strcasecmp(x,y);
222 #elif defined(HASH_KEY_COMPLEX)
224 #define HASH_KEY(x) HASH_KEY_COMPLEX(x)
227 #error You forgot to set the hash key type.
230 /* Defaults for missing parameters */
232 #ifndef HASH_GIVE_HASHFN
233 #error Unable to determine which hash function to use.
237 #error Unable to determine how to compare two keys.
240 #ifdef HASH_GIVE_EXTRA_SIZE
241 /* This trickery is needed to avoid `unused parameter' warnings */
242 #define HASH_EXTRA_SIZE P(extra_size)
245 * Beware, C macros are expanded iteratively, not recursively,
246 * hence we get only a _single_ argument, although the expansion
247 * of HASH_KEY contains commas.
249 #define HASH_EXTRA_SIZE(x) 0
252 #ifndef HASH_GIVE_INIT_KEY
253 #error Unable to determine how to initialize keys.
256 #ifndef HASH_GIVE_INIT_DATA
257 static inline void P(init_data) (P(node) *n UNUSED)
264 #ifdef HASH_GIVE_ALLOC
265 /* If the caller has requested to use his own allocation functions, do so */
266 static inline void P(init_alloc) (void) { }
267 static inline void P(cleanup_alloc) (void) { }
269 #elif defined(HASH_USE_POOL)
270 /* If the caller has requested to use his mempool, do so */
271 #include "lib/mempool.h"
272 static inline void * P(alloc) (unsigned int size) { return mp_alloc_fast(HASH_USE_POOL, size); }
273 static inline void P(init_alloc) (void) { }
274 static inline void P(cleanup_alloc) (void) { }
276 #elif defined(HASH_AUTO_POOL)
277 /* Use our own pools */
278 #include "lib/mempool.h"
279 static struct mempool *P(pool);
280 static inline void * P(alloc) (unsigned int size) { return mp_alloc_fast(P(pool), size); }
281 static inline void P(init_alloc) (void) { P(pool) = mp_new(HASH_AUTO_POOL); }
282 static inline void P(cleanup_alloc) (void) { mp_delete(P(pool)); }
285 /* The default allocation method */
286 static inline void * P(alloc) (unsigned int size) { return xmalloc(size); }
287 static inline void P(free) (void *x) { xfree(x); }
288 static inline void P(init_alloc) (void) { }
289 static inline void P(cleanup_alloc) (void) { }
293 #ifndef HASH_DEFAULT_SIZE
294 #define HASH_DEFAULT_SIZE 32
298 #define HASH_FN_BITS 32
301 /* Now the operations */
303 static void P(alloc_table) (void)
305 T.hash_size = nextprime(T.hash_size);
306 T.ht = xmalloc(sizeof(void *) * T.hash_size);
307 bzero(T.ht, sizeof(void *) * T.hash_size);
308 if (2*T.hash_size < T.hash_hard_max)
309 T.hash_max = 2*T.hash_size;
312 if (T.hash_size/2 > HASH_DEFAULT_SIZE)
313 T.hash_min = T.hash_size/4;
318 static void P(init) (void)
321 T.hash_size = HASH_DEFAULT_SIZE;
322 #if HASH_FN_BITS < 28
323 T.hash_hard_max = 1 << HASH_FN_BITS;
325 T.hash_hard_max = 1 << 28;
331 #ifdef HASH_WANT_CLEANUP
332 static void P(cleanup) (void)
334 #ifndef HASH_USE_POOL
338 for (i=0; i<T.hash_size; i++)
339 for (b=T.ht[i]; b; b=bb)
350 static inline uns P(bucket_hash) (P(bucket) *b)
352 #ifdef HASH_CONSERVE_SPACE
353 return P(hash)(HASH_KEY(b->n.));
359 static void P(rehash) (uns size)
362 P(bucket) **oldt = T.ht, **newt;
363 uns oldsize = T.hash_size;
366 // log(L_DEBUG, "Rehashing %d->%d at count %d", oldsize, size, T.hash_count);
370 for (i=0; i<oldsize; i++)
376 h = P(bucket_hash)(b) % T.hash_size;
385 #ifdef HASH_WANT_FIND
386 static P(node) * P(find) (HASH_KEY_DECL)
388 uns h0 = P(hash) (HASH_KEY( ));
389 uns h = h0 % T.hash_size;
392 for (b=T.ht[h]; b; b=b->next)
395 #ifndef HASH_CONSERVE_SPACE
398 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
405 #ifdef HASH_WANT_FIND_NEXT
406 static P(node) * P(find_next) (P(node) *start)
408 #ifndef HASH_CONSERVE_SPACE
409 uns h0 = P(hash) (HASH_KEY(start->));
411 P(bucket) *b = SKIP_BACK(P(bucket), n, start);
413 for (b=b->next; b; b=b->next)
416 #ifndef HASH_CONSERVE_SPACE
419 P(eq)(HASH_KEY(start->), HASH_KEY(b->n.)))
427 static P(node) * P(new) (HASH_KEY_DECL)
432 h0 = P(hash) (HASH_KEY( ));
433 h = h0 % T.hash_size;
434 b = P(alloc) (sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
437 #ifndef HASH_CONSERVE_SPACE
440 P(init_key)(&b->n, HASH_KEY( ));
442 if (T.hash_count++ >= T.hash_max)
443 P(rehash)(2*T.hash_size);
448 #ifdef HASH_WANT_LOOKUP
449 static P(node) * P(lookup) (HASH_KEY_DECL)
451 uns h0 = P(hash) (HASH_KEY( ));
452 uns h = h0 % T.hash_size;
455 for (b=T.ht[h]; b; b=b->next)
458 #ifndef HASH_CONSERVE_SPACE
461 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
465 b = P(alloc) (sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
468 #ifndef HASH_CONSERVE_SPACE
471 P(init_key)(&b->n, HASH_KEY( ));
473 if (T.hash_count++ >= T.hash_max)
474 P(rehash)(2*T.hash_size);
479 #ifdef HASH_WANT_DELETE
480 static int P(delete) (HASH_KEY_DECL)
482 uns h0 = P(hash) (HASH_KEY( ));
483 uns h = h0 % T.hash_size;
486 for (bb=&T.ht[h]; b=*bb; bb=&b->next)
489 #ifndef HASH_CONSERVE_SPACE
492 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
496 if (--T.hash_count < T.hash_min)
497 P(rehash)(T.hash_size/2);
505 #ifdef HASH_WANT_REMOVE
506 static void P(remove) (P(node) *n)
508 P(bucket) *x = SKIP_BACK(struct P(bucket), n, n);
509 uns h0 = P(bucket_hash)(x);
510 uns h = h0 % T.hash_size;
513 for (bb=&T.ht[h]; (b=*bb) && b != x; bb=&b->next)
518 if (--T.hash_count < T.hash_min)
519 P(rehash)(T.hash_size/2);
523 /* And the iterator */
527 #define HASH_FOR_ALL(h_px, h_var) \
530 struct GLUE_(h_px,bucket) *h_buck; \
531 for (h_slot=0; h_slot < GLUE_(h_px,table).hash_size; h_slot++) \
532 for (h_buck = GLUE_(h_px,table).ht[h_slot]; h_buck; h_buck = h_buck->next) \
534 GLUE_(h_px,node) *h_var = &h_buck->n;
535 #define HASH_END_FOR } } while(0)
537 #define HASH_CONTINUE continue
541 /* Finally, undefine all the parameters */
546 #undef HASH_ATOMIC_TYPE
547 #undef HASH_CONSERVE_SPACE
548 #undef HASH_DEFAULT_SIZE
549 #undef HASH_EXTRA_SIZE
551 #undef HASH_GIVE_ALLOC
553 #undef HASH_GIVE_EXTRA_SIZE
554 #undef HASH_GIVE_HASHFN
555 #undef HASH_GIVE_INIT_DATA
556 #undef HASH_GIVE_INIT_KEY
558 #undef HASH_KEY_ATOMIC
559 #undef HASH_KEY_COMPLEX
561 #undef HASH_KEY_ENDSTRING
562 #undef HASH_KEY_STRING
567 #undef HASH_AUTO_POOL
568 #undef HASH_WANT_CLEANUP
569 #undef HASH_WANT_DELETE
570 #undef HASH_WANT_FIND
571 #undef HASH_WANT_FIND_NEXT
572 #undef HASH_WANT_LOOKUP
574 #undef HASH_WANT_REMOVE