This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
htab.c
340 lines (266 loc) · 6.75 KB
/
htab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "elist.h"
#include "htab.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
struct htab_entry {
void *key;
void *value;
unsigned long hash;
struct elist_head chain_node;
};
struct htab_bucket {
struct elist_head chain;
};
struct htab {
htab_hash_fn hash_fn;
htab_cmp_fn cmp_fn;
htab_free_fn free_key_fn;
htab_free_fn free_value_fn;
unsigned int prime_idx;
struct htab_bucket *buckets;
unsigned int n_ent;
};
static const unsigned int primes[] = {
5,
/* 13, 23, 53, 97, */
/* 193, 389, */ 769,
/* 1543, 3079, */ 6151,
/* 12289, 24593, */ 49157,
98317, 196613, 393241, 786433, 1572869, 3145739, 6291469,
12582917, 25165843, 50331653, 100663319, 201326611, 402653189,
805306457, 1610612741
};
static inline unsigned int htab_sz(const struct htab *htab)
{
return primes[htab->prime_idx];
}
static void htab_init_buckets(struct htab_bucket *buckets, int sz)
{
int i;
for (i = 0; i < sz; i++) {
INIT_ELIST_HEAD(&buckets[i].chain);
}
}
unsigned int htab_size(struct htab *htab)
{
return htab->n_ent;
}
struct htab *htab_new(htab_hash_fn hash_fn,
htab_cmp_fn cmp_fn,
htab_free_fn free_key_fn,
htab_free_fn free_value_fn)
{
struct htab *htab;
int n_buckets;
/* allocate & init htab struct */
htab = calloc(1, sizeof(*htab));
if (!htab)
return NULL;
htab->hash_fn = hash_fn;
htab->cmp_fn = cmp_fn;
htab->free_key_fn = free_key_fn;
htab->free_value_fn = free_value_fn;
n_buckets = htab_sz(htab);
/* alloc & init hash table buckets */
htab->buckets = calloc(n_buckets, sizeof(struct htab_bucket));
if (!htab->buckets) {
free(htab);
return NULL;
}
htab_init_buckets(htab->buckets, n_buckets);
return htab;
}
struct htab *htab_str_new(bool free_key, bool free_value)
{
return htab_new(htab_str_hash, htab_str_cmp,
free_key ? free : NULL,
free_value ? free : NULL);
}
static void htab_free_ent(struct htab *htab, struct htab_entry *ent)
{
if (!htab || !ent)
return;
/* call destructors */
if (htab->free_key_fn)
htab->free_key_fn(ent->key);
if (htab->free_value_fn)
htab->free_value_fn(ent->value);
/* remove ourselves from the hash chain */
elist_del(&ent->chain_node);
/* delete hash entry */
free(ent);
/* account for entry's disapperance */
htab->n_ent--;
}
static void htab_clear(struct htab *htab)
{
struct htab_entry *ent, *iter;
unsigned int bucket;
/* remove each hash entry from each hash chain in each bucket */
for (bucket = 0; bucket < htab_sz(htab); bucket++) {
elist_for_each_entry_safe(ent, iter,
&htab->buckets[bucket].chain, chain_node) {
htab_free_ent(htab, ent);
}
}
}
void htab_free(struct htab *htab)
{
if (!htab)
return;
/* free all hash entries */
htab_clear(htab);
/* free hash table */
free(htab->buckets);
free(htab);
}
static bool htab_need_resize(const struct htab *htab)
{
unsigned int sz = htab_sz(htab);
unsigned int threshold = (sz * 2) / 3;
/* if we exceed 2/3 total table size... */
return (htab->n_ent > threshold);
}
static bool htab_resize(struct htab *htab)
{
unsigned int old_sz, new_sz;
struct htab_bucket *new_buckets;
unsigned int bucket;
/* calc old and new hash table sizes */
old_sz = htab_sz(htab);
new_sz = primes[htab->prime_idx + 1];
/* alloc & init new table */
new_buckets = calloc(new_sz, sizeof(struct htab_bucket));
if (!new_buckets)
return false;
htab_init_buckets(new_buckets, new_sz);
/* iterate through old table, moving each entry */
for (bucket = 0; bucket < old_sz; bucket++) {
struct htab_entry *ent, *iter;
elist_for_each_entry_safe(ent, iter,
&htab->buckets[bucket].chain,
chain_node) {
unsigned int new_bucket;
elist_del_init(&ent->chain_node);
new_bucket = ent->hash % new_sz;
elist_add_tail(&ent->chain_node,
&new_buckets[new_bucket].chain);
}
}
/* replace old table with new one */
free(htab->buckets);
htab->prime_idx++;
htab->buckets = new_buckets;
return true;
}
bool htab_put(struct htab *htab, void *key, void *value)
{
struct htab_entry *ent;
unsigned long hash = htab->hash_fn(key);
unsigned int bucket = hash % htab_sz(htab);
/* optionally resize table */
if (htab_need_resize(htab) && !htab_resize(htab))
return false;
/* alloc * init hash entry */
ent = calloc(1, sizeof(*ent));
if (!ent)
return false;
ent->key = key;
ent->value = value;
ent->hash = hash;
INIT_ELIST_HEAD(&ent->chain_node);
/* add hash entry to bucket's chain */
elist_add(&ent->chain_node, &htab->buckets[bucket].chain);
/* account for additional hash entry */
htab->n_ent++;
return true;
}
void *htab_get(struct htab *htab, const void *key)
{
struct htab_entry *ent;
unsigned long hash = htab->hash_fn(key);
unsigned int bucket = hash % htab_sz(htab);
/* search bucket's chain for key, returning first value found */
elist_for_each_entry(ent, &htab->buckets[bucket].chain, chain_node) {
if ((ent->hash == hash) &&
(htab->cmp_fn(ent->key, key) == 0))
return ent->value;
}
return NULL;
}
bool htab_del(struct htab *htab, const void *key)
{
struct htab_entry *ent, *iter;
unsigned long hash = htab->hash_fn(key);
unsigned int bucket = hash % htab_sz(htab);
bool found = false;
/* search bucket's chain for key, deleting all matching entries */
elist_for_each_entry_safe(ent, iter,
&htab->buckets[bucket].chain, chain_node) {
if ((ent->hash == hash) &&
(htab->cmp_fn(ent->key, key) == 0)) {
htab_free_ent(htab, ent);
found = true;
}
}
return found;
}
void htab_foreach(struct htab *htab, htab_iter_fn iter_fn, void *userdata)
{
int i;
for (i = 0; i < htab_sz(htab); i++) {
struct htab_entry *ent, *iter;
elist_for_each_entry_safe(ent, iter,
&htab->buckets[i].chain, chain_node) {
iter_fn(ent->key, ent->value, userdata);
}
}
}
/* "djb2"-derived hash function */
unsigned long htab_djb_hash(unsigned long hash, const void *_buf, size_t buflen)
{
const unsigned char *buf = _buf;
int c;
while (buflen > 0) {
c = *buf++;
buflen--;
hash = ((hash << 5) + hash) ^ c; /* hash * 33 ^ c */
}
return hash;
}
unsigned long htab_ulong_hash(const void *buf)
{
const unsigned long *v = buf;
return *v;
}
int htab_ulong_cmp(const void *data1, const void *data2)
{
const unsigned long *v1 = data1;
const unsigned long *v2 = data2;
return (*v1) - (*v2);
}
unsigned long htab_int_hash(const void *buf)
{
const unsigned int *v = buf;
return *v;
}
int htab_int_cmp(const void *data1, const void *data2)
{
const unsigned int *v1 = data1;
const unsigned int *v2 = data2;
return (*v1) - (*v2);
}
unsigned long htab_str_hash(const void *buf)
{
const char *s = buf;
return htab_djb_hash(5381, s, strlen(s));
}
int htab_str_cmp(const void *data1, const void *data2)
{
return strcmp(data1, data2);
}