-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_hashmap.h
More file actions
330 lines (243 loc) · 6.41 KB
/
Copy pathlc_hashmap.h
File metadata and controls
330 lines (243 loc) · 6.41 KB
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
#ifndef LC_HM_H
#define LC_HM_H
#ifndef LC_HM_INIT_SIZE
#define LC_HM_INIT_SIZE 16
#endif
#ifndef LC_HM_LOAD_FACTOR_HIGH
#define LC_HM_LOAD_FACTOR_HIGH 70
#endif
#ifndef LC_HM_LOAD_FACTOR_LOW
#define LC_HM_LOAD_FACTOR_LOW 50
#endif
#ifndef LC_HM_API
#define LC_HM_API extern
#endif
#include <stddef.h>
#include <stdbool.h>
struct lc_hash_entry {
size_t keylen;
const char *key;
const void *val;
};
struct lc_hashmap {
size_t count;
size_t capacity;
struct lc_hash_entry *buckets;
#ifdef LC_HM_CUSTOM_ALLOC
void *alloc_ctx;
void *(*realloc)(void *ctx, void *p, size_t size);
#endif
};
LC_HM_API bool lc_hashmap_get(const struct lc_hashmap *map, size_t n, const char key[restrict n], void *restrict out);
LC_HM_API bool lc_hashmap_put(struct lc_hashmap *map, size_t n, const char key[restrict n], const void *val, void *out);
LC_HM_API bool lc_hashmap_del(struct lc_hashmap *map, size_t n, const char key[restrict n], void *restrict out);
LC_HM_API bool lc_hashmap_iter(const struct lc_hashmap *map, size_t *it);
/**
* @breif Iterate the hashmap
*
* The function was designed to be used in the condition in for loop.
*
* @param map[in] Hashmap to iterate
* @param it[out] Pointer to index as iterator
*
* @return false, if iteration is done
*/
LC_HM_API bool lc_hashmap_iter(const struct lc_hashmap *map, size_t *it);
#if 0
LC_HM_API bool lc_hashmap_iter(const struct lc_hashmap *map, size_t *it);
// Example iteration
struct lc_hashmap map = {0};
/// ...
for (size_t i = 0; lc_hashmap_iter(&map, &i); i++) {
const void *key = map.buckets[i].key;
const void *val = map.buckets[i].val;
}
#endif
#ifdef LC_HAVE_SV
static inline bool
lc_hashmap_get_sv(const struct lc_hashmap *map, struct lc_sv key, void *restrict out)
{
return lc_hashmap_get(map, key.length, key.s, out);
}
static inline bool
lc_hashmap_put_sv(const struct lc_hashmap *map, struct lc_sv key, const void *val, void *out)
{
return lc_hashmap_put(map, key.length, key.s, val, out);
}
static inline bool
lc_hashmap_del_sv(const struct lc_hashmap *map, struct lc_sv key, void *restrict out)
{
return lc_hashmap_del(map, key.length, key.s, out);
}
#endif
#ifdef LC_IMPLEMENTATION
#ifdef LC_HM_CUSTOM_ALLOC
static void *
_lc_hm_realloc(void *_, void *p, size_t size)
{
extern void *memset(void *, int, size_t);
extern void *realloc(void *, size_t);
extern void free(void *);
if (size)
return memset(realloc(p, size), 0, size);
free(p);
return 0;
}
#define lc_hm_calloc(n, size) map->realloc(map->alloc_ctx, 0, (n) * (size))
#define lc_hm_free(p) map->realloc(map->alloc_ctx, p, 0)
#else
extern void *calloc(size_t n, size_t size);
extern void free(void *);
#define lc_hm_calloc(n, size) calloc(n, size)
#define lc_hm_free(p) free(p)
#endif
#include <stdint.h>
#ifdef LC_HM_FNV1A_64BIT
typedef uint64_t _lc_hm_hash;
#define FNV1_PRIME 0xCBF29CE484222325
#define FNV1_OFFSET 0x00000100000001B3
#else
typedef uint32_t _lc_hm_hash;
#define FNV1_PRIME 0x01000193
#define FNV1_OFFSET 0X811C9DC5
#endif
static inline _lc_hm_hash
_lc_hm_fnv1a_hash(size_t len, const char s[len])
{
_lc_hm_hash hash = FNV1_PRIME;
for (int i = 0; i < len; i++) {
hash ^= (uint8_t)s[i];
hash *= FNV1_OFFSET;
}
return hash;
}
static inline bool
key_entry_cmp(
const struct lc_hash_entry *entry,
size_t n,
const char key[restrict n]
)
{
extern int memcmp(const void *, const void *, size_t);
return entry->keylen == n
&& 0 == memcmp(entry->key, key, n);
}
static size_t
find_bucket(const struct lc_hashmap *map, size_t n, const char key[restrict n])
{
const _lc_hm_hash hash = _lc_hm_fnv1a_hash(n, key);
size_t i = hash % map->capacity;
while (map->buckets[i].key && !key_entry_cmp(&map->buckets[i], n, key))
i++, i %= map->capacity;
return i;
}
static void
rehash(struct lc_hashmap *map)
{
size_t keys = 0;
for (size_t i = 0; i < map->capacity; i++)
if (map->buckets[i].key)
keys++;
size_t capacity = map->capacity;
while ((keys * 100) / capacity >= LC_HM_LOAD_FACTOR_LOW)
capacity *= 2;
struct lc_hashmap new_map = {
.buckets = lc_hm_calloc(capacity, sizeof new_map.buckets[0]),
.capacity = capacity,
};
for (size_t i = 0; i < map->capacity; i++) {
struct lc_hash_entry *entry = &map->buckets[i];
if (!entry->key)
continue;
lc_hashmap_put(&new_map, entry->keylen, entry->key, entry->val, &(void *){0});
}
#ifdef assert
assert(new_map.count == keys);
#endif
lc_hm_free(map->buckets);
*map = new_map;
}
LC_HM_API bool
lc_hashmap_get(const struct lc_hashmap *map, size_t n, const char key[restrict n], void *restrict out)
{
size_t i = find_bucket(map, n, key);
const void **o = out;
if (map->buckets[i].key)
return (*o = map->buckets[i].val), true;
return (*o = 0);
}
LC_HM_API bool
lc_hashmap_put(struct lc_hashmap *map, size_t n, const char key[restrict n], const void *val, void *out)
{
#ifdef LC_HM_CUSTOM_ALLOC
if (!map->realloc)
map->realloc = _lc_hm_realloc;
#endif
if (!map->buckets) {
map->buckets = lc_hm_calloc(LC_HM_INIT_SIZE, sizeof map->buckets[0]);
map->capacity = LC_HM_INIT_SIZE;
}
size_t i = find_bucket(map, n, key);
const void **o = out;
if (map->buckets[i].key) {
*o = map->buckets[i].val;
map->buckets[i].val = val;
return true;
}
if ((map->count * 100) / map->capacity >= LC_HM_LOAD_FACTOR_HIGH) {
rehash(map);
i = find_bucket(map, n, key);
}
map->count++;
map->buckets[i].key = key;
map->buckets[i].keylen = n;
map->buckets[i].val = val;
return false;
}
LC_HM_API bool
lc_hashmap_del(struct lc_hashmap *map, size_t n, const char key[restrict n], void *restrict out)
{
size_t i = find_bucket(map, n, key);
if (!map->buckets[i].key)
return false;
const void **o = out;
*o = map->buckets[i].val;
map->buckets[i] = (struct lc_hash_entry){0};
map->count--;
size_t j = i;
for (;;) {
j += 1;
j %= map->capacity;
if (!map->buckets[j].key)
break;
const size_t k = _lc_hm_fnv1a_hash(map->buckets[j].keylen, map->buckets[j].key) % map->capacity;
if (i <= j) {
if (i < k && k <= j)
continue;
} else {
if (k <= j || i < k)
continue;
}
map->buckets[i] = map->buckets[j];
map->buckets[j] = (struct lc_hash_entry){0};
i = j;
}
return true;
}
LC_HM_API bool
lc_hashmap_iter(const struct lc_hashmap *map, size_t *it)
{
if (map->capacity <= *it)
return false;
for (size_t i = *it; i < map->capacity; i++) {
if (!map->buckets[i].key)
continue;
*it = i;
return true;
}
*it = map->capacity;
return false;
}
#endif
// vim: set ft=c:
#endif