diff --git a/kv/module.jai b/kv/module.jai index 9dcea6d..d0a8b72 100644 --- a/kv/module.jai +++ b/kv/module.jai @@ -25,7 +25,7 @@ init :: (kv: *Kv, allocator: Allocator) { } get :: (kv: *Kv, key: kv.Key) -> kv.Value, bool { - slot, ok := find_slot(kv, kv.hash_proc(key)); + slot, ok := find_slot(kv, get_hash(kv, key)); if !ok { return mem.zero_of(kv.Value), false; } @@ -34,7 +34,7 @@ get :: (kv: *Kv, key: kv.Key) -> kv.Value, bool { } set :: (kv: *Kv, key: kv.Key, value: kv.Value) { - hash := kv.hash_proc(key); + hash := get_hash(kv, key); slot, exists := find_slot(kv, hash); if !exists { slot = create_or_reuse_slot(kv); @@ -45,9 +45,14 @@ set :: (kv: *Kv, key: kv.Key, value: kv.Value) { slot.value = value; } +exists :: (kv: *Kv, key: kv.Key) -> bool { + _, exists := find_slot(kv, get_hash(kv, key)); + return exists; +} + // @note(judah): we use 'delete' instead of 'remove' because it's a keyword... delete :: (kv: *Kv, key: kv.Key) -> kv.Value, bool { - slot, ok, idx := find_slot(kv, kv.hash_proc(key)); + slot, ok, idx := find_slot(kv, get_hash(kv, key)); if !ok return mem.zero_of(kv.Value), false; last_value := slot.value; @@ -74,6 +79,12 @@ for_expansion :: (kv: *Kv, body: Code, flags: For_Flags) #expand { #scope_file; +get_hash :: inline (kv: *Kv, key: kv.Key) -> u32 { + hash := kv.hash_proc(key); + basic.assert(hash != kv.invalid_hash, "key % collided with invalid hash marker (%)", key, kv.invalid_hash); + return hash; +} + find_slot :: (kv: *Kv, hash: u32) -> *kv.Slot, bool, int { for * kv.slots if it.hash == hash { return it, true, it_index; @@ -122,6 +133,8 @@ mem :: #import "jc/memory"; array :: #import "jc/array"; hash :: #import "jc/hash"; +basic :: #import "Basic"; // @future + // ---------------------------------------------------------- // TESTS