hash.h revision 1.52 1 1.52 rillig /* $NetBSD: hash.h,v 1.52 2025/04/22 19:28:50 rillig Exp $ */
2 1.4 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.8 agc *
6 1.8 agc * This code is derived from software contributed to Berkeley by
7 1.8 agc * Adam de Boor.
8 1.8 agc *
9 1.8 agc * Redistribution and use in source and binary forms, with or without
10 1.8 agc * modification, are permitted provided that the following conditions
11 1.8 agc * are met:
12 1.8 agc * 1. Redistributions of source code must retain the above copyright
13 1.8 agc * notice, this list of conditions and the following disclaimer.
14 1.8 agc * 2. Redistributions in binary form must reproduce the above copyright
15 1.8 agc * notice, this list of conditions and the following disclaimer in the
16 1.8 agc * documentation and/or other materials provided with the distribution.
17 1.8 agc * 3. Neither the name of the University nor the names of its contributors
18 1.8 agc * may be used to endorse or promote products derived from this software
19 1.8 agc * without specific prior written permission.
20 1.8 agc *
21 1.8 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.8 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.8 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.8 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.8 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.8 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.8 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.8 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.8 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.8 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.8 agc * SUCH DAMAGE.
32 1.8 agc *
33 1.8 agc * from: @(#)hash.h 8.1 (Berkeley) 6/6/93
34 1.8 agc */
35 1.8 agc
36 1.8 agc /*
37 1.1 cgd * Copyright (c) 1988, 1989 by Adam de Boor
38 1.1 cgd * Copyright (c) 1989 by Berkeley Softworks
39 1.1 cgd * All rights reserved.
40 1.1 cgd *
41 1.1 cgd * This code is derived from software contributed to Berkeley by
42 1.1 cgd * Adam de Boor.
43 1.1 cgd *
44 1.1 cgd * Redistribution and use in source and binary forms, with or without
45 1.1 cgd * modification, are permitted provided that the following conditions
46 1.1 cgd * are met:
47 1.1 cgd * 1. Redistributions of source code must retain the above copyright
48 1.1 cgd * notice, this list of conditions and the following disclaimer.
49 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 cgd * notice, this list of conditions and the following disclaimer in the
51 1.1 cgd * documentation and/or other materials provided with the distribution.
52 1.1 cgd * 3. All advertising materials mentioning features or use of this software
53 1.1 cgd * must display the following acknowledgement:
54 1.1 cgd * This product includes software developed by the University of
55 1.1 cgd * California, Berkeley and its contributors.
56 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
57 1.1 cgd * may be used to endorse or promote products derived from this software
58 1.1 cgd * without specific prior written permission.
59 1.1 cgd *
60 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.1 cgd * SUCH DAMAGE.
71 1.1 cgd *
72 1.5 christos * from: @(#)hash.h 8.1 (Berkeley) 6/6/93
73 1.1 cgd */
74 1.1 cgd
75 1.48 rillig /* Hash tables with string keys and pointer values. */
76 1.1 cgd
77 1.37 rillig #ifndef MAKE_HASH_H
78 1.37 rillig #define MAKE_HASH_H
79 1.1 cgd
80 1.21 rillig /* A single key-value entry in the hash table. */
81 1.28 rillig typedef struct HashEntry {
82 1.34 rillig struct HashEntry *next; /* Used to link together all the entries
83 1.21 rillig * associated with the same bucket. */
84 1.34 rillig void *value;
85 1.52 rillig unsigned hash; /* hash value of the key */
86 1.34 rillig char key[1]; /* key string, variable length */
87 1.28 rillig } HashEntry;
88 1.1 cgd
89 1.21 rillig /* The hash table containing the entries. */
90 1.28 rillig typedef struct HashTable {
91 1.48 rillig HashEntry **buckets;
92 1.52 rillig unsigned bucketsSize;
93 1.52 rillig unsigned numEntries;
94 1.52 rillig unsigned bucketsMask; /* Used to select the bucket for a hash. */
95 1.28 rillig } HashTable;
96 1.1 cgd
97 1.27 rillig /* State of an iteration over all entries in a table. */
98 1.27 rillig typedef struct HashIter {
99 1.34 rillig HashTable *table; /* Table being searched. */
100 1.52 rillig unsigned nextBucket; /* Next bucket to check (after current). */
101 1.34 rillig HashEntry *entry; /* Next entry to check in current bucket. */
102 1.27 rillig } HashIter;
103 1.1 cgd
104 1.35 rillig /* A set of strings. */
105 1.35 rillig typedef struct HashSet {
106 1.35 rillig HashTable tbl;
107 1.35 rillig } HashSet;
108 1.35 rillig
109 1.42 rillig MAKE_INLINE void * MAKE_ATTR_USE
110 1.47 rillig HashEntry_Get(HashEntry *he)
111 1.20 rillig {
112 1.47 rillig return he->value;
113 1.20 rillig }
114 1.20 rillig
115 1.32 rillig MAKE_INLINE void
116 1.47 rillig HashEntry_Set(HashEntry *he, void *datum)
117 1.20 rillig {
118 1.47 rillig he->value = datum;
119 1.20 rillig }
120 1.1 cgd
121 1.41 rillig /* Set things up for iterating over all entries in the hash table. */
122 1.41 rillig MAKE_INLINE void
123 1.41 rillig HashIter_Init(HashIter *hi, HashTable *t)
124 1.41 rillig {
125 1.41 rillig hi->table = t;
126 1.41 rillig hi->nextBucket = 0;
127 1.41 rillig hi->entry = NULL;
128 1.41 rillig }
129 1.41 rillig
130 1.31 rillig void HashTable_Init(HashTable *);
131 1.31 rillig void HashTable_Done(HashTable *);
132 1.42 rillig HashEntry *HashTable_FindEntry(HashTable *, const char *) MAKE_ATTR_USE;
133 1.42 rillig void *HashTable_FindValue(HashTable *, const char *) MAKE_ATTR_USE;
134 1.52 rillig unsigned Hash_Substring(Substring) MAKE_ATTR_USE;
135 1.52 rillig void *HashTable_FindValueBySubstringHash(HashTable *, Substring, unsigned)
136 1.42 rillig MAKE_ATTR_USE;
137 1.39 rillig HashEntry *HashTable_CreateEntry(HashTable *, const char *, bool *);
138 1.43 rillig void HashTable_Set(HashTable *, const char *, void *);
139 1.31 rillig void HashTable_DeleteEntry(HashTable *, HashEntry *);
140 1.51 rillig void HashTable_DebugStats(const HashTable *, const char *);
141 1.27 rillig
142 1.50 rillig bool HashIter_Next(HashIter *) MAKE_ATTR_USE;
143 1.27 rillig
144 1.35 rillig MAKE_INLINE void
145 1.35 rillig HashSet_Init(HashSet *set)
146 1.35 rillig {
147 1.35 rillig HashTable_Init(&set->tbl);
148 1.35 rillig }
149 1.35 rillig
150 1.35 rillig MAKE_INLINE void
151 1.35 rillig HashSet_Done(HashSet *set)
152 1.35 rillig {
153 1.35 rillig HashTable_Done(&set->tbl);
154 1.35 rillig }
155 1.35 rillig
156 1.39 rillig MAKE_INLINE bool
157 1.35 rillig HashSet_Add(HashSet *set, const char *key)
158 1.35 rillig {
159 1.39 rillig bool isNew;
160 1.35 rillig
161 1.35 rillig (void)HashTable_CreateEntry(&set->tbl, key, &isNew);
162 1.35 rillig return isNew;
163 1.35 rillig }
164 1.35 rillig
165 1.42 rillig MAKE_INLINE bool MAKE_ATTR_USE
166 1.35 rillig HashSet_Contains(HashSet *set, const char *key)
167 1.35 rillig {
168 1.35 rillig return HashTable_FindEntry(&set->tbl, key) != NULL;
169 1.35 rillig }
170 1.35 rillig
171 1.36 rillig MAKE_INLINE void
172 1.36 rillig HashIter_InitSet(HashIter *hi, HashSet *set)
173 1.36 rillig {
174 1.38 rillig HashIter_Init(hi, &set->tbl);
175 1.36 rillig }
176 1.36 rillig
177 1.44 rillig #endif
178