/* $NetBSD: hash.h,v 1.52 2025/04/22 19:28:50 rillig Exp $ */12/*3* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.4*5* This code is derived from software contributed to Berkeley by6* Adam de Boor.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*32* from: @(#)hash.h 8.1 (Berkeley) 6/6/9333*/3435/*36* Copyright (c) 1988, 1989 by Adam de Boor37* Copyright (c) 1989 by Berkeley Softworks38* All rights reserved.39*40* This code is derived from software contributed to Berkeley by41* Adam de Boor.42*43* Redistribution and use in source and binary forms, with or without44* modification, are permitted provided that the following conditions45* are met:46* 1. Redistributions of source code must retain the above copyright47* notice, this list of conditions and the following disclaimer.48* 2. Redistributions in binary form must reproduce the above copyright49* notice, this list of conditions and the following disclaimer in the50* documentation and/or other materials provided with the distribution.51* 3. All advertising materials mentioning features or use of this software52* must display the following acknowledgement:53* This product includes software developed by the University of54* California, Berkeley and its contributors.55* 4. Neither the name of the University nor the names of its contributors56* may be used to endorse or promote products derived from this software57* without specific prior written permission.58*59* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND60* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE61* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE62* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE63* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL64* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS65* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)66* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT67* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY68* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF69* SUCH DAMAGE.70*71* from: @(#)hash.h 8.1 (Berkeley) 6/6/9372*/7374/* Hash tables with string keys and pointer values. */7576#ifndef MAKE_HASH_H77#define MAKE_HASH_H7879/* A single key-value entry in the hash table. */80typedef struct HashEntry {81struct HashEntry *next; /* Used to link together all the entries82* associated with the same bucket. */83void *value;84unsigned hash; /* hash value of the key */85char key[1]; /* key string, variable length */86} HashEntry;8788/* The hash table containing the entries. */89typedef struct HashTable {90HashEntry **buckets;91unsigned bucketsSize;92unsigned numEntries;93unsigned bucketsMask; /* Used to select the bucket for a hash. */94} HashTable;9596/* State of an iteration over all entries in a table. */97typedef struct HashIter {98HashTable *table; /* Table being searched. */99unsigned nextBucket; /* Next bucket to check (after current). */100HashEntry *entry; /* Next entry to check in current bucket. */101} HashIter;102103/* A set of strings. */104typedef struct HashSet {105HashTable tbl;106} HashSet;107108MAKE_INLINE void * MAKE_ATTR_USE109HashEntry_Get(HashEntry *he)110{111return he->value;112}113114MAKE_INLINE void115HashEntry_Set(HashEntry *he, void *datum)116{117he->value = datum;118}119120/* Set things up for iterating over all entries in the hash table. */121MAKE_INLINE void122HashIter_Init(HashIter *hi, HashTable *t)123{124hi->table = t;125hi->nextBucket = 0;126hi->entry = NULL;127}128129void HashTable_Init(HashTable *);130void HashTable_Done(HashTable *);131HashEntry *HashTable_FindEntry(HashTable *, const char *) MAKE_ATTR_USE;132void *HashTable_FindValue(HashTable *, const char *) MAKE_ATTR_USE;133unsigned Hash_Substring(Substring) MAKE_ATTR_USE;134void *HashTable_FindValueBySubstringHash(HashTable *, Substring, unsigned)135MAKE_ATTR_USE;136HashEntry *HashTable_CreateEntry(HashTable *, const char *, bool *);137void HashTable_Set(HashTable *, const char *, void *);138void HashTable_DeleteEntry(HashTable *, HashEntry *);139void HashTable_DebugStats(const HashTable *, const char *);140141bool HashIter_Next(HashIter *) MAKE_ATTR_USE;142143MAKE_INLINE void144HashSet_Init(HashSet *set)145{146HashTable_Init(&set->tbl);147}148149MAKE_INLINE void150HashSet_Done(HashSet *set)151{152HashTable_Done(&set->tbl);153}154155MAKE_INLINE bool156HashSet_Add(HashSet *set, const char *key)157{158bool isNew;159160(void)HashTable_CreateEntry(&set->tbl, key, &isNew);161return isNew;162}163164MAKE_INLINE bool MAKE_ATTR_USE165HashSet_Contains(HashSet *set, const char *key)166{167return HashTable_FindEntry(&set->tbl, key) != NULL;168}169170MAKE_INLINE void171HashIter_InitSet(HashIter *hi, HashSet *set)172{173HashIter_Init(hi, &set->tbl);174}175176#endif177178179