hash.c revision 1.64 1 1.64 rillig /* $NetBSD: hash.c,v 1.64 2021/04/11 12:46:54 rillig Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.12 agc * All rights reserved.
6 1.12 agc *
7 1.12 agc * This code is derived from software contributed to Berkeley by
8 1.12 agc * Adam de Boor.
9 1.12 agc *
10 1.12 agc * Redistribution and use in source and binary forms, with or without
11 1.12 agc * modification, are permitted provided that the following conditions
12 1.12 agc * are met:
13 1.12 agc * 1. Redistributions of source code must retain the above copyright
14 1.12 agc * notice, this list of conditions and the following disclaimer.
15 1.12 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.12 agc * notice, this list of conditions and the following disclaimer in the
17 1.12 agc * documentation and/or other materials provided with the distribution.
18 1.12 agc * 3. Neither the name of the University nor the names of its contributors
19 1.12 agc * may be used to endorse or promote products derived from this software
20 1.12 agc * without specific prior written permission.
21 1.12 agc *
22 1.12 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.12 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.12 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.12 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.12 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.12 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.12 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.12 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.12 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.12 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.12 agc * SUCH DAMAGE.
33 1.12 agc */
34 1.12 agc
35 1.12 agc /*
36 1.1 cgd * Copyright (c) 1988, 1989 by Adam de Boor
37 1.1 cgd * Copyright (c) 1989 by Berkeley Softworks
38 1.1 cgd * All rights reserved.
39 1.1 cgd *
40 1.1 cgd * This code is derived from software contributed to Berkeley by
41 1.1 cgd * Adam de Boor.
42 1.1 cgd *
43 1.1 cgd * Redistribution and use in source and binary forms, with or without
44 1.1 cgd * modification, are permitted provided that the following conditions
45 1.1 cgd * are met:
46 1.1 cgd * 1. Redistributions of source code must retain the above copyright
47 1.1 cgd * notice, this list of conditions and the following disclaimer.
48 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 cgd * notice, this list of conditions and the following disclaimer in the
50 1.1 cgd * documentation and/or other materials provided with the distribution.
51 1.1 cgd * 3. All advertising materials mentioning features or use of this software
52 1.1 cgd * must display the following acknowledgement:
53 1.1 cgd * This product includes software developed by the University of
54 1.1 cgd * California, Berkeley and its contributors.
55 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
56 1.1 cgd * may be used to endorse or promote products derived from this software
57 1.1 cgd * without specific prior written permission.
58 1.1 cgd *
59 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 cgd * SUCH DAMAGE.
70 1.1 cgd */
71 1.1 cgd
72 1.53 rillig /* Hash tables with string keys. */
73 1.34 rillig
74 1.4 cgd #include "make.h"
75 1.1 cgd
76 1.32 rillig /* "@(#)hash.c 8.1 (Berkeley) 6/6/93" */
77 1.64 rillig MAKE_RCSID("$NetBSD: hash.c,v 1.64 2021/04/11 12:46:54 rillig Exp $");
78 1.32 rillig
79 1.1 cgd /*
80 1.43 rillig * The ratio of # entries to # buckets at which we rebuild the table to
81 1.43 rillig * make it larger.
82 1.1 cgd */
83 1.43 rillig #define rebuildLimit 3
84 1.1 cgd
85 1.61 rillig /* This hash function matches Gosling's Emacs and java.lang.String. */
86 1.43 rillig static unsigned int
87 1.64 rillig Hash_String(const char *key, size_t *out_keylen)
88 1.43 rillig {
89 1.59 rillig unsigned int h;
90 1.59 rillig const char *p;
91 1.59 rillig
92 1.59 rillig h = 0;
93 1.59 rillig for (p = key; *p != '\0'; p++)
94 1.59 rillig h = 31 * h + (unsigned char)*p;
95 1.59 rillig
96 1.43 rillig if (out_keylen != NULL)
97 1.43 rillig *out_keylen = (size_t)(p - key);
98 1.43 rillig return h;
99 1.43 rillig }
100 1.23 sjg
101 1.64 rillig /* This hash function matches Gosling's Emacs and java.lang.String. */
102 1.48 rillig unsigned int
103 1.64 rillig Hash_Substring(Substring key)
104 1.48 rillig {
105 1.64 rillig unsigned int h;
106 1.64 rillig const char *p;
107 1.64 rillig
108 1.64 rillig h = 0;
109 1.64 rillig for (p = key.start; p != key.end; p++)
110 1.64 rillig h = 31 * h + (unsigned char)*p;
111 1.64 rillig return h;
112 1.48 rillig }
113 1.48 rillig
114 1.46 rillig static HashEntry *
115 1.46 rillig HashTable_Find(HashTable *t, unsigned int h, const char *key)
116 1.43 rillig {
117 1.46 rillig HashEntry *e;
118 1.44 rillig unsigned int chainlen = 0;
119 1.41 rillig
120 1.43 rillig #ifdef DEBUG_HASH_LOOKUP
121 1.55 rillig DEBUG4(HASH, "%s: %p h=%08x key=%s\n", __func__, t, h, key);
122 1.43 rillig #endif
123 1.41 rillig
124 1.43 rillig for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
125 1.43 rillig chainlen++;
126 1.47 rillig if (e->key_hash == h && strcmp(e->key, key) == 0)
127 1.43 rillig break;
128 1.43 rillig }
129 1.41 rillig
130 1.43 rillig if (chainlen > t->maxchain)
131 1.43 rillig t->maxchain = chainlen;
132 1.41 rillig
133 1.43 rillig return e;
134 1.43 rillig }
135 1.41 rillig
136 1.64 rillig static bool
137 1.64 rillig HashEntry_KeyEquals(const HashEntry *he, Substring key)
138 1.64 rillig {
139 1.64 rillig const char *heKey, *p;
140 1.64 rillig
141 1.64 rillig heKey = he->key;
142 1.64 rillig for (p = key.start; p != key.end; p++, heKey++)
143 1.64 rillig if (*p != *heKey || *heKey == '\0')
144 1.64 rillig return false;
145 1.64 rillig return *heKey == '\0';
146 1.64 rillig }
147 1.64 rillig
148 1.64 rillig static HashEntry *
149 1.64 rillig HashTable_FindEntryBySubstring(HashTable *t, Substring key, unsigned int h)
150 1.64 rillig {
151 1.64 rillig HashEntry *e;
152 1.64 rillig unsigned int chainlen = 0;
153 1.64 rillig
154 1.64 rillig #ifdef DEBUG_HASH_LOOKUP
155 1.64 rillig DEBUG4(HASH, "%s: %p h=%08x key=%.*s\n", __func__, t, h,
156 1.64 rillig (int)Substring_Length(key), key.start);
157 1.64 rillig #endif
158 1.64 rillig
159 1.64 rillig for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
160 1.64 rillig chainlen++;
161 1.64 rillig if (e->key_hash == h && HashEntry_KeyEquals(e, key))
162 1.64 rillig break;
163 1.64 rillig }
164 1.64 rillig
165 1.64 rillig if (chainlen > t->maxchain)
166 1.64 rillig t->maxchain = chainlen;
167 1.64 rillig
168 1.64 rillig return e;
169 1.64 rillig }
170 1.64 rillig
171 1.54 rillig /* Set up the hash table. */
172 1.1 cgd void
173 1.54 rillig HashTable_Init(HashTable *t)
174 1.1 cgd {
175 1.44 rillig unsigned int n = 16, i;
176 1.56 rillig HashEntry **buckets = bmake_malloc(sizeof *buckets * n);
177 1.50 rillig for (i = 0; i < n; i++)
178 1.50 rillig buckets[i] = NULL;
179 1.1 cgd
180 1.50 rillig t->buckets = buckets;
181 1.50 rillig t->bucketsSize = n;
182 1.1 cgd t->numEntries = 0;
183 1.50 rillig t->bucketsMask = n - 1;
184 1.25 sjg t->maxchain = 0;
185 1.1 cgd }
186 1.1 cgd
187 1.61 rillig /*
188 1.61 rillig * Remove everything from the hash table and free up the memory for the keys
189 1.61 rillig * of the hash table, but not for the values associated to these keys.
190 1.61 rillig */
191 1.1 cgd void
192 1.54 rillig HashTable_Done(HashTable *t)
193 1.1 cgd {
194 1.51 rillig HashEntry **buckets = t->buckets;
195 1.51 rillig size_t i, n = t->bucketsSize;
196 1.1 cgd
197 1.51 rillig for (i = 0; i < n; i++) {
198 1.51 rillig HashEntry *he = buckets[i];
199 1.51 rillig while (he != NULL) {
200 1.51 rillig HashEntry *next = he->next;
201 1.51 rillig free(he);
202 1.51 rillig he = next;
203 1.1 cgd }
204 1.1 cgd }
205 1.61 rillig
206 1.29 rillig free(t->buckets);
207 1.55 rillig #ifdef CLEANUP
208 1.29 rillig t->buckets = NULL;
209 1.55 rillig #endif
210 1.1 cgd }
211 1.1 cgd
212 1.54 rillig /* Find the entry corresponding to the key, or return NULL. */
213 1.46 rillig HashEntry *
214 1.54 rillig HashTable_FindEntry(HashTable *t, const char *key)
215 1.1 cgd {
216 1.64 rillig unsigned int h = Hash_String(key, NULL);
217 1.43 rillig return HashTable_Find(t, h, key);
218 1.1 cgd }
219 1.1 cgd
220 1.54 rillig /* Find the value corresponding to the key, or return NULL. */
221 1.33 rillig void *
222 1.54 rillig HashTable_FindValue(HashTable *t, const char *key)
223 1.33 rillig {
224 1.54 rillig HashEntry *he = HashTable_FindEntry(t, key);
225 1.43 rillig return he != NULL ? he->value : NULL;
226 1.43 rillig }
227 1.43 rillig
228 1.60 rillig /*
229 1.60 rillig * Find the value corresponding to the key and the precomputed hash,
230 1.60 rillig * or return NULL.
231 1.60 rillig */
232 1.48 rillig void *
233 1.64 rillig HashTable_FindValueBySubstringHash(HashTable *t, Substring key, unsigned int h)
234 1.48 rillig {
235 1.64 rillig HashEntry *he = HashTable_FindEntryBySubstring(t, key, h);
236 1.48 rillig return he != NULL ? he->value : NULL;
237 1.48 rillig }
238 1.48 rillig
239 1.60 rillig /*
240 1.60 rillig * Make the hash table larger. Any bucket numbers from the old table become
241 1.60 rillig * invalid; the hash codes stay valid though.
242 1.60 rillig */
243 1.43 rillig static void
244 1.54 rillig HashTable_Enlarge(HashTable *t)
245 1.43 rillig {
246 1.49 rillig unsigned int oldSize = t->bucketsSize;
247 1.49 rillig HashEntry **oldBuckets = t->buckets;
248 1.49 rillig unsigned int newSize = 2 * oldSize;
249 1.49 rillig unsigned int newMask = newSize - 1;
250 1.56 rillig HashEntry **newBuckets = bmake_malloc(sizeof *newBuckets * newSize);
251 1.49 rillig size_t i;
252 1.49 rillig
253 1.49 rillig for (i = 0; i < newSize; i++)
254 1.49 rillig newBuckets[i] = NULL;
255 1.49 rillig
256 1.49 rillig for (i = 0; i < oldSize; i++) {
257 1.49 rillig HashEntry *he = oldBuckets[i];
258 1.49 rillig while (he != NULL) {
259 1.49 rillig HashEntry *next = he->next;
260 1.49 rillig he->next = newBuckets[he->key_hash & newMask];
261 1.49 rillig newBuckets[he->key_hash & newMask] = he;
262 1.49 rillig he = next;
263 1.43 rillig }
264 1.43 rillig }
265 1.49 rillig
266 1.49 rillig free(oldBuckets);
267 1.49 rillig
268 1.49 rillig t->bucketsSize = newSize;
269 1.49 rillig t->bucketsMask = newMask;
270 1.49 rillig t->buckets = newBuckets;
271 1.43 rillig DEBUG5(HASH, "%s: %p size=%d entries=%d maxchain=%d\n",
272 1.63 rillig __func__, (void *)t, t->bucketsSize, t->numEntries, t->maxchain);
273 1.43 rillig t->maxchain = 0;
274 1.33 rillig }
275 1.33 rillig
276 1.60 rillig /*
277 1.60 rillig * Find or create an entry corresponding to the key.
278 1.60 rillig * Return in out_isNew whether a new entry has been created.
279 1.60 rillig */
280 1.46 rillig HashEntry *
281 1.62 rillig HashTable_CreateEntry(HashTable *t, const char *key, bool *out_isNew)
282 1.1 cgd {
283 1.43 rillig size_t keylen;
284 1.64 rillig unsigned int h = Hash_String(key, &keylen);
285 1.52 rillig HashEntry *he = HashTable_Find(t, h, key);
286 1.1 cgd
287 1.52 rillig if (he != NULL) {
288 1.52 rillig if (out_isNew != NULL)
289 1.62 rillig *out_isNew = false;
290 1.52 rillig return he;
291 1.42 rillig }
292 1.1 cgd
293 1.29 rillig if (t->numEntries >= rebuildLimit * t->bucketsSize)
294 1.54 rillig HashTable_Enlarge(t);
295 1.43 rillig
296 1.56 rillig he = bmake_malloc(sizeof *he + keylen);
297 1.52 rillig he->value = NULL;
298 1.52 rillig he->key_hash = h;
299 1.52 rillig memcpy(he->key, key, keylen + 1);
300 1.52 rillig
301 1.52 rillig he->next = t->buckets[h & t->bucketsMask];
302 1.52 rillig t->buckets[h & t->bucketsMask] = he;
303 1.1 cgd t->numEntries++;
304 1.1 cgd
305 1.52 rillig if (out_isNew != NULL)
306 1.62 rillig *out_isNew = true;
307 1.52 rillig return he;
308 1.1 cgd }
309 1.1 cgd
310 1.57 rillig HashEntry *
311 1.57 rillig HashTable_Set(HashTable *t, const char *key, void *value)
312 1.57 rillig {
313 1.57 rillig HashEntry *he = HashTable_CreateEntry(t, key, NULL);
314 1.57 rillig HashEntry_Set(he, value);
315 1.57 rillig return he;
316 1.57 rillig }
317 1.57 rillig
318 1.54 rillig /* Delete the entry from the table and free the associated memory. */
319 1.1 cgd void
320 1.54 rillig HashTable_DeleteEntry(HashTable *t, HashEntry *he)
321 1.1 cgd {
322 1.52 rillig HashEntry **ref = &t->buckets[he->key_hash & t->bucketsMask];
323 1.52 rillig HashEntry *p;
324 1.1 cgd
325 1.52 rillig for (; (p = *ref) != NULL; ref = &p->next) {
326 1.52 rillig if (p == he) {
327 1.52 rillig *ref = p->next;
328 1.16 christos free(p);
329 1.1 cgd t->numEntries--;
330 1.1 cgd return;
331 1.1 cgd }
332 1.1 cgd }
333 1.1 cgd abort();
334 1.1 cgd }
335 1.1 cgd
336 1.45 rillig /* Set things up for iterating over all entries in the hash table. */
337 1.45 rillig void
338 1.46 rillig HashIter_Init(HashIter *hi, HashTable *t)
339 1.1 cgd {
340 1.45 rillig hi->table = t;
341 1.45 rillig hi->nextBucket = 0;
342 1.45 rillig hi->entry = NULL;
343 1.1 cgd }
344 1.1 cgd
345 1.60 rillig /*
346 1.60 rillig * Return the next entry in the hash table, or NULL if the end of the table
347 1.60 rillig * is reached.
348 1.60 rillig */
349 1.46 rillig HashEntry *
350 1.45 rillig HashIter_Next(HashIter *hi)
351 1.1 cgd {
352 1.46 rillig HashTable *t = hi->table;
353 1.52 rillig HashEntry *he = hi->entry;
354 1.52 rillig HashEntry **buckets = t->buckets;
355 1.52 rillig unsigned int bucketsSize = t->bucketsSize;
356 1.1 cgd
357 1.52 rillig if (he != NULL)
358 1.52 rillig he = he->next; /* skip the most recently returned entry */
359 1.52 rillig
360 1.52 rillig while (he == NULL) { /* find the next nonempty chain */
361 1.52 rillig if (hi->nextBucket >= bucketsSize)
362 1.18 dsl return NULL;
363 1.52 rillig he = buckets[hi->nextBucket++];
364 1.1 cgd }
365 1.52 rillig hi->entry = he;
366 1.52 rillig return he;
367 1.1 cgd }
368 1.1 cgd
369 1.28 rillig void
370 1.54 rillig HashTable_DebugStats(HashTable *t, const char *name)
371 1.25 sjg {
372 1.46 rillig DEBUG4(HASH, "HashTable %s: size=%u numEntries=%u maxchain=%u\n",
373 1.43 rillig name, t->bucketsSize, t->numEntries, t->maxchain);
374 1.25 sjg }
375