19d62501fSDavid E. O'Brien /* $NetBSD: hash.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $ */
29d62501fSDavid E. O'Brien
3*df57947fSPedro F. Giffuni /*-
4*df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
5*df57947fSPedro F. Giffuni *
69d62501fSDavid E. O'Brien * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
79d62501fSDavid E. O'Brien * Copyright (c) 1988, 1989 by Adam de Boor
89d62501fSDavid E. O'Brien * Copyright (c) 1989 by Berkeley Softworks
99d62501fSDavid E. O'Brien * All rights reserved.
109d62501fSDavid E. O'Brien *
119d62501fSDavid E. O'Brien * This code is derived from software contributed to Berkeley by
129d62501fSDavid E. O'Brien * Adam de Boor.
139d62501fSDavid E. O'Brien *
149d62501fSDavid E. O'Brien * Redistribution and use in source and binary forms, with or without
159d62501fSDavid E. O'Brien * modification, are permitted provided that the following conditions
169d62501fSDavid E. O'Brien * are met:
179d62501fSDavid E. O'Brien * 1. Redistributions of source code must retain the above copyright
189d62501fSDavid E. O'Brien * notice, this list of conditions and the following disclaimer.
199d62501fSDavid E. O'Brien * 2. Redistributions in binary form must reproduce the above copyright
209d62501fSDavid E. O'Brien * notice, this list of conditions and the following disclaimer in the
219d62501fSDavid E. O'Brien * documentation and/or other materials provided with the distribution.
229d62501fSDavid E. O'Brien * 3. All advertising materials mentioning features or use of this software
239d62501fSDavid E. O'Brien * must display the following acknowledgement:
249d62501fSDavid E. O'Brien * This product includes software developed by the University of
259d62501fSDavid E. O'Brien * California, Berkeley and its contributors.
269d62501fSDavid E. O'Brien * 4. Neither the name of the University nor the names of its contributors
279d62501fSDavid E. O'Brien * may be used to endorse or promote products derived from this software
289d62501fSDavid E. O'Brien * without specific prior written permission.
299d62501fSDavid E. O'Brien *
309d62501fSDavid E. O'Brien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
319d62501fSDavid E. O'Brien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
329d62501fSDavid E. O'Brien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
339d62501fSDavid E. O'Brien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
349d62501fSDavid E. O'Brien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
359d62501fSDavid E. O'Brien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
369d62501fSDavid E. O'Brien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
379d62501fSDavid E. O'Brien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
389d62501fSDavid E. O'Brien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
399d62501fSDavid E. O'Brien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
409d62501fSDavid E. O'Brien * SUCH DAMAGE.
419d62501fSDavid E. O'Brien */
429d62501fSDavid E. O'Brien
439d62501fSDavid E. O'Brien #include <sys/types.h>
449d62501fSDavid E. O'Brien
459d62501fSDavid E. O'Brien #include <stdlib.h>
469d62501fSDavid E. O'Brien #include <string.h>
479d62501fSDavid E. O'Brien #include <unistd.h>
489d62501fSDavid E. O'Brien
499d62501fSDavid E. O'Brien /* hash.c --
509d62501fSDavid E. O'Brien *
519d62501fSDavid E. O'Brien * This module contains routines to manipulate a hash table.
529d62501fSDavid E. O'Brien * See hash.h for a definition of the structure of the hash
539d62501fSDavid E. O'Brien * table. Hash tables grow automatically as the amount of
549d62501fSDavid E. O'Brien * information increases.
559d62501fSDavid E. O'Brien */
569d62501fSDavid E. O'Brien #include "sprite.h"
579d62501fSDavid E. O'Brien #ifndef ORDER
589d62501fSDavid E. O'Brien #include "make.h"
599d62501fSDavid E. O'Brien #endif /* ORDER */
609d62501fSDavid E. O'Brien #include "hash.h"
619d62501fSDavid E. O'Brien #include "ealloc.h"
629d62501fSDavid E. O'Brien
639d62501fSDavid E. O'Brien /*
649d62501fSDavid E. O'Brien * Forward references to local procedures that are used before they're
659d62501fSDavid E. O'Brien * defined:
669d62501fSDavid E. O'Brien */
679d62501fSDavid E. O'Brien
68784bddbcSKevin Lo static void RebuildTable(Hash_Table *);
699d62501fSDavid E. O'Brien
709d62501fSDavid E. O'Brien /*
719d62501fSDavid E. O'Brien * The following defines the ratio of # entries to # buckets
729d62501fSDavid E. O'Brien * at which we rebuild the table to make it larger.
739d62501fSDavid E. O'Brien */
749d62501fSDavid E. O'Brien
759d62501fSDavid E. O'Brien #define rebuildLimit 8
769d62501fSDavid E. O'Brien
779d62501fSDavid E. O'Brien /*
789d62501fSDavid E. O'Brien *---------------------------------------------------------
799d62501fSDavid E. O'Brien *
809d62501fSDavid E. O'Brien * Hash_InitTable --
819d62501fSDavid E. O'Brien *
829d62501fSDavid E. O'Brien * This routine just sets up the hash table.
839d62501fSDavid E. O'Brien *
849d62501fSDavid E. O'Brien * Results:
859d62501fSDavid E. O'Brien * None.
869d62501fSDavid E. O'Brien *
879d62501fSDavid E. O'Brien * Side Effects:
889d62501fSDavid E. O'Brien * Memory is allocated for the initial bucket area.
899d62501fSDavid E. O'Brien *
909d62501fSDavid E. O'Brien *---------------------------------------------------------
919d62501fSDavid E. O'Brien */
929d62501fSDavid E. O'Brien
939d62501fSDavid E. O'Brien void
Hash_InitTable(register Hash_Table * t,int numBuckets)9410bc3a7fSEd Schouten Hash_InitTable(
9510bc3a7fSEd Schouten register Hash_Table *t, /* Structure to use to hold table. */
9610bc3a7fSEd Schouten int numBuckets) /* How many buckets to create for starters.
979d62501fSDavid E. O'Brien * This number is rounded up to a power of
989d62501fSDavid E. O'Brien * two. If <= 0, a reasonable default is
999d62501fSDavid E. O'Brien * chosen. The table will grow in size later
1009d62501fSDavid E. O'Brien * as needed. */
1019d62501fSDavid E. O'Brien {
1029d62501fSDavid E. O'Brien register int i;
1039d62501fSDavid E. O'Brien register struct Hash_Entry **hp;
1049d62501fSDavid E. O'Brien
1059d62501fSDavid E. O'Brien /*
1069d62501fSDavid E. O'Brien * Round up the size to a power of two.
1079d62501fSDavid E. O'Brien */
1089d62501fSDavid E. O'Brien if (numBuckets <= 0)
1099d62501fSDavid E. O'Brien i = 16;
1109d62501fSDavid E. O'Brien else {
1119d62501fSDavid E. O'Brien for (i = 2; i < numBuckets; i <<= 1)
1129d62501fSDavid E. O'Brien continue;
1139d62501fSDavid E. O'Brien }
1149d62501fSDavid E. O'Brien t->numEntries = 0;
1159d62501fSDavid E. O'Brien t->size = i;
1169d62501fSDavid E. O'Brien t->mask = i - 1;
1179d62501fSDavid E. O'Brien t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
1189d62501fSDavid E. O'Brien while (--i >= 0)
1199d62501fSDavid E. O'Brien *hp++ = NULL;
1209d62501fSDavid E. O'Brien }
1219d62501fSDavid E. O'Brien
1229d62501fSDavid E. O'Brien /*
1239d62501fSDavid E. O'Brien *---------------------------------------------------------
1249d62501fSDavid E. O'Brien *
1259d62501fSDavid E. O'Brien * Hash_DeleteTable --
1269d62501fSDavid E. O'Brien *
1279d62501fSDavid E. O'Brien * This routine removes everything from a hash table
1289d62501fSDavid E. O'Brien * and frees up the memory space it occupied (except for
1299d62501fSDavid E. O'Brien * the space in the Hash_Table structure).
1309d62501fSDavid E. O'Brien *
1319d62501fSDavid E. O'Brien * Results:
1329d62501fSDavid E. O'Brien * None.
1339d62501fSDavid E. O'Brien *
1349d62501fSDavid E. O'Brien * Side Effects:
1359d62501fSDavid E. O'Brien * Lots of memory is freed up.
1369d62501fSDavid E. O'Brien *
1379d62501fSDavid E. O'Brien *---------------------------------------------------------
1389d62501fSDavid E. O'Brien */
1399d62501fSDavid E. O'Brien
1409d62501fSDavid E. O'Brien void
Hash_DeleteTable(Hash_Table * t)14110bc3a7fSEd Schouten Hash_DeleteTable(Hash_Table *t)
1429d62501fSDavid E. O'Brien {
1439d62501fSDavid E. O'Brien register struct Hash_Entry **hp, *h, *nexth = NULL;
1449d62501fSDavid E. O'Brien register int i;
1459d62501fSDavid E. O'Brien
1469d62501fSDavid E. O'Brien for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
1479d62501fSDavid E. O'Brien for (h = *hp++; h != NULL; h = nexth) {
1489d62501fSDavid E. O'Brien nexth = h->next;
1499d62501fSDavid E. O'Brien free((char *)h);
1509d62501fSDavid E. O'Brien }
1519d62501fSDavid E. O'Brien }
1529d62501fSDavid E. O'Brien free((char *)t->bucketPtr);
1539d62501fSDavid E. O'Brien
1549d62501fSDavid E. O'Brien /*
1559d62501fSDavid E. O'Brien * Set up the hash table to cause memory faults on any future access
1569d62501fSDavid E. O'Brien * attempts until re-initialization.
1579d62501fSDavid E. O'Brien */
1589d62501fSDavid E. O'Brien t->bucketPtr = NULL;
1599d62501fSDavid E. O'Brien }
1609d62501fSDavid E. O'Brien
1619d62501fSDavid E. O'Brien /*
1629d62501fSDavid E. O'Brien *---------------------------------------------------------
1639d62501fSDavid E. O'Brien *
1649d62501fSDavid E. O'Brien * Hash_FindEntry --
1659d62501fSDavid E. O'Brien *
1669d62501fSDavid E. O'Brien * Searches a hash table for an entry corresponding to key.
1679d62501fSDavid E. O'Brien *
1689d62501fSDavid E. O'Brien * Results:
1699d62501fSDavid E. O'Brien * The return value is a pointer to the entry for key,
1709d62501fSDavid E. O'Brien * if key was present in the table. If key was not
1719d62501fSDavid E. O'Brien * present, NULL is returned.
1729d62501fSDavid E. O'Brien *
1739d62501fSDavid E. O'Brien * Side Effects:
1749d62501fSDavid E. O'Brien * None.
1759d62501fSDavid E. O'Brien *
1769d62501fSDavid E. O'Brien *---------------------------------------------------------
1779d62501fSDavid E. O'Brien */
1789d62501fSDavid E. O'Brien
1799d62501fSDavid E. O'Brien Hash_Entry *
Hash_FindEntry(Hash_Table * t,char * key)18010bc3a7fSEd Schouten Hash_FindEntry(
18110bc3a7fSEd Schouten Hash_Table *t, /* Hash table to search. */
18210bc3a7fSEd Schouten char *key) /* A hash key. */
1839d62501fSDavid E. O'Brien {
1849d62501fSDavid E. O'Brien register Hash_Entry *e;
1859d62501fSDavid E. O'Brien register unsigned h;
1869d62501fSDavid E. O'Brien register char *p;
1879d62501fSDavid E. O'Brien
1889d62501fSDavid E. O'Brien for (h = 0, p = key; *p;)
1899d62501fSDavid E. O'Brien h = (h << 5) - h + *p++;
1909d62501fSDavid E. O'Brien p = key;
1919d62501fSDavid E. O'Brien for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
1929d62501fSDavid E. O'Brien if (e->namehash == h && strcmp(e->name, p) == 0)
1939d62501fSDavid E. O'Brien return (e);
1949d62501fSDavid E. O'Brien return (NULL);
1959d62501fSDavid E. O'Brien }
1969d62501fSDavid E. O'Brien
1979d62501fSDavid E. O'Brien /*
1989d62501fSDavid E. O'Brien *---------------------------------------------------------
1999d62501fSDavid E. O'Brien *
2009d62501fSDavid E. O'Brien * Hash_CreateEntry --
2019d62501fSDavid E. O'Brien *
2029d62501fSDavid E. O'Brien * Searches a hash table for an entry corresponding to
2039d62501fSDavid E. O'Brien * key. If no entry is found, then one is created.
2049d62501fSDavid E. O'Brien *
2059d62501fSDavid E. O'Brien * Results:
2069d62501fSDavid E. O'Brien * The return value is a pointer to the entry. If *newPtr
2079d62501fSDavid E. O'Brien * isn't NULL, then *newPtr is filled in with TRUE if a
2089d62501fSDavid E. O'Brien * new entry was created, and FALSE if an entry already existed
2099d62501fSDavid E. O'Brien * with the given key.
2109d62501fSDavid E. O'Brien *
2119d62501fSDavid E. O'Brien * Side Effects:
2129d62501fSDavid E. O'Brien * Memory may be allocated, and the hash buckets may be modified.
2139d62501fSDavid E. O'Brien *---------------------------------------------------------
2149d62501fSDavid E. O'Brien */
2159d62501fSDavid E. O'Brien
2169d62501fSDavid E. O'Brien Hash_Entry *
Hash_CreateEntry(register Hash_Table * t,char * key,Boolean * newPtr)21710bc3a7fSEd Schouten Hash_CreateEntry(
21810bc3a7fSEd Schouten register Hash_Table *t, /* Hash table to search. */
21910bc3a7fSEd Schouten char *key, /* A hash key. */
22010bc3a7fSEd Schouten Boolean *newPtr) /* Filled in with TRUE if new entry created,
2219d62501fSDavid E. O'Brien * FALSE otherwise. */
2229d62501fSDavid E. O'Brien {
2239d62501fSDavid E. O'Brien register Hash_Entry *e;
2249d62501fSDavid E. O'Brien register unsigned h;
2259d62501fSDavid E. O'Brien register char *p;
2269d62501fSDavid E. O'Brien int keylen;
2279d62501fSDavid E. O'Brien struct Hash_Entry **hp;
2289d62501fSDavid E. O'Brien
2299d62501fSDavid E. O'Brien /*
2309d62501fSDavid E. O'Brien * Hash the key. As a side effect, save the length (strlen) of the
2319d62501fSDavid E. O'Brien * key in case we need to create the entry.
2329d62501fSDavid E. O'Brien */
2339d62501fSDavid E. O'Brien for (h = 0, p = key; *p;)
2349d62501fSDavid E. O'Brien h = (h << 5) - h + *p++;
2359d62501fSDavid E. O'Brien keylen = p - key;
2369d62501fSDavid E. O'Brien p = key;
2379d62501fSDavid E. O'Brien for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
2389d62501fSDavid E. O'Brien if (e->namehash == h && strcmp(e->name, p) == 0) {
2399d62501fSDavid E. O'Brien if (newPtr != NULL)
2409d62501fSDavid E. O'Brien *newPtr = FALSE;
2419d62501fSDavid E. O'Brien return (e);
2429d62501fSDavid E. O'Brien }
2439d62501fSDavid E. O'Brien }
2449d62501fSDavid E. O'Brien
2459d62501fSDavid E. O'Brien /*
2469d62501fSDavid E. O'Brien * The desired entry isn't there. Before allocating a new entry,
2479d62501fSDavid E. O'Brien * expand the table if necessary (and this changes the resulting
2489d62501fSDavid E. O'Brien * bucket chain).
2499d62501fSDavid E. O'Brien */
2509d62501fSDavid E. O'Brien if (t->numEntries >= rebuildLimit * t->size)
2519d62501fSDavid E. O'Brien RebuildTable(t);
2529d62501fSDavid E. O'Brien e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
2539d62501fSDavid E. O'Brien hp = &t->bucketPtr[h & t->mask];
2549d62501fSDavid E. O'Brien e->next = *hp;
2559d62501fSDavid E. O'Brien *hp = e;
2569d62501fSDavid E. O'Brien e->clientData = NULL;
2579d62501fSDavid E. O'Brien e->namehash = h;
2589d62501fSDavid E. O'Brien (void) strcpy(e->name, p);
2599d62501fSDavid E. O'Brien t->numEntries++;
2609d62501fSDavid E. O'Brien
2619d62501fSDavid E. O'Brien if (newPtr != NULL)
2629d62501fSDavid E. O'Brien *newPtr = TRUE;
2639d62501fSDavid E. O'Brien return (e);
2649d62501fSDavid E. O'Brien }
2659d62501fSDavid E. O'Brien
2669d62501fSDavid E. O'Brien /*
2679d62501fSDavid E. O'Brien *---------------------------------------------------------
2689d62501fSDavid E. O'Brien *
2699d62501fSDavid E. O'Brien * Hash_DeleteEntry --
2709d62501fSDavid E. O'Brien *
2719d62501fSDavid E. O'Brien * Delete the given hash table entry and free memory associated with
2729d62501fSDavid E. O'Brien * it.
2739d62501fSDavid E. O'Brien *
2749d62501fSDavid E. O'Brien * Results:
2759d62501fSDavid E. O'Brien * None.
2769d62501fSDavid E. O'Brien *
2779d62501fSDavid E. O'Brien * Side Effects:
2789d62501fSDavid E. O'Brien * Hash chain that entry lives in is modified and memory is freed.
2799d62501fSDavid E. O'Brien *
2809d62501fSDavid E. O'Brien *---------------------------------------------------------
2819d62501fSDavid E. O'Brien */
2829d62501fSDavid E. O'Brien
2839d62501fSDavid E. O'Brien void
Hash_DeleteEntry(Hash_Table * t,Hash_Entry * e)28410bc3a7fSEd Schouten Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
2859d62501fSDavid E. O'Brien {
2869d62501fSDavid E. O'Brien register Hash_Entry **hp, *p;
2879d62501fSDavid E. O'Brien
2889d62501fSDavid E. O'Brien if (e == NULL)
2899d62501fSDavid E. O'Brien return;
2909d62501fSDavid E. O'Brien for (hp = &t->bucketPtr[e->namehash & t->mask];
2919d62501fSDavid E. O'Brien (p = *hp) != NULL; hp = &p->next) {
2929d62501fSDavid E. O'Brien if (p == e) {
2939d62501fSDavid E. O'Brien *hp = p->next;
2949d62501fSDavid E. O'Brien free((char *)p);
2959d62501fSDavid E. O'Brien t->numEntries--;
2969d62501fSDavid E. O'Brien return;
2979d62501fSDavid E. O'Brien }
2989d62501fSDavid E. O'Brien }
2999d62501fSDavid E. O'Brien (void)write(2, "bad call to Hash_DeleteEntry\n", 29);
3009d62501fSDavid E. O'Brien abort();
3019d62501fSDavid E. O'Brien }
3029d62501fSDavid E. O'Brien
3039d62501fSDavid E. O'Brien /*
3049d62501fSDavid E. O'Brien *---------------------------------------------------------
3059d62501fSDavid E. O'Brien *
3069d62501fSDavid E. O'Brien * Hash_EnumFirst --
3079d62501fSDavid E. O'Brien * This procedure sets things up for a complete search
3089d62501fSDavid E. O'Brien * of all entries recorded in the hash table.
3099d62501fSDavid E. O'Brien *
3109d62501fSDavid E. O'Brien * Results:
3119d62501fSDavid E. O'Brien * The return value is the address of the first entry in
3129d62501fSDavid E. O'Brien * the hash table, or NULL if the table is empty.
3139d62501fSDavid E. O'Brien *
3149d62501fSDavid E. O'Brien * Side Effects:
3159d62501fSDavid E. O'Brien * The information in searchPtr is initialized so that successive
3169d62501fSDavid E. O'Brien * calls to Hash_Next will return successive HashEntry's
3179d62501fSDavid E. O'Brien * from the table.
3189d62501fSDavid E. O'Brien *
3199d62501fSDavid E. O'Brien *---------------------------------------------------------
3209d62501fSDavid E. O'Brien */
3219d62501fSDavid E. O'Brien
3229d62501fSDavid E. O'Brien Hash_Entry *
Hash_EnumFirst(Hash_Table * t,register Hash_Search * searchPtr)32310bc3a7fSEd Schouten Hash_EnumFirst(
32410bc3a7fSEd Schouten Hash_Table *t, /* Table to be searched. */
32510bc3a7fSEd Schouten register Hash_Search *searchPtr)/* Area in which to keep state
3269d62501fSDavid E. O'Brien * about search.*/
3279d62501fSDavid E. O'Brien {
3289d62501fSDavid E. O'Brien searchPtr->tablePtr = t;
3299d62501fSDavid E. O'Brien searchPtr->nextIndex = 0;
3309d62501fSDavid E. O'Brien searchPtr->hashEntryPtr = NULL;
3319d62501fSDavid E. O'Brien return Hash_EnumNext(searchPtr);
3329d62501fSDavid E. O'Brien }
3339d62501fSDavid E. O'Brien
3349d62501fSDavid E. O'Brien /*
3359d62501fSDavid E. O'Brien *---------------------------------------------------------
3369d62501fSDavid E. O'Brien *
3379d62501fSDavid E. O'Brien * Hash_EnumNext --
3389d62501fSDavid E. O'Brien * This procedure returns successive entries in the hash table.
3399d62501fSDavid E. O'Brien *
3409d62501fSDavid E. O'Brien * Results:
3419d62501fSDavid E. O'Brien * The return value is a pointer to the next HashEntry
3429d62501fSDavid E. O'Brien * in the table, or NULL when the end of the table is
3439d62501fSDavid E. O'Brien * reached.
3449d62501fSDavid E. O'Brien *
3459d62501fSDavid E. O'Brien * Side Effects:
3469d62501fSDavid E. O'Brien * The information in searchPtr is modified to advance to the
3479d62501fSDavid E. O'Brien * next entry.
3489d62501fSDavid E. O'Brien *
3499d62501fSDavid E. O'Brien *---------------------------------------------------------
3509d62501fSDavid E. O'Brien */
3519d62501fSDavid E. O'Brien
3529d62501fSDavid E. O'Brien Hash_Entry *
Hash_EnumNext(register Hash_Search * searchPtr)35310bc3a7fSEd Schouten Hash_EnumNext(
35410bc3a7fSEd Schouten register Hash_Search *searchPtr) /* Area used to keep state about
3559d62501fSDavid E. O'Brien search. */
3569d62501fSDavid E. O'Brien {
3579d62501fSDavid E. O'Brien register Hash_Entry *e;
3589d62501fSDavid E. O'Brien Hash_Table *t = searchPtr->tablePtr;
3599d62501fSDavid E. O'Brien
3609d62501fSDavid E. O'Brien /*
3619d62501fSDavid E. O'Brien * The hashEntryPtr field points to the most recently returned
3629d62501fSDavid E. O'Brien * entry, or is nil if we are starting up. If not nil, we have
3639d62501fSDavid E. O'Brien * to start at the next one in the chain.
3649d62501fSDavid E. O'Brien */
3659d62501fSDavid E. O'Brien e = searchPtr->hashEntryPtr;
3669d62501fSDavid E. O'Brien if (e != NULL)
3679d62501fSDavid E. O'Brien e = e->next;
3689d62501fSDavid E. O'Brien /*
3699d62501fSDavid E. O'Brien * If the chain ran out, or if we are starting up, we need to
3709d62501fSDavid E. O'Brien * find the next nonempty chain.
3719d62501fSDavid E. O'Brien */
3729d62501fSDavid E. O'Brien while (e == NULL) {
3739d62501fSDavid E. O'Brien if (searchPtr->nextIndex >= t->size)
3749d62501fSDavid E. O'Brien return (NULL);
3759d62501fSDavid E. O'Brien e = t->bucketPtr[searchPtr->nextIndex++];
3769d62501fSDavid E. O'Brien }
3779d62501fSDavid E. O'Brien searchPtr->hashEntryPtr = e;
3789d62501fSDavid E. O'Brien return (e);
3799d62501fSDavid E. O'Brien }
3809d62501fSDavid E. O'Brien
3819d62501fSDavid E. O'Brien /*
3829d62501fSDavid E. O'Brien *---------------------------------------------------------
3839d62501fSDavid E. O'Brien *
3849d62501fSDavid E. O'Brien * RebuildTable --
3859d62501fSDavid E. O'Brien * This local routine makes a new hash table that
3869d62501fSDavid E. O'Brien * is larger than the old one.
3879d62501fSDavid E. O'Brien *
3889d62501fSDavid E. O'Brien * Results:
3899d62501fSDavid E. O'Brien * None.
3909d62501fSDavid E. O'Brien *
3919d62501fSDavid E. O'Brien * Side Effects:
3929d62501fSDavid E. O'Brien * The entire hash table is moved, so any bucket numbers
3939d62501fSDavid E. O'Brien * from the old table are invalid.
3949d62501fSDavid E. O'Brien *
3959d62501fSDavid E. O'Brien *---------------------------------------------------------
3969d62501fSDavid E. O'Brien */
3979d62501fSDavid E. O'Brien
3989d62501fSDavid E. O'Brien static void
RebuildTable(register Hash_Table * t)39910bc3a7fSEd Schouten RebuildTable(register Hash_Table *t)
4009d62501fSDavid E. O'Brien {
4019d62501fSDavid E. O'Brien register Hash_Entry *e, *next = NULL, **hp, **xp;
4029d62501fSDavid E. O'Brien register int i, mask;
4039d62501fSDavid E. O'Brien register Hash_Entry **oldhp;
4049d62501fSDavid E. O'Brien int oldsize;
4059d62501fSDavid E. O'Brien
4069d62501fSDavid E. O'Brien oldhp = t->bucketPtr;
4079d62501fSDavid E. O'Brien oldsize = i = t->size;
4089d62501fSDavid E. O'Brien i <<= 1;
4099d62501fSDavid E. O'Brien t->size = i;
4109d62501fSDavid E. O'Brien t->mask = mask = i - 1;
4119d62501fSDavid E. O'Brien t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
4129d62501fSDavid E. O'Brien while (--i >= 0)
4139d62501fSDavid E. O'Brien *hp++ = NULL;
4149d62501fSDavid E. O'Brien for (hp = oldhp, i = oldsize; --i >= 0;) {
4159d62501fSDavid E. O'Brien for (e = *hp++; e != NULL; e = next) {
4169d62501fSDavid E. O'Brien next = e->next;
4179d62501fSDavid E. O'Brien xp = &t->bucketPtr[e->namehash & mask];
4189d62501fSDavid E. O'Brien e->next = *xp;
4199d62501fSDavid E. O'Brien *xp = e;
4209d62501fSDavid E. O'Brien }
4219d62501fSDavid E. O'Brien }
4229d62501fSDavid E. O'Brien free((char *)oldhp);
4239d62501fSDavid E. O'Brien }
424