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 --- src/hm.c | 391 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 391 insertions(+) create mode 100644 src/hm.c (limited to 'src/hm.c') 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