aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 03:00:48 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-06 07:09:55 +0000
commit4cef63789da7e80e3721699c92e090ab1d4fb1cc (patch)
treeb66c79fc35f6fb39fe31850159896e14db2fb96f
parentfeat(hash): add hashing functions (diff)
downloadcodebase-4cef63789da7e80e3721699c92e090ab1d4fb1cc.tar.gz
feat(hm): add hash map
-rw-r--r--include/hm.h137
-rw-r--r--src/hm.c391
2 files changed, 528 insertions, 0 deletions
diff --git a/include/hm.h b/include/hm.h
new file mode 100644
index 0000000..d6cdeb7
--- /dev/null
+++ b/include/hm.h
@@ -0,0 +1,137 @@
1// hm.h --- Hash Map -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// Code:
12
13#ifndef INCLUDED_COSMOS_HM_H
14#define INCLUDED_COSMOS_HM_H
15
16#include "ms.h"
17#include "gpa.h"
18
19#define HM_NO_FEAT_CSTR // TEMP(cmmm): actually implement.
20#define HM_NO_FEAT_EMB // TEMP(cmmm): actually implement.
21
22#ifndef HM_HASH
23#include "hash.h"
24#include "bits.h"
25#define HM_HASH(init, mv) hash_fnv1a_64_update (init, (mv).beg, mv_sz (mv))
26#define HM_INIT_HASH(seed) (HASH_FNV1A_64_INIT ^ splitmix64 (seed))
27#endif // HM_HASH
28
29#ifndef HM_INIT_HASH
30#error "HM_INIT_HASH not defined, while HM_HASH is user defined"
31#endif // HM_INIT_HASH
32
33#ifndef HM_INIT_SLOTS
34#define HM_INIT_SLOTS 256
35#endif // HM_INIT_SLOTS
36
37union _hm_key {
38 const void *ptr;
39 /* */ ptrdiff ofs;
40};
41
42struct _hm_slot {
43 /* */ u64 hash;
44 union _hm_key key;
45};
46
47struct hm {
48 struct gpa _gpa;
49 struct _hm_slot *_slots;
50 /* */ u64 _hash_init;
51 /* */ u32 _resize_at;
52 /* */ u32 _max_probe;
53 /* */ u32 _live_cnt;
54 /* */ u32 _tomb_cnt;
55 /* */ u32 _slot_cnt;
56 /* */ u32 _slot_msk;
57};
58
59#define HM_REL_NEW PTRDIFF_MIN
60
61#define HM_FROM(GPA) ((struct hm) { ._gpa = GPA });
62
63void hm_init (struct hm *, struct gpa);
64
65const struct mv **hm_upsert ( struct hm *, struct mv key);
66const struct mv *hm_lookup (const struct hm *, struct mv key);
67/* */ int hm_insert ( struct hm *, const struct mv *key);
68/* */ int hm_delete ( struct hm *, struct mv key);
69
70typedef const struct mv *const*hm_iter;
71hm_iter hm_iter_init (const struct hm *);
72hm_iter hm_iter_next (const struct hm *, hm_iter);
73
74/* */ ptrdiff *hm_rel_upsert ( struct hm *, struct mv key, const struct mv *base);
75const struct mv *hm_rel_lookup (const struct hm *, struct mv key, const struct mv *base);
76/* */ int hm_rel_insert ( struct hm *, const struct mv *key, const struct mv *base);
77/* */ int hm_rel_delete ( struct hm *, struct mv key, const struct mv *base);
78
79// hm_iter hm_rel_iter_init (const struct hm *, const struct mv *base);
80// hm_iter hm_rel_iter_next (const struct hm *, hm_iter, const struct mv *base);
81
82void hm_drop (struct hm *);
83void hm_clear (struct hm *);
84
85#ifndef HM_NO_FEAT_CSTR
86
87char **hm_upsert_cstr (struct hm *, char *key);
88char **hm_lookup_cstr (struct hm *, char *key);
89int hm_insert_cstr (struct hm *, char **key);
90int hm_delete_cstr (struct hm *, char *key);
91
92ptrdiff *hm_upsert_rel_cstr (struct hm *, char *key, char **base);
93int hm_insert_rel_cstr (struct hm *, char **key, char **base);
94char **hm_lookup_rel_cstr (struct hm *, char *key, char **base);
95int hm_delete_rel_cstr (struct hm *, char *key, char **base);
96
97#endif // HM_NO_FEAT_CSTR
98
99#ifndef HM_NO_FEAT_EMB
100
101void **hm_upsert_emb (struct hm *, void *key, usz key_sz);
102void **hm_lookup_emb (struct hm *, void *key, usz key_sz);
103int hm_insert_emb (struct hm *, void **key, usz key_sz);
104int hm_delete_emb (struct hm *, void *key, usz key_sz);
105
106ptrdiff *hm_upsert_rel_emb (struct hm *, void *key, usz key_sz, void **base);
107int hm_insert_rel_emb (struct hm *, void **key, usz key_sz, void **base);
108void **hm_lookup_rel_emb (struct hm *, void *key, usz key_sz, void **base);
109int hm_delete_rel_emb (struct hm *, void *key, usz key_sz, void **base);
110
111#endif // HM_NO_FEAT_CSTR
112
113#ifdef HM_INCLUDE_INTERNAL
114
115enum _hm_key_kind {
116 _HM_KEY_KIND_INVALID,
117 _HM_KEY_KIND_MV,
118 _HM_KEY_KIND_CSTR,
119 _HM_KEY_KIND_EMB,
120 _HM_KEY_KIND_CNT
121};
122
123#define _HM_SLOT_EMPTY 0
124#define _HM_SLOT_TOMB 1
125
126u64 _hm_hash (const struct hm *, struct mv);
127void _hm_resize (struct hm *, u32 new_slot_cnt);
128struct _hm_slot *_hm_slot (const struct hm *, const void *base, u64 hash, struct mv key, enum _hm_key_kind);
129struct _hm_slot *_hm_slot_ensure ( struct hm *, const void *base, u64 *hash, struct mv key, enum _hm_key_kind);
130usz _hm_key_sz (struct mv key, enum _hm_key_kind);
131struct mv _hm_key_ptr_to_mv (const void *, enum _hm_key_kind, usz key_sz);
132
133#endif // HM_INCLUDE_INTERNAL
134
135#endif // INCLUDED_COSMOS_HM_H
136
137// hm.h ends here
diff --git a/src/hm.c b/src/hm.c
new file mode 100644
index 0000000..3421564
--- /dev/null
+++ b/src/hm.c
@@ -0,0 +1,391 @@
1// hm.c --- Hash Map -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// Code:
12
13#define HM_INCLUDE_INTERNAL
14#include "hm.h"
15#include "dbg.h"
16
17#include <string.h> // TODO(cmmm): drop libc dep (memset)
18
19void
20hm_init (struct hm *hm, struct gpa gpa)
21{
22 DBG_ASSERT_NN (hm);
23 hm->_gpa = gpa;
24}
25
26const struct mv **
27hm_upsert (struct hm *hm, struct mv key)
28{
29 DBG_ASSERT_NN (hm);
30 DBG_ASSERT_MV (key);
31
32 u64 hash = _hm_hash (hm, key);
33 // DBG_TRACE ("info", "hash: %lu", hash);
34 struct _hm_slot *slot =
35 _hm_slot_ensure (hm, NULL, &hash, key, _HM_KEY_KIND_MV);
36 DBG_ASSERT_NN (slot);
37
38 if (slot->hash <= _HM_SLOT_TOMB) {
39 hm->_live_cnt += 1;
40 if (slot->hash == _HM_SLOT_TOMB)
41 hm->_tomb_cnt -= 1;
42 slot->hash = hash;
43 slot->key.ptr = NULL;
44 }
45
46 return (void *) &slot->key.ptr;
47}
48
49int
50hm_insert (struct hm *hm, const struct mv *key)
51{
52 DBG_ASSERT_NN ( hm);
53 DBG_ASSERT_NN ( key);
54 DBG_ASSERT_MV (*key);
55 DBG_ASSERT (!mv_eq (*key, MV_NULL));
56
57 u64 hash = _hm_hash (hm, *key);
58 struct _hm_slot *slot =
59 _hm_slot_ensure (hm, NULL, &hash, *key, _HM_KEY_KIND_MV);
60 DBG_ASSERT_NN (slot);
61
62 if (slot->hash > _HM_SLOT_TOMB) return 0;
63 if (slot->hash == _HM_SLOT_TOMB) hm->_tomb_cnt -= 1;
64
65 hm->_live_cnt += 1;
66 slot->hash = hash;
67 slot->key.ptr = key;
68
69 return 1;
70}
71
72const struct mv *
73hm_lookup (const struct hm *hm, struct mv key)
74{
75 DBG_ASSERT_NN (hm);
76 DBG_ASSERT_MV (key);
77
78 const struct _hm_slot *slot =
79 _hm_slot (hm, NULL, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);
80
81 if (slot == NULL) return NULL;
82 if (slot->hash <= _HM_SLOT_TOMB) return NULL;
83
84 return slot->key.ptr;
85}
86
87int
88hm_delete (struct hm *hm, struct mv key)
89{
90 DBG_ASSERT_NN (hm);
91 DBG_ASSERT_MV (key);
92
93 struct _hm_slot *slot =
94 _hm_slot (hm, NULL, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);
95
96 if (slot == NULL) return 0;
97 if (slot->hash <= _HM_SLOT_TOMB) return 0;
98
99 hm->_tomb_cnt += 1;
100 hm->_live_cnt -= 1;
101 slot->hash = _HM_SLOT_TOMB;
102
103 return 1;
104}
105
106hm_iter
107hm_iter_init (const struct hm *hm)
108{
109 DBG_ASSERT_NN (hm);
110 struct _hm_slot *slot = hm->_slots;
111 struct _hm_slot *slots_end = hm->_slots + hm->_slot_cnt;
112 while (slot < slots_end) {
113 if (slot->hash > _HM_SLOT_TOMB)
114 return (hm_iter) &slot->key.ptr;
115 slot += 1;
116 } return NULL;
117}
118
119hm_iter
120hm_iter_next (const struct hm *hm, hm_iter iter)
121{
122 DBG_ASSERT_NN (hm);
123 DBG_ASSERT_NN (iter);
124 struct _hm_slot *slot = CONTAINER_OF (iter, *slot, key);
125 struct _hm_slot *slots_end = hm->_slots + hm->_slot_cnt;
126 while (++slot < slots_end) {
127 if (slot->hash > _HM_SLOT_TOMB)
128 return (hm_iter) &slot->key.ptr;
129 } return NULL;
130}
131
132ptrdiff *
133hm_rel_upsert (struct hm *hm, struct mv key, const struct mv *base)
134{
135 DBG_ASSERT_NN (hm);
136 DBG_ASSERT_MV (key);
137 DBG_ASSERT_NN (base);
138
139 u64 hash = _hm_hash (hm, key);
140 // DBG_TRACE ("info", "hash: %lu", hash);
141 struct _hm_slot *slot =
142 _hm_slot_ensure (hm, base, &hash, key, _HM_KEY_KIND_MV);
143 DBG_ASSERT_NN (slot);
144
145 if (slot->hash <= _HM_SLOT_TOMB) {
146 hm->_live_cnt += 1;
147 if (slot->hash == _HM_SLOT_TOMB)
148 hm->_tomb_cnt -= 1;
149 slot->hash = hash;
150 slot->key.ofs = HM_REL_NEW;
151 }
152
153 return &slot->key.ofs;
154}
155
156int
157hm_rel_insert (struct hm *hm, const struct mv *key, const struct mv *base)
158{
159 DBG_ASSERT_NN ( hm);
160 DBG_ASSERT_NN ( key);
161 DBG_ASSERT_MV (*key);
162 DBG_ASSERT_NN ( base);
163
164 u64 hash = _hm_hash (hm, *key);
165 struct _hm_slot *slot =
166 _hm_slot_ensure (hm, base, &hash, *key, _HM_KEY_KIND_MV);
167 DBG_ASSERT_NN (slot);
168
169 if (slot->hash > _HM_SLOT_TOMB) return 0;
170 if (slot->hash == _HM_SLOT_TOMB) hm->_tomb_cnt -= 1;
171 hm->_live_cnt += 1;
172 slot->hash = hash;
173 slot->key.ofs = (const u8 *) key - (const u8 *) base;
174
175 return 1;
176}
177
178const struct mv *
179hm_rel_lookup (const struct hm *hm, struct mv key, const struct mv *base)
180{
181 DBG_ASSERT_NN (hm);
182 DBG_ASSERT_MV (key);
183 DBG_ASSERT_NN (base);
184
185 const struct _hm_slot *slot =
186 _hm_slot (hm, base, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);
187
188 if (slot == NULL) return NULL;
189 if (slot->hash <= _HM_SLOT_TOMB) return NULL;
190
191 return (void *) ((const u8 *) base + slot->key.ofs);
192}
193
194int
195hm_rel_delete (struct hm *hm, struct mv key, const struct mv *base)
196{
197 DBG_ASSERT_NN (hm);
198 DBG_ASSERT_MV (key);
199 DBG_ASSERT_NN (base);
200
201 struct _hm_slot *slot =
202 _hm_slot (hm, base, _hm_hash (hm, key), key, _HM_KEY_KIND_MV);
203
204 if (slot == NULL) return 0;
205 if (slot->hash <= _HM_SLOT_TOMB) return 0;
206
207 hm->_tomb_cnt += 1;
208 hm->_live_cnt -= 1;
209 slot->hash = _HM_SLOT_TOMB;
210
211 return 1;
212}
213
214const struct mv *const*hm_rel_iter_init (const struct hm *);
215const struct mv *const*hm_rel_iter_next (const struct hm *, const struct mv *const*);
216
217void
218hm_drop (struct hm *hm)
219{
220 DBG_ASSERT_NN (hm);
221 GPA_DROP_N (hm->_gpa, hm->_slots, hm->_slot_cnt);
222 hm->_slot_cnt = 0;
223 hm->_tomb_cnt = 0;
224}
225
226void
227hm_clear (struct hm *hm)
228{
229 DBG_ASSERT_NN (hm);
230 hm->_tomb_cnt = 0;
231 memset (hm->_slots, 0, hm->_slot_cnt*sizeof (*hm->_slots));
232}
233
234/* ================================ INTERNAL ================================ */
235
236void
237_hm_resize (struct hm *hm, u32 new_slot_cnt)
238{
239 DBG_ASSERT_NN (hm);
240 DBG_ASSERT_EQ (POPCOUNT (new_slot_cnt), 1, "%u", "%u");
241
242 u32 new_mask = new_slot_cnt - 1;
243 struct _hm_slot *new_slots = GPA_GRAB_N0 (hm->_gpa, new_slot_cnt, *new_slots);
244 struct _hm_slot *slots_end = &hm->_slots[hm->_slot_cnt];
245 u32 live_left = hm->_live_cnt;
246 for (struct _hm_slot *slot = hm->_slots; slot < slots_end; ++slot) {
247 if (slot->hash <= _HM_SLOT_TOMB)
248 continue;
249
250 u32 new_slot_idx = slot->hash & new_mask;
251 while (new_slots[new_slot_idx].hash != _HM_SLOT_EMPTY)
252 new_slot_idx = (new_slot_idx + 1) & new_mask;
253
254 new_slots[new_slot_idx] = *slot;
255
256 live_left -= 1;
257 if (live_left == 0)
258 break;
259 }
260
261 if (hm->_slots)
262 GPA_DROP_N (hm->_gpa, hm->_slots, hm->_slot_cnt);
263
264 hm->_tomb_cnt = 0;
265 hm->_slot_cnt = new_slot_cnt;
266 hm->_slot_msk = new_slot_cnt - 1;
267 hm->_resize_at = 4*new_slot_cnt/3;
268 hm->_slots = new_slots;
269}
270
271struct _hm_slot *
272_hm_slot (const struct hm *hm, const void *base, u64 hash,
273 struct mv key, enum _hm_key_kind key_kind)
274{
275 DBG_ASSERT_NN (hm);
276 DBG_ASSERT_NN (key.beg);
277 DBG_ASSERT_LT (key.beg, key.end, "%p", "%p");
278
279 if (hm->_slots == NULL)
280 return NULL;
281
282 u32 mask = hm->_slot_msk;
283 u32 slot_idx = hash & mask;
284 struct _hm_slot *slots = hm->_slots;
285 for (u32 i = 0; i < hm->_max_probe; ++i) {
286 u64 slot_hash = slots[slot_idx].hash;
287
288 if (slot_hash == _HM_SLOT_EMPTY)
289 return NULL;
290
291 if (slot_hash == hash) {
292 struct mv slot_key = _hm_key_ptr_to_mv (
293 base + slots[slot_idx].key.ofs, key_kind, _hm_key_sz (key, key_kind));
294 if (mv_equal (slot_key, key))
295 return &slots[slot_idx];
296 }
297
298 slot_idx = (slot_idx + 1) & mask;
299 }
300
301 return NULL;
302}
303
304struct _hm_slot *
305_hm_slot_ensure (struct hm *hm, const void *base, u64 *hash,
306 struct mv key, enum _hm_key_kind key_kind)
307{
308 DBG_ASSERT_NN (hm);
309 DBG_ASSERT_NN (hash);
310 DBG_ASSERT_IN (0, key_kind, _HM_KEY_KIND_CNT, "%d", "%d", "%d");
311
312 if (!hm->_slots) {
313 hm->_slot_cnt = HM_INIT_SLOTS;
314 hm->_slot_msk = HM_INIT_SLOTS - 1;
315 hm->_resize_at = 4*HM_INIT_SLOTS/3;
316 hm->_tomb_cnt = 0;
317 hm->_live_cnt = 0;
318 hm->_max_probe = 0;
319 hm->_slots = GPA_GRAB_N0 (hm->_gpa, hm->_slot_cnt, *hm->_slots);
320 if (!hm->_hash_init) {
321 hm->_hash_init = HM_INIT_HASH ((uptr) hm->_slots ^ (uptr) &hm);
322 *hash = _hm_hash (hm, key);
323 }
324 } else if (hm->_tomb_cnt + hm->_live_cnt + 1 >= hm->_resize_at) {
325 _hm_resize (hm, 2*hm->_slot_cnt);
326 }
327
328 u32 mask = hm->_slot_msk;
329 u32 slot_idx = *hash & mask;
330 struct _hm_slot *first_tomb = NULL;
331 struct _hm_slot *slots = hm->_slots;
332 for (u32 i = 0; i < hm->_slot_cnt; ++i) {
333 u64 slot_hash = slots[slot_idx].hash;
334
335 if (slot_hash == _HM_SLOT_EMPTY) {
336 if (i + 1 > hm->_max_probe)
337 hm->_max_probe = i + 1;
338 return first_tomb ?: &slots[slot_idx];
339 }
340
341 if (slot_hash == _HM_SLOT_TOMB) {
342 if (!first_tomb)
343 first_tomb = &slots[slot_idx];
344 } else if (slot_hash == *hash) {
345 struct mv slot_key = _hm_key_ptr_to_mv (
346 base + slots[slot_idx].key.ofs, key_kind, _hm_key_sz (key, key_kind));
347 if (mv_equal (slot_key, key)) {
348 if (i + 1 > hm->_max_probe)
349 hm->_max_probe = i + 1;
350 return &slots[slot_idx];
351 }
352 }
353
354 slot_idx = (slot_idx + 1) & mask;
355 }
356
357 DBG_UNREACHABLE ();
358 return NULL;
359}
360
361usz
362_hm_key_sz (struct mv key, enum _hm_key_kind key_kind)
363{
364 switch (key_kind) {
365 case _HM_KEY_KIND_MV: return sizeof (struct mv);
366 case _HM_KEY_KIND_CSTR: return sizeof (char *);
367 case _HM_KEY_KIND_EMB: return mv_sz (key);
368 } DBG_UNREACHABLE ();
369 return 0;
370}
371
372struct mv
373_hm_key_ptr_to_mv (const void *ptr, enum _hm_key_kind key_kind, usz key_sz)
374{
375 DBG_ASSERT_NN (ptr);
376 switch (key_kind) {
377 case _HM_KEY_KIND_MV: return *(const struct mv *) ptr;
378 case _HM_KEY_KIND_CSTR: return mv_from_cstr (*(const char **) ptr);
379 case _HM_KEY_KIND_EMB: return mv_from (ptr, (u8 *) ptr + key_sz);
380 } DBG_UNREACHABLE ();
381 return MV_NULL;
382}
383
384u64
385_hm_hash (const struct hm *hm, struct mv mv)
386{
387 u64 h = HM_HASH (hm->_hash_init, mv);
388 return h < 2 ? h + 2 : h;
389}
390
391// hm.c ends here