1 /* Implement simple hashing table with string based keys.
2 Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, October 1994.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 #include <sys/types.h>
39 #include "simple-hash.h"
41 #define obstack_chunk_alloc malloc
42 #define obstack_chunk_free free
45 # define BITSPERBYTE 8
49 # define LONGBITS (sizeof (long) * BITSPERBYTE)
53 # define bcopy(s, d, n) memcpy ((d), (s), (n))
56 void *xmalloc __P ((size_t __n));
58 typedef struct hash_entry
64 struct hash_entry *next;
68 /* Prototypes for local functions. */
69 static void insert_entry_2 __P ((hash_table *htab, const void *key,
70 size_t keylen, unsigned long hval,
71 size_t idx, void *data));
72 static size_t lookup __P ((hash_table *htab, const void *key, size_t keylen,
73 unsigned long int hval));
74 static size_t lookup_2 __P ((hash_table *htab, const void *key,
75 size_t keylen, unsigned long int hval));
76 static unsigned long compute_hashval __P ((const void *key, size_t keylen));
77 static int is_prime __P ((unsigned long int candidate));
81 init_hash (htab, init_size)
83 unsigned long int init_size;
85 /* We need the size to be a prime. */
86 init_size = next_prime (init_size);
88 /* Initialize the data structure. */
89 htab->size = init_size;
92 htab->table = (void *) xmalloc ((init_size + 1) * sizeof (hash_entry));
93 if (htab->table == NULL)
96 memset (htab->table, '\0', (init_size + 1) * sizeof (hash_entry));
97 obstack_init (&htab->mem_pool);
108 obstack_free (&htab->mem_pool, NULL);
114 insert_entry (htab, key, keylen, data)
120 unsigned long int hval = compute_hashval (key, keylen);
121 hash_entry *table = (hash_entry *) htab->table;
122 size_t idx = lookup (htab, key, keylen, hval);
125 /* We don't want to overwrite the old value. */
129 /* An empty bucket has been found. */
130 insert_entry_2 (htab, obstack_copy (&htab->mem_pool, key, keylen),
131 keylen, hval, idx, data);
137 insert_entry_2 (htab, key, keylen, hval, idx, data)
141 unsigned long int hval;
145 hash_entry *table = (hash_entry *) htab->table;
147 table[idx].used = hval;
148 table[idx].key = key;
149 table[idx].keylen = keylen;
150 table[idx].data = data;
152 /* List the new value in the list. */
153 if ((hash_entry *) htab->first == NULL)
155 table[idx].next = &table[idx];
156 *(hash_entry **) &htab->first = &table[idx];
160 table[idx].next = ((hash_entry *) htab->first)->next;
161 ((hash_entry *) htab->first)->next = &table[idx];
162 *(hash_entry **) &htab->first = &table[idx];
166 if (100 * htab->filled > 90 * htab->size)
168 /* Table is filled more than 90%. Resize the table. */
169 unsigned long int old_size = htab->size;
171 htab->size = next_prime (htab->size * 2);
174 htab->table = (void *) xmalloc ((1 + htab->size)
175 * sizeof (hash_entry));
176 memset (htab->table, '\0', (1 + htab->size) * sizeof (hash_entry));
178 for (idx = 1; idx <= old_size; ++idx)
180 insert_entry_2 (htab, table[idx].key, table[idx].keylen,
182 lookup_2 (htab, table[idx].key, table[idx].keylen,
192 find_entry (htab, key, keylen, result)
198 hash_entry *table = (hash_entry *) htab->table;
199 size_t idx = lookup (htab, key, keylen, compute_hashval (key, keylen));
201 if (table[idx].used == 0)
204 *result = table[idx].data;
210 set_entry (htab, key, keylen, newval)
216 hash_entry *table = (hash_entry *) htab->table;
217 size_t idx = lookup (htab, key, keylen, compute_hashval (key, keylen));
219 if (table[idx].used == 0)
222 table[idx].data = newval;
228 iterate_table (htab, ptr, key, keylen, data)
237 if (htab->first == NULL)
239 *ptr = (void *) ((hash_entry *) htab->first)->next;
243 if (*ptr == htab->first)
245 *ptr = (void *) (((hash_entry *) *ptr)->next);
248 *key = ((hash_entry *) *ptr)->key;
249 *keylen = ((hash_entry *) *ptr)->keylen;
250 *data = ((hash_entry *) *ptr)->data;
256 lookup (htab, key, keylen, hval)
264 hash_entry *table = (hash_entry *) htab->table;
266 /* First hash function: simply take the modul but prevent zero. */
267 hash = 1 + hval % htab->size;
273 if (table[idx].used == hval && table[idx].keylen == keylen
274 && memcmp (key, table[idx].key, keylen) == 0)
277 /* Second hash function as suggested in [Knuth]. */
278 hash = 1 + hval % (htab->size - 2);
283 idx = htab->size + idx - hash;
287 /* If entry is found use it. */
288 if (table[idx].used == hval && table[idx].keylen == keylen
289 && memcmp (key, table[idx].key, keylen) == 0)
292 while (table[idx].used);
299 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
300 [Knuth] The Art of Computer Programming, part3 (6.4) */
303 lookup_2 (htab, key, keylen, hval)
307 unsigned long int hval;
309 unsigned long int hash;
311 hash_entry *table = (hash_entry *) htab->table;
313 /* First hash function: simply take the modul but prevent zero. */
314 hash = 1 + hval % htab->size;
320 if (table[idx].used == hval && table[idx].keylen == keylen
321 && memcmp (table[idx].key, key, keylen) == 0)
324 /* Second hash function as suggested in [Knuth]. */
325 hash = 1 + hval % (htab->size - 2);
330 idx = htab->size + idx - hash;
334 /* If entry is found use it. */
335 if (table[idx].used == hval && table[idx].keylen == keylen
336 && memcmp (table[idx].key, key, keylen) == 0)
339 while (table[idx].used);
346 compute_hashval (key, keylen)
351 unsigned long int hval, g;
353 /* Compute the hash value for the given string. The algorithm
354 is taken from [Aho,Sethi,Ullman]. */
360 hval += (unsigned long int) *(((char *) key) + cnt++);
361 g = hval & ((unsigned long) 0xf << (LONGBITS - 4));
364 hval ^= g >> (LONGBITS - 8);
368 return hval != 0 ? hval : ~((unsigned long) 0);
374 unsigned long int seed;
376 /* Make it definitely odd. */
379 while (!is_prime (seed))
388 unsigned long int candidate;
390 /* No even number and none less than 10 will be passed here. */
391 unsigned long int divn = 3;
392 unsigned long int sq = divn * divn;
394 while (sq < candidate && candidate % divn != 0)
401 return candidate % divn != 0;