2 * Sherlock Library -- Universal Hash Table
4 * (c) 2002 Martin Mares <mj@ucw.cz>
8 * This is not a normal header file, it's a generator of hash tables.
9 * Each time you include it with parameters set in the corresponding
10 * preprocessor macros, it generates a hash table with the parameters
13 * You need to specify:
15 * HASH_NODE data type where a node dwells (usually a struct).
16 * HASH_PREFIX(x) macro to add a name prefix (used on all global names
17 * defined by the hash table generator).
19 * Then decide on type of keys:
21 * HASH_KEY_ATOMIC=f use node->f as a key of an atomic type (i.e.,
22 * a type which can be compared using `==')
23 * HASH_ATOMIC_TYPE (defaults to int).
24 * | HASH_KEY_STRING=f use node->f as a string key, allocated
25 * separately from the rest of the node.
26 * | HASH_KEY_ENDSTRING=f use node->f as a string key, allocated
27 * automatically at the end of the node struct
28 * (to be declared as "char f[1]" at the end).
29 * | HASH_KEY_COMPLEX use a multi-component key; as the name suggests,
30 * the passing of parameters is a bit complex then.
31 * The HASH_KEY_COMPLEX(x) macro should expand to
32 * `x k1, x k2, ... x kn' and you should also define:
33 * HASH_KEY_DECL declaration of function parameters in which key
34 * should be passed to all hash table operations.
35 * That is, `type1 k1, type2 k2, ... typen kn'.
36 * With complex keys, HASH_GIVE_HASHFN and HASH_GIVE_EQ
39 * Then specify what operations you request (all names are automatically
40 * prefixed by calling HASH_PREFIX):
42 * <always defined> init() -- initialize the hash table.
43 * HASH_WANT_CLEANUP cleanup() -- deallocate the hash table.
44 * HASH_WANT_FIND node *find(key) -- find node with the specified
45 * key, return NULL if no such node exists.
46 * HASH_WANT_NEW node *new(key) -- create new node with given key.
47 * Doesn't check whether it already exists.
48 * HASH_WANT_LOOKUP node *lookup(key) -- find node with given key,
49 * if it doesn't exist, create it. Defining
50 * HASH_GIVE_INIT_DATA is strongly recommended.
51 * HASH_WANT_DELETE int delete(key) -- delete and deallocate node
52 * with given key. Returns success.
53 * HASH_WANT_REMOVE remove(node *) -- delete and deallocate given node.
55 * You can also supply several functions:
57 * HASH_GIVE_HASHFN unsigned int hash(key) -- calculate hash value of key.
58 * We have sensible default hash functions for strings
60 * HASH_GIVE_EQ int eq(key1, key2) -- return whether keys are equal.
61 * By default, we use == for atomic types and either
62 * strcmp or strcasecmp for strings.
63 * HASH_GIVE_EXTRA_SIZE int extra_size(key) -- returns how many bytes after the
64 * node should be allocated for dynamic data. Default=0
65 * or length of the string with HASH_KEY_ENDSTRING.
66 * HASH_GIVE_INIT_KEY void init_key(node *,key) -- initialize key in a newly
67 * created node. Defaults: assignment for atomic keys
68 * and static strings, strcpy for end-allocated strings.
69 * HASH_GIVE_INIT_DATA void init_data(node *) -- initialize data fields in a
70 * newly created node. Very useful for lookup operations.
71 * HASH_GIVE_ALLOC void *alloc(unsigned int size) -- allocate space for
72 * a node. Default is either normal or pooled allocation
73 * depending on whether we want deletions.
74 * void free(void *) -- the converse.
76 * ... and a couple of extra parameters:
78 * HASH_NOCASE string comparisons should be case-insensitive.
79 * HASH_DEFAULT_SIZE=n initially, use hash table of approx. `n' entries.
80 * HASH_CONSERVE_SPACE use as little space as possible.
81 * HASH_FN_BITS=n The hash function gives only `n' significant bits.
82 * HASH_ATOMIC_TYPE=t Atomic values are of type `t' instead of int.
83 * HASH_USE_POOL=pool Allocate all nodes from given mempool.
84 * Collides with delete/remove functions.
86 * You also get a iterator macro at no extra charge:
88 * HASH_FOR_ALL(hash_prefix, variable)
90 * // node *variable gets declared automatically
91 * do_something_with_node(variable);
92 * // use HASH_BREAK and HASH_CONTINUE instead of break and continue
93 * // you must not alter contents of the hash table here
97 * Then include <lib/hashtable.h> and voila, you have a hash table
98 * suiting all your needs (at least those which you've revealed :) ).
100 * After including this file, all parameter macros are automatically
104 #ifndef _SHERLOCK_HASHFUNC_H
105 #include "lib/hashfunc.h"
110 #if !defined(HASH_NODE) || !defined(HASH_PREFIX)
111 #error Some of the mandatory configuration macros are missing.
114 #define P(x) HASH_PREFIX(x)
116 /* Declare buckets and the hash table */
118 typedef HASH_NODE P(node);
120 typedef struct P(bucket) {
121 struct P(bucket) *next;
122 #ifndef HASH_CONSERVE_SPACE
130 uns hash_count, hash_max, hash_min, hash_hard_max;
136 /* Preset parameters */
138 #if defined(HASH_KEY_ATOMIC)
140 #define HASH_KEY(x) x HASH_KEY_ATOMIC
142 #ifndef HASH_ATOMIC_TYPE
143 # define HASH_ATOMIC_TYPE int
145 #define HASH_KEY_DECL HASH_ATOMIC_TYPE HASH_KEY( )
147 #ifndef HASH_GIVE_HASHFN
148 # define HASH_GIVE_HASHFN
149 static inline int P(hash) (HASH_ATOMIC_TYPE x)
150 { return hash_int(x); }
154 # define HASH_GIVE_EQ
155 static inline int P(eq) (HASH_ATOMIC_TYPE x, HASH_ATOMIC_TYPE y)
159 #ifndef HASH_GIVE_INIT_KEY
160 # define HASH_GIVE_INIT_KEY
161 static inline void P(init_key) (P(node) *n, HASH_ATOMIC_TYPE k)
162 { HASH_KEY(n->) = k; }
165 #ifndef HASH_CONSERVE_SPACE
166 #define HASH_CONSERVE_SPACE
169 #elif defined(HASH_KEY_STRING) || defined(HASH_KEY_ENDSTRING)
171 #ifdef HASH_KEY_STRING
172 # define HASH_KEY(x) x HASH_KEY_STRING
173 # ifndef HASH_GIVE_INIT_KEY
174 # define HASH_GIVE_INIT_KEY
175 static inline void P(init_key) (P(node) *n, char *k)
176 { HASH_KEY(n->) = k; }
179 # define HASH_KEY(x) x HASH_KEY_ENDSTRING
180 # define HASH_GIVE_EXTRA_SIZE
181 static inline int P(extra_size) (char *k)
182 { return strlen(k); }
183 # ifndef HASH_GIVE_INIT_KEY
184 # define HASH_GIVE_INIT_KEY
185 static inline void P(init_key) (P(node) *n, char *k)
186 { strcpy(HASH_KEY(n->), k); }
189 #define HASH_KEY_DECL char *HASH_KEY( )
191 #ifndef HASH_GIVE_HASHFN
192 #define HASH_GIVE_HASHFN
193 static inline uns P(hash) (char *k)
196 return hash_string_nocase(k);
198 return hash_string(k);
204 # define HASH_GIVE_EQ
205 static inline int P(eq) (char *x, char *y)
208 return !strcasecmp(x,y);
215 #elif defined(HASH_KEY_COMPLEX)
217 #define HASH_KEY(x) HASH_KEY_COMPLEX(x)
220 #error You forgot to set the hash key type.
223 /* Defaults for missing parameters */
225 #ifndef HASH_GIVE_HASHFN
226 #error Unable to determine which hash function to use.
230 #error Unable to determine how to compare two keys.
233 #ifdef HASH_GIVE_EXTRA_SIZE
234 /* This trickery is needed to avoid `unused parameter' warnings */
235 #define HASH_EXTRA_SIZE P(extra_size)
238 * Beware, C macros are expanded iteratively, not recursively,
239 * hence we get only a _single_ argument, although the expansion
240 * of HASH_KEY contains commas.
242 #define HASH_EXTRA_SIZE(x) 0
245 #ifndef HASH_GIVE_INIT_KEY
246 #error Unable to determine how to initialize keys.
249 #ifndef HASH_GIVE_INIT_DATA
250 static inline void P(init_data) (P(node) *n UNUSED)
257 #ifndef HASH_GIVE_ALLOC
260 static inline void * P(alloc) (unsigned int size)
261 { return mp_alloc_fast(HASH_USE_POOL, size); }
265 static inline void * P(alloc) (unsigned int size)
266 { return xmalloc(size); }
268 static inline void P(free) (void *x)
274 #ifndef HASH_DEFAULT_SIZE
275 #define HASH_DEFAULT_SIZE 32
279 #define HASH_FN_BITS 32
282 /* Now the operations */
284 static void P(alloc_table) (void)
286 T.hash_size = nextprime(T.hash_size);
287 T.ht = xmalloc(sizeof(void *) * T.hash_size);
288 bzero(T.ht, sizeof(void *) * T.hash_size);
289 if (2*T.hash_size < T.hash_hard_max)
290 T.hash_max = 2*T.hash_size;
293 if (T.hash_size/2 > HASH_DEFAULT_SIZE)
294 T.hash_min = T.hash_size/4;
299 static void P(init) (void)
302 T.hash_size = HASH_DEFAULT_SIZE;
303 #if HASH_FN_BITS < 28
304 T.hash_hard_max = 1 << HASH_FN_BITS;
306 T.hash_hard_max = 1 << 28;
311 #ifdef HASH_WANT_CLEANUP
312 static void P(cleanup) (void)
314 #ifndef HASH_USE_POOL
318 for (i=0; i<T.hash_size; i++)
319 for (b=T.ht[i]; b; b=bb)
329 static inline uns P(bucket_hash) (P(bucket) *b)
331 #ifdef HASH_CONSERVE_SPACE
332 return P(hash)(HASH_KEY(b->n.));
338 static void P(rehash) (uns size)
341 P(bucket) **oldt = T.ht, **newt;
342 uns oldsize = T.hash_size;
345 // log(L_DEBUG, "Rehashing %d->%d at count %d", oldsize, size, T.hash_count);
349 for (i=0; i<oldsize; i++)
355 h = P(bucket_hash)(b) % T.hash_size;
364 #ifdef HASH_WANT_FIND
365 static P(node) * P(find) (HASH_KEY_DECL)
367 uns h0 = P(hash) (HASH_KEY( ));
368 uns h = h0 % T.hash_size;
371 for (b=T.ht[h]; b; b=b->next)
374 #ifndef HASH_CONSERVE_SPACE
377 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
385 static P(node) * P(new) (HASH_KEY_DECL)
390 h0 = P(hash) (HASH_KEY( ));
391 h = h0 % T.hash_size;
392 b = P(alloc) (sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
395 #ifndef HASH_CONSERVE_SPACE
398 P(init_key)(&b->n, HASH_KEY( ));
400 if (T.hash_count++ >= T.hash_max)
401 P(rehash)(2*T.hash_size);
406 #ifdef HASH_WANT_LOOKUP
407 static P(node) * P(lookup) (HASH_KEY_DECL)
409 uns h0 = P(hash) (HASH_KEY( ));
410 uns h = h0 % T.hash_size;
413 for (b=T.ht[h]; b; b=b->next)
416 #ifndef HASH_CONSERVE_SPACE
419 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
423 b = P(alloc) (sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
426 #ifndef HASH_CONSERVE_SPACE
429 P(init_key)(&b->n, HASH_KEY( ));
431 if (T.hash_count++ >= T.hash_max)
432 P(rehash)(2*T.hash_size);
437 #ifdef HASH_WANT_DELETE
438 static int P(delete) (HASH_KEY_DECL)
440 uns h0 = P(hash) (HASH_KEY( ));
441 uns h = h0 % T.hash_size;
444 for (bb=&T.ht[h]; b=*bb; bb=&b->next)
447 #ifndef HASH_CONSERVE_SPACE
450 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
454 if (--T.hash_count < T.hash_min)
455 P(rehash)(T.hash_size/2);
463 #ifdef HASH_WANT_REMOVE
464 static void P(remove) (P(node) *n)
466 P(bucket) *x = SKIP_BACK(struct P(bucket), n, n);
467 uns h0 = P(bucket_hash)(x);
468 uns h = h0 % T.hash_size;
471 for (bb=&T.ht[h]; (b=*bb) && b != x; bb=&b->next)
476 if (--T.hash_count < T.hash_min)
477 P(rehash)(T.hash_size/2);
481 /* And the iterator */
485 #define HASH_FOR_ALL(h_px, h_var) \
488 struct HASH_GLUE(h_px,bucket) *h_buck; \
489 for (h_slot=0; h_slot < HASH_GLUE(h_px,table).hash_size; h_slot++) \
490 for (h_buck = HASH_GLUE(h_px,table).ht[h_slot]; h_buck; h_buck = h_buck->next) \
492 HASH_GLUE(h_px,node) *h_var = &h_buck->n;
493 #define HASH_END_FOR } } while(0)
495 #define HASH_CONTINUE continue
496 #define HASH_GLUE(x,y) x##_##y
500 /* Finally, undefine all the parameters */
505 #undef HASH_ATOMIC_TYPE
506 #undef HASH_CONSERVE_SPACE
507 #undef HASH_DEFAULT_SIZE
508 #undef HASH_EXTRA_SIZE
510 #undef HASH_GIVE_ALLOC
512 #undef HASH_GIVE_EXTRA_SIZE
513 #undef HASH_GIVE_HASHFN
514 #undef HASH_GIVE_INIT_DATA
515 #undef HASH_GIVE_INIT_KEY
517 #undef HASH_KEY_ATOMIC
518 #undef HASH_KEY_COMPLEX
520 #undef HASH_KEY_ENDSTRING
521 #undef HASH_KEY_STRING
526 #undef HASH_WANT_CLEANUP
527 #undef HASH_WANT_DELETE
528 #undef HASH_WANT_FIND
529 #undef HASH_WANT_LOOKUP
531 #undef HASH_WANT_REMOVE