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