hash.c revision 1.3 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 /*static char sccsid[] = "from: @(#)hash.c 5.5 (Berkeley) 12/28/90";*/
41 static char rcsid[] = "$Id: hash.c,v 1.3 1994/01/13 21:01:49 jtc 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
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include "sprite.h"
56 #include "hash.h"
57
58 /*
59 * Forward references to local procedures that are used before they're
60 * defined:
61 */
62
63 static void RebuildTable();
64
65 /*
66 * The following defines the ratio of # entries to # buckets
67 * at which we rebuild the table to make it larger.
68 */
69
70 #define rebuildLimit 8
71
72 /*
73 *---------------------------------------------------------
74 *
75 * Hash_InitTable --
76 *
77 * This routine just sets up the hash table.
78 *
79 * Results:
80 * None.
81 *
82 * Side Effects:
83 * Memory is allocated for the initial bucket area.
84 *
85 *---------------------------------------------------------
86 */
87
88 void
89 Hash_InitTable(t, numBuckets)
90 register Hash_Table *t; /* Structure to use to hold table. */
91 int numBuckets; /* How many buckets to create for starters.
92 * This number is rounded up to a power of
93 * two. If <= 0, a reasonable default is
94 * chosen. The table will grow in size later
95 * as needed. */
96 {
97 register int i;
98 register struct Hash_Entry **hp;
99
100 /*
101 * Round up the size to a power of two.
102 */
103 if (numBuckets <= 0)
104 i = 16;
105 else {
106 for (i = 2; i < numBuckets; i <<= 1)
107 /* void */ ;
108 }
109 t->numEntries = 0;
110 t->size = i;
111 t->mask = i - 1;
112 t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
113 while (--i >= 0)
114 *hp++ = NULL;
115 }
116
117 /*
118 *---------------------------------------------------------
119 *
120 * Hash_DeleteTable --
121 *
122 * This routine removes everything from a hash table
123 * and frees up the memory space it occupied (except for
124 * the space in the Hash_Table structure).
125 *
126 * Results:
127 * None.
128 *
129 * Side Effects:
130 * Lots of memory is freed up.
131 *
132 *---------------------------------------------------------
133 */
134
135 void
136 Hash_DeleteTable(t)
137 Hash_Table *t;
138 {
139 register struct Hash_Entry **hp, *h, *nexth;
140 register int i;
141
142 for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
143 for (h = *hp++; h != NULL; h = nexth) {
144 nexth = h->next;
145 free((char *)h);
146 }
147 }
148 free((char *)t->bucketPtr);
149
150 /*
151 * Set up the hash table to cause memory faults on any future access
152 * attempts until re-initialization.
153 */
154 t->bucketPtr = NULL;
155 }
156
157 /*
158 *---------------------------------------------------------
159 *
160 * Hash_FindEntry --
161 *
162 * Searches a hash table for an entry corresponding to key.
163 *
164 * Results:
165 * The return value is a pointer to the entry for key,
166 * if key was present in the table. If key was not
167 * present, NULL is returned.
168 *
169 * Side Effects:
170 * None.
171 *
172 *---------------------------------------------------------
173 */
174
175 Hash_Entry *
176 Hash_FindEntry(t, key)
177 Hash_Table *t; /* Hash table to search. */
178 char *key; /* A hash key. */
179 {
180 register Hash_Entry *e;
181 register unsigned h;
182 register char *p;
183
184 for (h = 0, p = key; *p;)
185 h = (h << 5) - h + *p++;
186 p = key;
187 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
188 if (e->namehash == h && strcmp(e->name, p) == 0)
189 return (e);
190 return (NULL);
191 }
192
193 /*
194 *---------------------------------------------------------
195 *
196 * Hash_CreateEntry --
197 *
198 * Searches a hash table for an entry corresponding to
199 * key. If no entry is found, then one is created.
200 *
201 * Results:
202 * The return value is a pointer to the entry. If *newPtr
203 * isn't NULL, then *newPtr is filled in with TRUE if a
204 * new entry was created, and FALSE if an entry already existed
205 * with the given key.
206 *
207 * Side Effects:
208 * Memory may be allocated, and the hash buckets may be modified.
209 *---------------------------------------------------------
210 */
211
212 Hash_Entry *
213 Hash_CreateEntry(t, key, newPtr)
214 register Hash_Table *t; /* Hash table to search. */
215 char *key; /* A hash key. */
216 Boolean *newPtr; /* Filled in with TRUE if new entry created,
217 * FALSE otherwise. */
218 {
219 register Hash_Entry *e;
220 register unsigned h;
221 register char *p;
222 int keylen;
223 struct Hash_Entry **hp;
224
225 /*
226 * Hash the key. As a side effect, save the length (strlen) of the
227 * key in case we need to create the entry.
228 */
229 for (h = 0, p = key; *p;)
230 h = (h << 5) - h + *p++;
231 keylen = p - key;
232 p = key;
233 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
234 if (e->namehash == h && strcmp(e->name, p) == 0) {
235 if (newPtr != NULL)
236 *newPtr = FALSE;
237 return (e);
238 }
239 }
240
241 /*
242 * The desired entry isn't there. Before allocating a new entry,
243 * expand the table if necessary (and this changes the resulting
244 * bucket chain).
245 */
246 if (t->numEntries >= rebuildLimit * t->size)
247 RebuildTable(t);
248 e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
249 hp = &t->bucketPtr[h & t->mask];
250 e->next = *hp;
251 *hp = e;
252 e->clientData = NULL;
253 e->namehash = h;
254 (void) strcpy(e->name, p);
255 t->numEntries++;
256
257 if (newPtr != NULL)
258 *newPtr = TRUE;
259 return (e);
260 }
261
262 /*
263 *---------------------------------------------------------
264 *
265 * Hash_DeleteEntry --
266 *
267 * Delete the given hash table entry and free memory associated with
268 * it.
269 *
270 * Results:
271 * None.
272 *
273 * Side Effects:
274 * Hash chain that entry lives in is modified and memory is freed.
275 *
276 *---------------------------------------------------------
277 */
278
279 void
280 Hash_DeleteEntry(t, e)
281 Hash_Table *t;
282 Hash_Entry *e;
283 {
284 register Hash_Entry **hp, *p;
285
286 if (e == NULL)
287 return;
288 for (hp = &t->bucketPtr[e->namehash & t->mask];
289 (p = *hp) != NULL; hp = &p->next) {
290 if (p == e) {
291 *hp = p->next;
292 free((char *)p);
293 t->numEntries--;
294 return;
295 }
296 }
297 (void) write(2, "bad call to Hash_DeleteEntry\n", 29);
298 abort();
299 }
300
301 /*
302 *---------------------------------------------------------
303 *
304 * Hash_EnumFirst --
305 * This procedure sets things up for a complete search
306 * of all entries recorded in the hash table.
307 *
308 * Results:
309 * The return value is the address of the first entry in
310 * the hash table, or NULL if the table is empty.
311 *
312 * Side Effects:
313 * The information in searchPtr is initialized so that successive
314 * calls to Hash_Next will return successive HashEntry's
315 * from the table.
316 *
317 *---------------------------------------------------------
318 */
319
320 Hash_Entry *
321 Hash_EnumFirst(t, searchPtr)
322 Hash_Table *t; /* Table to be searched. */
323 register Hash_Search *searchPtr;/* Area in which to keep state
324 * about search.*/
325 {
326 searchPtr->tablePtr = t;
327 searchPtr->nextIndex = 0;
328 searchPtr->hashEntryPtr = NULL;
329 return Hash_EnumNext(searchPtr);
330 }
331
332 /*
333 *---------------------------------------------------------
334 *
335 * Hash_EnumNext --
336 * This procedure returns successive entries in the hash table.
337 *
338 * Results:
339 * The return value is a pointer to the next HashEntry
340 * in the table, or NULL when the end of the table is
341 * reached.
342 *
343 * Side Effects:
344 * The information in searchPtr is modified to advance to the
345 * next entry.
346 *
347 *---------------------------------------------------------
348 */
349
350 Hash_Entry *
351 Hash_EnumNext(searchPtr)
352 register Hash_Search *searchPtr; /* Area used to keep state about
353 search. */
354 {
355 register Hash_Entry *e;
356 Hash_Table *t = searchPtr->tablePtr;
357
358 /*
359 * The hashEntryPtr field points to the most recently returned
360 * entry, or is nil if we are starting up. If not nil, we have
361 * to start at the next one in the chain.
362 */
363 e = searchPtr->hashEntryPtr;
364 if (e != NULL)
365 e = e->next;
366 /*
367 * If the chain ran out, or if we are starting up, we need to
368 * find the next nonempty chain.
369 */
370 while (e == NULL) {
371 if (searchPtr->nextIndex >= t->size)
372 return (NULL);
373 e = t->bucketPtr[searchPtr->nextIndex++];
374 }
375 searchPtr->hashEntryPtr = e;
376 return (e);
377 }
378
379 /*
380 *---------------------------------------------------------
381 *
382 * RebuildTable --
383 * This local routine makes a new hash table that
384 * is larger than the old one.
385 *
386 * Results:
387 * None.
388 *
389 * Side Effects:
390 * The entire hash table is moved, so any bucket numbers
391 * from the old table are invalid.
392 *
393 *---------------------------------------------------------
394 */
395
396 static void
397 RebuildTable(t)
398 register Hash_Table *t;
399 {
400 register Hash_Entry *e, *next, **hp, **xp;
401 register int i, mask;
402 register Hash_Entry **oldhp;
403 int oldsize;
404
405 oldhp = t->bucketPtr;
406 oldsize = i = t->size;
407 i <<= 1;
408 t->size = i;
409 t->mask = mask = i - 1;
410 t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
411 while (--i >= 0)
412 *hp++ = NULL;
413 for (hp = oldhp, i = oldsize; --i >= 0;) {
414 for (e = *hp++; e != NULL; e = next) {
415 next = e->next;
416 xp = &t->bucketPtr[e->namehash & mask];
417 e->next = *xp;
418 *xp = e;
419 }
420 }
421 free((char *)oldhp);
422 }
423