Files
2026-11-19 04:54:17 -06:00

227 lines
7.0 KiB
Plaintext

#! @file keymap.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the key map, which finds a `u32` by a `u64` key, as the instance a
#! call uses by its table and node, in twelve bytes a slot, where a dictionary
#! takes forty: the key, its value, and nothing more, as an integer's hash is
#! quickly made again.
module sakura::analyzer;
#! A map of `u64` keys to `u32` values, held open: a power of two of slots, a
#! quarter of them free at least, and each key in the first slot from where its
#! hash begins that is free or holds it. Keys and values are each kept in a
#! block of their own, slot by slot, so finding a key reads only keys. A key of
#! all ones stands for a free slot, which no key the checker makes is: a
#! `pair_key` of two IDs, an index, or an ID.
pub struct keymap : owning {
var keys: *mut u64; #! Each slot's key, or `keymap::free`.
var values: *mut u32; #! Each slot's value.
var count: u64; #! How many keys it holds.
var room: u64; #! How many slots there are, a power of two, or 0.
}
#! The key of a free slot.
pub const keymap::free: u64 = 0xFFFFFFFFFFFFFFFF;
#! Makes a map holding nothing, which takes no memory until it holds a key.
#! @returns The map.
pub def keymap::new(): keymap {
return keymap { keys = null, values = null, count = 0, room = 0 };
}
#! Gives back the map's blocks, leaving it empty.
#! @param this The map.
pub def keymap::close(&mut this): void {
dispose::<u64>(nothing {}, this.keys, this.room);
dispose::<u32>(nothing {}, this.values, this.room);
this.keys = null;
this.values = null;
this.count = 0;
this.room = 0;
}
#! How many keys the map holds.
#! @param this The map.
#! @returns How many.
pub val keymap::length: u64 { get => this.count; }
#! How many slots the map has, which is more than it holds.
#! @param this The map.
#! @returns How many.
pub val keymap::capacity: u64 { get => this.room; }
#! The value of a key, where the map holds it.
#! @param this The map.
#! @param key The key.
#! @returns The value, or nothing.
pub def keymap::get(&this, key: &u64): maybe<u32> {
val at := this.find(key);
return nothing {} if at == this.room;
return this.values[at];
}
#! The value of a key, or another where the map does not hold it.
#! @param this The map.
#! @param key The key.
#! @param missing What is given where it does not.
#! @returns The value.
pub def keymap::get_or(&this, key: &u64, missing: u32): u32 {
val at := this.find(key);
return at == this.room ? missing : this.values[at];
}
#! Whether the map holds a key.
#! @param this The map.
#! @param key The key.
#! @returns Whether it does.
pub def keymap::contains(&this, key: &u64): bool {
return this.find(key) != this.room;
}
#! Gives a key a value, the one it held replaced.
#! @param this The map.
#! @param key The key, which is not `keymap::free`.
#! @param value The value.
pub def keymap::set(&mut this, key: u64, value: u32): void {
val at := this.place(key);
this.values[at] = value;
}
#! Gives a key a value where the map does not hold it already.
#! @param this The map.
#! @param key The key, which is not `keymap::free`.
#! @param value The value.
#! @returns Whether the key was new and took the value. A key the map already
#! holds keeps the value it has.
pub def keymap::insert(&mut this, key: u64, value: u32): bool {
val held := this.count;
val at := this.place(key);
return false if this.count == held;
this.values[at] = value;
return true;
}
#! Takes a key and its value from the map, where it holds it.
#! @param this The map.
#! @param key The key.
pub def keymap::remove(&mut this, key: &u64): void {
var at := this.find(key);
return if at == this.room;
# Each key after the gap that would be found past it moves back into it, so
# every key is still found from where its hash begins, and no slot is marked
# as having held one.
val mask := this.room - 1;
var next := (at + 1) & mask;
while this.keys[next] != keymap::free {
val home := keymap::home(this.keys[next], mask);
if ((next - home) & mask) >= ((next - at) & mask) {
this.keys[at] = this.keys[next];
this.values[at] = this.values[next];
at = next;
}
next = (next + 1) & mask;
}
this.keys[at] = keymap::free;
this.count--;
}
#! The key a slot holds, or `keymap::free`, for walking every key the map
#! holds, as each of its `capacity` slots: in no order the keys were given in.
#! @param this The map.
#! @param slot The slot, below `capacity`.
#! @returns The key.
pub def keymap::key_at(&this, slot: u64): u64 {
return this.keys[slot];
}
#! The value a slot holds, where it holds a key.
#! @param this The map.
#! @param slot The slot, below `capacity`.
#! @returns The value.
pub def keymap::value_at(&this, slot: u64): u32 {
return this.values[slot];
}
#! Takes every key from the map, keeping its slots.
#! @param this The map.
pub def keymap::clear(&mut this): void {
if this.room != 0 {
memset(this.keys as *mut u8, 0xFF, bytes_of::<u64>(this.room));
}
this.count = 0;
}
# The slot a key's search begins at: its bits mixed, as `mix` does, then
# masked.
def keymap::home(key: u64, mask: u64): u64 {
val bits := key * 0x9E3779B97F4A7C15;
return (bits ^ (bits >> 29)) & mask;
}
# The slot holding a key, or `room` where none does.
def keymap::find(&this, key: u64): u64 {
return this.room if this.count == 0;
val mask := this.room - 1;
var at := keymap::home(key, mask);
while true {
val held := this.keys[at];
return at if held == key;
return this.room if held == keymap::free;
at = (at + 1) & mask;
}
return this.room;
}
# The slot holding a key, which it is put in where it is new, growing the map
# first where a quarter of its slots would no longer be free.
def keymap::place(&mut this, key: u64): u64 {
if (this.count + 1) * 4 > this.room * 3 {
this.grow(this.room == 0 ? 16 : this.room * 2);
}
val mask := this.room - 1;
var at := keymap::home(key, mask);
while true {
val held := this.keys[at];
return at if held == key;
if held == keymap::free {
this.keys[at] = key;
this.count++;
return at;
}
at = (at + 1) & mask;
}
return at;
}
# Moves the keys into blocks of a number of slots, a power of two.
def keymap::grow(&mut this, size: u64): void {
val old_keys := this.keys;
val old_values := this.values;
val old_room := this.room;
this.keys = acquire::<u64>(nothing {}, size);
this.values = acquire::<u32>(nothing {}, size);
this.room = size;
memset(this.keys as *mut u8, 0xFF, bytes_of::<u64>(size));
val mask := size - 1;
for i in 0..old_room {
val key := old_keys[i];
continue if key == keymap::free;
var at := keymap::home(key, mask);
while this.keys[at] != keymap::free {
at = (at + 1) & mask;
}
this.keys[at] = key;
this.values[at] = old_values[i];
}
if old_room != 0 {
dispose::<u64>(nothing {}, old_keys, old_room);
dispose::<u32>(nothing {}, old_values, old_room);
}
}