-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.c
413 lines (322 loc) · 8.01 KB
/
hash.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "bolo.h"
static unsigned int
s_hash_djb2(const char *key)
{
unsigned int h = 5381;
for (; *key; key++)
h = ((h << 5) + h) + *key; /* h * 33 + c */
return h;
}
#define s_hash s_hash_djb2
struct hbkt {
char * key;
void * ptr;
uint64_t val;
struct hbkt *next;
};
#define HASH_STRIDE 255
struct hash {
size_t nset;
struct hbkt *hbkts[HASH_STRIDE];
/* for iteration */
int i;
struct hbkt *last;
};
struct hash *
hash_new()
{
return xalloc(1, sizeof(struct hash));
}
void
hash_free(struct hash *h)
{
int i;
struct hbkt *b, *tmp;
if (!h)
return;
for (i = 0; i < HASH_STRIDE; i++) {
for (b = h->hbkts[i]; b; ) {
free(b->key);
tmp = b->next;
free(b);
b = tmp;
}
}
free(h);
}
void
_hash_ebegn(struct hash *h, void *key, void *val)
{
CHECK(h != NULL, "hash_each() { ... } given a NULL hash to iterate over");
h->i = 0;
h->last = NULL;
_hash_enext(h, key, val);
}
void
_hash_enext(struct hash *h, void *key, void *val)
{
CHECK(h != NULL, "hash_each() { ... } given a NULL hash to iterate over");
CHECK(key != NULL || val != NULL, "hash_each() { ... } given NULL key and value destination pointers");
while (h->i < HASH_STRIDE && !h->last) {
h->last = h->hbkts[h->i++];
}
if (h->last) {
if (key) *(char **)key = h->last->key;
if (val) *(void **)val = h->last->ptr;
h->last = h->last->next;
}
}
int
_hash_edone(struct hash *h)
{
return h->i >= HASH_STRIDE && h->last == NULL;
}
size_t
hash_nset(struct hash *h)
{
CHECK(h != NULL, "hash_nset() given a NULL hash to query");
return h->nset;
}
int
hash_set(struct hash *h, const char *key, void *val)
{
unsigned int k;
struct hbkt *b;
CHECK(h != NULL, "hash_set() given a NULL hash to insert into");
CHECK(key != NULL, "hash_set() given a NULL key to insert");
k = s_hash(key) % HASH_STRIDE;
/* check existing data */
for (b = h->hbkts[k]; b; b = b->next) {
if (!streq(b->key, key))
continue;
b->ptr = val;
return 0;
}
/* not in existing data, prepend a new hbkt */
b = xalloc(1, sizeof(struct hbkt));
b->key = strdup(key);
b->ptr = val;
b->next = h->hbkts[k];
h->hbkts[k] = b;
h->nset++;
return 0;
}
int
hash_get(struct hash *h, void *dst, const char *key)
{
unsigned int k;
struct hbkt *b;
CHECK(h != NULL, "hash_get() given a NULL hash to query");
CHECK(key != NULL, "hash_get() given a NULL key to lookup");
k = s_hash(key) % HASH_STRIDE;
/* check existing data */
for (b = h->hbkts[k]; b; b = b->next) {
if (!streq(b->key, key))
continue;
if (dst)
*(void **)dst = b->ptr;
return 0;
}
/* key not set in the hash */
errno = BOLO_ENOTSET;
return -1;
}
struct hash *
hash_read(int from, hash_reader_fn reader, void *udata)
{
struct hash *h;
int fd;
FILE *in;
char buf[8192], *a, *b;
uint64_t value;
void *ptr;
CHECK(from >= 0, "hash_read() given an invalid file descriptor to read from");
lseek(from, 0, SEEK_SET);
fd = dup(from);
if (fd < 0)
return NULL;
h = NULL;
in = fdopen(fd, "r");
if (!in)
goto fail;
h = hash_new();
while (fgets(buf, 8192, in) != NULL) {
a = strchr(buf, '\t');
b = strchr(buf, '\n');
errno = BOLO_EBADHASH;
if (!a && !b)
goto fail;
*a++ = *b = '\0';
value = strtoul(a, &b, 10);
if (b && *b)
goto fail;
ptr = reader(buf, value, udata);
if (!ptr)
goto fail;
if (hash_set(h, buf, ptr) != 0)
goto fail;
}
fclose(in);
return h;
fail:
fclose(in);
hash_free(h);
return NULL;
}
int
hash_write(struct hash *h, int to, hash_writer_fn writer, void *udata)
{
FILE *out;
int i, fd;
struct hbkt *b;
CHECK(h != NULL, "hash_write() given a NULL hash pointer to write");
CHECK(to >= 0, "hash_write() given an invalid file descriptor to write to");
if (ftruncate(to, 0) < 0)
return -1;
lseek(to, 0, SEEK_SET);
fd = dup(to);
if (fd < 0)
return -1;
out = fdopen(fd, "w");
if (!out) {
close(fd);
return -1;
}
for (i = 0; i < HASH_STRIDE; i++)
for (b = h->hbkts[i]; b; b = b->next)
fprintf(out, "%s\t%lu\n", b->key, writer(b->key, b->ptr, udata));
fclose(out);
return 0;
}
#ifdef TEST
/* LCOV_EXCL_START */
struct data {
uint64_t id;
/* don't need any other fields... */
};
static uint64_t test_writer1(const char *k, void *_ptr, void *_)
{
insist(_ptr != NULL, "_ptr must not be NULL");
return ((struct data *)_ptr)->id;
}
/* _u (userdata) will be a contiguous array of
struct data pointers, NULL-terminated. */
static void * test_reader1(const char *k, uint64_t v, void *_u)
{
struct data **u, *i;
insist(_u != NULL, "_u must not be NULL");
u = (struct data **)_u;
for (i = *u; i; i++)
if (i->id == v)
return i;
/* not found! */
return NULL;
}
TESTS {
subtest {
isnt_unsigned(
s_hash("some string"),
s_hash("some other string"),
"s_hash() shouldn't collide on different keys");
is_unsigned(
s_hash("identical"),
s_hash("identical"),
"s_hash() should collide on identical keys");
}
subtest {
struct hash *h;
const char *v;
h = hash_new();
is_unsigned(hash_nset(h), 0, "hash initially has 0 keys");
ok(!hash_isset(h, "one"), "h[one] should not be set");
v = NULL;
ok(hash_get(h, &v, "one") != 0,
"hash_get(k) should fail, since k is not set in the hash");
ok(hash_set(h, "one", "the first value") == 0,
"hash_set(k,v) should succeed");
is_unsigned(hash_nset(h), 1, "hash has 1 key after a hash_set");
ok(hash_isset(h, "one"), "h[one] should be set");
v = NULL;
ok(hash_get(h, &v, "one") == 0,
"hash_get(k) should succeed");
is_string(v, "the first value",
"hash_get(k) should retrieve what we hash_set(k)");
hash_free(h);
}
subtest {
struct hash *h;
int fd;
struct data *data, *lst[2], *result;
h = hash_new();
fd = memfd("hash");
data = xmalloc(sizeof(struct data));
data->id = 0xdecafbad;
lst[0] = data;
lst[1] = NULL;
if (hash_set(h, "key", data) != 0)
BAIL_OUT("failed to set up the test for hash_read/write");
is_unsigned(hash_nset(h), 1, "hash has 1 value set before write/read");
ok(hash_write(h, fd, test_writer1, lst) == 0,
"hash_write() should succeed");
hash_free(h);
h = hash_read(fd, test_reader1, lst);
if (!h)
BAIL_OUT("failed to read hash back in from the fd");
is_unsigned(hash_nset(h), 1, "hash has 1 value set after re-read");
ok(hash_get(h, &result, "key") == 0,
"hash_get() should succeed after re-read");
is_ptr(result, data, "hash_get() should return pointer set by reader fn");
hash_free(h);
close(fd);
free(data);
}
subtest {
struct hash *h;
char *key;
int i;
struct data d;
h = hash_new();
is_unsigned(hash_nset(h), 0, "hash initially has 0 keys");
for (i = 0; i < HASH_STRIDE + 1; i++) {
if (asprintf(&key, "key%d", i) <= 0)
BAIL_OUT("failed to generate a key for pigeon-hole test");
if (hash_set(h, key, &d) != 0)
fail("failed to set key %s => (data) in pigeon-hole test");
free(key);
}
is_unsigned(hash_nset(h), HASH_STRIDE + 1, "each unique key/value increments hash cardinality");
hash_free(h);
}
subtest {
struct hash *h;
struct data d1, d2, *v;
h = hash_new();
ok(hash_set(h, "x", &d1) == 0, "should set x => d1");
ok(hash_get(h, &v, "x") == 0, "should be able to retrieve 'x'");
is_ptr(v, &d1, "'x' should be mapped to d1");
ok(hash_set(h, "x", &d2) == 0, "should set x => d2");
ok(hash_get(h, &v, "x") == 0, "should be able to retrieve 'x'");
is_ptr(v, &d2, "'x' should now be mapped to d2");
hash_free(h);
}
subtest {
struct hash *before, *after;
struct data d1, d2, *v;
char *key;
before = hash_new();
ok(hash_set(before, "d1", &d1) == 0, "should set before[d1] => d1");
ok(hash_set(before, "d2", &d2) == 0, "should set before[d2] => d2");
after = hash_new();
ok(hash_get(after, &v, "d1") != 0, "after[d1] is not set pre-traversal");
ok(hash_get(after, &v, "d2") != 0, "after[d2] is not set pre-traversal");
hash_each(before, &key, &v) {
hash_set(after, key, v);
}
ok(hash_get(after, &v, "d1") == 0, "after[d1] is set pre-traversal");
ok(hash_get(after, &v, "d2") == 0, "after[d2] is set pre-traversal");
hash_free(before);
hash_free(after);
}
}
/* LCOV_EXCL_STOP */
#endif