]> mj.ucw.cz Git - libucw.git/blob - ucw/hashtable.h
099ee7bdbadb106d0e0008a33c6acc5bd435e958
[libucw.git] / ucw / hashtable.h
1 /*
2  *      UCW Library -- Universal Hash Table
3  *
4  *      (c) 2002--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2002--2005 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 /*
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
15  *  given.
16  *
17  *  You need to specify:
18  *
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).
22  *
23  *  Then decide on type of keys:
24  *
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
41  *                      are mandatory.
42  *  | HASH_KEY_MEMORY=f use node->f as a raw data key, compared using
43  *                      memcmp
44  *    HASH_KEY_SIZE     the length of the key block
45  *
46  *  Then specify what operations you request (all names are automatically
47  *  prefixed by calling HASH_PREFIX):
48  *
49  *  <always defined>    init() -- initialize the hash table.
50  *  HASH_WANT_CLEANUP   cleanup() -- deallocate the hash table.
51  *  HASH_WANT_FIND      node *find(key) -- find first node with the specified
52  *                      key, return NULL if no such node exists.
53  *  HASH_WANT_FIND_NEXT node *find(node *start) -- find next node with the
54  *                      specified key, return NULL if no such node exists.
55  *  HASH_WANT_NEW       node *new(key) -- create new node with given key.
56  *                      Doesn't check whether it already exists.
57  *  HASH_WANT_LOOKUP    node *lookup(key) -- find node with given key,
58  *                      if it doesn't exist, create it. Defining
59  *                      HASH_GIVE_INIT_DATA is strongly recommended.
60  *  HASH_WANT_DELETE    int delete(key) -- delete and deallocate node
61  *                      with given key. Returns success.
62  *  HASH_WANT_REMOVE    remove(node *) -- delete and deallocate given node.
63  *
64  *  You can also supply several functions:
65  *
66  *  HASH_GIVE_HASHFN    unsigned int hash(key) -- calculate hash value of key.
67  *                      We have sensible default hash functions for strings
68  *                      and integers.
69  *  HASH_GIVE_EQ        int eq(key1, key2) -- return whether keys are equal.
70  *                      By default, we use == for atomic types and either
71  *                      strcmp or strcasecmp for strings.
72  *  HASH_GIVE_EXTRA_SIZE int extra_size(key) -- returns how many bytes after the
73  *                      node should be allocated for dynamic data. Default=0
74  *                      or length of the string with HASH_KEY_ENDSTRING.
75  *  HASH_GIVE_INIT_KEY  void init_key(node *,key) -- initialize key in a newly
76  *                      created node. Defaults: assignment for atomic keys
77  *                      and static strings, strcpy for end-allocated strings.
78  *  HASH_GIVE_INIT_DATA void init_data(node *) -- initialize data fields in a
79  *                      newly created node. Very useful for lookup operations.
80  *  HASH_GIVE_ALLOC     void *alloc(unsigned int size) -- allocate space for
81  *                      a node. Default is xmalloc() or pooled allocation, depending
82  *                      on HASH_USE_POOL, HASH_AUTO_POOL, HASH_USE_ELTPOOL
83  *                      and HASH_AUTO_ELTPOOL switches. void free(void *) -- the converse.
84  * HASH_GIVE_TABLE_ALLOC void *table_alloc(unsigned int size), void *table_free(void *)
85  *                      Allocate or free space for the table itself. Default is xmalloc()
86  *                      or the functions defined by HASH_GIVE_ALLOC if HASH_TABLE_ALLOC is set.
87  *
88  *  ... and a couple of extra parameters:
89  *
90  *  HASH_NOCASE         String comparisons should be case-insensitive.
91  *  HASH_DEFAULT_SIZE=n Initially, use hash table of approx. `n' entries.
92  *  HASH_CONSERVE_SPACE Use as little space as possible.
93  *  HASH_FN_BITS=n      The hash function gives only `n' significant bits.
94  *  HASH_ATOMIC_TYPE=t  Atomic values are of type `t' instead of int.
95  *  HASH_USE_POOL=pool  Allocate all nodes from given mempool. Note, however, that
96  *                      deallocation is not supported by mempools, so delete/remove
97  *                      will leak pool memory.
98  *  HASH_AUTO_POOL=size Create a pool of the given block size automatically.
99  *  HASH_USE_ELTPOOL=pool Allocate all nodes from given eltpool.
100  *                      Only works for nodes of limited size.
101  *  HASH_AUTO_ELTPOOL=count Create an eltpool of the given number of elements in each chunk.
102  *                      Only works for fixed-sized nodes and zero HASH_GIVE_EXTRA_SIZE.
103  *  HASH_ZERO_FILL      New entries should be initialized to all zeroes.
104  *  HASH_TABLE_ALLOC    The hash table itself will be allocated and freed using
105  *                      the same allocation functions as the nodes instead of
106  *                      the default xmalloc().
107  *  HASH_TABLE_GROWING  Never decrease the size of the hash table itself
108  *  HASH_TABLE_DYNAMIC  Support multiple hash tables; the first parameter of all
109  *                      hash table operations is struct HASH_PREFIX(table) *.
110  *  HASH_TABLE_VARS     Extra variables to be defined in table structure
111  *
112  *  You also get a iterator macro at no extra charge:
113  *
114  *  HASH_FOR_ALL(hash_prefix, variable)
115  *    {
116  *      // node *variable gets declared automatically
117  *      do_something_with_node(variable);
118  *      // use HASH_BREAK and HASH_CONTINUE instead of break and continue
119  *      // you must not alter contents of the hash table here
120  *    }
121  *  HASH_END_FOR;
122  *
123  *  (For dynamic tables, use HASH_FOR_ALL_DYNAMIC(hash_prefix, hash_table, variable) instead.)
124  *
125  *  Then include "ucw/hashtable.h" and voila, you have a hash table
126  *  suiting all your needs (at least those which you've revealed :) ).
127  *
128  *  After including this file, all parameter macros are automatically
129  *  undef'd.
130  */
131
132 #ifndef _UCW_HASHFUNC_H
133 #include "ucw/hashfunc.h"
134 #endif
135
136 #include "ucw/prime.h"
137
138 #include <string.h>
139
140 /* Initial setup of parameters */
141
142 #if !defined(HASH_NODE) || !defined(HASH_PREFIX)
143 #error Some of the mandatory configuration macros are missing.
144 #endif
145
146 #if defined(HASH_KEY_ATOMIC) && !defined(HASH_CONSERVE_SPACE)
147 #define HASH_CONSERVE_SPACE
148 #endif
149
150 #define P(x) HASH_PREFIX(x)
151
152 /* Declare buckets and the hash table */
153
154 typedef HASH_NODE P(node);
155
156 typedef struct P(bucket) {
157   struct P(bucket) *next;
158 #ifndef HASH_CONSERVE_SPACE
159   uns hash;
160 #endif
161   P(node) n;
162 } P(bucket);
163
164 struct P(table) {
165   uns hash_size;
166   uns hash_count, hash_max, hash_min, hash_hard_max;
167   P(bucket) **ht;
168 #ifdef HASH_AUTO_POOL
169   struct mempool *pool;
170 #endif
171 #ifdef HASH_AUTO_ELTPOOL
172   struct eltpool *eltpool;
173 #endif
174 #ifdef HASH_TABLE_VARS
175   HASH_TABLE_VARS
176 #endif
177 };
178
179 #ifdef HASH_TABLE_DYNAMIC
180 #define T (*table)
181 #define TA struct P(table) *table
182 #define TAC TA,
183 #define TAU TA UNUSED
184 #define TAUC TA UNUSED,
185 #define TT table
186 #define TTC table,
187 #else
188 struct P(table) P(table);
189 #define T P(table)
190 #define TA void
191 #define TAC
192 #define TAU void
193 #define TAUC
194 #define TT
195 #define TTC
196 #endif
197
198 /* Preset parameters */
199
200 #if defined(HASH_KEY_ATOMIC)
201
202 #define HASH_KEY(x) x HASH_KEY_ATOMIC
203
204 #ifndef HASH_ATOMIC_TYPE
205 #  define HASH_ATOMIC_TYPE int
206 #endif
207 #define HASH_KEY_DECL HASH_ATOMIC_TYPE HASH_KEY( )
208
209 #ifndef HASH_GIVE_HASHFN
210 #  define HASH_GIVE_HASHFN
211    static inline int P(hash) (TAUC HASH_ATOMIC_TYPE x)
212    { return ((sizeof(x) <= 4) ? hash_u32(x) : hash_u64(x)); }
213 #endif
214
215 #ifndef HASH_GIVE_EQ
216 #  define HASH_GIVE_EQ
217    static inline int P(eq) (TAUC HASH_ATOMIC_TYPE x, HASH_ATOMIC_TYPE y)
218    { return x == y; }
219 #endif
220
221 #ifndef HASH_GIVE_INIT_KEY
222 #  define HASH_GIVE_INIT_KEY
223    static inline void P(init_key) (TAUC P(node) *n, HASH_ATOMIC_TYPE k)
224    { HASH_KEY(n->) = k; }
225 #endif
226
227 #elif defined(HASH_KEY_MEMORY)
228
229 #define HASH_KEY(x) x HASH_KEY_MEMORY
230
231 #define HASH_KEY_DECL byte HASH_KEY( )[HASH_KEY_SIZE]
232
233 #ifndef HASH_GIVE_HASHFN
234 #  define HASH_GIVE_HASHFN
235    static inline int P(hash) (TAUC byte *x)
236    { return hash_block(x, HASH_KEY_SIZE); }
237 #endif
238
239 #ifndef HASH_GIVE_EQ
240 #  define HASH_GIVE_EQ
241    static inline int P(eq) (TAUC byte *x, byte *y)
242    { return !memcmp(x, y, HASH_KEY_SIZE); }
243 #endif
244
245 #ifndef HASH_GIVE_INIT_KEY
246 #  define HASH_GIVE_INIT_KEY
247    static inline void P(init_key) (TAUC P(node) *n, byte *k)
248    { memcpy(HASH_KEY(n->), k, HASH_KEY_SIZE); }
249 #endif
250
251 #elif defined(HASH_KEY_STRING) || defined(HASH_KEY_ENDSTRING)
252
253 #ifdef HASH_KEY_STRING
254 #  define HASH_KEY(x) x HASH_KEY_STRING
255 #  ifndef HASH_GIVE_INIT_KEY
256 #    define HASH_GIVE_INIT_KEY
257      static inline void P(init_key) (TAUC P(node) *n, char *k)
258      { HASH_KEY(n->) = k; }
259 #  endif
260 #else
261 #  define HASH_KEY(x) x HASH_KEY_ENDSTRING
262 #  define HASH_GIVE_EXTRA_SIZE
263    static inline int P(extra_size) (TAUC char *k)
264    { return strlen(k); }
265 #  ifndef HASH_GIVE_INIT_KEY
266 #    define HASH_GIVE_INIT_KEY
267      static inline void P(init_key) (TAUC P(node) *n, char *k)
268      { strcpy(HASH_KEY(n->), k); }
269 #  endif
270 #endif
271 #define HASH_KEY_DECL char *HASH_KEY( )
272
273 #ifndef HASH_GIVE_HASHFN
274 #define HASH_GIVE_HASHFN
275   static inline uns P(hash) (TAUC char *k)
276    {
277 #    ifdef HASH_NOCASE
278        return hash_string_nocase(k);
279 #    else
280        return hash_string(k);
281 #    endif
282    }
283 #endif
284
285 #ifndef HASH_GIVE_EQ
286 #  define HASH_GIVE_EQ
287    static inline int P(eq) (TAUC char *x, char *y)
288    {
289 #    ifdef HASH_NOCASE
290        return !strcasecmp(x,y);
291 #    else
292        return !strcmp(x,y);
293 #    endif
294    }
295 #endif
296
297 #elif defined(HASH_KEY_COMPLEX)
298
299 #define HASH_KEY(x) HASH_KEY_COMPLEX(x)
300
301 #else
302 #error You forgot to set the hash key type.
303 #endif
304
305 /* Defaults for missing parameters */
306
307 #ifndef HASH_GIVE_HASHFN
308 #error Unable to determine which hash function to use.
309 #endif
310
311 #ifndef HASH_GIVE_EQ
312 #error Unable to determine how to compare two keys.
313 #endif
314
315 #ifdef HASH_GIVE_EXTRA_SIZE
316 /* This trickery is needed to avoid `unused parameter' warnings */
317 #define HASH_EXTRA_SIZE(x) P(extra_size)(TTC x)
318 #else
319 /*
320  *  Beware, C macros are expanded iteratively, not recursively,
321  *  hence we get only a _single_ argument, although the expansion
322  *  of HASH_KEY contains commas.
323  */
324 #define HASH_EXTRA_SIZE(x) 0
325 #endif
326
327 #ifndef HASH_GIVE_INIT_KEY
328 #error Unable to determine how to initialize keys.
329 #endif
330
331 #ifndef HASH_GIVE_INIT_DATA
332 static inline void P(init_data) (TAUC P(node) *n UNUSED)
333 {
334 }
335 #endif
336
337 #ifdef HASH_GIVE_ALLOC
338 /* If the caller has requested to use his own allocation functions, do so */
339 static inline void P(init_alloc) (TAU) { }
340 static inline void P(cleanup_alloc) (TAU) { }
341
342 #elif defined(HASH_USE_POOL)
343 /* If the caller has requested to use his mempool, do so */
344 #include "ucw/mempool.h"
345 static inline void * P(alloc) (TAUC unsigned int size) { return mp_alloc_fast(HASH_USE_POOL, size); }
346 static inline void P(free) (TAUC void *x UNUSED) { }
347 static inline void P(init_alloc) (TAU) { }
348 static inline void P(cleanup_alloc) (TAU) { }
349
350 #elif defined(HASH_AUTO_POOL)
351 /* Use our own pools */
352 #include "ucw/mempool.h"
353 static inline void * P(alloc) (TAUC unsigned int size) { return mp_alloc_fast(T.pool, size); }
354 static inline void P(free) (TAUC void *x UNUSED) { }
355 static inline void P(init_alloc) (TAU) { T.pool = mp_new(HASH_AUTO_POOL); }
356 static inline void P(cleanup_alloc) (TAU) { mp_delete(T.pool); }
357 #define HASH_USE_POOL
358
359 #elif defined(HASH_USE_ELTPOOL)
360 /* If the caller has requested to use his eltpool, do so */
361 #include "ucw/eltpool.h"
362 static inline void * P(alloc) (TAUC unsigned int size UNUSED) { ASSERT(size <= (HASH_USE_ELTPOOL)->elt_size); return ep_alloc(HASH_USE_ELTPOOL); }
363 static inline void P(free) (TAUC void *x) { ep_free(HASH_USE_ELTPOOL, x); }
364 static inline void P(init_alloc) (TAU) { }
365 static inline void P(cleanup_alloc) (TAU) { }
366
367 #elif defined(HASH_AUTO_ELTPOOL)
368 /* Use our own eltpools */
369 #ifdef HASH_GIVE_EXTRA_SIZE
370 #error HASH_AUTO_ELTPOOL not supported in combination with variable-sized nodes
371 #endif
372 #include "ucw/eltpool.h"
373 static inline void * P(alloc) (TAUC unsigned int size UNUSED) { return ep_alloc(T.eltpool); }
374 static inline void P(free) (TAUC void *x) { ep_free(T.eltpool, x); }
375 static inline void P(init_alloc) (TAU) { T.eltpool = ep_new(sizeof(P(bucket)), HASH_AUTO_ELTPOOL); }
376 static inline void P(cleanup_alloc) (TAU) { ep_delete(T.eltpool); }
377 #define HASH_USE_ELTPOOL
378
379 #else
380 /* The default allocation method */
381 static inline void * P(alloc) (TAUC unsigned int size) { return xmalloc(size); }
382 static inline void P(free) (TAUC void *x) { xfree(x); }
383 static inline void P(init_alloc) (TAU) { }
384 static inline void P(cleanup_alloc) (TAU) { }
385
386 #endif
387
388 #ifdef HASH_GIVE_TABLE_ALLOC
389 /* If the caller has requested to use his own allocation functions, do so */
390 #elif defined(HASH_TABLE_ALLOC)
391 #ifdef HASH_USE_ELTPOOL
392 #error HASH_TABLE_ALLOC not supported in combination with eltpools
393 #endif
394 static inline void * P(table_alloc) (TAUC unsigned int size) { return P(alloc)(TTC size); }
395 static inline void P(table_free) (TAUC void *x) { P(free)(TTC x); }
396 #else
397 static inline void * P(table_alloc) (TAUC unsigned int size) { return xmalloc(size); }
398 static inline void P(table_free) (TAUC void *x) { xfree(x); }
399 #endif
400
401 #if defined(HASH_USE_POOL) && defined(HASH_TABLE_ALLOC) && !defined(HASH_TABLE_GROWING)
402 #define HASH_TABLE_GROWING
403 #endif
404
405 #ifndef HASH_DEFAULT_SIZE
406 #define HASH_DEFAULT_SIZE 32
407 #endif
408
409 #ifndef HASH_FN_BITS
410 #define HASH_FN_BITS 32
411 #endif
412
413 #ifdef HASH_ZERO_FILL
414 static inline void * P(new_bucket)(TAUC uns size)
415 {
416   byte *buck = P(alloc)(TTC size);
417   bzero(buck, size);
418   return buck;
419 }
420 #else
421 static inline void * P(new_bucket)(TAUC uns size) { return P(alloc)(TTC size); }
422 #endif
423
424 /* Now the operations */
425
426 static void P(alloc_table) (TAU)
427 {
428   T.hash_size = next_table_prime(T.hash_size);
429   T.ht = P(table_alloc)(TTC sizeof(void *) * T.hash_size);
430   bzero(T.ht, sizeof(void *) * T.hash_size);
431   if (2*T.hash_size < T.hash_hard_max)
432     T.hash_max = 2*T.hash_size;
433   else
434     T.hash_max = ~0U;
435 #ifndef HASH_TABLE_GROWING
436   if (T.hash_size/2 > HASH_DEFAULT_SIZE)
437     T.hash_min = T.hash_size/4;
438   else
439 #endif
440     T.hash_min = 0;
441 }
442
443 /**
444  * Initializes the hash table.
445  * This one is available no matter what `HASH_WANT_` macros you defined or not.
446  **/
447 static void HASH_PREFIX(init)(TA)
448 {
449   T.hash_count = 0;
450   T.hash_size = HASH_DEFAULT_SIZE;
451 #if HASH_FN_BITS < 28
452   T.hash_hard_max = 1 << HASH_FN_BITS;
453 #else
454   T.hash_hard_max = 1 << 28;
455 #endif
456   P(init_alloc)(TT);
457   P(alloc_table)(TT);
458 }
459
460 #ifdef HASH_WANT_CLEANUP
461 /**
462  * Deallocates the hash table, including the nodes.
463  * It is available if you defined <<want_cleanup,`HASH_WANT_CLEANUP`>>.
464  **/
465 static void HASH_PREFIX(cleanup)(TA)
466 {
467 #ifndef HASH_USE_POOL
468   uns i;
469   P(bucket) *b, *bb;
470
471   for (i=0; i<T.hash_size; i++)
472     for (b=T.ht[i]; b; b=bb)
473       {
474         bb = b->next;
475         P(free)(TTC b);
476       }
477 #endif
478   P(cleanup_alloc)(TT);
479   P(table_free)(TTC T.ht);
480 }
481 #endif
482
483 static inline uns P(bucket_hash) (TAUC P(bucket) *b)
484 {
485 #ifdef HASH_CONSERVE_SPACE
486   return P(hash)(TTC HASH_KEY(b->n.));
487 #else
488   return b->hash;
489 #endif
490 }
491
492 static void P(rehash) (TAC uns size)
493 {
494   P(bucket) *b, *nb;
495   P(bucket) **oldt = T.ht, **newt;
496   uns oldsize = T.hash_size;
497   uns i, h;
498
499   DBG("Rehashing %d->%d at count %d", oldsize, size, T.hash_count);
500   T.hash_size = size;
501   P(alloc_table)(TT);
502   newt = T.ht;
503   for (i=0; i<oldsize; i++)
504     {
505       b = oldt[i];
506       while (b)
507         {
508           nb = b->next;
509           h = P(bucket_hash)(TTC b) % T.hash_size;
510           b->next = newt[h];
511           newt[h] = b;
512           b = nb;
513         }
514     }
515   P(table_free)(TTC oldt);
516 }
517
518 #ifdef HASH_WANT_FIND
519 /**
520  * Finds a node with given key (specified in the @HAS_KEY_DECL parameter).
521  * If it does not exist, NULL is returned.
522  *
523  * Enabled by the <<want_find,`HASH_WANT_FIND`>> macro.
524  **/
525 static HASH_NODE* HASH_PREFIX(find)(TAC HASH_KEY_DECL)
526 {
527   uns h0 = P(hash) (TTC HASH_KEY( ));
528   uns h = h0 % T.hash_size;
529   P(bucket) *b;
530
531   for (b=T.ht[h]; b; b=b->next)
532     {
533       if (
534 #ifndef HASH_CONSERVE_SPACE
535           b->hash == h0 &&
536 #endif
537           P(eq)(TTC HASH_KEY( ), HASH_KEY(b->n.)))
538         return &b->n;
539     }
540   return NULL;
541 }
542 #endif
543
544 #ifdef HASH_WANT_FIND_NEXT
545 /**
546  * Finds next node with the same key. Returns NULL if it does not exist.
547  *
548  * Enabled by the <<want_find_next,`HASH_WANT_FIND_NEXT`>> macro.
549  **/
550 static HASH_NODE* HASH_PREFIX(find_next)(TAC P(node) *start)
551 {
552 #ifndef HASH_CONSERVE_SPACE
553   uns h0 = P(hash) (TTC HASH_KEY(start->));
554 #endif
555   P(bucket) *b = SKIP_BACK(P(bucket), n, start);
556
557   for (b=b->next; b; b=b->next)
558     {
559       if (
560 #ifndef HASH_CONSERVE_SPACE
561           b->hash == h0 &&
562 #endif
563           P(eq)(TTC HASH_KEY(start->), HASH_KEY(b->n.)))
564         return &b->n;
565     }
566   return NULL;
567 }
568 #endif
569
570 #ifdef HASH_WANT_NEW
571 /**
572  * Generates a new node with a given key.
573  *
574  * Enabled by the <<want_new,`HASH_WANT_NEW`>> macro.
575  **/
576 static HASH_NODE * HASH_PREFIX(new)(TAC HASH_KEY_DECL)
577 {
578   uns h0, h;
579   P(bucket) *b;
580
581   h0 = P(hash) (TTC HASH_KEY( ));
582   h = h0 % T.hash_size;
583   b = P(new_bucket) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
584   b->next = T.ht[h];
585   T.ht[h] = b;
586 #ifndef HASH_CONSERVE_SPACE
587   b->hash = h0;
588 #endif
589   P(init_key)(TTC &b->n, HASH_KEY( ));
590   P(init_data)(TTC &b->n);
591   if (T.hash_count++ >= T.hash_max)
592     P(rehash)(TTC 2*T.hash_size);
593   return &b->n;
594 }
595 #endif
596
597 #ifdef HASH_WANT_LOOKUP
598 /**
599  * Finds a node with a given key. If it does not exist, a new one is created.
600  * It is strongly recommended to use <<give_init_data,`HASH_GIVE_INIT_DATA`>>.
601  *
602  * This one is enabled by the <<want_lookup,`HASH_WANT_LOOKUP`>> macro.
603  **/
604 static HASH_NODE* HASH_PREFIX(lookup)(TAC HASH_KEY_DECL)
605 {
606   uns h0 = P(hash) (TTC HASH_KEY( ));
607   uns h = h0 % T.hash_size;
608   P(bucket) *b;
609
610   for (b=T.ht[h]; b; b=b->next)
611     {
612       if (
613 #ifndef HASH_CONSERVE_SPACE
614           b->hash == h0 &&
615 #endif
616           P(eq)(TTC HASH_KEY( ), HASH_KEY(b->n.)))
617         return &b->n;
618     }
619
620   b = P(new_bucket) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
621   b->next = T.ht[h];
622   T.ht[h] = b;
623 #ifndef HASH_CONSERVE_SPACE
624   b->hash = h0;
625 #endif
626   P(init_key)(TTC &b->n, HASH_KEY( ));
627   P(init_data)(TTC &b->n);
628   if (T.hash_count++ >= T.hash_max)
629     P(rehash)(TTC 2*T.hash_size);
630   return &b->n;
631 }
632 #endif
633
634 #ifdef HASH_WANT_DELETE
635 /**
636  * Removes a node with the given key from hash table and deallocates it.
637  *
638  * Success is returned.
639  *
640  * This one is enabled by <<want_delete,`HASH_WANT_DELETE`>> macro.
641  **/
642 static int HASH_PREFIX(delete)(TAC HASH_KEY_DECL)
643 {
644   uns h0 = P(hash) (TTC HASH_KEY( ));
645   uns h = h0 % T.hash_size;
646   P(bucket) *b, **bb;
647
648   for (bb=&T.ht[h]; b=*bb; bb=&b->next)
649     {
650       if (
651 #ifndef HASH_CONSERVE_SPACE
652           b->hash == h0 &&
653 #endif
654           P(eq)(TTC HASH_KEY( ), HASH_KEY(b->n.)))
655         {
656           *bb = b->next;
657           P(free)(TTC b);
658 #ifndef HASH_TABLE_GROWING
659           if (--T.hash_count < T.hash_min)
660             P(rehash)(TTC T.hash_size/2);
661 #endif
662           return 1;
663         }
664     }
665   return 0;
666 }
667 #endif
668
669 #ifdef HASH_WANT_REMOVE
670 /**
671  * Removes a given node and deallocates it.
672  * It differs from <<fun__GENERIC_LINK|HASH_PREFIX|delete,`HASH_PREFIX(delete)()`>>
673  * in its type of parameter -- this one deletes a specific node, that one looks for it by a key.
674  *
675  * Enabled by <<want_remove,`HASH_WANT_REMOVE`>> macro.
676  **/
677 static void HASH_PREFIX(remove)(TAC HASH_NODE *n)
678 {
679   P(bucket) *x = SKIP_BACK(struct P(bucket), n, n);
680   uns h0 = P(bucket_hash)(TTC x);
681   uns h = h0 % T.hash_size;
682   P(bucket) *b, **bb;
683
684   for (bb=&T.ht[h]; (b=*bb) && b != x; bb=&b->next)
685     ;
686   ASSERT(b);
687   *bb = b->next;
688   P(free)(TTC b);
689 #ifndef HASH_TABLE_GROWING
690   if (--T.hash_count < T.hash_min)
691     P(rehash)(TTC T.hash_size/2);
692 #endif
693 }
694 #endif
695
696 /* And the iterator */
697
698 #ifndef HASH_FOR_ALL
699
700 #define HASH_FOR_ALL_DYNAMIC(h_px, h_table, h_var)                                      \
701 do {                                                                                    \
702   uns h_slot;                                                                           \
703   struct GLUE_(h_px,bucket) *h_buck;                                                    \
704   for (h_slot=0; h_slot < (h_table)->hash_size; h_slot++)                               \
705     for (h_buck = (h_table)->ht[h_slot]; h_buck; h_buck = h_buck->next)                 \
706       {                                                                                 \
707         GLUE_(h_px,node) *h_var = &h_buck->n;
708 #define HASH_FOR_ALL(h_px, h_var) HASH_FOR_ALL_DYNAMIC(h_px, &GLUE_(h_px,table), h_var)
709 #define HASH_END_FOR } } while(0)
710 #define HASH_BREAK
711 #define HASH_CONTINUE continue
712
713 #endif
714
715 /* Finally, undefine all the parameters */
716
717 #undef P
718 #undef T
719 #undef TA
720 #undef TAC
721 #undef TAU
722 #undef TAUC
723 #undef TT
724 #undef TTC
725
726 #undef HASH_ATOMIC_TYPE
727 #undef HASH_CONSERVE_SPACE
728 #undef HASH_DEFAULT_SIZE
729 #undef HASH_EXTRA_SIZE
730 #undef HASH_FN_BITS
731 #undef HASH_GIVE_ALLOC
732 #undef HASH_GIVE_TABLE_ALLOC
733 #undef HASH_GIVE_EQ
734 #undef HASH_GIVE_EXTRA_SIZE
735 #undef HASH_GIVE_HASHFN
736 #undef HASH_GIVE_INIT_DATA
737 #undef HASH_GIVE_INIT_KEY
738 #undef HASH_KEY
739 #undef HASH_KEY_ATOMIC
740 #undef HASH_KEY_COMPLEX
741 #undef HASH_KEY_DECL
742 #undef HASH_KEY_ENDSTRING
743 #undef HASH_KEY_STRING
744 #undef HASH_KEY_MEMORY
745 #undef HASH_KEY_SIZE
746 #undef HASH_NOCASE
747 #undef HASH_NODE
748 #undef HASH_PREFIX
749 #undef HASH_USE_POOL
750 #undef HASH_AUTO_POOL
751 #undef HASH_USE_ELTPOOL
752 #undef HASH_AUTO_ELTPOOL
753 #undef HASH_WANT_CLEANUP
754 #undef HASH_WANT_DELETE
755 #undef HASH_WANT_FIND
756 #undef HASH_WANT_FIND_NEXT
757 #undef HASH_WANT_LOOKUP
758 #undef HASH_WANT_NEW
759 #undef HASH_WANT_REMOVE
760 #undef HASH_TABLE_ALLOC
761 #undef HASH_TABLE_GROWING
762 #undef HASH_TABLE_DYNAMIC
763 #undef HASH_TABLE_VARS
764 #undef HASH_ZERO_FILL