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