1 /* hash - implement simple hashing table with string based keys.
2 Copyright (C) 1994-1995, 2000-2006, 2015 Free Software Foundation,
4 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, October 1994.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include <sys/types.h>
30 /* Since this simple implementation of hash tables allows only insertion, no
31 removal of entries, the right data structure for the memory holding all keys
35 /* Use checked memory allocation. */
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
42 typedef struct hash_entry
44 unsigned long used; /* Hash code of the key, or 0 for an unused entry. */
45 const void *key; /* Key. */
47 void *data; /* Value. */
48 struct hash_entry *next;
53 /* Given an odd CANDIDATE > 1, return true if it is a prime number. */
55 is_prime (unsigned long int candidate)
57 /* No even number and none less than 10 will be passed here. */
58 unsigned long int divn = 3;
59 unsigned long int sq = divn * divn;
61 while (sq < candidate && candidate % divn != 0)
68 return candidate % divn != 0;
72 /* Given SEED > 1, return the smallest odd prime number >= SEED. */
74 next_prime (unsigned long int seed)
76 /* Make it definitely odd. */
79 while (!is_prime (seed))
86 /* Initialize a hash table. INIT_SIZE > 1 is the initial number of available
88 Return 0 upon successful completion, -1 upon memory allocation error. */
90 hash_init (hash_table *htab, unsigned long int init_size)
92 /* We need the size to be a prime. */
93 init_size = next_prime (init_size);
95 /* Initialize the data structure. */
96 htab->size = init_size;
99 htab->table = XCALLOC (init_size + 1, hash_entry);
101 obstack_init (&htab->mem_pool);
107 /* Delete a hash table's contents.
110 hash_destroy (hash_table *htab)
113 obstack_free (&htab->mem_pool, NULL);
118 /* Compute a hash code for a key consisting of KEYLEN bytes starting at KEY
121 compute_hashval (const void *key, size_t keylen)
124 unsigned long int hval;
126 /* Compute the hash value for the given string. The algorithm
127 is taken from [Aho,Sethi,Ullman], fixed according to
128 http://www.haible.de/bruno/hashfunc.html. */
133 hval = (hval << 9) | (hval >> (sizeof (unsigned long) * CHAR_BIT - 9));
134 hval += (unsigned long int) *(((const char *) key) + cnt++);
136 return hval != 0 ? hval : ~((unsigned long) 0);
141 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
142 [Knuth] The Art of Computer Programming, part3 (6.4) */
144 /* Look up a given key in the hash table.
145 Return the index of the entry, if present, or otherwise the index a free
146 entry where it could be inserted. */
148 lookup (hash_table *htab,
149 const void *key, size_t keylen,
150 unsigned long int hval)
152 unsigned long int hash;
154 hash_entry *table = htab->table;
156 /* First hash function: simply take the modul but prevent zero. */
157 hash = 1 + hval % htab->size;
163 if (table[idx].used == hval && table[idx].keylen == keylen
164 && memcmp (table[idx].key, key, keylen) == 0)
167 /* Second hash function as suggested in [Knuth]. */
168 hash = 1 + hval % (htab->size - 2);
173 idx = htab->size + idx - hash;
177 /* If entry is found use it. */
178 if (table[idx].used == hval && table[idx].keylen == keylen
179 && memcmp (table[idx].key, key, keylen) == 0)
182 while (table[idx].used);
188 /* Look up the value of a key in the given table.
189 If found, return 0 and set *RESULT to it. Otherwise return -1. */
191 hash_find_entry (hash_table *htab, const void *key, size_t keylen,
194 hash_entry *table = htab->table;
195 size_t idx = lookup (htab, key, keylen, compute_hashval (key, keylen));
197 if (table[idx].used == 0)
200 *result = table[idx].data;
205 /* Insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table at index IDX.
206 HVAL is the key's hash code. IDX depends on it. The table entry at index
207 IDX is known to be unused. */
209 insert_entry_2 (hash_table *htab,
210 const void *key, size_t keylen,
211 unsigned long int hval, size_t idx, void *data)
213 hash_entry *table = htab->table;
215 table[idx].used = hval;
216 table[idx].key = key;
217 table[idx].keylen = keylen;
218 table[idx].data = data;
220 /* List the new value in the list. */
221 if (htab->first == NULL)
223 table[idx].next = &table[idx];
224 htab->first = &table[idx];
228 table[idx].next = htab->first->next;
229 htab->first->next = &table[idx];
230 htab->first = &table[idx];
237 /* Grow the hash table. */
239 resize (hash_table *htab)
241 unsigned long int old_size = htab->size;
242 hash_entry *table = htab->table;
245 htab->size = next_prime (htab->size * 2);
248 htab->table = XCALLOC (1 + htab->size, hash_entry);
250 for (idx = 1; idx <= old_size; ++idx)
252 insert_entry_2 (htab, table[idx].key, table[idx].keylen,
254 lookup (htab, table[idx].key, table[idx].keylen,
262 /* Try to insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table.
263 Return non-NULL (more precisely, the address of the KEY inside the table's
264 memory pool) if successful, or NULL if there is already an entry with the
267 hash_insert_entry (hash_table *htab,
268 const void *key, size_t keylen,
271 unsigned long int hval = compute_hashval (key, keylen);
272 hash_entry *table = htab->table;
273 size_t idx = lookup (htab, key, keylen, hval);
276 /* We don't want to overwrite the old value. */
280 /* An empty bucket has been found. */
281 void *keycopy = obstack_copy (&htab->mem_pool, key, keylen);
282 insert_entry_2 (htab, keycopy, keylen, hval, idx, data);
283 if (100 * htab->filled > 75 * htab->size)
284 /* Table is filled more than 75%. Resize the table. */
291 /* Insert the pair (KEY[0..KEYLEN-1], DATA) in the hash table.
294 hash_set_value (hash_table *htab,
295 const void *key, size_t keylen,
298 unsigned long int hval = compute_hashval (key, keylen);
299 hash_entry *table = htab->table;
300 size_t idx = lookup (htab, key, keylen, hval);
304 /* Overwrite the old value. */
305 table[idx].data = data;
310 /* An empty bucket has been found. */
311 void *keycopy = obstack_copy (&htab->mem_pool, key, keylen);
312 insert_entry_2 (htab, keycopy, keylen, hval, idx, data);
313 if (100 * htab->filled > 75 * htab->size)
314 /* Table is filled more than 75%. Resize the table. */
321 /* Steps *PTR forward to the next used entry in the given hash table. *PTR
322 should be initially set to NULL. Store information about the next entry
323 in *KEY, *KEYLEN, *DATA.
324 Return 0 normally, -1 when the whole hash table has been traversed. */
326 hash_iterate (hash_table *htab, void **ptr, const void **key, size_t *keylen,
333 if (htab->first == NULL)
339 if (*ptr == htab->first)
341 curr = (hash_entry *) *ptr;
344 *ptr = (void *) curr;
347 *keylen = curr->keylen;
353 /* Steps *PTR forward to the next used entry in the given hash table. *PTR
354 should be initially set to NULL. Store information about the next entry
355 in *KEY, *KEYLEN, *DATAP. *DATAP is set to point to the storage of the
356 value; modifying **DATAP will modify the value of the entry.
357 Return 0 normally, -1 when the whole hash table has been traversed. */
359 hash_iterate_modify (hash_table *htab, void **ptr,
360 const void **key, size_t *keylen,
367 if (htab->first == NULL)
373 if (*ptr == htab->first)
375 curr = (hash_entry *) *ptr;
378 *ptr = (void *) curr;
381 *keylen = curr->keylen;
382 *datap = &curr->data;