-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
359 lines (295 loc) · 6.94 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
#include "hash.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MIN_BUCKET_COUNT 32 /* Totally arbitrary */
/* Chosen based on these notes from Cornell's data structures course:
* https://www.cs.cornell.edu/Courses/cs312/2008sp/lectures/lec20.html
* We're not super concerned with performance, so any value that is basically
* sane will do here.
*/
#define GROW_THRESHOLD 1.5
#define SHRINK_THRESHOLD (GROW_THRESHOLD / 4.0)
struct hash_entry {
char *key;
int value;
struct hash_entry *next;
};
struct hash_table {
double load_factor;
size_t bucket_count;
struct hash_entry *buckets[]; /* Flexible array */
};
/* djb2 hash function
* From http://www.cse.yorku.ca/~oz/hash.html
*/
static unsigned long
hash(const char *key)
{
unsigned long hash = 5381;
int c;
while ((c = *key++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
/* make_hash: allocate and initialize a hash table.
*
* Returns the created hash table, or NULL on allocation failure.
*
* Private helper function
*/
static Hash *
make_hash(size_t bucket_count)
{
Hash *h = malloc(sizeof *h + bucket_count * sizeof h->buckets[0]);
if (!h) return NULL;
h->load_factor = 0.0;
h->bucket_count = bucket_count;
for (size_t i = 0; i < h->bucket_count; i++) {
h->buckets[i] = NULL;
}
return h;
}
/* hash_new: create a new, empty hash table.
*
* Public method
*/
Hash *
hash_new()
{
return make_hash(MIN_BUCKET_COUNT);
}
/* delete_bucket: free an entire "chain" of hash entries.
*
* Private helper function
*/
static void
delete_bucket(struct hash_entry *head)
{
struct hash_entry *next;
while (head) {
next = head->next;
free(head->key);
free(head);
head = next;
}
}
/* hash_delete: free all memory associated with a hash table.
*
* Public method
*/
void
hash_delete(Hash *self)
{
if (!self) return;
for (size_t i = 0; i < self->bucket_count; i++) {
delete_bucket(self->buckets[i]);
}
free(self);
}
/* make_entry: allocate and initialize a new hash_entry structure.
*
* Returns the created entry, or NULL on allocation failure.
*
* Private helper function
*/
static struct hash_entry *
make_entry(const char *key, const int value)
{
struct hash_entry *p = NULL;
p = malloc(sizeof *p);
if (p == NULL) goto error;
p->key = malloc(strlen(key) + 1);
if (p->key == NULL) goto error;
strcpy(p->key, key);
p->value = value;
p->next = NULL;
return p;
error:
free(p);
return NULL;
}
/* try_set: attempt to add a key-value pair to a hash table.
*
* Returns 1 on success, 0 on failure.
*
* Private helper function
*/
static int
try_set(Hash *self, const char *key, const int value)
{
size_t i = hash(key) % self->bucket_count;
struct hash_entry *entry;
/* Search for key */
entry = self->buckets[i];
while (entry && strcmp(key, entry->key) !=0)
entry = entry->next;
if (entry) {
/* Key found */
entry->value = value;
} else {
/* Key not found */
entry = make_entry(key, value);
if (!entry) return 0;
entry->next = self->buckets[i];
self->buckets[i] = entry;
self->load_factor += 1.0 / self->bucket_count;
}
return 1;
}
/* hash_foreach: iterate over pairs in a hash table.
*
* self must not be NULL.
*
* Public method
*/
void
hash_foreach(Hash *self, void (*callback)(const char *, int, void *),
void *context)
{
assert(self);
for (size_t i = 0; i < self->bucket_count; i++)
for (struct hash_entry *e = self->buckets[i]; e; e = e->next)
callback(e->key, e->value, context);
}
/* Context struct for resize_callback */
struct resize_ctx {
int success; /* Was the resize successful? */
Hash *new_self; /* The resized hash table */
};
/* resize_callback: try to insert the pair (k, v) into context->new_self.
*
* If any of the inserts fail, context->success will be 0 at the end of
* iteration.
*
* Callback function for hash_foreach
*/
static void
resize_callback(const char *k, int v, void *context)
{
/* Unpack context */
Hash *new_self = ((struct resize_ctx *) context)->new_self;
int *success = &((struct resize_ctx*) context)->success;
/* Attempt the insert and record the result */
*success = *success && try_set(new_self, k, v);
}
/* try_resize: attempt to create a hash with new_size buckets and the same
* entries as self.
*
* Returns a pointer to the new hash on success, NULL on failure.
*
* Private helper function
*/
static Hash *
try_resize(Hash *self, size_t new_size)
{
struct resize_ctx result = {
.success = 1,
.new_self = make_hash(new_size)
};
if (!result.new_self) return NULL;
hash_foreach(self, resize_callback, &result);
if (!result.success) {
hash_delete(result.new_self);
return NULL;
}
return result.new_self;
}
/* hash_set: add a key-value pair to a hash table.
*
* *selfp and key must not be NULL.
*
* Public method
*/
void
hash_set(Hash **selfp, const char *key, const int value)
{
assert(*selfp);
assert(key);
Hash *self = *selfp;
if (!try_set(self, key, value)) goto error;
/* Rehash if necessary */
if (self->load_factor > GROW_THRESHOLD) {
Hash *new_self = try_resize(self, self->bucket_count * 2);
/* If it didn't work, just keep using the old hash for now. */
if (!new_self) return;
*selfp = new_self;
hash_delete(self);
}
return;
error:
/* Out of memory */
/* FIXME: is there a better way to handle this? */
abort();
}
/* hash_get: search for a key in a hash.
*
* Returns 1 if the key is found, and 0 if not. Additionally, if the key is
* found and value_out is non-NULL, the corresponding value is stored in
* *value_out.
*
* self and key must not be NULL.
*
* Public method
*/
int
hash_get(const Hash *self, const char *key, int *value_out)
{
assert(self);
assert(key);
size_t i = hash(key) % self->bucket_count;
struct hash_entry *entry;
/* Search for key */
entry = self->buckets[i];
while (entry && strcmp(key, entry->key) != 0)
entry = entry->next;
if (entry) {
/* Key found */
if (value_out) *value_out = entry->value;
return 1;
} else {
/* Key not found */
return 0;
}
}
/* hash_remove: remove a key and its associated value from a hash table.
*
* Also free the memory associated with the removed pair.
*
* *selfp and key must not be NULL.
*
* Public method
*/
void
hash_remove(Hash **selfp, const char *key)
{
assert(*selfp);
assert(key);
Hash *self = *selfp;
size_t i = hash(key) % self->bucket_count;
struct hash_entry **entryp;
/* Search for key */
entryp = &self->buckets[i];
while (*entryp && strcmp(key, (*entryp)->key) != 0) {
entryp = &(*entryp)->next;
}
if (*entryp) {
/* Key found */
struct hash_entry *entry = *entryp;
*entryp = entry->next;
free(entry->key);
free(entry);
self->load_factor -= 1.0 / self->bucket_count;
}
/* Rehash if necessary */
if (self->load_factor < SHRINK_THRESHOLD &&
self->bucket_count > MIN_BUCKET_COUNT)
{
Hash *new_self = try_resize(self, self->bucket_count / 2);
/* If it didn't work, just keep using the old hash for now. */
if (!new_self) return;
*selfp = new_self;
hash_delete(self);
}
return;
}