aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parentfeat(hash): add hashing functions (diff)
downloadcodebase-4cef63789da7e80e3721699c92e090ab1d4fb1cc.tar.gz
feat(hm): add hash map
Diffstat (limited to 'src')
-rw-r--r--src/hm.c391
1 files changed, 391 insertions, 0 deletions
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