-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.h
269 lines (219 loc) · 5.04 KB
/
hashmap.h
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
#ifndef HASHMAP_H
#define HASHMAP_H
#include "common.h"
typedef struct HMNode {
struct HMNode *next;
char *key;
void *data;
} HMNode;
typedef struct HMIter {
struct HMNode *cur;
u64 idx;
} HMIter;
typedef struct HashMap {
HMNode **map;
u64 *idx_map;
u64 idx_map_size;
u64 size;
u64 capacity;
} HashMap;
HashMap *hm_sized_init(u64 capacity) {
HashMap *hm = (HashMap *)calloc(1, sizeof(HashMap));
hm->size = 0;
hm->idx_map_size = 0;
hm->capacity = capacity;
hm->map = (HMNode **)calloc(hm->capacity, sizeof(HMNode *));
hm->idx_map = (u64 *)calloc(hm->capacity, sizeof(u64));
return hm;
}
HashMap *hm_init() {
HashMap *hm = hm_sized_init(1);
return hm;
}
void print_idx_map(HashMap *hm) {
printf("[PRINT_IDX_MAP] idx_map size: %llu, map size: %llu\n", hm->idx_map_size, hm->size);
for (u64 i = 0; i < hm->idx_map_size; i++) {
printf("[PRINT_IDX_MAP] %llu\n", hm->idx_map[i]);
}
}
void print_hm(HashMap *hm) {
for (u64 i = 0; i < hm->idx_map_size; i++) {
HMNode *bucket = hm->map[hm->idx_map[i]];
printf("[PRINT_MAP] key: %s\n", bucket->key);
u64 sub_key = 0;
while (bucket->next != NULL) {
printf("[PRINT_MAP] key: %s\n", bucket->next->key);
bucket = bucket->next;
sub_key += 1;
}
printf("[PRINT_MAP] sub_key count: %llu\n", sub_key);
}
}
char *hm_iter_key(HashMap *hm, HMIter *iter) {
if (iter->idx >= hm->idx_map_size) {
return NULL;
}
if (!iter->cur) {
iter->cur = hm->map[hm->idx_map[iter->idx]];
return iter->cur->key;
}
if (iter->cur->next) {
iter->cur = iter->cur->next;
return iter->cur->key;
}
iter->idx++;
if (iter->idx == hm->idx_map_size) {
return NULL;
}
iter->cur = hm->map[hm->idx_map[iter->idx]];
return iter->cur->key;
}
u64 hm_hash(HashMap *hm, char *key) {
u64 hash = 0;
u64 key_len = strlen(key);
for (u32 i = 0; i < key_len; i++) {
hash = (hash << 4) ^ (hash >> 28) ^ (u64)key[i];
}
hash = hash % hm->capacity;
return hash;
}
HMNode *new_hmnode(char *key, void *value) {
HMNode *tmp = (HMNode *)malloc(sizeof(HMNode));
tmp->data = value;
tmp->key = strdup(key);
tmp->next = NULL;
return tmp;
}
HashMap *hm_grow_capacity(HashMap *hm, u64 capacity);
void hm_insert(HashMap **hm, char *key, void *value) {
if ((*hm)->size > (((*hm)->capacity >> 2) + ((*hm)->capacity >> 1))) {
*hm = hm_grow_capacity(*hm, (*hm)->capacity * 2);
}
u64 idx = hm_hash(*hm, key);
if (!((*hm)->map[idx])) {
(*hm)->map[idx] = new_hmnode(key, value);
(*hm)->idx_map[(*hm)->idx_map_size] = idx;
(*hm)->idx_map_size++;
(*hm)->size++;
return;
}
HMNode *tmp = (*hm)->map[idx];
if (!strcmp(tmp->key, key)) {
return;
}
while (tmp->next) {
tmp = tmp->next;
if (!strcmp(tmp->key, key)) {
return;
}
}
tmp->next = new_hmnode(key, value);
(*hm)->size++;
}
void hn_free(HMNode *bucket) {
free(bucket->key);
free(bucket);
}
void hn_free_data(HMNode *bucket) {
free(bucket->data);
hn_free(bucket);
}
HashMap *hm_grow_capacity(HashMap *hm, u64 capacity) {
debug("[HM] growing capacity from %llu to %llu because size is %llu\n", hm->capacity, hm->capacity * 2, hm->size);
HashMap *new_hm = hm_sized_init(capacity);
for (u64 i = 0; i < hm->idx_map_size; i++) {
HMNode *bucket = hm->map[hm->idx_map[i]];
HMNode *tmp = bucket;
while (tmp->next) {
hm_insert(&new_hm, tmp->key, tmp->data);
HMNode *prev = tmp;
tmp = tmp->next;
hn_free(prev);
}
hm_insert(&new_hm, tmp->key, tmp->data);
hn_free(tmp);
}
free(hm->map);
free(hm->idx_map);
free(hm);
return new_hm;
}
static HMNode *_hm_get(HashMap *hm, u64 idx, char *key) {
HMNode *bucket = hm->map[idx];
if (!bucket) {
debug("1 key %s, hm->map[%llu] is NULL?\n", key, idx);
return NULL;
}
if (!strcmp(bucket->key, key)) {
return bucket;
}
HMNode *tmp = bucket;
while (tmp->next) {
tmp = tmp->next;
if (!strcmp(tmp->key, key)) {
return tmp;
}
}
debug("2 key %s, hm->map[%llu] is NULL?\n", key, idx);
return NULL;
}
void *hm_get(HashMap *hm, char *key) {
u64 idx = hm_hash(hm, key);
HMNode *ret = _hm_get(hm, idx, key);
if (ret) {
return ret->data;
}
return NULL;
}
bool hm_remove(HashMap *hm, char *key) {
u64 idx = hm_hash(hm, key);
HMNode *node = _hm_get(hm, idx, key);
if (!node) {
return false;
}
HMNode *tmp = node->next;
if (!tmp) {
u64 size = hm->idx_map_size;
u64 *idx_map = hm->idx_map;
idx_map[idx] = idx_map[size - 1];
idx_map[size] = -1;
hm->idx_map_size -= 1;
}
hn_free(node);
hm->map[idx] = tmp;
hm->size--;
return true;
}
void hm_free(HashMap *hm) {
for (u64 i = 0; i < hm->idx_map_size; i++) {
HMNode *bucket = hm->map[hm->idx_map[i]];
HMNode *tmp = bucket;
HMNode *prev = tmp;
while (tmp->next) {
prev = tmp;
tmp = tmp->next;
hn_free(prev);
}
hn_free(tmp);
}
free(hm->map);
free(hm->idx_map);
free(hm);
}
void hm_free_data(HashMap *hm) {
for (u64 i = 0; i < hm->idx_map_size; i++) {
HMNode *bucket = hm->map[hm->idx_map[i]];
HMNode *tmp = bucket;
HMNode *prev = tmp;
while (tmp->next) {
prev = tmp;
tmp = tmp->next;
hn_free_data(prev);
}
hn_free_data(tmp);
}
free(hm->map);
free(hm->idx_map);
free(hm);
}
#endif