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