diff options
| author | 2026-11-06 03:00:48 +0000 | |
|---|---|---|
| committer | 2026-11-06 07:09:55 +0000 | |
| commit | 4cef63789da7e80e3721699c92e090ab1d4fb1cc (patch) | |
| tree | b66c79fc35f6fb39fe31850159896e14db2fb96f /include/hm.h | |
| parent | feat(hash): add hashing functions (diff) | |
| download | codebase-4cef63789da7e80e3721699c92e090ab1d4fb1cc.tar.gz | |
feat(hm): add hash map
Diffstat (limited to 'include/hm.h')
| -rw-r--r-- | include/hm.h | 137 |
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 | |||
| 37 | union _hm_key { | ||
| 38 | const void *ptr; | ||
| 39 | /* */ ptrdiff ofs; | ||
| 40 | }; | ||
| 41 | |||
| 42 | struct _hm_slot { | ||
| 43 | /* */ u64 hash; | ||
| 44 | union _hm_key key; | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct 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 | |||
| 63 | void hm_init (struct hm *, struct gpa); | ||
| 64 | |||
| 65 | const struct mv **hm_upsert ( struct hm *, struct mv key); | ||
| 66 | const 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 | |||
| 70 | typedef const struct mv *const*hm_iter; | ||
| 71 | hm_iter hm_iter_init (const struct hm *); | ||
| 72 | hm_iter hm_iter_next (const struct hm *, hm_iter); | ||
| 73 | |||
| 74 | /* */ ptrdiff *hm_rel_upsert ( struct hm *, struct mv key, const struct mv *base); | ||
| 75 | const 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 | |||
| 82 | void hm_drop (struct hm *); | ||
| 83 | void hm_clear (struct hm *); | ||
| 84 | |||
| 85 | #ifndef HM_NO_FEAT_CSTR | ||
| 86 | |||
| 87 | char **hm_upsert_cstr (struct hm *, char *key); | ||
| 88 | char **hm_lookup_cstr (struct hm *, char *key); | ||
| 89 | int hm_insert_cstr (struct hm *, char **key); | ||
| 90 | int hm_delete_cstr (struct hm *, char *key); | ||
| 91 | |||
| 92 | ptrdiff *hm_upsert_rel_cstr (struct hm *, char *key, char **base); | ||
| 93 | int hm_insert_rel_cstr (struct hm *, char **key, char **base); | ||
| 94 | char **hm_lookup_rel_cstr (struct hm *, char *key, char **base); | ||
| 95 | int 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 | |||
| 101 | void **hm_upsert_emb (struct hm *, void *key, usz key_sz); | ||
| 102 | void **hm_lookup_emb (struct hm *, void *key, usz key_sz); | ||
| 103 | int hm_insert_emb (struct hm *, void **key, usz key_sz); | ||
| 104 | int hm_delete_emb (struct hm *, void *key, usz key_sz); | ||
| 105 | |||
| 106 | ptrdiff *hm_upsert_rel_emb (struct hm *, void *key, usz key_sz, void **base); | ||
| 107 | int hm_insert_rel_emb (struct hm *, void **key, usz key_sz, void **base); | ||
| 108 | void **hm_lookup_rel_emb (struct hm *, void *key, usz key_sz, void **base); | ||
| 109 | int 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 | |||
| 115 | enum _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 | |||
| 126 | u64 _hm_hash (const struct hm *, struct mv); | ||
| 127 | void _hm_resize (struct hm *, u32 new_slot_cnt); | ||
| 128 | struct _hm_slot *_hm_slot (const struct hm *, const void *base, u64 hash, struct mv key, enum _hm_key_kind); | ||
| 129 | struct _hm_slot *_hm_slot_ensure ( struct hm *, const void *base, u64 *hash, struct mv key, enum _hm_key_kind); | ||
| 130 | usz _hm_key_sz (struct mv key, enum _hm_key_kind); | ||
| 131 | struct 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 | ||
