2 * Sherlock Library -- Universal Hash Table
4 * (c) 2002 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.
92 * You also get a iterator macro at no extra charge:
94 * HASH_FOR_ALL(hash_prefix, variable)
96 * // node *variable gets declared automatically
97 * do_something_with_node(variable);
98 * // use HASH_BREAK and HASH_CONTINUE instead of break and continue
99 * // you must not alter contents of the hash table here
103 * Then include "lib/hashtable.h" and voila, you have a hash table
104 * suiting all your needs (at least those which you've revealed :) ).
106 * After including this file, all parameter macros are automatically
110 #ifndef _SHERLOCK_HASHFUNC_H
111 #include "lib/hashfunc.h"
116 #if !defined(HASH_NODE) || !defined(HASH_PREFIX)
117 #error Some of the mandatory configuration macros are missing.
120 #define P(x) HASH_PREFIX(x)
122 /* Declare buckets and the hash table */
124 typedef HASH_NODE P(node);
126 typedef struct P(bucket) {
127 struct P(bucket) *next;
128 #ifndef HASH_CONSERVE_SPACE
136 uns hash_count, hash_max, hash_min, hash_hard_max;
142 /* Preset parameters */
144 #if defined(HASH_KEY_ATOMIC)
146 #define HASH_KEY(x) x HASH_KEY_ATOMIC
148 #ifndef HASH_ATOMIC_TYPE
149 # define HASH_ATOMIC_TYPE int
151 #define HASH_KEY_DECL HASH_ATOMIC_TYPE HASH_KEY( )
153 #ifndef HASH_GIVE_HASHFN
154 # define HASH_GIVE_HASHFN
155 static inline int P(hash) (HASH_ATOMIC_TYPE x)
156 { return hash_int(x); }
160 # define HASH_GIVE_EQ
161 static inline int P(eq) (HASH_ATOMIC_TYPE x, HASH_ATOMIC_TYPE y)
165 #ifndef HASH_GIVE_INIT_KEY
166 # define HASH_GIVE_INIT_KEY
167 static inline void P(init_key) (P(node) *n, HASH_ATOMIC_TYPE k)
168 { HASH_KEY(n->) = k; }
171 #ifndef HASH_CONSERVE_SPACE
172 #define HASH_CONSERVE_SPACE
175 #elif defined(HASH_KEY_STRING) || defined(HASH_KEY_ENDSTRING)
177 #ifdef HASH_KEY_STRING
178 # define HASH_KEY(x) x HASH_KEY_STRING
179 # ifndef HASH_GIVE_INIT_KEY
180 # define HASH_GIVE_INIT_KEY
181 static inline void P(init_key) (P(node) *n, char *k)
182 { HASH_KEY(n->) = k; }
185 # define HASH_KEY(x) x HASH_KEY_ENDSTRING
186 # define HASH_GIVE_EXTRA_SIZE
187 static inline int P(extra_size) (char *k)
188 { return strlen(k); }
189 # ifndef HASH_GIVE_INIT_KEY
190 # define HASH_GIVE_INIT_KEY
191 static inline void P(init_key) (P(node) *n, char *k)
192 { strcpy(HASH_KEY(n->), k); }
195 #define HASH_KEY_DECL char *HASH_KEY( )
197 #ifndef HASH_GIVE_HASHFN
198 #define HASH_GIVE_HASHFN
199 static inline uns P(hash) (char *k)
202 return hash_string_nocase(k);
204 return hash_string(k);
210 # define HASH_GIVE_EQ
211 static inline int P(eq) (char *x, char *y)
214 return !strcasecmp(x,y);
221 #elif defined(HASH_KEY_COMPLEX)
223 #define HASH_KEY(x) HASH_KEY_COMPLEX(x)
226 #error You forgot to set the hash key type.
229 /* Defaults for missing parameters */
231 #ifndef HASH_GIVE_HASHFN
232 #error Unable to determine which hash function to use.
236 #error Unable to determine how to compare two keys.
239 #ifdef HASH_GIVE_EXTRA_SIZE
240 /* This trickery is needed to avoid `unused parameter' warnings */
241 #define HASH_EXTRA_SIZE P(extra_size)
244 * Beware, C macros are expanded iteratively, not recursively,
245 * hence we get only a _single_ argument, although the expansion
246 * of HASH_KEY contains commas.
248 #define HASH_EXTRA_SIZE(x) 0
251 #ifndef HASH_GIVE_INIT_KEY
252 #error Unable to determine how to initialize keys.
255 #ifndef HASH_GIVE_INIT_DATA
256 static inline void P(init_data) (P(node) *n UNUSED)
263 #ifndef HASH_GIVE_ALLOC
266 static inline void * P(alloc) (unsigned int size)
267 { return mp_alloc_fast(HASH_USE_POOL, size); }
271 static inline void * P(alloc) (unsigned int size)
272 { return xmalloc(size); }
274 static inline void P(free) (void *x)
280 #ifndef HASH_DEFAULT_SIZE
281 #define HASH_DEFAULT_SIZE 32
285 #define HASH_FN_BITS 32
288 /* Now the operations */
290 static void P(alloc_table) (void)
292 T.hash_size = nextprime(T.hash_size);
293 T.ht = xmalloc(sizeof(void *) * T.hash_size);
294 bzero(T.ht, sizeof(void *) * T.hash_size);
295 if (2*T.hash_size < T.hash_hard_max)
296 T.hash_max = 2*T.hash_size;
299 if (T.hash_size/2 > HASH_DEFAULT_SIZE)
300 T.hash_min = T.hash_size/4;
305 static void P(init) (void)
308 T.hash_size = HASH_DEFAULT_SIZE;
309 #if HASH_FN_BITS < 28
310 T.hash_hard_max = 1 << HASH_FN_BITS;
312 T.hash_hard_max = 1 << 28;
317 #ifdef HASH_WANT_CLEANUP
318 static void P(cleanup) (void)
320 #ifndef HASH_USE_POOL
324 for (i=0; i<T.hash_size; i++)
325 for (b=T.ht[i]; b; b=bb)
335 static inline uns P(bucket_hash) (P(bucket) *b)
337 #ifdef HASH_CONSERVE_SPACE
338 return P(hash)(HASH_KEY(b->n.));
344 static void P(rehash) (uns size)
347 P(bucket) **oldt = T.ht, **newt;
348 uns oldsize = T.hash_size;
351 // log(L_DEBUG, "Rehashing %d->%d at count %d", oldsize, size, T.hash_count);
355 for (i=0; i<oldsize; i++)
361 h = P(bucket_hash)(b) % T.hash_size;
370 #ifdef HASH_WANT_FIND
371 static P(node) * P(find) (HASH_KEY_DECL)
373 uns h0 = P(hash) (HASH_KEY( ));
374 uns h = h0 % T.hash_size;
377 for (b=T.ht[h]; b; b=b->next)
380 #ifndef HASH_CONSERVE_SPACE
383 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
390 #ifdef HASH_WANT_FIND_NEXT
391 static P(node) * P(find_next) (P(node) *start)
393 #ifndef HASH_CONSERVE_SPACE
394 uns h0 = P(hash) (HASH_KEY(start->));
396 P(bucket) *b = SKIP_BACK(P(bucket), n, start);
398 for (b=b->next; b; b=b->next)
401 #ifndef HASH_CONSERVE_SPACE
404 P(eq)(HASH_KEY(start->), HASH_KEY(b->n.)))
412 static P(node) * P(new) (HASH_KEY_DECL)
417 h0 = P(hash) (HASH_KEY( ));
418 h = h0 % T.hash_size;
419 b = P(alloc) (sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
422 #ifndef HASH_CONSERVE_SPACE
425 P(init_key)(&b->n, HASH_KEY( ));
427 if (T.hash_count++ >= T.hash_max)
428 P(rehash)(2*T.hash_size);
433 #ifdef HASH_WANT_LOOKUP
434 static P(node) * P(lookup) (HASH_KEY_DECL)
436 uns h0 = P(hash) (HASH_KEY( ));
437 uns h = h0 % T.hash_size;
440 for (b=T.ht[h]; b; b=b->next)
443 #ifndef HASH_CONSERVE_SPACE
446 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
450 b = P(alloc) (sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
453 #ifndef HASH_CONSERVE_SPACE
456 P(init_key)(&b->n, HASH_KEY( ));
458 if (T.hash_count++ >= T.hash_max)
459 P(rehash)(2*T.hash_size);
464 #ifdef HASH_WANT_DELETE
465 static int P(delete) (HASH_KEY_DECL)
467 uns h0 = P(hash) (HASH_KEY( ));
468 uns h = h0 % T.hash_size;
471 for (bb=&T.ht[h]; b=*bb; bb=&b->next)
474 #ifndef HASH_CONSERVE_SPACE
477 P(eq)(HASH_KEY( ), HASH_KEY(b->n.)))
481 if (--T.hash_count < T.hash_min)
482 P(rehash)(T.hash_size/2);
490 #ifdef HASH_WANT_REMOVE
491 static void P(remove) (P(node) *n)
493 P(bucket) *x = SKIP_BACK(struct P(bucket), n, n);
494 uns h0 = P(bucket_hash)(x);
495 uns h = h0 % T.hash_size;
498 for (bb=&T.ht[h]; (b=*bb) && b != x; bb=&b->next)
503 if (--T.hash_count < T.hash_min)
504 P(rehash)(T.hash_size/2);
508 /* And the iterator */
512 #define HASH_FOR_ALL(h_px, h_var) \
515 struct HASH_GLUE(h_px,bucket) *h_buck; \
516 for (h_slot=0; h_slot < HASH_GLUE(h_px,table).hash_size; h_slot++) \
517 for (h_buck = HASH_GLUE(h_px,table).ht[h_slot]; h_buck; h_buck = h_buck->next) \
519 HASH_GLUE(h_px,node) *h_var = &h_buck->n;
520 #define HASH_END_FOR } } while(0)
522 #define HASH_CONTINUE continue
523 #define HASH_GLUE(x,y) x##_##y
527 /* Finally, undefine all the parameters */
532 #undef HASH_ATOMIC_TYPE
533 #undef HASH_CONSERVE_SPACE
534 #undef HASH_DEFAULT_SIZE
535 #undef HASH_EXTRA_SIZE
537 #undef HASH_GIVE_ALLOC
539 #undef HASH_GIVE_EXTRA_SIZE
540 #undef HASH_GIVE_HASHFN
541 #undef HASH_GIVE_INIT_DATA
542 #undef HASH_GIVE_INIT_KEY
544 #undef HASH_KEY_ATOMIC
545 #undef HASH_KEY_COMPLEX
547 #undef HASH_KEY_ENDSTRING
548 #undef HASH_KEY_STRING
553 #undef HASH_WANT_CLEANUP
554 #undef HASH_WANT_DELETE
555 #undef HASH_WANT_FIND
556 #undef HASH_WANT_FIND_NEXT
557 #undef HASH_WANT_LOOKUP
559 #undef HASH_WANT_REMOVE