From 4cef63789da7e80e3721699c92e090ab1d4fb1cc Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Fri, 6 Nov 2026 06:00:48 +0300 Subject: feat(hm): add hash map --- include/hm.h | 137 +++++++++++++++++++++ src/hm.c | 391 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 528 insertions(+) create mode 100644 include/hm.h create mode 100644 src/hm.c 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 @@ +// hm.h --- Hash Map -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#ifndef INCLUDED_COSMOS_HM_H +#define INCLUDED_COSMOS_HM_H + +#include "ms.h" +#include "gpa.h" + +#define HM_NO_FEAT_CSTR // TEMP(cmmm): actually implement. +#define HM_NO_FEAT_EMB // TEMP(cmmm): actually implement. + +#ifndef HM_HASH +#include "hash.h" +#include "bits.h" +#define HM_HASH(init, mv) hash_fnv1a_64_update (init, (mv).beg, mv_sz (mv)) +#define HM_INIT_HASH(seed) (HASH_FNV1A_64_INIT ^ splitmix64 (seed)) +#endif // HM_HASH + +#ifndef HM_INIT_HASH +#error "HM_INIT_HASH not defined, while HM_HASH is user defined" +#endif // HM_INIT_HASH + +#ifndef HM_INIT_SLOTS +#define HM_INIT_SLOTS 256 +#endif // HM_INIT_SLOTS + +union _hm_key { + const void *ptr; + /* */ ptrdiff ofs; +}; + +struct _hm_slot { + /* */ u64 hash; + union _hm_key key; +}; + +struct hm { + struct gpa _gpa; + struct _hm_slot *_slots; + /* */ u64 _hash_init; + /* */ u32 _resize_at; + /* */ u32 _max_probe; + /* */ u32 _live_cnt; + /* */ u32 _tomb_cnt; + /* */ u32 _slot_cnt; + /* */ u32 _slot_msk; +}; + +#define HM_REL_NEW PTRDIFF_MIN + +#define HM_FROM(GPA) ((struct hm) { ._gpa = GPA }); + +void hm_init (struct hm *, struct gpa); + +const struct mv **hm_upsert ( struct hm *, struct mv key); +const struct mv *hm_lookup (const struct hm *, struct mv key); +/* */ int hm_insert ( struct hm *, const struct mv *key); +/* */ int hm_delete ( struct hm *, struct mv key); + +typedef const struct mv *const*hm_iter; +hm_iter hm_iter_init (const struct hm *); +hm_iter hm_iter_next (const struct hm *, hm_iter); + +/* */ ptrdiff *hm_rel_upsert ( struct hm *, struct mv key, const struct mv *base); +const struct mv *hm_rel_lookup (const struct hm *, struct mv key, const struct mv *base); +/* */ int hm_rel_insert ( struct hm *, const struct mv *key, const struct mv *base); +/* */ int hm_rel_delete ( struct hm *, struct mv key, const struct mv *base); + +// hm_iter hm_rel_iter_init (const struct hm *, const struct mv *base); +// hm_iter hm_rel_iter_next (const struct hm *, hm_iter, const struct mv *base); + +void hm_drop (struct hm *); +void hm_clear (struct hm *); + +#ifndef HM_NO_FEAT_CSTR + +char **hm_upsert_cstr (struct hm *, char *key); +char **hm_lookup_cstr (struct hm *, char *key); +int hm_insert_cstr (struct hm *, char **key); +int hm_delete_cstr (struct hm *, char *key); + +ptrdiff *hm_upsert_rel_cstr (struct hm *, char *key, char **base); +int hm_insert_rel_cstr (struct hm *, char **key, char **base); +char **hm_lookup_rel_cstr (struct hm *, char *key, char **base); +int hm_delete_rel_cstr (struct hm *, char *key, char **base); + +#endif // HM_NO_FEAT_CSTR + +#ifndef HM_NO_FEAT_EMB + +void **hm_upsert_emb (struct hm *, void *key, usz key_sz); +void **hm_lookup_emb (struct hm *, void *key, usz key_sz); +int hm_insert_emb (struct hm *, void **key, usz key_sz); +int hm_delete_emb (struct hm *, void *key, usz key_sz); + +ptrdiff *hm_upsert_rel_emb (struct hm *, void *key, usz key_sz, void **base); +int hm_insert_rel_emb (struct hm *, void **key, usz key_sz, void **base); +void **hm_lookup_rel_emb (struct hm *, void *key, usz key_sz, void **base); +int hm_delete_rel_emb (struct hm *, void *key, usz key_sz, void **base); + +#endif // HM_NO_FEAT_CSTR + +#ifdef HM_INCLUDE_INTERNAL + +enum _hm_key_kind { + _HM_KEY_KIND_INVALID, + _HM_KEY_KIND_MV, + _HM_KEY_KIND_CSTR, + _HM_KEY_KIND_EMB, + _HM_KEY_KIND_CNT +}; + +#define _HM_SLOT_EMPTY 0 +#define _HM_SLOT_TOMB 1 + +u64 _hm_hash (const struct hm *, struct mv); +void _hm_resize (struct hm *, u32 new_slot_cnt); +struct _hm_slot *_hm_slot (const struct hm *, const void *base, u64 hash, struct mv key, enum _hm_key_kind); +struct _hm_slot *_hm_slot_ensure ( struct hm *, const void *base, u64 *hash, struct mv key, enum _hm_key_kind); +usz _hm_key_sz (struct mv key, enum _hm_key_kind); +struct mv _hm_key_ptr_to_mv (const void *, enum _hm_key_kind, usz key_sz); + +#endif // HM_INCLUDE_INTERNAL + +#endif // INCLUDED_COSMOS_HM_H + +// 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 @@ +// hm.c --- Hash Map -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#define HM_INCLUDE_INTERNAL +#include "hm.h" +#include "dbg.h" + +#include // TODO(cmmm): drop libc dep (memset) + +void +hm_init (struct hm *hm, struct gpa gpa) +{ + DBG_ASSERT_NN (hm); + hm->_gpa = gpa; +} + +const struct mv ** +hm_upsert (struct hm *hm, struct mv key) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_MV (key); + + u64 hash = _hm_hash (hm, key); + // DBG_TRACE ("info", "hash: %lu", hash); + struct _hm_slot *slot = + _hm_slot_ensure (hm, NULL, &hash, key, _HM_KEY_KIND_MV); + DBG_ASSERT_NN (slot); + + if (slot->hash <= _HM_SLOT_TOMB) { + hm->_live_cnt += 1; + if (slot->hash == _HM_SLOT_TOMB) + hm->_tomb_cnt -= 1; + slot->hash = hash; + slot->key.ptr = NULL; + } + + return (void *) &slot->key.ptr; +} + +int +hm_insert (struct hm *hm, const struct mv *key) +{ + DBG_ASSERT_NN ( hm); + DBG_ASSERT_NN ( key); + DBG_ASSERT_MV (*key); + DBG_ASSERT (!mv_eq (*key, MV_NULL)); + + u64 hash = _hm_hash (hm, *key); + struct _hm_slot *slot = + _hm_slot_ensure (hm, NULL, &hash, *key, _HM_KEY_KIND_MV); + DBG_ASSERT_NN (slot); + + if (slot->hash > _HM_SLOT_TOMB) return 0; + if (slot->hash == _HM_SLOT_TOMB) hm->_tomb_cnt -= 1; + + hm->_live_cnt += 1; + slot->hash = hash; + slot->key.ptr = key; + + return 1; +} + +const struct mv * +hm_lookup (const struct hm *hm, struct mv key) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_MV (key); + + const struct _hm_slot *slot = + _hm_slot (hm, NULL, _hm_hash (hm, key), key, _HM_KEY_KIND_MV); + + if (slot == NULL) return NULL; + if (slot->hash <= _HM_SLOT_TOMB) return NULL; + + return slot->key.ptr; +} + +int +hm_delete (struct hm *hm, struct mv key) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_MV (key); + + struct _hm_slot *slot = + _hm_slot (hm, NULL, _hm_hash (hm, key), key, _HM_KEY_KIND_MV); + + if (slot == NULL) return 0; + if (slot->hash <= _HM_SLOT_TOMB) return 0; + + hm->_tomb_cnt += 1; + hm->_live_cnt -= 1; + slot->hash = _HM_SLOT_TOMB; + + return 1; +} + +hm_iter +hm_iter_init (const struct hm *hm) +{ + DBG_ASSERT_NN (hm); + struct _hm_slot *slot = hm->_slots; + struct _hm_slot *slots_end = hm->_slots + hm->_slot_cnt; + while (slot < slots_end) { + if (slot->hash > _HM_SLOT_TOMB) + return (hm_iter) &slot->key.ptr; + slot += 1; + } return NULL; +} + +hm_iter +hm_iter_next (const struct hm *hm, hm_iter iter) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_NN (iter); + struct _hm_slot *slot = CONTAINER_OF (iter, *slot, key); + struct _hm_slot *slots_end = hm->_slots + hm->_slot_cnt; + while (++slot < slots_end) { + if (slot->hash > _HM_SLOT_TOMB) + return (hm_iter) &slot->key.ptr; + } return NULL; +} + +ptrdiff * +hm_rel_upsert (struct hm *hm, struct mv key, const struct mv *base) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_MV (key); + DBG_ASSERT_NN (base); + + u64 hash = _hm_hash (hm, key); + // DBG_TRACE ("info", "hash: %lu", hash); + struct _hm_slot *slot = + _hm_slot_ensure (hm, base, &hash, key, _HM_KEY_KIND_MV); + DBG_ASSERT_NN (slot); + + if (slot->hash <= _HM_SLOT_TOMB) { + hm->_live_cnt += 1; + if (slot->hash == _HM_SLOT_TOMB) + hm->_tomb_cnt -= 1; + slot->hash = hash; + slot->key.ofs = HM_REL_NEW; + } + + return &slot->key.ofs; +} + +int +hm_rel_insert (struct hm *hm, const struct mv *key, const struct mv *base) +{ + DBG_ASSERT_NN ( hm); + DBG_ASSERT_NN ( key); + DBG_ASSERT_MV (*key); + DBG_ASSERT_NN ( base); + + u64 hash = _hm_hash (hm, *key); + struct _hm_slot *slot = + _hm_slot_ensure (hm, base, &hash, *key, _HM_KEY_KIND_MV); + DBG_ASSERT_NN (slot); + + if (slot->hash > _HM_SLOT_TOMB) return 0; + if (slot->hash == _HM_SLOT_TOMB) hm->_tomb_cnt -= 1; + hm->_live_cnt += 1; + slot->hash = hash; + slot->key.ofs = (const u8 *) key - (const u8 *) base; + + return 1; +} + +const struct mv * +hm_rel_lookup (const struct hm *hm, struct mv key, const struct mv *base) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_MV (key); + DBG_ASSERT_NN (base); + + const struct _hm_slot *slot = + _hm_slot (hm, base, _hm_hash (hm, key), key, _HM_KEY_KIND_MV); + + if (slot == NULL) return NULL; + if (slot->hash <= _HM_SLOT_TOMB) return NULL; + + return (void *) ((const u8 *) base + slot->key.ofs); +} + +int +hm_rel_delete (struct hm *hm, struct mv key, const struct mv *base) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_MV (key); + DBG_ASSERT_NN (base); + + struct _hm_slot *slot = + _hm_slot (hm, base, _hm_hash (hm, key), key, _HM_KEY_KIND_MV); + + if (slot == NULL) return 0; + if (slot->hash <= _HM_SLOT_TOMB) return 0; + + hm->_tomb_cnt += 1; + hm->_live_cnt -= 1; + slot->hash = _HM_SLOT_TOMB; + + return 1; +} + +const struct mv *const*hm_rel_iter_init (const struct hm *); +const struct mv *const*hm_rel_iter_next (const struct hm *, const struct mv *const*); + +void +hm_drop (struct hm *hm) +{ + DBG_ASSERT_NN (hm); + GPA_DROP_N (hm->_gpa, hm->_slots, hm->_slot_cnt); + hm->_slot_cnt = 0; + hm->_tomb_cnt = 0; +} + +void +hm_clear (struct hm *hm) +{ + DBG_ASSERT_NN (hm); + hm->_tomb_cnt = 0; + memset (hm->_slots, 0, hm->_slot_cnt*sizeof (*hm->_slots)); +} + +/* ================================ INTERNAL ================================ */ + +void +_hm_resize (struct hm *hm, u32 new_slot_cnt) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_EQ (POPCOUNT (new_slot_cnt), 1, "%u", "%u"); + + u32 new_mask = new_slot_cnt - 1; + struct _hm_slot *new_slots = GPA_GRAB_N0 (hm->_gpa, new_slot_cnt, *new_slots); + struct _hm_slot *slots_end = &hm->_slots[hm->_slot_cnt]; + u32 live_left = hm->_live_cnt; + for (struct _hm_slot *slot = hm->_slots; slot < slots_end; ++slot) { + if (slot->hash <= _HM_SLOT_TOMB) + continue; + + u32 new_slot_idx = slot->hash & new_mask; + while (new_slots[new_slot_idx].hash != _HM_SLOT_EMPTY) + new_slot_idx = (new_slot_idx + 1) & new_mask; + + new_slots[new_slot_idx] = *slot; + + live_left -= 1; + if (live_left == 0) + break; + } + + if (hm->_slots) + GPA_DROP_N (hm->_gpa, hm->_slots, hm->_slot_cnt); + + hm->_tomb_cnt = 0; + hm->_slot_cnt = new_slot_cnt; + hm->_slot_msk = new_slot_cnt - 1; + hm->_resize_at = 4*new_slot_cnt/3; + hm->_slots = new_slots; +} + +struct _hm_slot * +_hm_slot (const struct hm *hm, const void *base, u64 hash, + struct mv key, enum _hm_key_kind key_kind) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_NN (key.beg); + DBG_ASSERT_LT (key.beg, key.end, "%p", "%p"); + + if (hm->_slots == NULL) + return NULL; + + u32 mask = hm->_slot_msk; + u32 slot_idx = hash & mask; + struct _hm_slot *slots = hm->_slots; + for (u32 i = 0; i < hm->_max_probe; ++i) { + u64 slot_hash = slots[slot_idx].hash; + + if (slot_hash == _HM_SLOT_EMPTY) + return NULL; + + if (slot_hash == hash) { + struct mv slot_key = _hm_key_ptr_to_mv ( + base + slots[slot_idx].key.ofs, key_kind, _hm_key_sz (key, key_kind)); + if (mv_equal (slot_key, key)) + return &slots[slot_idx]; + } + + slot_idx = (slot_idx + 1) & mask; + } + + return NULL; +} + +struct _hm_slot * +_hm_slot_ensure (struct hm *hm, const void *base, u64 *hash, + struct mv key, enum _hm_key_kind key_kind) +{ + DBG_ASSERT_NN (hm); + DBG_ASSERT_NN (hash); + DBG_ASSERT_IN (0, key_kind, _HM_KEY_KIND_CNT, "%d", "%d", "%d"); + + if (!hm->_slots) { + hm->_slot_cnt = HM_INIT_SLOTS; + hm->_slot_msk = HM_INIT_SLOTS - 1; + hm->_resize_at = 4*HM_INIT_SLOTS/3; + hm->_tomb_cnt = 0; + hm->_live_cnt = 0; + hm->_max_probe = 0; + hm->_slots = GPA_GRAB_N0 (hm->_gpa, hm->_slot_cnt, *hm->_slots); + if (!hm->_hash_init) { + hm->_hash_init = HM_INIT_HASH ((uptr) hm->_slots ^ (uptr) &hm); + *hash = _hm_hash (hm, key); + } + } else if (hm->_tomb_cnt + hm->_live_cnt + 1 >= hm->_resize_at) { + _hm_resize (hm, 2*hm->_slot_cnt); + } + + u32 mask = hm->_slot_msk; + u32 slot_idx = *hash & mask; + struct _hm_slot *first_tomb = NULL; + struct _hm_slot *slots = hm->_slots; + for (u32 i = 0; i < hm->_slot_cnt; ++i) { + u64 slot_hash = slots[slot_idx].hash; + + if (slot_hash == _HM_SLOT_EMPTY) { + if (i + 1 > hm->_max_probe) + hm->_max_probe = i + 1; + return first_tomb ?: &slots[slot_idx]; + } + + if (slot_hash == _HM_SLOT_TOMB) { + if (!first_tomb) + first_tomb = &slots[slot_idx]; + } else if (slot_hash == *hash) { + struct mv slot_key = _hm_key_ptr_to_mv ( + base + slots[slot_idx].key.ofs, key_kind, _hm_key_sz (key, key_kind)); + if (mv_equal (slot_key, key)) { + if (i + 1 > hm->_max_probe) + hm->_max_probe = i + 1; + return &slots[slot_idx]; + } + } + + slot_idx = (slot_idx + 1) & mask; + } + + DBG_UNREACHABLE (); + return NULL; +} + +usz +_hm_key_sz (struct mv key, enum _hm_key_kind key_kind) +{ + switch (key_kind) { + case _HM_KEY_KIND_MV: return sizeof (struct mv); + case _HM_KEY_KIND_CSTR: return sizeof (char *); + case _HM_KEY_KIND_EMB: return mv_sz (key); + } DBG_UNREACHABLE (); + return 0; +} + +struct mv +_hm_key_ptr_to_mv (const void *ptr, enum _hm_key_kind key_kind, usz key_sz) +{ + DBG_ASSERT_NN (ptr); + switch (key_kind) { + case _HM_KEY_KIND_MV: return *(const struct mv *) ptr; + case _HM_KEY_KIND_CSTR: return mv_from_cstr (*(const char **) ptr); + case _HM_KEY_KIND_EMB: return mv_from (ptr, (u8 *) ptr + key_sz); + } DBG_UNREACHABLE (); + return MV_NULL; +} + +u64 +_hm_hash (const struct hm *hm, struct mv mv) +{ + u64 h = HM_HASH (hm->_hash_init, mv); + return h < 2 ? h + 2 : h; +} + +// hm.c ends here -- cgit v1.3.1