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