xref: /dflybsd-src/contrib/gdb-7/libiberty/hashtab.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /* An expandable hash tables datatype.
2*86d7f5d3SJohn Marino    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2009, 2010
3*86d7f5d3SJohn Marino    Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino    Contributed by Vladimir Makarov (vmakarov@cygnus.com).
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino This file is part of the libiberty library.
7*86d7f5d3SJohn Marino Libiberty is free software; you can redistribute it and/or
8*86d7f5d3SJohn Marino modify it under the terms of the GNU Library General Public
9*86d7f5d3SJohn Marino License as published by the Free Software Foundation; either
10*86d7f5d3SJohn Marino version 2 of the License, or (at your option) any later version.
11*86d7f5d3SJohn Marino 
12*86d7f5d3SJohn Marino Libiberty is distributed in the hope that it will be useful,
13*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
14*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*86d7f5d3SJohn Marino Library General Public License for more details.
16*86d7f5d3SJohn Marino 
17*86d7f5d3SJohn Marino You should have received a copy of the GNU Library General Public
18*86d7f5d3SJohn Marino License along with libiberty; see the file COPYING.LIB.  If
19*86d7f5d3SJohn Marino not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20*86d7f5d3SJohn Marino Boston, MA 02110-1301, USA.  */
21*86d7f5d3SJohn Marino 
22*86d7f5d3SJohn Marino /* This package implements basic hash table functionality.  It is possible
23*86d7f5d3SJohn Marino    to search for an entry, create an entry and destroy an entry.
24*86d7f5d3SJohn Marino 
25*86d7f5d3SJohn Marino    Elements in the table are generic pointers.
26*86d7f5d3SJohn Marino 
27*86d7f5d3SJohn Marino    The size of the table is not fixed; if the occupancy of the table
28*86d7f5d3SJohn Marino    grows too high the hash table will be expanded.
29*86d7f5d3SJohn Marino 
30*86d7f5d3SJohn Marino    The abstract data implementation is based on generalized Algorithm D
31*86d7f5d3SJohn Marino    from Knuth's book "The art of computer programming".  Hash table is
32*86d7f5d3SJohn Marino    expanded by creation of new hash table and transferring elements from
33*86d7f5d3SJohn Marino    the old table to the new table. */
34*86d7f5d3SJohn Marino 
35*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
36*86d7f5d3SJohn Marino #include "config.h"
37*86d7f5d3SJohn Marino #endif
38*86d7f5d3SJohn Marino 
39*86d7f5d3SJohn Marino #include <sys/types.h>
40*86d7f5d3SJohn Marino 
41*86d7f5d3SJohn Marino #ifdef HAVE_STDLIB_H
42*86d7f5d3SJohn Marino #include <stdlib.h>
43*86d7f5d3SJohn Marino #endif
44*86d7f5d3SJohn Marino #ifdef HAVE_STRING_H
45*86d7f5d3SJohn Marino #include <string.h>
46*86d7f5d3SJohn Marino #endif
47*86d7f5d3SJohn Marino #ifdef HAVE_MALLOC_H
48*86d7f5d3SJohn Marino #include <malloc.h>
49*86d7f5d3SJohn Marino #endif
50*86d7f5d3SJohn Marino #ifdef HAVE_LIMITS_H
51*86d7f5d3SJohn Marino #include <limits.h>
52*86d7f5d3SJohn Marino #endif
53*86d7f5d3SJohn Marino #ifdef HAVE_INTTYPES_H
54*86d7f5d3SJohn Marino #include <inttypes.h>
55*86d7f5d3SJohn Marino #endif
56*86d7f5d3SJohn Marino #ifdef HAVE_STDINT_H
57*86d7f5d3SJohn Marino #include <stdint.h>
58*86d7f5d3SJohn Marino #endif
59*86d7f5d3SJohn Marino 
60*86d7f5d3SJohn Marino #include <stdio.h>
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino #include "libiberty.h"
63*86d7f5d3SJohn Marino #include "ansidecl.h"
64*86d7f5d3SJohn Marino #include "hashtab.h"
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino #ifndef CHAR_BIT
67*86d7f5d3SJohn Marino #define CHAR_BIT 8
68*86d7f5d3SJohn Marino #endif
69*86d7f5d3SJohn Marino 
70*86d7f5d3SJohn Marino static unsigned int higher_prime_index (unsigned long);
71*86d7f5d3SJohn Marino static hashval_t htab_mod_1 (hashval_t, hashval_t, hashval_t, int);
72*86d7f5d3SJohn Marino static hashval_t htab_mod (hashval_t, htab_t);
73*86d7f5d3SJohn Marino static hashval_t htab_mod_m2 (hashval_t, htab_t);
74*86d7f5d3SJohn Marino static hashval_t hash_pointer (const void *);
75*86d7f5d3SJohn Marino static int eq_pointer (const void *, const void *);
76*86d7f5d3SJohn Marino static int htab_expand (htab_t);
77*86d7f5d3SJohn Marino static PTR *find_empty_slot_for_expand (htab_t, hashval_t);
78*86d7f5d3SJohn Marino 
79*86d7f5d3SJohn Marino /* At some point, we could make these be NULL, and modify the
80*86d7f5d3SJohn Marino    hash-table routines to handle NULL specially; that would avoid
81*86d7f5d3SJohn Marino    function-call overhead for the common case of hashing pointers.  */
82*86d7f5d3SJohn Marino htab_hash htab_hash_pointer = hash_pointer;
83*86d7f5d3SJohn Marino htab_eq htab_eq_pointer = eq_pointer;
84*86d7f5d3SJohn Marino 
85*86d7f5d3SJohn Marino /* Table of primes and multiplicative inverses.
86*86d7f5d3SJohn Marino 
87*86d7f5d3SJohn Marino    Note that these are not minimally reduced inverses.  Unlike when generating
88*86d7f5d3SJohn Marino    code to divide by a constant, we want to be able to use the same algorithm
89*86d7f5d3SJohn Marino    all the time.  All of these inverses (are implied to) have bit 32 set.
90*86d7f5d3SJohn Marino 
91*86d7f5d3SJohn Marino    For the record, here's the function that computed the table; it's a
92*86d7f5d3SJohn Marino    vastly simplified version of the function of the same name from gcc.  */
93*86d7f5d3SJohn Marino 
94*86d7f5d3SJohn Marino #if 0
95*86d7f5d3SJohn Marino unsigned int
96*86d7f5d3SJohn Marino ceil_log2 (unsigned int x)
97*86d7f5d3SJohn Marino {
98*86d7f5d3SJohn Marino   int i;
99*86d7f5d3SJohn Marino   for (i = 31; i >= 0 ; --i)
100*86d7f5d3SJohn Marino     if (x > (1u << i))
101*86d7f5d3SJohn Marino       return i+1;
102*86d7f5d3SJohn Marino   abort ();
103*86d7f5d3SJohn Marino }
104*86d7f5d3SJohn Marino 
105*86d7f5d3SJohn Marino unsigned int
106*86d7f5d3SJohn Marino choose_multiplier (unsigned int d, unsigned int *mlp, unsigned char *shiftp)
107*86d7f5d3SJohn Marino {
108*86d7f5d3SJohn Marino   unsigned long long mhigh;
109*86d7f5d3SJohn Marino   double nx;
110*86d7f5d3SJohn Marino   int lgup, post_shift;
111*86d7f5d3SJohn Marino   int pow, pow2;
112*86d7f5d3SJohn Marino   int n = 32, precision = 32;
113*86d7f5d3SJohn Marino 
114*86d7f5d3SJohn Marino   lgup = ceil_log2 (d);
115*86d7f5d3SJohn Marino   pow = n + lgup;
116*86d7f5d3SJohn Marino   pow2 = n + lgup - precision;
117*86d7f5d3SJohn Marino 
118*86d7f5d3SJohn Marino   nx = ldexp (1.0, pow) + ldexp (1.0, pow2);
119*86d7f5d3SJohn Marino   mhigh = nx / d;
120*86d7f5d3SJohn Marino 
121*86d7f5d3SJohn Marino   *shiftp = lgup - 1;
122*86d7f5d3SJohn Marino   *mlp = mhigh;
123*86d7f5d3SJohn Marino   return mhigh >> 32;
124*86d7f5d3SJohn Marino }
125*86d7f5d3SJohn Marino #endif
126*86d7f5d3SJohn Marino 
127*86d7f5d3SJohn Marino struct prime_ent
128*86d7f5d3SJohn Marino {
129*86d7f5d3SJohn Marino   hashval_t prime;
130*86d7f5d3SJohn Marino   hashval_t inv;
131*86d7f5d3SJohn Marino   hashval_t inv_m2;	/* inverse of prime-2 */
132*86d7f5d3SJohn Marino   hashval_t shift;
133*86d7f5d3SJohn Marino };
134*86d7f5d3SJohn Marino 
135*86d7f5d3SJohn Marino static struct prime_ent const prime_tab[] = {
136*86d7f5d3SJohn Marino   {          7, 0x24924925, 0x9999999b, 2 },
137*86d7f5d3SJohn Marino   {         13, 0x3b13b13c, 0x745d1747, 3 },
138*86d7f5d3SJohn Marino   {         31, 0x08421085, 0x1a7b9612, 4 },
139*86d7f5d3SJohn Marino   {         61, 0x0c9714fc, 0x15b1e5f8, 5 },
140*86d7f5d3SJohn Marino   {        127, 0x02040811, 0x0624dd30, 6 },
141*86d7f5d3SJohn Marino   {        251, 0x05197f7e, 0x073260a5, 7 },
142*86d7f5d3SJohn Marino   {        509, 0x01824366, 0x02864fc8, 8 },
143*86d7f5d3SJohn Marino   {       1021, 0x00c0906d, 0x014191f7, 9 },
144*86d7f5d3SJohn Marino   {       2039, 0x0121456f, 0x0161e69e, 10 },
145*86d7f5d3SJohn Marino   {       4093, 0x00300902, 0x00501908, 11 },
146*86d7f5d3SJohn Marino   {       8191, 0x00080041, 0x00180241, 12 },
147*86d7f5d3SJohn Marino   {      16381, 0x000c0091, 0x00140191, 13 },
148*86d7f5d3SJohn Marino   {      32749, 0x002605a5, 0x002a06e6, 14 },
149*86d7f5d3SJohn Marino   {      65521, 0x000f00e2, 0x00110122, 15 },
150*86d7f5d3SJohn Marino   {     131071, 0x00008001, 0x00018003, 16 },
151*86d7f5d3SJohn Marino   {     262139, 0x00014002, 0x0001c004, 17 },
152*86d7f5d3SJohn Marino   {     524287, 0x00002001, 0x00006001, 18 },
153*86d7f5d3SJohn Marino   {    1048573, 0x00003001, 0x00005001, 19 },
154*86d7f5d3SJohn Marino   {    2097143, 0x00004801, 0x00005801, 20 },
155*86d7f5d3SJohn Marino   {    4194301, 0x00000c01, 0x00001401, 21 },
156*86d7f5d3SJohn Marino   {    8388593, 0x00001e01, 0x00002201, 22 },
157*86d7f5d3SJohn Marino   {   16777213, 0x00000301, 0x00000501, 23 },
158*86d7f5d3SJohn Marino   {   33554393, 0x00001381, 0x00001481, 24 },
159*86d7f5d3SJohn Marino   {   67108859, 0x00000141, 0x000001c1, 25 },
160*86d7f5d3SJohn Marino   {  134217689, 0x000004e1, 0x00000521, 26 },
161*86d7f5d3SJohn Marino   {  268435399, 0x00000391, 0x000003b1, 27 },
162*86d7f5d3SJohn Marino   {  536870909, 0x00000019, 0x00000029, 28 },
163*86d7f5d3SJohn Marino   { 1073741789, 0x0000008d, 0x00000095, 29 },
164*86d7f5d3SJohn Marino   { 2147483647, 0x00000003, 0x00000007, 30 },
165*86d7f5d3SJohn Marino   /* Avoid "decimal constant so large it is unsigned" for 4294967291.  */
166*86d7f5d3SJohn Marino   { 0xfffffffb, 0x00000006, 0x00000008, 31 }
167*86d7f5d3SJohn Marino };
168*86d7f5d3SJohn Marino 
169*86d7f5d3SJohn Marino /* The following function returns an index into the above table of the
170*86d7f5d3SJohn Marino    nearest prime number which is greater than N, and near a power of two. */
171*86d7f5d3SJohn Marino 
172*86d7f5d3SJohn Marino static unsigned int
higher_prime_index(unsigned long n)173*86d7f5d3SJohn Marino higher_prime_index (unsigned long n)
174*86d7f5d3SJohn Marino {
175*86d7f5d3SJohn Marino   unsigned int low = 0;
176*86d7f5d3SJohn Marino   unsigned int high = sizeof(prime_tab) / sizeof(prime_tab[0]);
177*86d7f5d3SJohn Marino 
178*86d7f5d3SJohn Marino   while (low != high)
179*86d7f5d3SJohn Marino     {
180*86d7f5d3SJohn Marino       unsigned int mid = low + (high - low) / 2;
181*86d7f5d3SJohn Marino       if (n > prime_tab[mid].prime)
182*86d7f5d3SJohn Marino 	low = mid + 1;
183*86d7f5d3SJohn Marino       else
184*86d7f5d3SJohn Marino 	high = mid;
185*86d7f5d3SJohn Marino     }
186*86d7f5d3SJohn Marino 
187*86d7f5d3SJohn Marino   /* If we've run out of primes, abort.  */
188*86d7f5d3SJohn Marino   if (n > prime_tab[low].prime)
189*86d7f5d3SJohn Marino     {
190*86d7f5d3SJohn Marino       fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
191*86d7f5d3SJohn Marino       abort ();
192*86d7f5d3SJohn Marino     }
193*86d7f5d3SJohn Marino 
194*86d7f5d3SJohn Marino   return low;
195*86d7f5d3SJohn Marino }
196*86d7f5d3SJohn Marino 
197*86d7f5d3SJohn Marino /* Returns a hash code for P.  */
198*86d7f5d3SJohn Marino 
199*86d7f5d3SJohn Marino static hashval_t
hash_pointer(const PTR p)200*86d7f5d3SJohn Marino hash_pointer (const PTR p)
201*86d7f5d3SJohn Marino {
202*86d7f5d3SJohn Marino   return (hashval_t) ((intptr_t)p >> 3);
203*86d7f5d3SJohn Marino }
204*86d7f5d3SJohn Marino 
205*86d7f5d3SJohn Marino /* Returns non-zero if P1 and P2 are equal.  */
206*86d7f5d3SJohn Marino 
207*86d7f5d3SJohn Marino static int
eq_pointer(const PTR p1,const PTR p2)208*86d7f5d3SJohn Marino eq_pointer (const PTR p1, const PTR p2)
209*86d7f5d3SJohn Marino {
210*86d7f5d3SJohn Marino   return p1 == p2;
211*86d7f5d3SJohn Marino }
212*86d7f5d3SJohn Marino 
213*86d7f5d3SJohn Marino 
214*86d7f5d3SJohn Marino /* The parens around the function names in the next two definitions
215*86d7f5d3SJohn Marino    are essential in order to prevent macro expansions of the name.
216*86d7f5d3SJohn Marino    The bodies, however, are expanded as expected, so they are not
217*86d7f5d3SJohn Marino    recursive definitions.  */
218*86d7f5d3SJohn Marino 
219*86d7f5d3SJohn Marino /* Return the current size of given hash table.  */
220*86d7f5d3SJohn Marino 
221*86d7f5d3SJohn Marino #define htab_size(htab)  ((htab)->size)
222*86d7f5d3SJohn Marino 
size_t(htab_size)223*86d7f5d3SJohn Marino size_t
224*86d7f5d3SJohn Marino (htab_size) (htab_t htab)
225*86d7f5d3SJohn Marino {
226*86d7f5d3SJohn Marino   return htab_size (htab);
227*86d7f5d3SJohn Marino }
228*86d7f5d3SJohn Marino 
229*86d7f5d3SJohn Marino /* Return the current number of elements in given hash table. */
230*86d7f5d3SJohn Marino 
231*86d7f5d3SJohn Marino #define htab_elements(htab)  ((htab)->n_elements - (htab)->n_deleted)
232*86d7f5d3SJohn Marino 
size_t(htab_elements)233*86d7f5d3SJohn Marino size_t
234*86d7f5d3SJohn Marino (htab_elements) (htab_t htab)
235*86d7f5d3SJohn Marino {
236*86d7f5d3SJohn Marino   return htab_elements (htab);
237*86d7f5d3SJohn Marino }
238*86d7f5d3SJohn Marino 
239*86d7f5d3SJohn Marino /* Return X % Y.  */
240*86d7f5d3SJohn Marino 
241*86d7f5d3SJohn Marino static inline hashval_t
htab_mod_1(hashval_t x,hashval_t y,hashval_t inv,int shift)242*86d7f5d3SJohn Marino htab_mod_1 (hashval_t x, hashval_t y, hashval_t inv, int shift)
243*86d7f5d3SJohn Marino {
244*86d7f5d3SJohn Marino   /* The multiplicative inverses computed above are for 32-bit types, and
245*86d7f5d3SJohn Marino      requires that we be able to compute a highpart multiply.  */
246*86d7f5d3SJohn Marino #ifdef UNSIGNED_64BIT_TYPE
247*86d7f5d3SJohn Marino   __extension__ typedef UNSIGNED_64BIT_TYPE ull;
248*86d7f5d3SJohn Marino   if (sizeof (hashval_t) * CHAR_BIT <= 32)
249*86d7f5d3SJohn Marino     {
250*86d7f5d3SJohn Marino       hashval_t t1, t2, t3, t4, q, r;
251*86d7f5d3SJohn Marino 
252*86d7f5d3SJohn Marino       t1 = ((ull)x * inv) >> 32;
253*86d7f5d3SJohn Marino       t2 = x - t1;
254*86d7f5d3SJohn Marino       t3 = t2 >> 1;
255*86d7f5d3SJohn Marino       t4 = t1 + t3;
256*86d7f5d3SJohn Marino       q  = t4 >> shift;
257*86d7f5d3SJohn Marino       r  = x - (q * y);
258*86d7f5d3SJohn Marino 
259*86d7f5d3SJohn Marino       return r;
260*86d7f5d3SJohn Marino     }
261*86d7f5d3SJohn Marino #endif
262*86d7f5d3SJohn Marino 
263*86d7f5d3SJohn Marino   /* Otherwise just use the native division routines.  */
264*86d7f5d3SJohn Marino   return x % y;
265*86d7f5d3SJohn Marino }
266*86d7f5d3SJohn Marino 
267*86d7f5d3SJohn Marino /* Compute the primary hash for HASH given HTAB's current size.  */
268*86d7f5d3SJohn Marino 
269*86d7f5d3SJohn Marino static inline hashval_t
htab_mod(hashval_t hash,htab_t htab)270*86d7f5d3SJohn Marino htab_mod (hashval_t hash, htab_t htab)
271*86d7f5d3SJohn Marino {
272*86d7f5d3SJohn Marino   const struct prime_ent *p = &prime_tab[htab->size_prime_index];
273*86d7f5d3SJohn Marino   return htab_mod_1 (hash, p->prime, p->inv, p->shift);
274*86d7f5d3SJohn Marino }
275*86d7f5d3SJohn Marino 
276*86d7f5d3SJohn Marino /* Compute the secondary hash for HASH given HTAB's current size.  */
277*86d7f5d3SJohn Marino 
278*86d7f5d3SJohn Marino static inline hashval_t
htab_mod_m2(hashval_t hash,htab_t htab)279*86d7f5d3SJohn Marino htab_mod_m2 (hashval_t hash, htab_t htab)
280*86d7f5d3SJohn Marino {
281*86d7f5d3SJohn Marino   const struct prime_ent *p = &prime_tab[htab->size_prime_index];
282*86d7f5d3SJohn Marino   return 1 + htab_mod_1 (hash, p->prime - 2, p->inv_m2, p->shift);
283*86d7f5d3SJohn Marino }
284*86d7f5d3SJohn Marino 
285*86d7f5d3SJohn Marino /* This function creates table with length slightly longer than given
286*86d7f5d3SJohn Marino    source length.  Created hash table is initiated as empty (all the
287*86d7f5d3SJohn Marino    hash table entries are HTAB_EMPTY_ENTRY).  The function returns the
288*86d7f5d3SJohn Marino    created hash table, or NULL if memory allocation fails.  */
289*86d7f5d3SJohn Marino 
290*86d7f5d3SJohn Marino htab_t
htab_create_alloc(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f,htab_alloc alloc_f,htab_free free_f)291*86d7f5d3SJohn Marino htab_create_alloc (size_t size, htab_hash hash_f, htab_eq eq_f,
292*86d7f5d3SJohn Marino                    htab_del del_f, htab_alloc alloc_f, htab_free free_f)
293*86d7f5d3SJohn Marino {
294*86d7f5d3SJohn Marino   return htab_create_typed_alloc (size, hash_f, eq_f, del_f, alloc_f, alloc_f,
295*86d7f5d3SJohn Marino 				  free_f);
296*86d7f5d3SJohn Marino }
297*86d7f5d3SJohn Marino 
298*86d7f5d3SJohn Marino /* As above, but uses the variants of ALLOC_F and FREE_F which accept
299*86d7f5d3SJohn Marino    an extra argument.  */
300*86d7f5d3SJohn Marino 
301*86d7f5d3SJohn Marino htab_t
htab_create_alloc_ex(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f,void * alloc_arg,htab_alloc_with_arg alloc_f,htab_free_with_arg free_f)302*86d7f5d3SJohn Marino htab_create_alloc_ex (size_t size, htab_hash hash_f, htab_eq eq_f,
303*86d7f5d3SJohn Marino 		      htab_del del_f, void *alloc_arg,
304*86d7f5d3SJohn Marino 		      htab_alloc_with_arg alloc_f,
305*86d7f5d3SJohn Marino 		      htab_free_with_arg free_f)
306*86d7f5d3SJohn Marino {
307*86d7f5d3SJohn Marino   htab_t result;
308*86d7f5d3SJohn Marino   unsigned int size_prime_index;
309*86d7f5d3SJohn Marino 
310*86d7f5d3SJohn Marino   size_prime_index = higher_prime_index (size);
311*86d7f5d3SJohn Marino   size = prime_tab[size_prime_index].prime;
312*86d7f5d3SJohn Marino 
313*86d7f5d3SJohn Marino   result = (htab_t) (*alloc_f) (alloc_arg, 1, sizeof (struct htab));
314*86d7f5d3SJohn Marino   if (result == NULL)
315*86d7f5d3SJohn Marino     return NULL;
316*86d7f5d3SJohn Marino   result->entries = (PTR *) (*alloc_f) (alloc_arg, size, sizeof (PTR));
317*86d7f5d3SJohn Marino   if (result->entries == NULL)
318*86d7f5d3SJohn Marino     {
319*86d7f5d3SJohn Marino       if (free_f != NULL)
320*86d7f5d3SJohn Marino 	(*free_f) (alloc_arg, result);
321*86d7f5d3SJohn Marino       return NULL;
322*86d7f5d3SJohn Marino     }
323*86d7f5d3SJohn Marino   result->size = size;
324*86d7f5d3SJohn Marino   result->size_prime_index = size_prime_index;
325*86d7f5d3SJohn Marino   result->hash_f = hash_f;
326*86d7f5d3SJohn Marino   result->eq_f = eq_f;
327*86d7f5d3SJohn Marino   result->del_f = del_f;
328*86d7f5d3SJohn Marino   result->alloc_arg = alloc_arg;
329*86d7f5d3SJohn Marino   result->alloc_with_arg_f = alloc_f;
330*86d7f5d3SJohn Marino   result->free_with_arg_f = free_f;
331*86d7f5d3SJohn Marino   return result;
332*86d7f5d3SJohn Marino }
333*86d7f5d3SJohn Marino 
334*86d7f5d3SJohn Marino /*
335*86d7f5d3SJohn Marino 
336*86d7f5d3SJohn Marino @deftypefn Supplemental htab_t htab_create_typed_alloc (size_t @var{size}, @
337*86d7f5d3SJohn Marino htab_hash @var{hash_f}, htab_eq @var{eq_f}, htab_del @var{del_f}, @
338*86d7f5d3SJohn Marino htab_alloc @var{alloc_tab_f}, htab_alloc @var{alloc_f}, @
339*86d7f5d3SJohn Marino htab_free @var{free_f})
340*86d7f5d3SJohn Marino 
341*86d7f5d3SJohn Marino This function creates a hash table that uses two different allocators
342*86d7f5d3SJohn Marino @var{alloc_tab_f} and @var{alloc_f} to use for allocating the table itself
343*86d7f5d3SJohn Marino and its entries respectively.  This is useful when variables of different
344*86d7f5d3SJohn Marino types need to be allocated with different allocators.
345*86d7f5d3SJohn Marino 
346*86d7f5d3SJohn Marino The created hash table is slightly larger than @var{size} and it is
347*86d7f5d3SJohn Marino initially empty (all the hash table entries are @code{HTAB_EMPTY_ENTRY}).
348*86d7f5d3SJohn Marino The function returns the created hash table, or @code{NULL} if memory
349*86d7f5d3SJohn Marino allocation fails.
350*86d7f5d3SJohn Marino 
351*86d7f5d3SJohn Marino @end deftypefn
352*86d7f5d3SJohn Marino 
353*86d7f5d3SJohn Marino */
354*86d7f5d3SJohn Marino 
355*86d7f5d3SJohn Marino htab_t
htab_create_typed_alloc(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f,htab_alloc alloc_tab_f,htab_alloc alloc_f,htab_free free_f)356*86d7f5d3SJohn Marino htab_create_typed_alloc (size_t size, htab_hash hash_f, htab_eq eq_f,
357*86d7f5d3SJohn Marino 			 htab_del del_f, htab_alloc alloc_tab_f,
358*86d7f5d3SJohn Marino 			 htab_alloc alloc_f, htab_free free_f)
359*86d7f5d3SJohn Marino {
360*86d7f5d3SJohn Marino   htab_t result;
361*86d7f5d3SJohn Marino   unsigned int size_prime_index;
362*86d7f5d3SJohn Marino 
363*86d7f5d3SJohn Marino   size_prime_index = higher_prime_index (size);
364*86d7f5d3SJohn Marino   size = prime_tab[size_prime_index].prime;
365*86d7f5d3SJohn Marino 
366*86d7f5d3SJohn Marino   result = (htab_t) (*alloc_tab_f) (1, sizeof (struct htab));
367*86d7f5d3SJohn Marino   if (result == NULL)
368*86d7f5d3SJohn Marino     return NULL;
369*86d7f5d3SJohn Marino   result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR));
370*86d7f5d3SJohn Marino   if (result->entries == NULL)
371*86d7f5d3SJohn Marino     {
372*86d7f5d3SJohn Marino       if (free_f != NULL)
373*86d7f5d3SJohn Marino 	(*free_f) (result);
374*86d7f5d3SJohn Marino       return NULL;
375*86d7f5d3SJohn Marino     }
376*86d7f5d3SJohn Marino   result->size = size;
377*86d7f5d3SJohn Marino   result->size_prime_index = size_prime_index;
378*86d7f5d3SJohn Marino   result->hash_f = hash_f;
379*86d7f5d3SJohn Marino   result->eq_f = eq_f;
380*86d7f5d3SJohn Marino   result->del_f = del_f;
381*86d7f5d3SJohn Marino   result->alloc_f = alloc_f;
382*86d7f5d3SJohn Marino   result->free_f = free_f;
383*86d7f5d3SJohn Marino   return result;
384*86d7f5d3SJohn Marino }
385*86d7f5d3SJohn Marino 
386*86d7f5d3SJohn Marino 
387*86d7f5d3SJohn Marino /* Update the function pointers and allocation parameter in the htab_t.  */
388*86d7f5d3SJohn Marino 
389*86d7f5d3SJohn Marino void
htab_set_functions_ex(htab_t htab,htab_hash hash_f,htab_eq eq_f,htab_del del_f,PTR alloc_arg,htab_alloc_with_arg alloc_f,htab_free_with_arg free_f)390*86d7f5d3SJohn Marino htab_set_functions_ex (htab_t htab, htab_hash hash_f, htab_eq eq_f,
391*86d7f5d3SJohn Marino                        htab_del del_f, PTR alloc_arg,
392*86d7f5d3SJohn Marino                        htab_alloc_with_arg alloc_f, htab_free_with_arg free_f)
393*86d7f5d3SJohn Marino {
394*86d7f5d3SJohn Marino   htab->hash_f = hash_f;
395*86d7f5d3SJohn Marino   htab->eq_f = eq_f;
396*86d7f5d3SJohn Marino   htab->del_f = del_f;
397*86d7f5d3SJohn Marino   htab->alloc_arg = alloc_arg;
398*86d7f5d3SJohn Marino   htab->alloc_with_arg_f = alloc_f;
399*86d7f5d3SJohn Marino   htab->free_with_arg_f = free_f;
400*86d7f5d3SJohn Marino }
401*86d7f5d3SJohn Marino 
402*86d7f5d3SJohn Marino /* These functions exist solely for backward compatibility.  */
403*86d7f5d3SJohn Marino 
404*86d7f5d3SJohn Marino #undef htab_create
405*86d7f5d3SJohn Marino htab_t
htab_create(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f)406*86d7f5d3SJohn Marino htab_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f)
407*86d7f5d3SJohn Marino {
408*86d7f5d3SJohn Marino   return htab_create_alloc (size, hash_f, eq_f, del_f, xcalloc, free);
409*86d7f5d3SJohn Marino }
410*86d7f5d3SJohn Marino 
411*86d7f5d3SJohn Marino htab_t
htab_try_create(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f)412*86d7f5d3SJohn Marino htab_try_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f)
413*86d7f5d3SJohn Marino {
414*86d7f5d3SJohn Marino   return htab_create_alloc (size, hash_f, eq_f, del_f, calloc, free);
415*86d7f5d3SJohn Marino }
416*86d7f5d3SJohn Marino 
417*86d7f5d3SJohn Marino /* This function frees all memory allocated for given hash table.
418*86d7f5d3SJohn Marino    Naturally the hash table must already exist. */
419*86d7f5d3SJohn Marino 
420*86d7f5d3SJohn Marino void
htab_delete(htab_t htab)421*86d7f5d3SJohn Marino htab_delete (htab_t htab)
422*86d7f5d3SJohn Marino {
423*86d7f5d3SJohn Marino   size_t size = htab_size (htab);
424*86d7f5d3SJohn Marino   PTR *entries = htab->entries;
425*86d7f5d3SJohn Marino   int i;
426*86d7f5d3SJohn Marino 
427*86d7f5d3SJohn Marino   if (htab->del_f)
428*86d7f5d3SJohn Marino     for (i = size - 1; i >= 0; i--)
429*86d7f5d3SJohn Marino       if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY)
430*86d7f5d3SJohn Marino 	(*htab->del_f) (entries[i]);
431*86d7f5d3SJohn Marino 
432*86d7f5d3SJohn Marino   if (htab->free_f != NULL)
433*86d7f5d3SJohn Marino     {
434*86d7f5d3SJohn Marino       (*htab->free_f) (entries);
435*86d7f5d3SJohn Marino       (*htab->free_f) (htab);
436*86d7f5d3SJohn Marino     }
437*86d7f5d3SJohn Marino   else if (htab->free_with_arg_f != NULL)
438*86d7f5d3SJohn Marino     {
439*86d7f5d3SJohn Marino       (*htab->free_with_arg_f) (htab->alloc_arg, entries);
440*86d7f5d3SJohn Marino       (*htab->free_with_arg_f) (htab->alloc_arg, htab);
441*86d7f5d3SJohn Marino     }
442*86d7f5d3SJohn Marino }
443*86d7f5d3SJohn Marino 
444*86d7f5d3SJohn Marino /* This function clears all entries in the given hash table.  */
445*86d7f5d3SJohn Marino 
446*86d7f5d3SJohn Marino void
htab_empty(htab_t htab)447*86d7f5d3SJohn Marino htab_empty (htab_t htab)
448*86d7f5d3SJohn Marino {
449*86d7f5d3SJohn Marino   size_t size = htab_size (htab);
450*86d7f5d3SJohn Marino   PTR *entries = htab->entries;
451*86d7f5d3SJohn Marino   int i;
452*86d7f5d3SJohn Marino 
453*86d7f5d3SJohn Marino   if (htab->del_f)
454*86d7f5d3SJohn Marino     for (i = size - 1; i >= 0; i--)
455*86d7f5d3SJohn Marino       if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY)
456*86d7f5d3SJohn Marino 	(*htab->del_f) (entries[i]);
457*86d7f5d3SJohn Marino 
458*86d7f5d3SJohn Marino   /* Instead of clearing megabyte, downsize the table.  */
459*86d7f5d3SJohn Marino   if (size > 1024*1024 / sizeof (PTR))
460*86d7f5d3SJohn Marino     {
461*86d7f5d3SJohn Marino       int nindex = higher_prime_index (1024 / sizeof (PTR));
462*86d7f5d3SJohn Marino       int nsize = prime_tab[nindex].prime;
463*86d7f5d3SJohn Marino 
464*86d7f5d3SJohn Marino       if (htab->free_f != NULL)
465*86d7f5d3SJohn Marino 	(*htab->free_f) (htab->entries);
466*86d7f5d3SJohn Marino       else if (htab->free_with_arg_f != NULL)
467*86d7f5d3SJohn Marino 	(*htab->free_with_arg_f) (htab->alloc_arg, htab->entries);
468*86d7f5d3SJohn Marino       if (htab->alloc_with_arg_f != NULL)
469*86d7f5d3SJohn Marino 	htab->entries = (PTR *) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize,
470*86d7f5d3SJohn Marino 						           sizeof (PTR *));
471*86d7f5d3SJohn Marino       else
472*86d7f5d3SJohn Marino 	htab->entries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR *));
473*86d7f5d3SJohn Marino      htab->size = nsize;
474*86d7f5d3SJohn Marino      htab->size_prime_index = nindex;
475*86d7f5d3SJohn Marino     }
476*86d7f5d3SJohn Marino   else
477*86d7f5d3SJohn Marino     memset (entries, 0, size * sizeof (PTR));
478*86d7f5d3SJohn Marino   htab->n_deleted = 0;
479*86d7f5d3SJohn Marino   htab->n_elements = 0;
480*86d7f5d3SJohn Marino }
481*86d7f5d3SJohn Marino 
482*86d7f5d3SJohn Marino /* Similar to htab_find_slot, but without several unwanted side effects:
483*86d7f5d3SJohn Marino     - Does not call htab->eq_f when it finds an existing entry.
484*86d7f5d3SJohn Marino     - Does not change the count of elements/searches/collisions in the
485*86d7f5d3SJohn Marino       hash table.
486*86d7f5d3SJohn Marino    This function also assumes there are no deleted entries in the table.
487*86d7f5d3SJohn Marino    HASH is the hash value for the element to be inserted.  */
488*86d7f5d3SJohn Marino 
489*86d7f5d3SJohn Marino static PTR *
find_empty_slot_for_expand(htab_t htab,hashval_t hash)490*86d7f5d3SJohn Marino find_empty_slot_for_expand (htab_t htab, hashval_t hash)
491*86d7f5d3SJohn Marino {
492*86d7f5d3SJohn Marino   hashval_t index = htab_mod (hash, htab);
493*86d7f5d3SJohn Marino   size_t size = htab_size (htab);
494*86d7f5d3SJohn Marino   PTR *slot = htab->entries + index;
495*86d7f5d3SJohn Marino   hashval_t hash2;
496*86d7f5d3SJohn Marino 
497*86d7f5d3SJohn Marino   if (*slot == HTAB_EMPTY_ENTRY)
498*86d7f5d3SJohn Marino     return slot;
499*86d7f5d3SJohn Marino   else if (*slot == HTAB_DELETED_ENTRY)
500*86d7f5d3SJohn Marino     abort ();
501*86d7f5d3SJohn Marino 
502*86d7f5d3SJohn Marino   hash2 = htab_mod_m2 (hash, htab);
503*86d7f5d3SJohn Marino   for (;;)
504*86d7f5d3SJohn Marino     {
505*86d7f5d3SJohn Marino       index += hash2;
506*86d7f5d3SJohn Marino       if (index >= size)
507*86d7f5d3SJohn Marino 	index -= size;
508*86d7f5d3SJohn Marino 
509*86d7f5d3SJohn Marino       slot = htab->entries + index;
510*86d7f5d3SJohn Marino       if (*slot == HTAB_EMPTY_ENTRY)
511*86d7f5d3SJohn Marino 	return slot;
512*86d7f5d3SJohn Marino       else if (*slot == HTAB_DELETED_ENTRY)
513*86d7f5d3SJohn Marino 	abort ();
514*86d7f5d3SJohn Marino     }
515*86d7f5d3SJohn Marino }
516*86d7f5d3SJohn Marino 
517*86d7f5d3SJohn Marino /* The following function changes size of memory allocated for the
518*86d7f5d3SJohn Marino    entries and repeatedly inserts the table elements.  The occupancy
519*86d7f5d3SJohn Marino    of the table after the call will be about 50%.  Naturally the hash
520*86d7f5d3SJohn Marino    table must already exist.  Remember also that the place of the
521*86d7f5d3SJohn Marino    table entries is changed.  If memory allocation failures are allowed,
522*86d7f5d3SJohn Marino    this function will return zero, indicating that the table could not be
523*86d7f5d3SJohn Marino    expanded.  If all goes well, it will return a non-zero value.  */
524*86d7f5d3SJohn Marino 
525*86d7f5d3SJohn Marino static int
htab_expand(htab_t htab)526*86d7f5d3SJohn Marino htab_expand (htab_t htab)
527*86d7f5d3SJohn Marino {
528*86d7f5d3SJohn Marino   PTR *oentries;
529*86d7f5d3SJohn Marino   PTR *olimit;
530*86d7f5d3SJohn Marino   PTR *p;
531*86d7f5d3SJohn Marino   PTR *nentries;
532*86d7f5d3SJohn Marino   size_t nsize, osize, elts;
533*86d7f5d3SJohn Marino   unsigned int oindex, nindex;
534*86d7f5d3SJohn Marino 
535*86d7f5d3SJohn Marino   oentries = htab->entries;
536*86d7f5d3SJohn Marino   oindex = htab->size_prime_index;
537*86d7f5d3SJohn Marino   osize = htab->size;
538*86d7f5d3SJohn Marino   olimit = oentries + osize;
539*86d7f5d3SJohn Marino   elts = htab_elements (htab);
540*86d7f5d3SJohn Marino 
541*86d7f5d3SJohn Marino   /* Resize only when table after removal of unused elements is either
542*86d7f5d3SJohn Marino      too full or too empty.  */
543*86d7f5d3SJohn Marino   if (elts * 2 > osize || (elts * 8 < osize && osize > 32))
544*86d7f5d3SJohn Marino     {
545*86d7f5d3SJohn Marino       nindex = higher_prime_index (elts * 2);
546*86d7f5d3SJohn Marino       nsize = prime_tab[nindex].prime;
547*86d7f5d3SJohn Marino     }
548*86d7f5d3SJohn Marino   else
549*86d7f5d3SJohn Marino     {
550*86d7f5d3SJohn Marino       nindex = oindex;
551*86d7f5d3SJohn Marino       nsize = osize;
552*86d7f5d3SJohn Marino     }
553*86d7f5d3SJohn Marino 
554*86d7f5d3SJohn Marino   if (htab->alloc_with_arg_f != NULL)
555*86d7f5d3SJohn Marino     nentries = (PTR *) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize,
556*86d7f5d3SJohn Marino 						  sizeof (PTR *));
557*86d7f5d3SJohn Marino   else
558*86d7f5d3SJohn Marino     nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR *));
559*86d7f5d3SJohn Marino   if (nentries == NULL)
560*86d7f5d3SJohn Marino     return 0;
561*86d7f5d3SJohn Marino   htab->entries = nentries;
562*86d7f5d3SJohn Marino   htab->size = nsize;
563*86d7f5d3SJohn Marino   htab->size_prime_index = nindex;
564*86d7f5d3SJohn Marino   htab->n_elements -= htab->n_deleted;
565*86d7f5d3SJohn Marino   htab->n_deleted = 0;
566*86d7f5d3SJohn Marino 
567*86d7f5d3SJohn Marino   p = oentries;
568*86d7f5d3SJohn Marino   do
569*86d7f5d3SJohn Marino     {
570*86d7f5d3SJohn Marino       PTR x = *p;
571*86d7f5d3SJohn Marino 
572*86d7f5d3SJohn Marino       if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
573*86d7f5d3SJohn Marino 	{
574*86d7f5d3SJohn Marino 	  PTR *q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x));
575*86d7f5d3SJohn Marino 
576*86d7f5d3SJohn Marino 	  *q = x;
577*86d7f5d3SJohn Marino 	}
578*86d7f5d3SJohn Marino 
579*86d7f5d3SJohn Marino       p++;
580*86d7f5d3SJohn Marino     }
581*86d7f5d3SJohn Marino   while (p < olimit);
582*86d7f5d3SJohn Marino 
583*86d7f5d3SJohn Marino   if (htab->free_f != NULL)
584*86d7f5d3SJohn Marino     (*htab->free_f) (oentries);
585*86d7f5d3SJohn Marino   else if (htab->free_with_arg_f != NULL)
586*86d7f5d3SJohn Marino     (*htab->free_with_arg_f) (htab->alloc_arg, oentries);
587*86d7f5d3SJohn Marino   return 1;
588*86d7f5d3SJohn Marino }
589*86d7f5d3SJohn Marino 
590*86d7f5d3SJohn Marino /* This function searches for a hash table entry equal to the given
591*86d7f5d3SJohn Marino    element.  It cannot be used to insert or delete an element.  */
592*86d7f5d3SJohn Marino 
593*86d7f5d3SJohn Marino PTR
htab_find_with_hash(htab_t htab,const PTR element,hashval_t hash)594*86d7f5d3SJohn Marino htab_find_with_hash (htab_t htab, const PTR element, hashval_t hash)
595*86d7f5d3SJohn Marino {
596*86d7f5d3SJohn Marino   hashval_t index, hash2;
597*86d7f5d3SJohn Marino   size_t size;
598*86d7f5d3SJohn Marino   PTR entry;
599*86d7f5d3SJohn Marino 
600*86d7f5d3SJohn Marino   htab->searches++;
601*86d7f5d3SJohn Marino   size = htab_size (htab);
602*86d7f5d3SJohn Marino   index = htab_mod (hash, htab);
603*86d7f5d3SJohn Marino 
604*86d7f5d3SJohn Marino   entry = htab->entries[index];
605*86d7f5d3SJohn Marino   if (entry == HTAB_EMPTY_ENTRY
606*86d7f5d3SJohn Marino       || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element)))
607*86d7f5d3SJohn Marino     return entry;
608*86d7f5d3SJohn Marino 
609*86d7f5d3SJohn Marino   hash2 = htab_mod_m2 (hash, htab);
610*86d7f5d3SJohn Marino   for (;;)
611*86d7f5d3SJohn Marino     {
612*86d7f5d3SJohn Marino       htab->collisions++;
613*86d7f5d3SJohn Marino       index += hash2;
614*86d7f5d3SJohn Marino       if (index >= size)
615*86d7f5d3SJohn Marino 	index -= size;
616*86d7f5d3SJohn Marino 
617*86d7f5d3SJohn Marino       entry = htab->entries[index];
618*86d7f5d3SJohn Marino       if (entry == HTAB_EMPTY_ENTRY
619*86d7f5d3SJohn Marino 	  || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element)))
620*86d7f5d3SJohn Marino 	return entry;
621*86d7f5d3SJohn Marino     }
622*86d7f5d3SJohn Marino }
623*86d7f5d3SJohn Marino 
624*86d7f5d3SJohn Marino /* Like htab_find_slot_with_hash, but compute the hash value from the
625*86d7f5d3SJohn Marino    element.  */
626*86d7f5d3SJohn Marino 
627*86d7f5d3SJohn Marino PTR
htab_find(htab_t htab,const PTR element)628*86d7f5d3SJohn Marino htab_find (htab_t htab, const PTR element)
629*86d7f5d3SJohn Marino {
630*86d7f5d3SJohn Marino   return htab_find_with_hash (htab, element, (*htab->hash_f) (element));
631*86d7f5d3SJohn Marino }
632*86d7f5d3SJohn Marino 
633*86d7f5d3SJohn Marino /* This function searches for a hash table slot containing an entry
634*86d7f5d3SJohn Marino    equal to the given element.  To delete an entry, call this with
635*86d7f5d3SJohn Marino    insert=NO_INSERT, then call htab_clear_slot on the slot returned
636*86d7f5d3SJohn Marino    (possibly after doing some checks).  To insert an entry, call this
637*86d7f5d3SJohn Marino    with insert=INSERT, then write the value you want into the returned
638*86d7f5d3SJohn Marino    slot.  When inserting an entry, NULL may be returned if memory
639*86d7f5d3SJohn Marino    allocation fails.  */
640*86d7f5d3SJohn Marino 
641*86d7f5d3SJohn Marino PTR *
htab_find_slot_with_hash(htab_t htab,const PTR element,hashval_t hash,enum insert_option insert)642*86d7f5d3SJohn Marino htab_find_slot_with_hash (htab_t htab, const PTR element,
643*86d7f5d3SJohn Marino                           hashval_t hash, enum insert_option insert)
644*86d7f5d3SJohn Marino {
645*86d7f5d3SJohn Marino   PTR *first_deleted_slot;
646*86d7f5d3SJohn Marino   hashval_t index, hash2;
647*86d7f5d3SJohn Marino   size_t size;
648*86d7f5d3SJohn Marino   PTR entry;
649*86d7f5d3SJohn Marino 
650*86d7f5d3SJohn Marino   size = htab_size (htab);
651*86d7f5d3SJohn Marino   if (insert == INSERT && size * 3 <= htab->n_elements * 4)
652*86d7f5d3SJohn Marino     {
653*86d7f5d3SJohn Marino       if (htab_expand (htab) == 0)
654*86d7f5d3SJohn Marino 	return NULL;
655*86d7f5d3SJohn Marino       size = htab_size (htab);
656*86d7f5d3SJohn Marino     }
657*86d7f5d3SJohn Marino 
658*86d7f5d3SJohn Marino   index = htab_mod (hash, htab);
659*86d7f5d3SJohn Marino 
660*86d7f5d3SJohn Marino   htab->searches++;
661*86d7f5d3SJohn Marino   first_deleted_slot = NULL;
662*86d7f5d3SJohn Marino 
663*86d7f5d3SJohn Marino   entry = htab->entries[index];
664*86d7f5d3SJohn Marino   if (entry == HTAB_EMPTY_ENTRY)
665*86d7f5d3SJohn Marino     goto empty_entry;
666*86d7f5d3SJohn Marino   else if (entry == HTAB_DELETED_ENTRY)
667*86d7f5d3SJohn Marino     first_deleted_slot = &htab->entries[index];
668*86d7f5d3SJohn Marino   else if ((*htab->eq_f) (entry, element))
669*86d7f5d3SJohn Marino     return &htab->entries[index];
670*86d7f5d3SJohn Marino 
671*86d7f5d3SJohn Marino   hash2 = htab_mod_m2 (hash, htab);
672*86d7f5d3SJohn Marino   for (;;)
673*86d7f5d3SJohn Marino     {
674*86d7f5d3SJohn Marino       htab->collisions++;
675*86d7f5d3SJohn Marino       index += hash2;
676*86d7f5d3SJohn Marino       if (index >= size)
677*86d7f5d3SJohn Marino 	index -= size;
678*86d7f5d3SJohn Marino 
679*86d7f5d3SJohn Marino       entry = htab->entries[index];
680*86d7f5d3SJohn Marino       if (entry == HTAB_EMPTY_ENTRY)
681*86d7f5d3SJohn Marino 	goto empty_entry;
682*86d7f5d3SJohn Marino       else if (entry == HTAB_DELETED_ENTRY)
683*86d7f5d3SJohn Marino 	{
684*86d7f5d3SJohn Marino 	  if (!first_deleted_slot)
685*86d7f5d3SJohn Marino 	    first_deleted_slot = &htab->entries[index];
686*86d7f5d3SJohn Marino 	}
687*86d7f5d3SJohn Marino       else if ((*htab->eq_f) (entry, element))
688*86d7f5d3SJohn Marino 	return &htab->entries[index];
689*86d7f5d3SJohn Marino     }
690*86d7f5d3SJohn Marino 
691*86d7f5d3SJohn Marino  empty_entry:
692*86d7f5d3SJohn Marino   if (insert == NO_INSERT)
693*86d7f5d3SJohn Marino     return NULL;
694*86d7f5d3SJohn Marino 
695*86d7f5d3SJohn Marino   if (first_deleted_slot)
696*86d7f5d3SJohn Marino     {
697*86d7f5d3SJohn Marino       htab->n_deleted--;
698*86d7f5d3SJohn Marino       *first_deleted_slot = HTAB_EMPTY_ENTRY;
699*86d7f5d3SJohn Marino       return first_deleted_slot;
700*86d7f5d3SJohn Marino     }
701*86d7f5d3SJohn Marino 
702*86d7f5d3SJohn Marino   htab->n_elements++;
703*86d7f5d3SJohn Marino   return &htab->entries[index];
704*86d7f5d3SJohn Marino }
705*86d7f5d3SJohn Marino 
706*86d7f5d3SJohn Marino /* Like htab_find_slot_with_hash, but compute the hash value from the
707*86d7f5d3SJohn Marino    element.  */
708*86d7f5d3SJohn Marino 
709*86d7f5d3SJohn Marino PTR *
htab_find_slot(htab_t htab,const PTR element,enum insert_option insert)710*86d7f5d3SJohn Marino htab_find_slot (htab_t htab, const PTR element, enum insert_option insert)
711*86d7f5d3SJohn Marino {
712*86d7f5d3SJohn Marino   return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element),
713*86d7f5d3SJohn Marino 				   insert);
714*86d7f5d3SJohn Marino }
715*86d7f5d3SJohn Marino 
716*86d7f5d3SJohn Marino /* This function deletes an element with the given value from hash
717*86d7f5d3SJohn Marino    table (the hash is computed from the element).  If there is no matching
718*86d7f5d3SJohn Marino    element in the hash table, this function does nothing.  */
719*86d7f5d3SJohn Marino 
720*86d7f5d3SJohn Marino void
htab_remove_elt(htab_t htab,PTR element)721*86d7f5d3SJohn Marino htab_remove_elt (htab_t htab, PTR element)
722*86d7f5d3SJohn Marino {
723*86d7f5d3SJohn Marino   htab_remove_elt_with_hash (htab, element, (*htab->hash_f) (element));
724*86d7f5d3SJohn Marino }
725*86d7f5d3SJohn Marino 
726*86d7f5d3SJohn Marino 
727*86d7f5d3SJohn Marino /* This function deletes an element with the given value from hash
728*86d7f5d3SJohn Marino    table.  If there is no matching element in the hash table, this
729*86d7f5d3SJohn Marino    function does nothing.  */
730*86d7f5d3SJohn Marino 
731*86d7f5d3SJohn Marino void
htab_remove_elt_with_hash(htab_t htab,PTR element,hashval_t hash)732*86d7f5d3SJohn Marino htab_remove_elt_with_hash (htab_t htab, PTR element, hashval_t hash)
733*86d7f5d3SJohn Marino {
734*86d7f5d3SJohn Marino   PTR *slot;
735*86d7f5d3SJohn Marino 
736*86d7f5d3SJohn Marino   slot = htab_find_slot_with_hash (htab, element, hash, NO_INSERT);
737*86d7f5d3SJohn Marino   if (*slot == HTAB_EMPTY_ENTRY)
738*86d7f5d3SJohn Marino     return;
739*86d7f5d3SJohn Marino 
740*86d7f5d3SJohn Marino   if (htab->del_f)
741*86d7f5d3SJohn Marino     (*htab->del_f) (*slot);
742*86d7f5d3SJohn Marino 
743*86d7f5d3SJohn Marino   *slot = HTAB_DELETED_ENTRY;
744*86d7f5d3SJohn Marino   htab->n_deleted++;
745*86d7f5d3SJohn Marino }
746*86d7f5d3SJohn Marino 
747*86d7f5d3SJohn Marino /* This function clears a specified slot in a hash table.  It is
748*86d7f5d3SJohn Marino    useful when you've already done the lookup and don't want to do it
749*86d7f5d3SJohn Marino    again.  */
750*86d7f5d3SJohn Marino 
751*86d7f5d3SJohn Marino void
htab_clear_slot(htab_t htab,PTR * slot)752*86d7f5d3SJohn Marino htab_clear_slot (htab_t htab, PTR *slot)
753*86d7f5d3SJohn Marino {
754*86d7f5d3SJohn Marino   if (slot < htab->entries || slot >= htab->entries + htab_size (htab)
755*86d7f5d3SJohn Marino       || *slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY)
756*86d7f5d3SJohn Marino     abort ();
757*86d7f5d3SJohn Marino 
758*86d7f5d3SJohn Marino   if (htab->del_f)
759*86d7f5d3SJohn Marino     (*htab->del_f) (*slot);
760*86d7f5d3SJohn Marino 
761*86d7f5d3SJohn Marino   *slot = HTAB_DELETED_ENTRY;
762*86d7f5d3SJohn Marino   htab->n_deleted++;
763*86d7f5d3SJohn Marino }
764*86d7f5d3SJohn Marino 
765*86d7f5d3SJohn Marino /* This function scans over the entire hash table calling
766*86d7f5d3SJohn Marino    CALLBACK for each live entry.  If CALLBACK returns false,
767*86d7f5d3SJohn Marino    the iteration stops.  INFO is passed as CALLBACK's second
768*86d7f5d3SJohn Marino    argument.  */
769*86d7f5d3SJohn Marino 
770*86d7f5d3SJohn Marino void
htab_traverse_noresize(htab_t htab,htab_trav callback,PTR info)771*86d7f5d3SJohn Marino htab_traverse_noresize (htab_t htab, htab_trav callback, PTR info)
772*86d7f5d3SJohn Marino {
773*86d7f5d3SJohn Marino   PTR *slot;
774*86d7f5d3SJohn Marino   PTR *limit;
775*86d7f5d3SJohn Marino 
776*86d7f5d3SJohn Marino   slot = htab->entries;
777*86d7f5d3SJohn Marino   limit = slot + htab_size (htab);
778*86d7f5d3SJohn Marino 
779*86d7f5d3SJohn Marino   do
780*86d7f5d3SJohn Marino     {
781*86d7f5d3SJohn Marino       PTR x = *slot;
782*86d7f5d3SJohn Marino 
783*86d7f5d3SJohn Marino       if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
784*86d7f5d3SJohn Marino 	if (!(*callback) (slot, info))
785*86d7f5d3SJohn Marino 	  break;
786*86d7f5d3SJohn Marino     }
787*86d7f5d3SJohn Marino   while (++slot < limit);
788*86d7f5d3SJohn Marino }
789*86d7f5d3SJohn Marino 
790*86d7f5d3SJohn Marino /* Like htab_traverse_noresize, but does resize the table when it is
791*86d7f5d3SJohn Marino    too empty to improve effectivity of subsequent calls.  */
792*86d7f5d3SJohn Marino 
793*86d7f5d3SJohn Marino void
htab_traverse(htab_t htab,htab_trav callback,PTR info)794*86d7f5d3SJohn Marino htab_traverse (htab_t htab, htab_trav callback, PTR info)
795*86d7f5d3SJohn Marino {
796*86d7f5d3SJohn Marino   size_t size = htab_size (htab);
797*86d7f5d3SJohn Marino   if (htab_elements (htab) * 8 < size && size > 32)
798*86d7f5d3SJohn Marino     htab_expand (htab);
799*86d7f5d3SJohn Marino 
800*86d7f5d3SJohn Marino   htab_traverse_noresize (htab, callback, info);
801*86d7f5d3SJohn Marino }
802*86d7f5d3SJohn Marino 
803*86d7f5d3SJohn Marino /* Return the fraction of fixed collisions during all work with given
804*86d7f5d3SJohn Marino    hash table. */
805*86d7f5d3SJohn Marino 
806*86d7f5d3SJohn Marino double
htab_collisions(htab_t htab)807*86d7f5d3SJohn Marino htab_collisions (htab_t htab)
808*86d7f5d3SJohn Marino {
809*86d7f5d3SJohn Marino   if (htab->searches == 0)
810*86d7f5d3SJohn Marino     return 0.0;
811*86d7f5d3SJohn Marino 
812*86d7f5d3SJohn Marino   return (double) htab->collisions / (double) htab->searches;
813*86d7f5d3SJohn Marino }
814*86d7f5d3SJohn Marino 
815*86d7f5d3SJohn Marino /* Hash P as a null-terminated string.
816*86d7f5d3SJohn Marino 
817*86d7f5d3SJohn Marino    Copied from gcc/hashtable.c.  Zack had the following to say with respect
818*86d7f5d3SJohn Marino    to applicability, though note that unlike hashtable.c, this hash table
819*86d7f5d3SJohn Marino    implementation re-hashes rather than chain buckets.
820*86d7f5d3SJohn Marino 
821*86d7f5d3SJohn Marino    http://gcc.gnu.org/ml/gcc-patches/2001-08/msg01021.html
822*86d7f5d3SJohn Marino    From: Zack Weinberg <zackw@panix.com>
823*86d7f5d3SJohn Marino    Date: Fri, 17 Aug 2001 02:15:56 -0400
824*86d7f5d3SJohn Marino 
825*86d7f5d3SJohn Marino    I got it by extracting all the identifiers from all the source code
826*86d7f5d3SJohn Marino    I had lying around in mid-1999, and testing many recurrences of
827*86d7f5d3SJohn Marino    the form "H_n = H_{n-1} * K + c_n * L + M" where K, L, M were either
828*86d7f5d3SJohn Marino    prime numbers or the appropriate identity.  This was the best one.
829*86d7f5d3SJohn Marino    I don't remember exactly what constituted "best", except I was
830*86d7f5d3SJohn Marino    looking at bucket-length distributions mostly.
831*86d7f5d3SJohn Marino 
832*86d7f5d3SJohn Marino    So it should be very good at hashing identifiers, but might not be
833*86d7f5d3SJohn Marino    as good at arbitrary strings.
834*86d7f5d3SJohn Marino 
835*86d7f5d3SJohn Marino    I'll add that it thoroughly trounces the hash functions recommended
836*86d7f5d3SJohn Marino    for this use at http://burtleburtle.net/bob/hash/index.html, both
837*86d7f5d3SJohn Marino    on speed and bucket distribution.  I haven't tried it against the
838*86d7f5d3SJohn Marino    function they just started using for Perl's hashes.  */
839*86d7f5d3SJohn Marino 
840*86d7f5d3SJohn Marino hashval_t
htab_hash_string(const PTR p)841*86d7f5d3SJohn Marino htab_hash_string (const PTR p)
842*86d7f5d3SJohn Marino {
843*86d7f5d3SJohn Marino   const unsigned char *str = (const unsigned char *) p;
844*86d7f5d3SJohn Marino   hashval_t r = 0;
845*86d7f5d3SJohn Marino   unsigned char c;
846*86d7f5d3SJohn Marino 
847*86d7f5d3SJohn Marino   while ((c = *str++) != 0)
848*86d7f5d3SJohn Marino     r = r * 67 + c - 113;
849*86d7f5d3SJohn Marino 
850*86d7f5d3SJohn Marino   return r;
851*86d7f5d3SJohn Marino }
852*86d7f5d3SJohn Marino 
853*86d7f5d3SJohn Marino /* DERIVED FROM:
854*86d7f5d3SJohn Marino --------------------------------------------------------------------
855*86d7f5d3SJohn Marino lookup2.c, by Bob Jenkins, December 1996, Public Domain.
856*86d7f5d3SJohn Marino hash(), hash2(), hash3, and mix() are externally useful functions.
857*86d7f5d3SJohn Marino Routines to test the hash are included if SELF_TEST is defined.
858*86d7f5d3SJohn Marino You can use this free for any purpose.  It has no warranty.
859*86d7f5d3SJohn Marino --------------------------------------------------------------------
860*86d7f5d3SJohn Marino */
861*86d7f5d3SJohn Marino 
862*86d7f5d3SJohn Marino /*
863*86d7f5d3SJohn Marino --------------------------------------------------------------------
864*86d7f5d3SJohn Marino mix -- mix 3 32-bit values reversibly.
865*86d7f5d3SJohn Marino For every delta with one or two bit set, and the deltas of all three
866*86d7f5d3SJohn Marino   high bits or all three low bits, whether the original value of a,b,c
867*86d7f5d3SJohn Marino   is almost all zero or is uniformly distributed,
868*86d7f5d3SJohn Marino * If mix() is run forward or backward, at least 32 bits in a,b,c
869*86d7f5d3SJohn Marino   have at least 1/4 probability of changing.
870*86d7f5d3SJohn Marino * If mix() is run forward, every bit of c will change between 1/3 and
871*86d7f5d3SJohn Marino   2/3 of the time.  (Well, 22/100 and 78/100 for some 2-bit deltas.)
872*86d7f5d3SJohn Marino mix() was built out of 36 single-cycle latency instructions in a
873*86d7f5d3SJohn Marino   structure that could supported 2x parallelism, like so:
874*86d7f5d3SJohn Marino       a -= b;
875*86d7f5d3SJohn Marino       a -= c; x = (c>>13);
876*86d7f5d3SJohn Marino       b -= c; a ^= x;
877*86d7f5d3SJohn Marino       b -= a; x = (a<<8);
878*86d7f5d3SJohn Marino       c -= a; b ^= x;
879*86d7f5d3SJohn Marino       c -= b; x = (b>>13);
880*86d7f5d3SJohn Marino       ...
881*86d7f5d3SJohn Marino   Unfortunately, superscalar Pentiums and Sparcs can't take advantage
882*86d7f5d3SJohn Marino   of that parallelism.  They've also turned some of those single-cycle
883*86d7f5d3SJohn Marino   latency instructions into multi-cycle latency instructions.  Still,
884*86d7f5d3SJohn Marino   this is the fastest good hash I could find.  There were about 2^^68
885*86d7f5d3SJohn Marino   to choose from.  I only looked at a billion or so.
886*86d7f5d3SJohn Marino --------------------------------------------------------------------
887*86d7f5d3SJohn Marino */
888*86d7f5d3SJohn Marino /* same, but slower, works on systems that might have 8 byte hashval_t's */
889*86d7f5d3SJohn Marino #define mix(a,b,c) \
890*86d7f5d3SJohn Marino { \
891*86d7f5d3SJohn Marino   a -= b; a -= c; a ^= (c>>13); \
892*86d7f5d3SJohn Marino   b -= c; b -= a; b ^= (a<< 8); \
893*86d7f5d3SJohn Marino   c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
894*86d7f5d3SJohn Marino   a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
895*86d7f5d3SJohn Marino   b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
896*86d7f5d3SJohn Marino   c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
897*86d7f5d3SJohn Marino   a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
898*86d7f5d3SJohn Marino   b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
899*86d7f5d3SJohn Marino   c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
900*86d7f5d3SJohn Marino }
901*86d7f5d3SJohn Marino 
902*86d7f5d3SJohn Marino /*
903*86d7f5d3SJohn Marino --------------------------------------------------------------------
904*86d7f5d3SJohn Marino hash() -- hash a variable-length key into a 32-bit value
905*86d7f5d3SJohn Marino   k     : the key (the unaligned variable-length array of bytes)
906*86d7f5d3SJohn Marino   len   : the length of the key, counting by bytes
907*86d7f5d3SJohn Marino   level : can be any 4-byte value
908*86d7f5d3SJohn Marino Returns a 32-bit value.  Every bit of the key affects every bit of
909*86d7f5d3SJohn Marino the return value.  Every 1-bit and 2-bit delta achieves avalanche.
910*86d7f5d3SJohn Marino About 36+6len instructions.
911*86d7f5d3SJohn Marino 
912*86d7f5d3SJohn Marino The best hash table sizes are powers of 2.  There is no need to do
913*86d7f5d3SJohn Marino mod a prime (mod is sooo slow!).  If you need less than 32 bits,
914*86d7f5d3SJohn Marino use a bitmask.  For example, if you need only 10 bits, do
915*86d7f5d3SJohn Marino   h = (h & hashmask(10));
916*86d7f5d3SJohn Marino In which case, the hash table should have hashsize(10) elements.
917*86d7f5d3SJohn Marino 
918*86d7f5d3SJohn Marino If you are hashing n strings (ub1 **)k, do it like this:
919*86d7f5d3SJohn Marino   for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
920*86d7f5d3SJohn Marino 
921*86d7f5d3SJohn Marino By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.  You may use this
922*86d7f5d3SJohn Marino code any way you wish, private, educational, or commercial.  It's free.
923*86d7f5d3SJohn Marino 
924*86d7f5d3SJohn Marino See http://burtleburtle.net/bob/hash/evahash.html
925*86d7f5d3SJohn Marino Use for hash table lookup, or anything where one collision in 2^32 is
926*86d7f5d3SJohn Marino acceptable.  Do NOT use for cryptographic purposes.
927*86d7f5d3SJohn Marino --------------------------------------------------------------------
928*86d7f5d3SJohn Marino */
929*86d7f5d3SJohn Marino 
930*86d7f5d3SJohn Marino hashval_t
iterative_hash(const PTR k_in,register size_t length,register hashval_t initval)931*86d7f5d3SJohn Marino iterative_hash (const PTR k_in /* the key */,
932*86d7f5d3SJohn Marino                 register size_t  length /* the length of the key */,
933*86d7f5d3SJohn Marino                 register hashval_t initval /* the previous hash, or
934*86d7f5d3SJohn Marino                                               an arbitrary value */)
935*86d7f5d3SJohn Marino {
936*86d7f5d3SJohn Marino   register const unsigned char *k = (const unsigned char *)k_in;
937*86d7f5d3SJohn Marino   register hashval_t a,b,c,len;
938*86d7f5d3SJohn Marino 
939*86d7f5d3SJohn Marino   /* Set up the internal state */
940*86d7f5d3SJohn Marino   len = length;
941*86d7f5d3SJohn Marino   a = b = 0x9e3779b9;  /* the golden ratio; an arbitrary value */
942*86d7f5d3SJohn Marino   c = initval;           /* the previous hash value */
943*86d7f5d3SJohn Marino 
944*86d7f5d3SJohn Marino   /*---------------------------------------- handle most of the key */
945*86d7f5d3SJohn Marino #ifndef WORDS_BIGENDIAN
946*86d7f5d3SJohn Marino   /* On a little-endian machine, if the data is 4-byte aligned we can hash
947*86d7f5d3SJohn Marino      by word for better speed.  This gives nondeterministic results on
948*86d7f5d3SJohn Marino      big-endian machines.  */
949*86d7f5d3SJohn Marino   if (sizeof (hashval_t) == 4 && (((size_t)k)&3) == 0)
950*86d7f5d3SJohn Marino     while (len >= 12)    /* aligned */
951*86d7f5d3SJohn Marino       {
952*86d7f5d3SJohn Marino 	a += *(hashval_t *)(k+0);
953*86d7f5d3SJohn Marino 	b += *(hashval_t *)(k+4);
954*86d7f5d3SJohn Marino 	c += *(hashval_t *)(k+8);
955*86d7f5d3SJohn Marino 	mix(a,b,c);
956*86d7f5d3SJohn Marino 	k += 12; len -= 12;
957*86d7f5d3SJohn Marino       }
958*86d7f5d3SJohn Marino   else /* unaligned */
959*86d7f5d3SJohn Marino #endif
960*86d7f5d3SJohn Marino     while (len >= 12)
961*86d7f5d3SJohn Marino       {
962*86d7f5d3SJohn Marino 	a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24));
963*86d7f5d3SJohn Marino 	b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24));
964*86d7f5d3SJohn Marino 	c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24));
965*86d7f5d3SJohn Marino 	mix(a,b,c);
966*86d7f5d3SJohn Marino 	k += 12; len -= 12;
967*86d7f5d3SJohn Marino       }
968*86d7f5d3SJohn Marino 
969*86d7f5d3SJohn Marino   /*------------------------------------- handle the last 11 bytes */
970*86d7f5d3SJohn Marino   c += length;
971*86d7f5d3SJohn Marino   switch(len)              /* all the case statements fall through */
972*86d7f5d3SJohn Marino     {
973*86d7f5d3SJohn Marino     case 11: c+=((hashval_t)k[10]<<24);
974*86d7f5d3SJohn Marino     case 10: c+=((hashval_t)k[9]<<16);
975*86d7f5d3SJohn Marino     case 9 : c+=((hashval_t)k[8]<<8);
976*86d7f5d3SJohn Marino       /* the first byte of c is reserved for the length */
977*86d7f5d3SJohn Marino     case 8 : b+=((hashval_t)k[7]<<24);
978*86d7f5d3SJohn Marino     case 7 : b+=((hashval_t)k[6]<<16);
979*86d7f5d3SJohn Marino     case 6 : b+=((hashval_t)k[5]<<8);
980*86d7f5d3SJohn Marino     case 5 : b+=k[4];
981*86d7f5d3SJohn Marino     case 4 : a+=((hashval_t)k[3]<<24);
982*86d7f5d3SJohn Marino     case 3 : a+=((hashval_t)k[2]<<16);
983*86d7f5d3SJohn Marino     case 2 : a+=((hashval_t)k[1]<<8);
984*86d7f5d3SJohn Marino     case 1 : a+=k[0];
985*86d7f5d3SJohn Marino       /* case 0: nothing left to add */
986*86d7f5d3SJohn Marino     }
987*86d7f5d3SJohn Marino   mix(a,b,c);
988*86d7f5d3SJohn Marino   /*-------------------------------------------- report the result */
989*86d7f5d3SJohn Marino   return c;
990*86d7f5d3SJohn Marino }
991