2020-11-07 19:39:21 +00:00
|
|
|
/* $NetBSD: hash.c,v 1.55 2020/10/25 19:28:44 rillig Exp $ */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
1996-10-06 15:57:15 +00:00
|
|
|
/*
|
2012-06-08 21:57:36 +00:00
|
|
|
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1989 by Adam de Boor
|
1996-10-06 15:57:15 +00:00
|
|
|
* Copyright (c) 1989 by Berkeley Softworks
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Hash tables with string keys. */
|
1996-10-06 15:57:15 +00:00
|
|
|
|
|
|
|
#include "make.h"
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* "@(#)hash.c 8.1 (Berkeley) 6/6/93" */
|
|
|
|
MAKE_RCSID("$NetBSD: hash.c,v 1.55 2020/10/25 19:28:44 rillig Exp $");
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2020-11-07 19:39:21 +00:00
|
|
|
* The ratio of # entries to # buckets at which we rebuild the table to
|
|
|
|
* make it larger.
|
1996-10-06 15:57:15 +00:00
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
#define rebuildLimit 3
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* This hash function matches Gosling's emacs and java.lang.String. */
|
|
|
|
static unsigned int
|
|
|
|
hash(const char *key, size_t *out_keylen)
|
|
|
|
{
|
|
|
|
unsigned int h = 0;
|
|
|
|
const char *p = key;
|
|
|
|
while (*p != '\0')
|
|
|
|
h = (h << 5) - h + (unsigned char)*p++;
|
|
|
|
if (out_keylen != NULL)
|
|
|
|
*out_keylen = (size_t)(p - key);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
Hash_Hash(const char *key)
|
|
|
|
{
|
|
|
|
return hash(key, NULL);
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static HashEntry *
|
|
|
|
HashTable_Find(HashTable *t, unsigned int h, const char *key)
|
|
|
|
{
|
|
|
|
HashEntry *e;
|
|
|
|
unsigned int chainlen = 0;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
#ifdef DEBUG_HASH_LOOKUP
|
|
|
|
DEBUG4(HASH, "%s: %p h=%08x key=%s\n", __func__, t, h, key);
|
2020-09-05 16:11:04 +00:00
|
|
|
#endif
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
|
|
|
|
chainlen++;
|
|
|
|
if (e->key_hash == h && strcmp(e->key, key) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chainlen > t->maxchain)
|
|
|
|
t->maxchain = chainlen;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the hash table. */
|
1996-10-06 15:57:15 +00:00
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
HashTable_Init(HashTable *t)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
unsigned int n = 16, i;
|
|
|
|
HashEntry **buckets = bmake_malloc(sizeof(*buckets) * n);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
buckets[i] = NULL;
|
|
|
|
|
|
|
|
t->buckets = buckets;
|
|
|
|
t->bucketsSize = n;
|
1996-10-06 15:57:15 +00:00
|
|
|
t->numEntries = 0;
|
2020-11-07 19:39:21 +00:00
|
|
|
t->bucketsMask = n - 1;
|
2020-09-05 16:11:04 +00:00
|
|
|
t->maxchain = 0;
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Remove everything from the hash table and frees up the memory. */
|
1996-10-06 15:57:15 +00:00
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
HashTable_Done(HashTable *t)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
HashEntry **buckets = t->buckets;
|
|
|
|
size_t i, n = t->bucketsSize;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
HashEntry *he = buckets[i];
|
|
|
|
while (he != NULL) {
|
|
|
|
HashEntry *next = he->next;
|
|
|
|
free(he);
|
|
|
|
he = next;
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
free(t->buckets);
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
#ifdef CLEANUP
|
2020-09-05 16:11:04 +00:00
|
|
|
t->buckets = NULL;
|
2020-11-07 19:39:21 +00:00
|
|
|
#endif
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Find the entry corresponding to the key, or return NULL. */
|
|
|
|
HashEntry *
|
|
|
|
HashTable_FindEntry(HashTable *t, const char *key)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
unsigned int h = hash(key, NULL);
|
|
|
|
return HashTable_Find(t, h, key);
|
|
|
|
}
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Find the value corresponding to the key, or return NULL. */
|
|
|
|
void *
|
|
|
|
HashTable_FindValue(HashTable *t, const char *key)
|
|
|
|
{
|
|
|
|
HashEntry *he = HashTable_FindEntry(t, key);
|
|
|
|
return he != NULL ? he->value : NULL;
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Find the value corresponding to the key and the precomputed hash,
|
|
|
|
* or return NULL. */
|
|
|
|
void *
|
|
|
|
HashTable_FindValueHash(HashTable *t, const char *key, unsigned int h)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
HashEntry *he = HashTable_Find(t, h, key);
|
|
|
|
return he != NULL ? he->value : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the hash table larger. Any bucket numbers from the old table become
|
|
|
|
* invalid; the hash codes stay valid though. */
|
|
|
|
static void
|
|
|
|
HashTable_Enlarge(HashTable *t)
|
|
|
|
{
|
|
|
|
unsigned int oldSize = t->bucketsSize;
|
|
|
|
HashEntry **oldBuckets = t->buckets;
|
|
|
|
unsigned int newSize = 2 * oldSize;
|
|
|
|
unsigned int newMask = newSize - 1;
|
|
|
|
HashEntry **newBuckets = bmake_malloc(sizeof(*newBuckets) * newSize);
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < newSize; i++)
|
|
|
|
newBuckets[i] = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < oldSize; i++) {
|
|
|
|
HashEntry *he = oldBuckets[i];
|
|
|
|
while (he != NULL) {
|
|
|
|
HashEntry *next = he->next;
|
|
|
|
he->next = newBuckets[he->key_hash & newMask];
|
|
|
|
newBuckets[he->key_hash & newMask] = he;
|
|
|
|
he = next;
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
|
|
|
|
free(oldBuckets);
|
|
|
|
|
|
|
|
t->bucketsSize = newSize;
|
|
|
|
t->bucketsMask = newMask;
|
|
|
|
t->buckets = newBuckets;
|
|
|
|
DEBUG5(HASH, "%s: %p size=%d entries=%d maxchain=%d\n",
|
|
|
|
__func__, t, t->bucketsSize, t->numEntries, t->maxchain);
|
|
|
|
t->maxchain = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find or create an entry corresponding to the key.
|
|
|
|
* Return in out_isNew whether a new entry has been created. */
|
|
|
|
HashEntry *
|
|
|
|
HashTable_CreateEntry(HashTable *t, const char *key, Boolean *out_isNew)
|
|
|
|
{
|
|
|
|
size_t keylen;
|
|
|
|
unsigned int h = hash(key, &keylen);
|
|
|
|
HashEntry *he = HashTable_Find(t, h, key);
|
|
|
|
|
|
|
|
if (he != NULL) {
|
|
|
|
if (out_isNew != NULL)
|
|
|
|
*out_isNew = FALSE;
|
|
|
|
return he;
|
|
|
|
}
|
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
if (t->numEntries >= rebuildLimit * t->bucketsSize)
|
2020-11-07 19:39:21 +00:00
|
|
|
HashTable_Enlarge(t);
|
|
|
|
|
|
|
|
he = bmake_malloc(sizeof(*he) + keylen);
|
|
|
|
he->value = NULL;
|
|
|
|
he->key_hash = h;
|
|
|
|
memcpy(he->key, key, keylen + 1);
|
|
|
|
|
|
|
|
he->next = t->buckets[h & t->bucketsMask];
|
|
|
|
t->buckets[h & t->bucketsMask] = he;
|
1996-10-06 15:57:15 +00:00
|
|
|
t->numEntries++;
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (out_isNew != NULL)
|
|
|
|
*out_isNew = TRUE;
|
|
|
|
return he;
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Delete the entry from the table and free the associated memory. */
|
1996-10-06 15:57:15 +00:00
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
HashTable_DeleteEntry(HashTable *t, HashEntry *he)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
HashEntry **ref = &t->buckets[he->key_hash & t->bucketsMask];
|
|
|
|
HashEntry *p;
|
|
|
|
|
|
|
|
for (; (p = *ref) != NULL; ref = &p->next) {
|
|
|
|
if (p == he) {
|
|
|
|
*ref = p->next;
|
2012-06-08 21:57:36 +00:00
|
|
|
free(p);
|
1996-10-06 15:57:15 +00:00
|
|
|
t->numEntries--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Set things up for iterating over all entries in the hash table. */
|
|
|
|
void
|
|
|
|
HashIter_Init(HashIter *hi, HashTable *t)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
hi->table = t;
|
|
|
|
hi->nextBucket = 0;
|
|
|
|
hi->entry = NULL;
|
1996-10-06 15:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Return the next entry in the hash table, or NULL if the end of the table
|
|
|
|
* is reached. */
|
|
|
|
HashEntry *
|
|
|
|
HashIter_Next(HashIter *hi)
|
1996-10-06 15:57:15 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
HashTable *t = hi->table;
|
|
|
|
HashEntry *he = hi->entry;
|
|
|
|
HashEntry **buckets = t->buckets;
|
|
|
|
unsigned int bucketsSize = t->bucketsSize;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (he != NULL)
|
|
|
|
he = he->next; /* skip the most recently returned entry */
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
while (he == NULL) { /* find the next nonempty chain */
|
|
|
|
if (hi->nextBucket >= bucketsSize)
|
|
|
|
return NULL;
|
|
|
|
he = buckets[hi->nextBucket++];
|
|
|
|
}
|
|
|
|
hi->entry = he;
|
|
|
|
return he;
|
2020-07-08 18:32:15 +00:00
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
|
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
HashTable_DebugStats(HashTable *t, const char *name)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG4(HASH, "HashTable %s: size=%u numEntries=%u maxchain=%u\n",
|
|
|
|
name, t->bucketsSize, t->numEntries, t->maxchain);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|