2021-01-14 01:24:34 +00:00
|
|
|
/* $NetBSD: hash.h,v 1.38 2020/12/15 01:23:55 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* from: @(#)hash.h 8.1 (Berkeley) 6/6/93
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-06-08 21:57:36 +00:00
|
|
|
* from: @(#)hash.h 8.1 (Berkeley) 6/6/93
|
1996-10-06 15:57:15 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
/* Hash tables with strings as keys and arbitrary pointers as values. */
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
#ifndef MAKE_HASH_H
|
|
|
|
#define MAKE_HASH_H
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
/* A single key-value entry in the hash table. */
|
2020-11-07 19:39:21 +00:00
|
|
|
typedef struct HashEntry {
|
2021-01-14 01:24:34 +00:00
|
|
|
struct HashEntry *next; /* Used to link together all the entries
|
2020-09-05 16:11:04 +00:00
|
|
|
* associated with the same bucket. */
|
2021-01-14 01:24:34 +00:00
|
|
|
void *value;
|
|
|
|
unsigned int key_hash; /* hash value of the key */
|
|
|
|
char key[1]; /* key string, variable length */
|
2020-11-07 19:39:21 +00:00
|
|
|
} HashEntry;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
/* The hash table containing the entries. */
|
2020-11-07 19:39:21 +00:00
|
|
|
typedef struct HashTable {
|
2021-01-14 01:24:34 +00:00
|
|
|
HashEntry **buckets; /* Pointers to HashEntry, one
|
2020-09-05 16:11:04 +00:00
|
|
|
* for each bucket in the table. */
|
2021-01-14 01:24:34 +00:00
|
|
|
unsigned int bucketsSize;
|
|
|
|
unsigned int numEntries; /* Number of entries in the table. */
|
|
|
|
unsigned int bucketsMask; /* Used to select the bucket for a hash. */
|
|
|
|
unsigned int maxchain; /* max length of chain detected */
|
2020-11-07 19:39:21 +00:00
|
|
|
} HashTable;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* State of an iteration over all entries in a table. */
|
|
|
|
typedef struct HashIter {
|
2021-01-14 01:24:34 +00:00
|
|
|
HashTable *table; /* Table being searched. */
|
|
|
|
unsigned int nextBucket; /* Next bucket to check (after current). */
|
|
|
|
HashEntry *entry; /* Next entry to check in current bucket. */
|
2020-11-07 19:39:21 +00:00
|
|
|
} HashIter;
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* A set of strings. */
|
|
|
|
typedef struct HashSet {
|
|
|
|
HashTable tbl;
|
|
|
|
} HashSet;
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
MAKE_INLINE void *
|
2020-11-07 19:39:21 +00:00
|
|
|
HashEntry_Get(HashEntry *h)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
return h->value;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
MAKE_INLINE void
|
2020-11-07 19:39:21 +00:00
|
|
|
HashEntry_Set(HashEntry *h, void *datum)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
h->value = datum;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
void HashTable_Init(HashTable *);
|
|
|
|
void HashTable_Done(HashTable *);
|
|
|
|
HashEntry *HashTable_FindEntry(HashTable *, const char *);
|
|
|
|
void *HashTable_FindValue(HashTable *, const char *);
|
|
|
|
unsigned int Hash_Hash(const char *);
|
|
|
|
void *HashTable_FindValueHash(HashTable *, const char *, unsigned int);
|
|
|
|
HashEntry *HashTable_CreateEntry(HashTable *, const char *, Boolean *);
|
2020-11-20 03:54:37 +00:00
|
|
|
HashEntry *HashTable_Set(HashTable *, const char *, void *);
|
2020-11-07 19:39:21 +00:00
|
|
|
void HashTable_DeleteEntry(HashTable *, HashEntry *);
|
|
|
|
void HashTable_DebugStats(HashTable *, const char *);
|
|
|
|
|
|
|
|
void HashIter_Init(HashIter *, HashTable *);
|
|
|
|
HashEntry *HashIter_Next(HashIter *);
|
1996-10-06 15:57:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
MAKE_INLINE void
|
|
|
|
HashSet_Init(HashSet *set)
|
|
|
|
{
|
|
|
|
HashTable_Init(&set->tbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_INLINE void
|
|
|
|
HashSet_Done(HashSet *set)
|
|
|
|
{
|
|
|
|
HashTable_Done(&set->tbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_INLINE Boolean
|
|
|
|
HashSet_Add(HashSet *set, const char *key)
|
|
|
|
{
|
|
|
|
Boolean isNew;
|
|
|
|
|
|
|
|
(void)HashTable_CreateEntry(&set->tbl, key, &isNew);
|
|
|
|
return isNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_INLINE Boolean
|
|
|
|
HashSet_Contains(HashSet *set, const char *key)
|
|
|
|
{
|
|
|
|
return HashTable_FindEntry(&set->tbl, key) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_INLINE void
|
|
|
|
HashIter_InitSet(HashIter *hi, HashSet *set)
|
|
|
|
{
|
|
|
|
HashIter_Init(hi, &set->tbl);
|
|
|
|
}
|
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
#endif /* MAKE_HASH_H */
|