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
|
#include "hmap.h"
#include <errno.h>
#include <stdbool.h>
#include <string.h>
static inline size_t
hmap_key_bucket(const struct hmap *map, struct hmap_key key)
{
return map->hash(key) % map->bucketcnt;
}
int
hmap_init(struct hmap *map, size_t size, hmap_hash_func hasher,
hmap_keycmp_func keycmp, const struct allocator *allocator)
{
int rc;
LIBHMAP_ABORT_ON_ARGS(!size || !hasher || !keycmp || !allocator);
map->allocator = allocator;
map->keycmp = keycmp;
map->bucketcnt = size;
map->hash = hasher;
map->buckets = map->allocator->alloc(map->allocator,
sizeof(void *) * size, &rc);
if (!map->buckets) return -rc;
memset(map->buckets, 0, size * sizeof(void *));
return 0;
}
int
hmap_deinit(struct hmap *map)
{
LIBHMAP_ABORT_ON_ARGS(!map);
hmap_clear(map);
return map->allocator->free(map->allocator, map->buckets);
}
struct hmap *
hmap_alloc(size_t size, hmap_hash_func hasher,
hmap_keycmp_func keycmp, const struct allocator *allocator, int *_rc)
{
struct hmap *map;
int *rc, stub;
LIBHMAP_ABORT_ON_ARGS(!allocator);
rc = _rc ? _rc : &stub;
map = allocator->alloc(allocator, sizeof(struct hmap), rc);
if (!map && _rc) *rc = -*rc;
if (!map) return NULL;
*rc = hmap_init(map, size, hasher, keycmp, allocator);
if (*rc) {
allocator->free(allocator, map);
return NULL;
}
return map;
}
int
hmap_free(struct hmap *map)
{
const struct allocator *allocator;
int rc;
LIBHMAP_ABORT_ON_ARGS(!map);
allocator = map->allocator;
rc = hmap_deinit(map);
if (rc) return rc;
rc = allocator->free(allocator, map);
if (rc) return -rc;
return 0;
}
int
hmap_copy(const struct hmap *dst, const struct hmap *src)
{
struct hmap_iter iter;
int rc;
LIBHMAP_ABORT_ON_ARGS(!dst || !src);
for (HMAP_ITER(src, iter)) {
rc = hmap_set(dst, iter.link->key, iter.link->value);
if (rc) return rc;
}
return 0;
}
void
hmap_swap(struct hmap *m1, struct hmap *m2)
{
struct hmap tmp;
LIBHMAP_ABORT_ON_ARGS(!m1 || !m2);
memcpy(&tmp, m1, sizeof(struct hmap));
memcpy(m1, m2, sizeof(struct hmap));
memcpy(m2, &tmp, sizeof(struct hmap));
}
void
hmap_clear(const struct hmap *map)
{
struct hmap_iter iter;
struct hmap_link *prev;
size_t i;
LIBHMAP_ABORT_ON_ARGS(!map);
prev = NULL;
for (HMAP_ITER(map, iter)) {
map->allocator->free(map->allocator, prev);
prev = iter.link;
}
map->allocator->free(map->allocator, prev);
for (i = 0; i < map->bucketcnt; i++)
map->buckets[i] = NULL;
}
struct hmap_link **
hmap_link_get(const struct hmap *map, struct hmap_key key)
{
struct hmap_link **iter, *link;
LIBHMAP_ABORT_ON_ARGS(!map);
iter = &map->buckets[hmap_key_bucket(map, key)];
while (*iter != NULL) {
link = *iter;
if (map->keycmp(link->key, key))
return iter;
iter = &(*iter)->next;
}
return NULL;
}
struct hmap_link **
hmap_link_pos(const struct hmap *map, struct hmap_key key)
{
struct hmap_link **iter, *link;
LIBHMAP_ABORT_ON_ARGS(!map);
iter = &map->buckets[hmap_key_bucket(map, key)];
while (*iter != NULL) {
link = *iter;
if (map->keycmp(link->key, key))
return iter;
iter = &(*iter)->next;
}
return iter;
}
struct hmap_link *
hmap_link_pop(const struct hmap *map, struct hmap_link **link)
{
struct hmap_link *tmp;
LIBHMAP_ABORT_ON_ARGS(!map || !link);
tmp = *link;
*link = (*link)->next;
return tmp;
}
struct hmap_link *
hmap_link_alloc(const struct hmap *map,
struct hmap_key key, struct hmap_val value, int *rc)
{
struct hmap_link *link;
LIBHMAP_ABORT_ON_ARGS(!map);
link = map->allocator->alloc(map->allocator,
sizeof(struct hmap_link), rc);
if (!link && rc) *rc = -*rc;
if (!link) return NULL;
link->key = key;
link->value = value;
link->next = NULL;
return link;
}
struct hmap_link *
hmap_get(const struct hmap *map, struct hmap_key key)
{
struct hmap_link **iter;
LIBHMAP_ABORT_ON_ARGS(!map);
iter = hmap_link_get(map, key);
return iter ? *iter : NULL;
}
int
hmap_set(const struct hmap *map, struct hmap_key key, struct hmap_val value)
{
struct hmap_link **iter;
LIBHMAP_ABORT_ON_ARGS(!map);
iter = hmap_link_pos(map, key);
if (!*iter) return HMAP_KEY_MISSING;
(*iter)->value = value;
return 0;
}
int
hmap_rm(const struct hmap *map, struct hmap_key key)
{
struct hmap_link *link, **linkp;
int rc;
LIBHMAP_ABORT_ON_ARGS(!map);
linkp = hmap_link_get(map, key);
if (!linkp) return HMAP_KEY_MISSING;
link = hmap_link_pop(map, linkp);
rc = map->allocator->free(map->allocator, link);
if (rc) return -rc;
return 0;
}
int
hmap_add(const struct hmap *map, struct hmap_key key, struct hmap_val value)
{
struct hmap_link **iter;
int rc;
LIBHMAP_ABORT_ON_ARGS(!map);
iter = hmap_link_pos(map, key);
if (*iter) return HMAP_KEY_EXISTS;
*iter = hmap_link_alloc(map, key, value, &rc);
if (!*iter) return rc;
return 0;
}
void
hmap_iter_init(struct hmap_iter *iter)
{
LIBHMAP_ABORT_ON_ARGS(!iter);
iter->bucket = 0;
iter->link = NULL;
}
bool
hmap_iter_next(const struct hmap *map, struct hmap_iter *iter)
{
size_t i;
LIBHMAP_ABORT_ON_ARGS(!map || !iter);
if (iter->link && iter->link->next) {
iter->link = iter->link->next;
return true;
}
i = iter->bucket + (iter->link ? 1 : 0);
for (; i < map->bucketcnt; i++) {
if (map->buckets[i]) {
iter->bucket = i;
iter->link = map->buckets[i];
return true;
}
}
iter->link = NULL;
return false;
}
uint32_t
hmap_str_hash(struct hmap_key key)
{
const char *c;
uint32_t hash;
LIBHMAP_ABORT_ON_ARGS(!key.p);
hash = 5381;
for (c = key.p; *c; c++)
hash = 33 * hash + (uint8_t) *c;
return hash;
}
bool
hmap_str_keycmp(struct hmap_key k1, struct hmap_key k2)
{
LIBHMAP_ABORT_ON_ARGS(!k1.p || !k2.p);
return !strcmp(k1.p, k2.p);
}
|