hash.c revision 1.1 1 1.1 mrg /* $NetBSD: hash.c,v 1.1 1999/11/23 05:28:20 mrg Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.1 mrg * Copyright (c) 1988, 1989 by Adam de Boor
6 1.1 mrg * Copyright (c) 1989 by Berkeley Softworks
7 1.1 mrg * All rights reserved.
8 1.1 mrg *
9 1.1 mrg * This code is derived from software contributed to Berkeley by
10 1.1 mrg * Adam de Boor.
11 1.1 mrg *
12 1.1 mrg * Redistribution and use in source and binary forms, with or without
13 1.1 mrg * modification, are permitted provided that the following conditions
14 1.1 mrg * are met:
15 1.1 mrg * 1. Redistributions of source code must retain the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer.
17 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 mrg * notice, this list of conditions and the following disclaimer in the
19 1.1 mrg * documentation and/or other materials provided with the distribution.
20 1.1 mrg * 3. All advertising materials mentioning features or use of this software
21 1.1 mrg * must display the following acknowledgement:
22 1.1 mrg * This product includes software developed by the University of
23 1.1 mrg * California, Berkeley and its contributors.
24 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
25 1.1 mrg * may be used to endorse or promote products derived from this software
26 1.1 mrg * without specific prior written permission.
27 1.1 mrg *
28 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 mrg * SUCH DAMAGE.
39 1.1 mrg */
40 1.1 mrg
41 1.1 mrg #ifdef MAKE_BOOTSTRAP
42 1.1 mrg static char rcsid[] = "$NetBSD: hash.c,v 1.1 1999/11/23 05:28:20 mrg Exp $";
43 1.1 mrg #else
44 1.1 mrg #include <sys/cdefs.h>
45 1.1 mrg #ifndef lint
46 1.1 mrg #if 0
47 1.1 mrg static char sccsid[] = "@(#)hash.c 8.1 (Berkeley) 6/6/93";
48 1.1 mrg #else
49 1.1 mrg __RCSID("$NetBSD: hash.c,v 1.1 1999/11/23 05:28:20 mrg Exp $");
50 1.1 mrg #endif
51 1.1 mrg #endif /* not lint */
52 1.1 mrg #endif
53 1.1 mrg
54 1.1 mrg #include <sys/types.h>
55 1.1 mrg
56 1.1 mrg #include <stdlib.h>
57 1.1 mrg #include <string.h>
58 1.1 mrg #include <unistd.h>
59 1.1 mrg
60 1.1 mrg /* hash.c --
61 1.1 mrg *
62 1.1 mrg * This module contains routines to manipulate a hash table.
63 1.1 mrg * See hash.h for a definition of the structure of the hash
64 1.1 mrg * table. Hash tables grow automatically as the amount of
65 1.1 mrg * information increases.
66 1.1 mrg */
67 1.1 mrg #include "sprite.h"
68 1.1 mrg #ifndef ORDER
69 1.1 mrg #include "make.h"
70 1.1 mrg #endif /* ORDER */
71 1.1 mrg #include "hash.h"
72 1.1 mrg #include "ealloc.h"
73 1.1 mrg
74 1.1 mrg /*
75 1.1 mrg * Forward references to local procedures that are used before they're
76 1.1 mrg * defined:
77 1.1 mrg */
78 1.1 mrg
79 1.1 mrg static void RebuildTable __P((Hash_Table *));
80 1.1 mrg
81 1.1 mrg /*
82 1.1 mrg * The following defines the ratio of # entries to # buckets
83 1.1 mrg * at which we rebuild the table to make it larger.
84 1.1 mrg */
85 1.1 mrg
86 1.1 mrg #define rebuildLimit 8
87 1.1 mrg
88 1.1 mrg /*
89 1.1 mrg *---------------------------------------------------------
90 1.1 mrg *
91 1.1 mrg * Hash_InitTable --
92 1.1 mrg *
93 1.1 mrg * This routine just sets up the hash table.
94 1.1 mrg *
95 1.1 mrg * Results:
96 1.1 mrg * None.
97 1.1 mrg *
98 1.1 mrg * Side Effects:
99 1.1 mrg * Memory is allocated for the initial bucket area.
100 1.1 mrg *
101 1.1 mrg *---------------------------------------------------------
102 1.1 mrg */
103 1.1 mrg
104 1.1 mrg void
105 1.1 mrg Hash_InitTable(t, numBuckets)
106 1.1 mrg register Hash_Table *t; /* Structure to use to hold table. */
107 1.1 mrg int numBuckets; /* How many buckets to create for starters.
108 1.1 mrg * This number is rounded up to a power of
109 1.1 mrg * two. If <= 0, a reasonable default is
110 1.1 mrg * chosen. The table will grow in size later
111 1.1 mrg * as needed. */
112 1.1 mrg {
113 1.1 mrg register int i;
114 1.1 mrg register struct Hash_Entry **hp;
115 1.1 mrg
116 1.1 mrg /*
117 1.1 mrg * Round up the size to a power of two.
118 1.1 mrg */
119 1.1 mrg if (numBuckets <= 0)
120 1.1 mrg i = 16;
121 1.1 mrg else {
122 1.1 mrg for (i = 2; i < numBuckets; i <<= 1)
123 1.1 mrg continue;
124 1.1 mrg }
125 1.1 mrg t->numEntries = 0;
126 1.1 mrg t->size = i;
127 1.1 mrg t->mask = i - 1;
128 1.1 mrg t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
129 1.1 mrg while (--i >= 0)
130 1.1 mrg *hp++ = NULL;
131 1.1 mrg }
132 1.1 mrg
133 1.1 mrg /*
134 1.1 mrg *---------------------------------------------------------
135 1.1 mrg *
136 1.1 mrg * Hash_DeleteTable --
137 1.1 mrg *
138 1.1 mrg * This routine removes everything from a hash table
139 1.1 mrg * and frees up the memory space it occupied (except for
140 1.1 mrg * the space in the Hash_Table structure).
141 1.1 mrg *
142 1.1 mrg * Results:
143 1.1 mrg * None.
144 1.1 mrg *
145 1.1 mrg * Side Effects:
146 1.1 mrg * Lots of memory is freed up.
147 1.1 mrg *
148 1.1 mrg *---------------------------------------------------------
149 1.1 mrg */
150 1.1 mrg
151 1.1 mrg void
152 1.1 mrg Hash_DeleteTable(t)
153 1.1 mrg Hash_Table *t;
154 1.1 mrg {
155 1.1 mrg register struct Hash_Entry **hp, *h, *nexth = NULL;
156 1.1 mrg register int i;
157 1.1 mrg
158 1.1 mrg for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
159 1.1 mrg for (h = *hp++; h != NULL; h = nexth) {
160 1.1 mrg nexth = h->next;
161 1.1 mrg free((char *)h);
162 1.1 mrg }
163 1.1 mrg }
164 1.1 mrg free((char *)t->bucketPtr);
165 1.1 mrg
166 1.1 mrg /*
167 1.1 mrg * Set up the hash table to cause memory faults on any future access
168 1.1 mrg * attempts until re-initialization.
169 1.1 mrg */
170 1.1 mrg t->bucketPtr = NULL;
171 1.1 mrg }
172 1.1 mrg
173 1.1 mrg /*
174 1.1 mrg *---------------------------------------------------------
175 1.1 mrg *
176 1.1 mrg * Hash_FindEntry --
177 1.1 mrg *
178 1.1 mrg * Searches a hash table for an entry corresponding to key.
179 1.1 mrg *
180 1.1 mrg * Results:
181 1.1 mrg * The return value is a pointer to the entry for key,
182 1.1 mrg * if key was present in the table. If key was not
183 1.1 mrg * present, NULL is returned.
184 1.1 mrg *
185 1.1 mrg * Side Effects:
186 1.1 mrg * None.
187 1.1 mrg *
188 1.1 mrg *---------------------------------------------------------
189 1.1 mrg */
190 1.1 mrg
191 1.1 mrg Hash_Entry *
192 1.1 mrg Hash_FindEntry(t, key)
193 1.1 mrg Hash_Table *t; /* Hash table to search. */
194 1.1 mrg char *key; /* A hash key. */
195 1.1 mrg {
196 1.1 mrg register Hash_Entry *e;
197 1.1 mrg register unsigned h;
198 1.1 mrg register char *p;
199 1.1 mrg
200 1.1 mrg for (h = 0, p = key; *p;)
201 1.1 mrg h = (h << 5) - h + *p++;
202 1.1 mrg p = key;
203 1.1 mrg for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
204 1.1 mrg if (e->namehash == h && strcmp(e->name, p) == 0)
205 1.1 mrg return (e);
206 1.1 mrg return (NULL);
207 1.1 mrg }
208 1.1 mrg
209 1.1 mrg /*
210 1.1 mrg *---------------------------------------------------------
211 1.1 mrg *
212 1.1 mrg * Hash_CreateEntry --
213 1.1 mrg *
214 1.1 mrg * Searches a hash table for an entry corresponding to
215 1.1 mrg * key. If no entry is found, then one is created.
216 1.1 mrg *
217 1.1 mrg * Results:
218 1.1 mrg * The return value is a pointer to the entry. If *newPtr
219 1.1 mrg * isn't NULL, then *newPtr is filled in with TRUE if a
220 1.1 mrg * new entry was created, and FALSE if an entry already existed
221 1.1 mrg * with the given key.
222 1.1 mrg *
223 1.1 mrg * Side Effects:
224 1.1 mrg * Memory may be allocated, and the hash buckets may be modified.
225 1.1 mrg *---------------------------------------------------------
226 1.1 mrg */
227 1.1 mrg
228 1.1 mrg Hash_Entry *
229 1.1 mrg Hash_CreateEntry(t, key, newPtr)
230 1.1 mrg register Hash_Table *t; /* Hash table to search. */
231 1.1 mrg char *key; /* A hash key. */
232 1.1 mrg Boolean *newPtr; /* Filled in with TRUE if new entry created,
233 1.1 mrg * FALSE otherwise. */
234 1.1 mrg {
235 1.1 mrg register Hash_Entry *e;
236 1.1 mrg register unsigned h;
237 1.1 mrg register char *p;
238 1.1 mrg int keylen;
239 1.1 mrg struct Hash_Entry **hp;
240 1.1 mrg
241 1.1 mrg /*
242 1.1 mrg * Hash the key. As a side effect, save the length (strlen) of the
243 1.1 mrg * key in case we need to create the entry.
244 1.1 mrg */
245 1.1 mrg for (h = 0, p = key; *p;)
246 1.1 mrg h = (h << 5) - h + *p++;
247 1.1 mrg keylen = p - key;
248 1.1 mrg p = key;
249 1.1 mrg for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
250 1.1 mrg if (e->namehash == h && strcmp(e->name, p) == 0) {
251 1.1 mrg if (newPtr != NULL)
252 1.1 mrg *newPtr = FALSE;
253 1.1 mrg return (e);
254 1.1 mrg }
255 1.1 mrg }
256 1.1 mrg
257 1.1 mrg /*
258 1.1 mrg * The desired entry isn't there. Before allocating a new entry,
259 1.1 mrg * expand the table if necessary (and this changes the resulting
260 1.1 mrg * bucket chain).
261 1.1 mrg */
262 1.1 mrg if (t->numEntries >= rebuildLimit * t->size)
263 1.1 mrg RebuildTable(t);
264 1.1 mrg e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
265 1.1 mrg hp = &t->bucketPtr[h & t->mask];
266 1.1 mrg e->next = *hp;
267 1.1 mrg *hp = e;
268 1.1 mrg e->clientData = NULL;
269 1.1 mrg e->namehash = h;
270 1.1 mrg (void) strcpy(e->name, p);
271 1.1 mrg t->numEntries++;
272 1.1 mrg
273 1.1 mrg if (newPtr != NULL)
274 1.1 mrg *newPtr = TRUE;
275 1.1 mrg return (e);
276 1.1 mrg }
277 1.1 mrg
278 1.1 mrg /*
279 1.1 mrg *---------------------------------------------------------
280 1.1 mrg *
281 1.1 mrg * Hash_DeleteEntry --
282 1.1 mrg *
283 1.1 mrg * Delete the given hash table entry and free memory associated with
284 1.1 mrg * it.
285 1.1 mrg *
286 1.1 mrg * Results:
287 1.1 mrg * None.
288 1.1 mrg *
289 1.1 mrg * Side Effects:
290 1.1 mrg * Hash chain that entry lives in is modified and memory is freed.
291 1.1 mrg *
292 1.1 mrg *---------------------------------------------------------
293 1.1 mrg */
294 1.1 mrg
295 1.1 mrg void
296 1.1 mrg Hash_DeleteEntry(t, e)
297 1.1 mrg Hash_Table *t;
298 1.1 mrg Hash_Entry *e;
299 1.1 mrg {
300 1.1 mrg register Hash_Entry **hp, *p;
301 1.1 mrg
302 1.1 mrg if (e == NULL)
303 1.1 mrg return;
304 1.1 mrg for (hp = &t->bucketPtr[e->namehash & t->mask];
305 1.1 mrg (p = *hp) != NULL; hp = &p->next) {
306 1.1 mrg if (p == e) {
307 1.1 mrg *hp = p->next;
308 1.1 mrg free((char *)p);
309 1.1 mrg t->numEntries--;
310 1.1 mrg return;
311 1.1 mrg }
312 1.1 mrg }
313 1.1 mrg (void)write(2, "bad call to Hash_DeleteEntry\n", 29);
314 1.1 mrg abort();
315 1.1 mrg }
316 1.1 mrg
317 1.1 mrg /*
318 1.1 mrg *---------------------------------------------------------
319 1.1 mrg *
320 1.1 mrg * Hash_EnumFirst --
321 1.1 mrg * This procedure sets things up for a complete search
322 1.1 mrg * of all entries recorded in the hash table.
323 1.1 mrg *
324 1.1 mrg * Results:
325 1.1 mrg * The return value is the address of the first entry in
326 1.1 mrg * the hash table, or NULL if the table is empty.
327 1.1 mrg *
328 1.1 mrg * Side Effects:
329 1.1 mrg * The information in searchPtr is initialized so that successive
330 1.1 mrg * calls to Hash_Next will return successive HashEntry's
331 1.1 mrg * from the table.
332 1.1 mrg *
333 1.1 mrg *---------------------------------------------------------
334 1.1 mrg */
335 1.1 mrg
336 1.1 mrg Hash_Entry *
337 1.1 mrg Hash_EnumFirst(t, searchPtr)
338 1.1 mrg Hash_Table *t; /* Table to be searched. */
339 1.1 mrg register Hash_Search *searchPtr;/* Area in which to keep state
340 1.1 mrg * about search.*/
341 1.1 mrg {
342 1.1 mrg searchPtr->tablePtr = t;
343 1.1 mrg searchPtr->nextIndex = 0;
344 1.1 mrg searchPtr->hashEntryPtr = NULL;
345 1.1 mrg return Hash_EnumNext(searchPtr);
346 1.1 mrg }
347 1.1 mrg
348 1.1 mrg /*
349 1.1 mrg *---------------------------------------------------------
350 1.1 mrg *
351 1.1 mrg * Hash_EnumNext --
352 1.1 mrg * This procedure returns successive entries in the hash table.
353 1.1 mrg *
354 1.1 mrg * Results:
355 1.1 mrg * The return value is a pointer to the next HashEntry
356 1.1 mrg * in the table, or NULL when the end of the table is
357 1.1 mrg * reached.
358 1.1 mrg *
359 1.1 mrg * Side Effects:
360 1.1 mrg * The information in searchPtr is modified to advance to the
361 1.1 mrg * next entry.
362 1.1 mrg *
363 1.1 mrg *---------------------------------------------------------
364 1.1 mrg */
365 1.1 mrg
366 1.1 mrg Hash_Entry *
367 1.1 mrg Hash_EnumNext(searchPtr)
368 1.1 mrg register Hash_Search *searchPtr; /* Area used to keep state about
369 1.1 mrg search. */
370 1.1 mrg {
371 1.1 mrg register Hash_Entry *e;
372 1.1 mrg Hash_Table *t = searchPtr->tablePtr;
373 1.1 mrg
374 1.1 mrg /*
375 1.1 mrg * The hashEntryPtr field points to the most recently returned
376 1.1 mrg * entry, or is nil if we are starting up. If not nil, we have
377 1.1 mrg * to start at the next one in the chain.
378 1.1 mrg */
379 1.1 mrg e = searchPtr->hashEntryPtr;
380 1.1 mrg if (e != NULL)
381 1.1 mrg e = e->next;
382 1.1 mrg /*
383 1.1 mrg * If the chain ran out, or if we are starting up, we need to
384 1.1 mrg * find the next nonempty chain.
385 1.1 mrg */
386 1.1 mrg while (e == NULL) {
387 1.1 mrg if (searchPtr->nextIndex >= t->size)
388 1.1 mrg return (NULL);
389 1.1 mrg e = t->bucketPtr[searchPtr->nextIndex++];
390 1.1 mrg }
391 1.1 mrg searchPtr->hashEntryPtr = e;
392 1.1 mrg return (e);
393 1.1 mrg }
394 1.1 mrg
395 1.1 mrg /*
396 1.1 mrg *---------------------------------------------------------
397 1.1 mrg *
398 1.1 mrg * RebuildTable --
399 1.1 mrg * This local routine makes a new hash table that
400 1.1 mrg * is larger than the old one.
401 1.1 mrg *
402 1.1 mrg * Results:
403 1.1 mrg * None.
404 1.1 mrg *
405 1.1 mrg * Side Effects:
406 1.1 mrg * The entire hash table is moved, so any bucket numbers
407 1.1 mrg * from the old table are invalid.
408 1.1 mrg *
409 1.1 mrg *---------------------------------------------------------
410 1.1 mrg */
411 1.1 mrg
412 1.1 mrg static void
413 1.1 mrg RebuildTable(t)
414 1.1 mrg register Hash_Table *t;
415 1.1 mrg {
416 1.1 mrg register Hash_Entry *e, *next = NULL, **hp, **xp;
417 1.1 mrg register int i, mask;
418 1.1 mrg register Hash_Entry **oldhp;
419 1.1 mrg int oldsize;
420 1.1 mrg
421 1.1 mrg oldhp = t->bucketPtr;
422 1.1 mrg oldsize = i = t->size;
423 1.1 mrg i <<= 1;
424 1.1 mrg t->size = i;
425 1.1 mrg t->mask = mask = i - 1;
426 1.1 mrg t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
427 1.1 mrg while (--i >= 0)
428 1.1 mrg *hp++ = NULL;
429 1.1 mrg for (hp = oldhp, i = oldsize; --i >= 0;) {
430 1.1 mrg for (e = *hp++; e != NULL; e = next) {
431 1.1 mrg next = e->next;
432 1.1 mrg xp = &t->bucketPtr[e->namehash & mask];
433 1.1 mrg e->next = *xp;
434 1.1 mrg *xp = e;
435 1.1 mrg }
436 1.1 mrg }
437 1.1 mrg free((char *)oldhp);
438 1.1 mrg }
439