-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
352 lines (278 loc) · 9.61 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
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* Toy cuckoo hashing example.
I made this this morning while practising for an interview.
Probably really, really buggy and unfit for production use.
*/
struct table;
struct evict {
bool found;
char const *key;
void *value;
};
enum rehashed { REHASHED, FAIL };
/*
salt: see http://ocert.org/advisories/ocert-2011-003.html
*/
struct table *table_new(char const *salt, size_t capacity);
void table_delete(struct table *table);
struct evict table_put(struct table *table, char const *key, void *val);
void *table_get(struct table *table, char const *key);
enum rehashed table_copy(struct table const *table, struct table *new);
static struct {
char const *key;
char const *val;
} keyvals[] = {
{ .key = "A", .val = "foo" },
{ .key = "B", .val = "bar" },
{ .key = "C", .val = "gizmo" },
{ .key = "D", .val = "baz" },
};
enum { INITIAL_CAPACITY = 80 };
int main(int argc, char **argv)
{
/* This should really be a random parameter */
static char const salt[] = { "ASALT" };
size_t capacity = INITIAL_CAPACITY;
struct table *table = table_new(salt, capacity);
for (int ii = 0; ii < sizeof keyvals/sizeof keyvals[0]; ++ii) {
char const *key = keyvals[ii].key;
char const *val = keyvals[ii].val;
struct evict evict;
for (;;) {
evict = table_put(table, key, (void*)val);
if (!evict.found) {
break;
}
/* FIXME infinite loop */
printf("collision! %s %s\n", key, val);
size_t new_capacity = 2 * capacity + 1;
struct table *new = table_new(salt, new_capacity);
if (table_copy(table, new) != REHASHED) {
puts("rehash failed");
abort();
}
table_delete(table);
table = new;
capacity = new_capacity;
}
}
for (int ii = 0; ii < sizeof keyvals/sizeof keyvals[0]; ++ii) {
char const *key = keyvals[ii].key;
char const *val = (char const *)table_get(table, key);
printf("%s |-> %s\n", key, val);
}
table_delete(table);
return 0;
}
static size_t djb2_kernel(size_t state, char c) {
return (state * (size_t)33) ^ (size_t) c;
}
static size_t sbdm_kernel(size_t state, char c) {
return state * ((size_t)65599) + (size_t) c;
}
static size_t djb2(char const *salt, char const *str) {
/* Daniel J. Berstein's hash function */
/* http://www.cse.yorku.ca/~oz/hash.html */
size_t state = 5381;
for (int ii = 0; salt[ii] != '\0'; ++ii) {
state = djb2_kernel(state, salt[ii]);
}
for (int ii = 0; str[ii] != '\0'; ++ii) {
state = djb2_kernel(state, str[ii]);
}
return state;
}
static size_t sbdm(char const *salt, char const *str) {
/* SBDM's hash function */
/* http://www.cse.yorku.ca/~oz/hash.html */
size_t state = 31;
for (int ii = 0; salt[ii] != '\0'; ++ii) {
state = sbdm_kernel(state, salt[ii]);
}
for (int ii = 0; str[ii] != '\0'; ++ii) {
state = sbdm_kernel(state, str[ii]);
}
return state;
}
static size_t strhash(bool hash, char const *salt, char const *str) {
return hash ? djb2(salt, str) : sbdm(salt, str);
}
struct hash;
struct lookup {
bool found;
void *value;
};
static struct hash *hash_new(char const *salt, size_t capacity);
static void hash_delete(struct hash *hash);
static enum rehashed hash_copy(struct hash const *hash, struct table *table);
static struct evict hash1_put(struct hash *hash, char const *key, void *val);
static struct lookup hash1_get(struct hash *hash, char const *key);
static struct evict hash2_put(struct hash *hash, char const *key, void *val);
static struct lookup hash2_get(struct hash *hash, char const *key);
struct table {
struct hash *restrict hash1;
struct hash *restrict hash2;
};
enum { MAX_EVICT = 8 };
struct table *table_new(char const *salt, size_t capacity)
{
struct table *ptr = malloc(sizeof *ptr);
*ptr = (struct table) {
.hash1 = hash_new(salt, capacity),
.hash2 = hash_new(salt, capacity),
};
return ptr;
}
void table_delete(struct table *table)
{
hash_delete(table->hash1);
hash_delete(table->hash2);
free(table);
}
enum rehashed table_copy(struct table const *table, struct table *new)
{
enum rehashed rehashed;
rehashed = hash_copy(table->hash1, new);
if (rehashed != REHASHED) {
return rehashed;
}
rehashed = hash_copy(table->hash2, new);
if (rehashed != REHASHED) {
return rehashed;
}
return rehashed;
}
struct evict table_put(struct table *table, char const *key, void *val)
{
struct evict evict;
for (int ii = 0; ii < MAX_EVICT; ++ii) {
evict = hash1_put(table->hash1, key, val);
if (!evict.found) {
return (struct evict){.found = false};
}
key = evict.key;
val = evict.value;
evict = hash2_put(table->hash2, key, val);
if (!evict.found) {
return (struct evict){.found = false};
}
key = evict.key;
val = evict.value;
}
return evict;
}
void *table_get(struct table *table, char const *key)
{
struct lookup lookup;
lookup = hash1_get(table->hash1, key);
if (lookup.found) {
return lookup.value;
}
lookup = hash2_get(table->hash2, key);
if (lookup.found) {
return lookup.value;
}
return 0;
}
struct hash {
char const *salt;
size_t capacity;
char const **restrict keys;
bool *restrict used;
void **restrict value;
};
static struct hash *hash_new(char const *salt, size_t capacity)
{
char const **keys = calloc(capacity, sizeof keys[0]);
bool *used = calloc(capacity, sizeof used[0]);
void **value = calloc(capacity, sizeof value[0]);
struct hash *ptr = malloc(sizeof *ptr);
*ptr = (struct hash) {
.salt = salt,
.capacity = capacity,
.keys = keys,
.used = used,
.value = value
};
return ptr;
}
static void hash_delete(struct hash *hash)
{
free(hash);
}
static enum rehashed hash_copy(struct hash const *hash, struct table *table)
{
/* assume if it's time to rehash we have a decent load factor,
* say 0.5 so it's okay to iterate over everything */
size_t capacity = hash->capacity;
for (size_t ii = 0; ii < capacity; ++ii) {
if (!hash->used[ii]) {
continue;
}
char const *key = hash->keys[ii];
void *val = hash->value[ii];
struct evict evict = table_put(table, key, val);
if (evict.found) {
/* FIXME: not sure how to handle collisions
* during rehashing */
return FAIL;
}
}
return REHASHED;
}
static struct evict hash_put(bool n, struct hash *hash, char const *key, void *val);
static struct lookup hash_get(bool n, struct hash *hash, char const *key);
static struct evict hash1_put(struct hash *hash, char const *key, void *val)
{
return hash_put(0, hash, key, val);
}
static struct lookup hash1_get(struct hash *hash, char const *key)
{
return hash_get(0, hash, key);
}
static struct evict hash2_put(struct hash *hash, char const *key, void *val)
{
return hash_put(1, hash, key, val);
}
static struct lookup hash2_get(struct hash *hash, char const *key)
{
return hash_get(1, hash, key);
}
static struct evict hash_put(bool p, struct hash *hash, char const *key, void *val)
{
size_t capacity = hash->capacity;
char const *salt = hash->salt;
size_t index = strhash(p, salt, key) % capacity;
if (hash->used[index]) {
char const *old_key = hash->keys[index];
if (strcmp(old_key, key) != 0) {
void *old = hash->value[index];
hash->value[index] = val;
return (struct evict) { .found = true, .key = old_key, .value = old };
}
}
hash->used[index] = true;
hash->keys[index] = key;
hash->value[index] = val;
return (struct evict) { .found = false };
}
static struct lookup hash_get(bool p, struct hash *hash, char const *key)
{
size_t capacity = hash->capacity;
char const *salt = hash->salt;
size_t index = strhash(p, salt, key) % capacity;
if (!hash->used[index]) {
return (struct lookup) {.found = false };
}
char const *old_key = hash->keys[index];
if (strcmp(old_key, key) != 0) {
return (struct lookup) {.found = false };
}
return (struct lookup) {
.found = true,
.value = hash->value[index] };
}