improve kv
This commit is contained in:
parent
f5b0a248ab
commit
f954a8276d
1 changed files with 16 additions and 3 deletions
|
|
@ -25,7 +25,7 @@ init :: (kv: *Kv, allocator: Allocator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
get :: (kv: *Kv, key: kv.Key) -> kv.Value, bool {
|
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 {
|
if !ok {
|
||||||
return mem.zero_of(kv.Value), false;
|
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) {
|
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);
|
slot, exists := find_slot(kv, hash);
|
||||||
if !exists {
|
if !exists {
|
||||||
slot = create_or_reuse_slot(kv);
|
slot = create_or_reuse_slot(kv);
|
||||||
|
|
@ -45,9 +45,14 @@ set :: (kv: *Kv, key: kv.Key, value: kv.Value) {
|
||||||
slot.value = 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...
|
// @note(judah): we use 'delete' instead of 'remove' because it's a keyword...
|
||||||
delete :: (kv: *Kv, key: kv.Key) -> kv.Value, bool {
|
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;
|
if !ok return mem.zero_of(kv.Value), false;
|
||||||
|
|
||||||
last_value := slot.value;
|
last_value := slot.value;
|
||||||
|
|
@ -74,6 +79,12 @@ for_expansion :: (kv: *Kv, body: Code, flags: For_Flags) #expand {
|
||||||
|
|
||||||
#scope_file;
|
#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 {
|
find_slot :: (kv: *Kv, hash: u32) -> *kv.Slot, bool, int {
|
||||||
for * kv.slots if it.hash == hash {
|
for * kv.slots if it.hash == hash {
|
||||||
return it, true, it_index;
|
return it, true, it_index;
|
||||||
|
|
@ -122,6 +133,8 @@ mem :: #import "jc/memory";
|
||||||
array :: #import "jc/array";
|
array :: #import "jc/array";
|
||||||
hash :: #import "jc/hash";
|
hash :: #import "jc/hash";
|
||||||
|
|
||||||
|
basic :: #import "Basic"; // @future
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------
|
// ----------------------------------------------------------
|
||||||
// TESTS
|
// TESTS
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue