-
Notifications
You must be signed in to change notification settings - Fork 1
/
htable.c
285 lines (226 loc) · 7.67 KB
/
htable.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
/*
* htable.h : A generic hash table implementation
*
*
* Copyright (c) 2018 Partha Susarla <mail@spartha.org>
*/
#include "htable.h"
#include "util.h"
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define FNV32_BASE ((unsigned int) 0x811c9dc5)
#define FNV32_PRIME ((unsigned int) 0x01000193)
#define HTABLE_INIT_SIZE 64
#define HTABLE_RESIZE_BITS 2
#define HTABLE_RESIZE_THRESHOLD 80
/* Private functions */
static int default_cmp_fn(const void *data _unused_,
const void *entry1 _unused_,
const void *entry2 _unused_,
const void *kdata _unused_)
{
return 0;
}
static void alloc_htable(struct htable *ht, size_t size)
{
ht->size = size;
ht->table = xcalloc(size, sizeof(struct htable *));
ht->grow_mark = size * HTABLE_RESIZE_THRESHOLD / 100;
if (size <= HTABLE_INIT_SIZE)
ht->shrink_mark = 0;
else
ht->shrink_mark = ht->grow_mark / ((1 << HTABLE_RESIZE_BITS) + 1);
}
static inline int entries_equal(const struct htable *ht,
const struct htable_entry *e1,
const struct htable_entry *e2,
const void *kdata)
{
return (e1 == e2) ||
(e1->hash == e2->hash &&
!ht->cmpfn(ht->cmpfndata, e1, e2, kdata));
}
static inline unsigned int bucket(const struct htable *ht,
const struct htable_entry *key)
{
return key->hash & (ht->size - 1);
}
static inline struct htable_entry **find_entry(const struct htable *ht,
const struct htable_entry *key,
const void *keydata)
{
struct htable_entry **e = &ht->table[bucket(ht, key)];
while (*e && !entries_equal(ht, *e, key, keydata))
e = &(*e)->next;
return e;
}
static void rehash(struct htable *ht, unsigned int newsize)
{
unsigned int i, oldsize = ht->size;
struct htable_entry **oldtable = ht->table;
alloc_htable(ht, newsize);
for (i = 0; i < oldsize; i++) {
struct htable_entry *e = oldtable[i];
while (e) {
struct htable_entry *n = e->next;
unsigned int b = bucket(ht, e);
e->next = ht->table[b];
ht->table[b] = e;
e = n;
}
}
free(oldtable);
}
/* Public functions */
unsigned int bufhash(const void *buf, size_t len)
{
unsigned int hash = FNV32_BASE;
const unsigned char *ptr = buf;
while (len--) {
unsigned int c = *ptr++;
hash = (hash * FNV32_PRIME) ^ c;
}
return hash;
}
void htable_init(struct htable *ht, htable_cmp_fn cmp_fn,
const void *cmpfndata, size_t size)
{
size_t init_size = HTABLE_INIT_SIZE;
memset(ht, 0, sizeof(struct htable));
ht->cmpfn = cmp_fn ? cmp_fn : default_cmp_fn;
ht->cmpfndata = cmpfndata;
ht->iter_head = ht->iter_tail = UINT_MAX;
/* calculate initial size */
size = size * 100 / HTABLE_RESIZE_THRESHOLD;
while (size > init_size)
init_size <<= HTABLE_RESIZE_BITS;
alloc_htable(ht, init_size);
}
void htable_free(struct htable *ht, int free_entries)
{
if (!ht || !ht->table)
return;
if (free_entries) {
struct htable_iter iter;
struct htable_entry *e;
htable_iter_init(ht, &iter);
while((e = htable_iter_next(&iter)))
free(e);
}
free(ht->table);
memset(ht, 0, sizeof(struct htable));
}
void *htable_get(const struct htable *ht, const void *key, const void *keydata)
{
return *find_entry(ht, key, keydata);
}
const void *htable_get_next(const struct htable *ht, const void *entry)
{
const struct htable_entry *e = ((const struct htable_entry *) entry)->next;
for (; e; e = e->next)
if (entries_equal(ht, entry, e, NULL))
return e;
return NULL;
}
void htable_put(struct htable *ht, void *entry)
{
unsigned int b = bucket(ht, entry);
int collision = 0;
collision = ht->table[b] ? 1 : 0;
if (collision) {
((struct htable_entry *) entry)->iter_prev =
((struct htable_entry *) ht->table[b])->iter_prev;
((struct htable_entry *) entry)->iter_next =
((struct htable_entry *) ht->table[b])->iter_next;
((struct htable_entry *) entry)->count =
((struct htable_entry *) ht->table[b])->count;
}
((struct htable_entry *) entry)->next = ht->table[b];
ht->table[b] = entry;
((struct htable_entry *) entry)->count += 1;
if (ht->iter_head == UINT_MAX) { /* First entry */
((struct htable_entry *) entry)->iter_prev = UINT_MAX;
((struct htable_entry *) entry)->iter_next = UINT_MAX;
ht->iter_head = b;
ht->iter_tail = b;
} else {
if (!collision) {
((struct htable_entry *) ht->table[ht->iter_tail])->iter_next = b;
((struct htable_entry *) entry)->iter_prev = ht->iter_tail;
ht->iter_tail = b;
}
}
ht->count++;
if (ht->count < ht->shrink_mark)
rehash(ht, ht->size << HTABLE_RESIZE_BITS);
}
void *htable_remove(struct htable *ht, const void *key,
const void *keydata)
{
struct htable_entry *old;
struct htable_entry **e = find_entry(ht, key, keydata);
if (!*e)
return NULL;
old = *e;
*e = old->next;
old->next = NULL;
/* TODO: Update entry count */
/* TODO: Update the iter_prev and iter_next values */
ht->count--;
if (ht->count < ht->shrink_mark)
rehash(ht, ht->size >> HTABLE_RESIZE_BITS);
return old;
}
void *htable_replace(struct htable *ht, void *entry)
{
struct htable_entry *old = htable_remove(ht, entry, NULL);
htable_put(ht, entry);
return old;
}
void htable_iter_init(struct htable *ht, struct htable_iter *iter)
{
iter->ht = ht;
iter->pos = 0;
iter->next = NULL;
}
void *htable_iter_next(struct htable_iter *iter)
{
struct htable_entry *current = iter->next;
for (;;){
if (current) {
iter->next = current->next;
return current;
}
if (iter->pos >= iter->ht->size)
return NULL;
current = iter->ht->table[iter->pos++];
}
}
void htable_iter_init_ordered(struct htable *ht, struct htable_iter *iter)
{
iter->ht = ht;
iter->pos = ht->iter_head;
iter->count = 0;
iter->next = NULL;
}
void *htable_iter_ordered_get(struct htable_iter *iter)
{
struct htable_entry *current = iter->ht->table[iter->pos];
return current;
}
void *htable_iter_next_ordered(struct htable_iter *iter)
{
struct htable_entry *current = NULL;
iter->count += 1;
if (iter->pos == UINT_MAX)
return NULL;
if (iter->count > iter->ht->count)
return NULL;
current = iter->ht->table[iter->pos];
if (current) {
iter->pos = current->iter_next;
return current;
}
return NULL;
}