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