1 /* Implement simple hashing table with string based keys.
2 Copyright (C) 1994-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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
8 by the Free Software Foundation; version 2 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>
36 #include "simple-hash.h"
38 #define obstack_chunk_alloc malloc
39 #define obstack_chunk_free free
42 # define BITSPERBYTE 8
45 #define hashval_t uint32_t
48 #include <programs/xmalloc.h>
50 typedef struct hash_entry
56 struct hash_entry *next;
60 /* Prototypes for local functions. */
61 static void insert_entry_2 (hash_table *htab, const void *key, size_t keylen,
62 unsigned long hval, size_t idx, void *data);
63 static size_t lookup (const hash_table *htab, const void *key, size_t keylen,
64 unsigned long int hval);
65 static int is_prime (unsigned long int candidate);
69 init_hash (htab, init_size)
71 unsigned long int init_size;
73 /* We need the size to be a prime. */
74 init_size = next_prime (init_size);
76 /* Initialize the data structure. */
77 htab->size = init_size;
80 htab->table = (void *) xcalloc (init_size + 1, sizeof (hash_entry));
81 if (htab->table == NULL)
84 obstack_init (&htab->mem_pool);
95 obstack_free (&htab->mem_pool, NULL);
101 insert_entry (htab, key, keylen, data)
107 unsigned long int hval = compute_hashval (key, keylen);
108 hash_entry *table = (hash_entry *) htab->table;
109 size_t idx = lookup (htab, key, keylen, hval);
112 /* We don't want to overwrite the old value. */
116 /* An empty bucket has been found. */
117 insert_entry_2 (htab, obstack_copy (&htab->mem_pool, key, keylen),
118 keylen, hval, idx, data);
124 insert_entry_2 (htab, key, keylen, hval, idx, data)
128 unsigned long int hval;
132 hash_entry *table = (hash_entry *) htab->table;
134 table[idx].used = hval;
135 table[idx].key = key;
136 table[idx].keylen = keylen;
137 table[idx].data = data;
139 /* List the new value in the list. */
140 if ((hash_entry *) htab->first == NULL)
142 table[idx].next = &table[idx];
143 htab->first = &table[idx];
147 table[idx].next = ((hash_entry *) htab->first)->next;
148 ((hash_entry *) htab->first)->next = &table[idx];
149 htab->first = &table[idx];
153 if (100 * htab->filled > 75 * htab->size)
155 /* Table is filled more than 75%. Resize the table.
156 Experiments have shown that for best performance, this threshold
157 must lie between 40% and 85%. */
158 unsigned long int old_size = htab->size;
160 htab->size = next_prime (htab->size * 2);
163 htab->table = (void *) xcalloc (1 + htab->size, sizeof (hash_entry));
165 for (idx = 1; idx <= old_size; ++idx)
167 insert_entry_2 (htab, table[idx].key, table[idx].keylen,
169 lookup (htab, table[idx].key, table[idx].keylen,
179 find_entry (htab, key, keylen, result)
180 const hash_table *htab;
185 hash_entry *table = (hash_entry *) htab->table;
186 size_t idx = lookup (htab, key, keylen, compute_hashval (key, keylen));
188 if (table[idx].used == 0)
191 *result = table[idx].data;
197 set_entry (htab, key, keylen, newval)
203 hash_entry *table = (hash_entry *) htab->table;
204 size_t idx = lookup (htab, key, keylen, compute_hashval (key, keylen));
206 if (table[idx].used == 0)
209 table[idx].data = newval;
215 iterate_table (htab, ptr, key, keylen, data)
216 const hash_table *htab;
224 if (htab->first == NULL)
226 *ptr = (void *) ((hash_entry *) htab->first)->next;
230 if (*ptr == htab->first)
232 *ptr = (void *) (((hash_entry *) *ptr)->next);
235 *key = ((hash_entry *) *ptr)->key;
236 *keylen = ((hash_entry *) *ptr)->keylen;
237 *data = ((hash_entry *) *ptr)->data;
243 [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
244 [Knuth] The Art of Computer Programming, part3 (6.4) */
247 lookup (htab, key, keylen, hval)
248 const hash_table *htab;
251 unsigned long int hval;
253 unsigned long int hash;
255 hash_entry *table = (hash_entry *) htab->table;
257 /* First hash function: simply take the modul but prevent zero. */
258 hash = 1 + hval % htab->size;
264 if (table[idx].used == hval && table[idx].keylen == keylen
265 && memcmp (table[idx].key, key, keylen) == 0)
268 /* Second hash function as suggested in [Knuth]. */
269 hash = 1 + hval % (htab->size - 2);
274 idx = htab->size + idx - hash;
278 /* If entry is found use it. */
279 if (table[idx].used == hval && table[idx].keylen == keylen
280 && memcmp (table[idx].key, key, keylen) == 0)
283 while (table[idx].used);
291 unsigned long int seed;
293 /* Make it definitely odd. */
296 while (!is_prime (seed))
305 unsigned long int candidate;
307 /* No even number and none less than 10 will be passed here. */
308 unsigned long int divn = 3;
309 unsigned long int sq = divn * divn;
311 while (sq < candidate && candidate % divn != 0)
318 return candidate % divn != 0;