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
#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#define BINS 33703
uint32_t hash_key(uint32_t val)
{
return (val * 2654435761) % BINS;
}
/* first, make a hashmap that's just a linked list of key, value pairs */
struct LinkedListNode;
typedef struct LinkedListNode LinkedListNode;
struct LinkedListNode
{
uint32_t key;
void *value;
LinkedListNode *next;
};
typedef struct ListHashmap
{
LinkedListNode *head;
} ListHashmap;
LinkedListNode *LinkedListNode_init(uint32_t key, void *value, LinkedListNode *next)
{
LinkedListNode *lln = malloc(sizeof(LinkedListNode));
assert(lln);
lln->key = key;
lln->value = value;
lln->next = next;
return lln;
}
void LinkedListNode_free(LinkedListNode *lln)
{
free(lln);
}
void ListHashmap_init(ListHashmap *lh)
{
lh->head = NULL;
}
void ListHashmap_deinit(ListHashmap *lh)
{
LinkedListNode *pos = lh->head;
while (pos)
{
LinkedListNode *next = pos->next;
LinkedListNode_free(pos);
pos = next;
}
}
void ListHashmap_update(ListHashmap *lh, uint32_t key, void *value)
{
LinkedListNode *pos = lh->head;
LinkedListNode *prev = NULL;
while (pos)
{
if (pos->key == key)
{
pos->value = value;
return;
}
prev = pos;
pos = pos->next;
}
/* must make a new pair */
LinkedListNode *new = LinkedListNode_init(key, value, NULL);
if (prev)
prev->next = new;
else
lh->head = new;
}
bool ListHashmap_contains(ListHashmap *lh, uint32_t key)
{
LinkedListNode *pos = lh->head;
while (pos)
{
if (pos->key == key) return true;
pos = pos->next;
}
return false;
}
void *ListHashmap_get(ListHashmap *lh, uint32_t key)
{
LinkedListNode *pos = lh->head;
while (pos)
{
if (pos->key == key) return pos->value;
pos = pos->next;
}
assert(false);
}
void ListHashmap_delete(ListHashmap *lh, uint32_t key)
{
assert(ListHashmap_contains(lh, key));
if (lh->head->key == key)
{
LinkedListNode *next = lh->head->next;
LinkedListNode_free(lh->head);
lh->head = next;
}
else
{
LinkedListNode *pos = lh->head->next;
LinkedListNode *prev = lh->head;
while (pos)
{
if (pos->key == key)
{
prev->next = pos->next;
LinkedListNode_free(pos);
return;
}
prev = pos;
pos = pos->next;
}
}
}
void ListHashmap_printPairs(ListHashmap *lh)
{
LinkedListNode *pos = lh->head;
while (pos)
{
uint32_t key = pos->key;
int *ptr = ListHashmap_get(lh, key);
printf("(%d, %d)\n", key, *ptr);
pos = pos->next;
}
}
struct Hashmap
{
ListHashmap bins[BINS];
};
Hashmap *Hashmap_init()
{
Hashmap *hm = malloc(sizeof(Hashmap));
assert(hm);
for (size_t i = 0; i < BINS; i++)
{
ListHashmap_init(&hm->bins[i]);
}
return hm;
}
void Hashmap_free(Hashmap *hm)
{
for (size_t i = 0; i < BINS; i++)
{
ListHashmap_deinit(&hm->bins[i]);
}
free(hm);
}
void Hashmap_update(Hashmap *hm, uint32_t key, void *value)
{
uint32_t hash = hash_key(key);
ListHashmap_update(&hm->bins[hash], key, value);
}
bool Hashmap_contains(Hashmap *hm, uint32_t key)
{
uint32_t hash = hash_key(key);
return ListHashmap_contains(&hm->bins[hash], key);
}
void *Hashmap_get(Hashmap *hm, uint32_t key)
{
uint32_t hash = hash_key(key);
return ListHashmap_get(&hm->bins[hash], key);
}
void Hashmap_delete(Hashmap *hm, uint32_t key)
{
uint32_t hash = hash_key(key);
ListHashmap_delete(&hm->bins[hash], key);
}