1 /* hash - implement simple hashing table with string based keys.
2 Copyright (C) 1994-1995, 2000-2006 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, October 1994.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include <sys/types.h>
29 /* Since this simple implementation of hash tables allows only insertion, no
30 removal of entries, the right data structure for the memory holding all keys
34 /* Use checked memory allocation. */
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
41 typedef struct hash_entry
43 unsigned long used; /* Hash code of the key, or 0 for an unused entry. */
44 const void *key; /* Key. */
46 void *data; /* Value. */
47 struct hash_entry *next;
52 /* Given an odd CANDIDATE > 1, return true if it is a prime number. */
54 is_prime (unsigned long int candidate)
56 /* No even number and none less than 10 will be passed here. */
57 unsigned long int divn = 3;
58 unsigned long int sq = divn * divn;
60 while (sq < candidate && candidate % divn != 0)
67 return candidate % divn != 0;
71 /* Given SEED > 1, return the smallest odd prime number >= SEED. */
73 next_prime (unsigned long int seed)
75 /* Make it definitely odd. */
78 while (!is_prime (seed))
85 /* Initialize a hash table. INIT_SIZE > 1 is the initial number of available
87 Return 0 upon successful completion, -1 upon memory allocation error. */
89 hash_init (hash_table *htab, unsigned long int init_size)
91 /* We need the size to be a prime. */
92 init_size = next_prime (init_size);
94 /* Initialize the data structure. */
95 htab->size = init_size;
98 htab->table = XCALLOC (init_size + 1, hash_entry);
100 obstack_init (&htab->mem_pool);
106 /* Delete a hash table's contents.
109 hash_destroy (hash_table *htab)
112 obstack_free (&htab->mem_pool, NULL);
117 /* Compute a hash code for a key consisting of KEYLEN bytes starting at KEY
120 compute_hashval (const void *key, size_t keylen)
123 unsigned long int hval;
125 /* Compute the hash value for the given string. The algorithm
126 is taken from [Aho,Sethi,Ullman], fixed according to
127 http://www.haible.de/bruno/hashfunc.html. */
132 hval = (hval << 9) | (hval >> (sizeof (unsigned long) * CHAR_BIT - 9));
133 hval += (unsigned long int) *(((const char *) key) + cnt++);
135 return hval != 0 ? hval : ~((unsigned long) 0);
140 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
141 [Knuth] The Art of Computer Programming, part3 (6.4) */
143 /* Look up a given key in the hash table.
144 Return the index of the entry, if present, or otherwise the index a free
145 entry where it could be inserted. */
147 lookup (hash_table *htab,
148 const void *key, size_t keylen,
149 unsigned long int hval)
151 unsigned long int hash;
153 hash_entry *table = htab->table;
155 /* First hash function: simply take the modul but prevent zero. */
156 hash = 1 + hval % htab->size;
162 if (table[idx].used == hval && table[idx].keylen == keylen
163 && memcmp (table[idx].key, key, keylen) == 0)
166 /* Second hash function as suggested in [Knuth]. */
167 hash = 1 + hval % (htab->size - 2);
172 idx = htab->size + idx - hash;
176 /* If entry is found use it. */
177 if (table[idx].used == hval && table[idx].keylen == keylen
178 && memcmp (table[idx].key, key, keylen) == 0)
181 while (table[idx].used);
187 /* Look up the value of a key in the given table.
188 If found, return 0 and set *RESULT to it. Otherwise return -1. */
190 hash_find_entry (hash_table *htab, const void *key, size_t keylen,
193 hash_entry *table = htab->table;
194 size_t idx = lookup (htab, key, keylen, compute_hashval (key, keylen));
196 if (table[idx].used == 0)
199 *result = table[idx].data;
204 /* Insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table at index IDX.
205 HVAL is the key's hash code. IDX depends on it. The table entry at index
206 IDX is known to be unused. */
208 insert_entry_2 (hash_table *htab,
209 const void *key, size_t keylen,
210 unsigned long int hval, size_t idx, void *data)
212 hash_entry *table = htab->table;
214 table[idx].used = hval;
215 table[idx].key = key;
216 table[idx].keylen = keylen;
217 table[idx].data = data;
219 /* List the new value in the list. */
220 if (htab->first == NULL)
222 table[idx].next = &table[idx];
223 htab->first = &table[idx];
227 table[idx].next = htab->first->next;
228 htab->first->next = &table[idx];
229 htab->first = &table[idx];
236 /* Grow the hash table. */
238 resize (hash_table *htab)
240 unsigned long int old_size = htab->size;
241 hash_entry *table = htab->table;
244 htab->size = next_prime (htab->size * 2);
247 htab->table = XCALLOC (1 + htab->size, hash_entry);
249 for (idx = 1; idx <= old_size; ++idx)
251 insert_entry_2 (htab, table[idx].key, table[idx].keylen,
253 lookup (htab, table[idx].key, table[idx].keylen,
261 /* Try to insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table.
262 Return non-NULL (more precisely, the address of the KEY inside the table's
263 memory pool) if successful, or NULL if there is already an entry with the
266 hash_insert_entry (hash_table *htab,
267 const void *key, size_t keylen,
270 unsigned long int hval = compute_hashval (key, keylen);
271 hash_entry *table = htab->table;
272 size_t idx = lookup (htab, key, keylen, hval);
275 /* We don't want to overwrite the old value. */
279 /* An empty bucket has been found. */
280 void *keycopy = obstack_copy (&htab->mem_pool, key, keylen);
281 insert_entry_2 (htab, keycopy, keylen, hval, idx, data);
282 if (100 * htab->filled > 75 * htab->size)
283 /* Table is filled more than 75%. Resize the table. */
290 /* Insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table.
293 hash_set_value (hash_table *htab,
294 const void *key, size_t keylen,
297 unsigned long int hval = compute_hashval (key, keylen);
298 hash_entry *table = htab->table;
299 size_t idx = lookup (htab, key, keylen, hval);
303 /* Overwrite the old value. */
304 table[idx].data = data;
309 /* An empty bucket has been found. */
310 void *keycopy = obstack_copy (&htab->mem_pool, key, keylen);
311 insert_entry_2 (htab, keycopy, keylen, hval, idx, data);
312 if (100 * htab->filled > 75 * htab->size)
313 /* Table is filled more than 75%. Resize the table. */
320 /* Steps *PTR forward to the next used entry in the given hash table. *PTR
321 should be initially set to NULL. Store information about the next entry
322 in *KEY, *KEYLEN, *DATA.
323 Return 0 normally, -1 when the whole hash table has been traversed. */
325 hash_iterate (hash_table *htab, void **ptr, const void **key, size_t *keylen,
332 if (htab->first == NULL)
338 if (*ptr == htab->first)
340 curr = (hash_entry *) *ptr;
343 *ptr = (void *) curr;
346 *keylen = curr->keylen;
352 /* Steps *PTR forward to the next used entry in the given hash table. *PTR
353 should be initially set to NULL. Store information about the next entry
354 in *KEY, *KEYLEN, *DATAP. *DATAP is set to point to the storage of the
355 value; modifying **DATAP will modify the value of the entry.
356 Return 0 normally, -1 when the whole hash table has been traversed. */
358 hash_iterate_modify (hash_table *htab, void **ptr,
359 const void **key, size_t *keylen,
366 if (htab->first == NULL)
372 if (*ptr == htab->first)
374 curr = (hash_entry *) *ptr;
377 *ptr = (void *) curr;
380 *keylen = curr->keylen;
381 *datap = &curr->data;