1 /* $OpenBSD: hash.c,v 1.6 2024/02/26 08:25:51 yasuoka Exp $ */
2 /*-
3 * Copyright (c) 2009 Internet Initiative Japan Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27 #include <sys/types.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "hash.h"
33
34 /* hash_create - Create a new hash table.
35 * Returns a pointer of new hash_table on success. Otherwise returns
36 * NULL.
37 */
38 hash_table *
hash_create(int (* cmp_func)(const void *,const void *),uint32_t (* hash_func)(const void *,int),int hsz)39 hash_create(int (*cmp_func) (const void *, const void *),
40 uint32_t (*hash_func) (const void *, int), int hsz)
41 {
42 hash_table *htbl;
43
44 htbl = malloc(sizeof(hash_table));
45 if (htbl == NULL)
46 return NULL;
47
48 if (hsz < 1)
49 htbl->size = HASH_SIZE;
50 else
51 htbl->size = hsz;
52
53 htbl->bucket = calloc(htbl->size, sizeof(hash_link *));
54 htbl->cmp = cmp_func;
55 htbl->hash = hash_func;
56 htbl->cur = 0;
57 htbl->bucket_cur = NULL;
58
59 return htbl;
60 }
61
62 /* hash_first - Get first item from hash table.
63 * Returns a pointer of first bucket on success. Otherwise returns
64 * NULL.
65 */
66 hash_link *
hash_first(hash_table * htbl)67 hash_first(hash_table *htbl)
68 {
69 htbl->cur = 0;
70 htbl->bucket_cur = NULL;
71 return hash_next(htbl);
72 }
73
74 /* hash_next - Get next item from hash table.
75 * Returns a pointer of next bucket on success. Otherwise returns
76 * NULL.
77 */
78 hash_link *
hash_next(hash_table * htbl)79 hash_next(hash_table *htbl)
80 {
81 hash_link *hlink;
82
83 if (htbl->bucket_cur != NULL) {
84 hlink = htbl->bucket_cur;
85 htbl->bucket_cur = hlink->next;
86 return hlink;
87 }
88 while (htbl->cur < htbl->size) {
89 if (htbl->bucket[htbl->cur] != NULL) {
90 hlink = htbl->bucket[htbl->cur++];
91 htbl->bucket_cur = hlink->next;
92 return hlink;
93 }
94 htbl->cur++;
95 }
96 return NULL;
97 }
98
99 /* hash_lookup - Lookup item under the key in hash table.
100 * Return a pointer of the bucket on success. Otherwise returns
101 * NULL
102 */
103 hash_link *
hash_lookup(hash_table * htbl,const void * k)104 hash_lookup(hash_table *htbl, const void *k)
105 {
106 int c;
107 hash_link *w;
108
109 if (htbl == NULL || k == NULL)
110 return NULL;
111 c = (htbl->hash) (k, (int)htbl->size);
112 for (w = htbl->bucket[c]; w != NULL; w = w->next)
113 if (htbl->cmp(w->key, k) == 0)
114 return w;
115 return NULL;
116 }
117
118 /* hash_insert - Insert a item into hash table.
119 * Return 0 on success. Return -1 on failure.
120 */
121 int
hash_insert(hash_table * htbl,const void * k,void * i)122 hash_insert(hash_table *htbl, const void *k, void *i)
123 {
124 int c;
125 hash_link *n;
126
127 if (htbl == NULL || k == NULL)
128 return -1;
129
130 if ((n = malloc(sizeof(hash_link))) == NULL) {
131 return -1;
132 }
133
134 c = (htbl->hash) (k, (int)htbl->size);
135
136 n->item = i;
137 n->key = k;
138 n->next = htbl->bucket[c];
139 htbl->bucket[c] = n;
140
141 return 0;
142 }
143
144 /* hash_delete - Remove a item from hash table.
145 * If memfree then free the item. Return 0 on success. Return -1
146 * on failure.
147 */
148 int
hash_delete(hash_table * htbl,const void * k,int memfree)149 hash_delete(hash_table *htbl, const void *k, int memfree)
150 {
151 int i;
152 hash_link *b, *w;
153
154 if (htbl == NULL || k == NULL)
155 return -1;
156
157 i = (htbl->hash) (k, (int)htbl->size);
158
159 for (w = htbl->bucket[i], b = NULL; w != NULL; w = w->next) {
160 if (htbl->cmp(w->key, k) == 0) {
161 if (b != NULL)
162 b->next = w->next;
163 else
164 htbl->bucket[i] = w->next;
165
166 if (htbl->bucket_cur == w)
167 htbl->bucket_cur = w->next;
168
169 if (w->item != NULL && memfree) {
170 free(w->item);
171 }
172 free(w);
173 return 0;
174 }
175 b = w;
176 }
177 return -1;
178 }
179
180 /*
181 * delete all items from this hash_table.
182 * If memfree != 0 then free items.
183 */
184 void
hash_delete_all(hash_table * htbl,int memfree)185 hash_delete_all(hash_table *htbl, int memfree)
186 {
187 int i;
188 hash_link *w, *hl;
189
190 for (i = 0; i < htbl->size; i++) {
191 hl = htbl->bucket[i];
192 htbl->bucket[i] = NULL;
193 while (hl != NULL) {
194 w = hl;
195 hl = hl->next;
196 if (memfree && w->item != NULL)
197 free(w->item);
198 free(w);
199 }
200 }
201 }
202
203 /* hash_free - Free hash table and all buckets.
204 */
205 void
hash_free(hash_table * htbl)206 hash_free(hash_table *htbl)
207 {
208 if (htbl != NULL) {
209 free(htbl->bucket);
210 free(htbl);
211 }
212 }
213