-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.c
208 lines (166 loc) · 4.4 KB
/
hashmap.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
#include <string.h>
#include <stdlib.h>
#include "hashmap.h"
/* Algorithm from:
* http://stackoverflow.com/questions/7666509/hash-function-for-string */
static unsigned int hm_hash_function(const char *key) {
const char *c = key;
unsigned int hash = 5381;
while(*c != '\0') {
hash = ((hash << 5) + hash) + *c;
c++;
}
return hash;
}
void hm_fprint(FILE *stream, struct hashmap *hm, char print_empty) {
int i;
struct hm_item *item;
for (i = 0; i < hm->size; i++) {
item = hm->buckets[i];
if (!print_empty && item == NULL) {
continue;
}
fprintf(stream, "%3d: ", i);
while (item != NULL) {
fprintf(stream, "`%s` -> ", item->key);
item = item->next;
}
fprintf(stream, "NULL\n");
}
}
int hm_initialize(unsigned int size, float load_factor, size_t value_size, struct hashmap *hm) {
struct hm_item **buckets;
buckets = calloc(size, sizeof(struct hm_item *));
if (!buckets) {
return -1;
}
hm->size = size;
hm->used = 0;
hm->load_factor = load_factor;
hm->value_size = value_size;
hm->buckets = buckets;
return 0;
}
static void hm_terminate_bucket(struct hm_item *first_item) {
if (first_item == NULL) {
return;
}
hm_terminate_bucket(first_item->next);
free(first_item->key);
free(first_item->value);
free(first_item);
}
void hm_terminate(struct hashmap *hm) {
int i;
for (i = 0; i < hm->size; i++) {
hm_terminate_bucket(hm->buckets[i]);
}
free(hm->buckets);
}
/* Return a pointer to the slot where a key is or to where it should be if
* inserted next.
*
* Example:
*
* If hash("bar")%size == 0, and hm->buckets is:
*
* 0: NULL
* 1: `foo` -> `baz` -> NULL
* 2: NULL
* 3: NULL
*
* Giving hm_find the key "foo", it'll return a pointer to the second
* position of hm->buckets, and dereferencing that pointer
* would give you the address of the item with key "foo".
*
* Giving hm_find the key "bar", it'll return a pointer to the first
* position of hm->buckets, and dereferencing that pointer would give you
* NULL.
*
* Giving hm_find the key "baz", it'll return a pointer to the `next`
* member of the item with key "foo", and dereferencing the pointer would give
* you the address of the item with key "baz".
*/
static struct hm_item **hm_find(struct hashmap *hm, const char *key) {
int index = hm_hash_function(key)%hm->size;
struct hm_item **slot;
slot = hm->buckets + index;
while (*slot != NULL && strcmp((*slot)->key, key) != 0) {
slot = &((*slot)->next);
}
return slot;
}
/* Double the size of the hashmap. */
static int hm_grow(struct hashmap *hm) {
/* remember old size and buckets */
int i;
unsigned int old_size = hm->size;
struct hm_item **old_buckets = hm->buckets;
/* alloc new buckets */
hm->size = 2*hm->size;
hm->buckets = calloc(hm->size, sizeof(struct hm_item *));
if (!hm->buckets) {
return -1;
}
/* insert old values */
struct hm_item *curr_item, *next_item;
struct hm_item **new_slot;
for (i = 0; i < old_size; i++) {
curr_item = old_buckets[i];
while (curr_item != NULL) {
new_slot = hm_find(hm, curr_item->key);
*new_slot = curr_item;
next_item = curr_item->next;
curr_item->next = NULL;
curr_item = next_item;
}
}
free(old_buckets);
return 0;
}
int hm_put(struct hashmap *hm, const char *key, const void *value) {
struct hm_item **slot = hm_find(hm, key);
if (*slot != NULL) {
/* key already present */
return -1;
}
/* Before anyone complains about the use of goto:
* http://stackoverflow.com/questions/245742/examples-of-good-gotos-in-c-or-c */
struct hm_item *new = malloc(sizeof(struct hm_item));
if (new == NULL) goto alloc_fail1;
new->key = malloc(strlen(key) + 1);
if (new->key == NULL) goto alloc_fail2;
new->value = malloc(hm->value_size);
if (new->value == NULL) goto alloc_fail3;
strcpy(new->key, key);
memcpy(new->value, value, hm->value_size);
new->next = NULL;
*slot = new;
hm->used++;
if ((float)hm->used/hm->size > hm->load_factor) {
hm_grow(hm);
}
return 0;
alloc_fail3:
free(new->key);
alloc_fail2:
free(new);
alloc_fail1:
return -2;
}
struct hm_item *hm_getref(struct hashmap *hm, const char *key) {
struct hm_item **item = hm_find(hm, key);
return *item;
}
int hm_get(struct hashmap *hm, const char *key, void *value) {
struct hm_item **item = hm_find(hm, key);
if (*item == NULL) {
/* item does not exist */
return -1;
}
memcpy(value, (*item)->value, hm->value_size);
return 0;
}
int hm_remove(struct hashmap *hm, const char *key) {
return 0;
}