d8c21609fd
ci / build (ubuntu) (push) Has been cancelled
ci / macos smoke (push) Has been cancelled
ci / windows smoke (push) Has been cancelled
selfhost-loop / bootstrap determinism (push) Has been cancelled
ci / unit + fmt (push) Has been cancelled
ci / examples (push) Has been cancelled
ci / goldens + tools (push) Has been cancelled
ci / apps (push) Has been cancelled
ci / selfhost smoke (push) Has been cancelled
ci / CI gate (push) Has been cancelled
- Array_Push grows from cap 0 (same min as Insert) to avoid segfault - Map/StringMap/Set: default min cap 8 and auto-rehash at ~50% load - Integer / and % emit bux_div_i64 / bux_mod_i64 (panic instead of SIGFPE) - Bump version to 1.0.2; stdlib golden regressions; fmt clean
370 lines
11 KiB
Plaintext
370 lines
11 KiB
Plaintext
module Std::Map {
|
|
|
|
extern func bux_hash_bytes(ptr: *void, size: uint) -> uint;
|
|
extern func bux_hash_string(s: String) -> uint;
|
|
extern func bux_alloc(size: uint) -> *void;
|
|
extern func bux_free(ptr: *void);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Generic Map<K, V> — works with value-type keys (int, float, etc.)
|
|
// For String keys, use StringMap below.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
struct MapEntry<K, V> {
|
|
key: K,
|
|
value: V,
|
|
occupied: bool,
|
|
}
|
|
|
|
struct Map<K, V> {
|
|
entries: *MapEntry<K, V>,
|
|
cap: uint,
|
|
len: uint,
|
|
}
|
|
|
|
/// Create a map with at least `cap` slots (open addressing).
|
|
/// `cap == 0` defaults to 8 so inserts never hit `% 0` or a full table with no growth room.
|
|
func Map_New<K, V>(cap: uint) -> Map<K, V> {
|
|
var c: uint = cap;
|
|
if c == 0 {
|
|
c = 8;
|
|
}
|
|
let total: uint = c * sizeof(MapEntry<K, V>);
|
|
let data: *MapEntry<K, V> = bux_alloc(total) as *MapEntry<K, V>;
|
|
var i: uint = 0;
|
|
while i < c {
|
|
data[i].occupied = false;
|
|
i = i + 1;
|
|
}
|
|
return Map<K, V> { entries: data, cap: c, len: 0 };
|
|
}
|
|
|
|
/// Grow / rehash to `newCap` (must be > 0). Transfers ownership; does not Drop `fresh`.
|
|
func Map_Rehash<K, V>(m: *Map<K, V>, newCap: uint) {
|
|
var nc: uint = newCap;
|
|
if nc == 0 {
|
|
nc = 8;
|
|
}
|
|
var fresh: Map<K, V> = Map_New<K, V>(nc);
|
|
var i: uint = 0;
|
|
while i < m.cap {
|
|
if m.entries[i].occupied {
|
|
Map_SetInsertOnly<K, V>(&fresh, m.entries[i].key, m.entries[i].value);
|
|
}
|
|
i = i + 1;
|
|
}
|
|
bux_free(m.entries as *void);
|
|
m.entries = fresh.entries;
|
|
m.cap = fresh.cap;
|
|
m.len = fresh.len;
|
|
fresh.entries = null as *MapEntry<K, V>;
|
|
fresh.cap = 0;
|
|
fresh.len = 0;
|
|
}
|
|
|
|
/// Insert assuming free slots exist (no grow). Used by rehash.
|
|
func Map_SetInsertOnly<K, V>(m: *Map<K, V>, key: K, value: V) {
|
|
var keyPtr: *K = &key;
|
|
let hash: uint = bux_hash_bytes(keyPtr as *void, sizeof(K));
|
|
var idx: uint = hash % m.cap;
|
|
while m.entries[idx].occupied {
|
|
if m.entries[idx].key == key {
|
|
m.entries[idx].value = value;
|
|
return;
|
|
}
|
|
idx = (idx + 1) % m.cap;
|
|
}
|
|
m.entries[idx].key = key;
|
|
m.entries[idx].value = value;
|
|
m.entries[idx].occupied = true;
|
|
m.len = m.len + 1;
|
|
}
|
|
|
|
func Map_Set<K, V>(m: *Map<K, V>, key: K, value: V) {
|
|
// Keep load factor under ~50% for open addressing (also handles cap==0).
|
|
if m.cap == 0 || m.len * 2 >= m.cap {
|
|
var nc: uint = m.cap * 2;
|
|
if nc < 8 {
|
|
nc = 8;
|
|
}
|
|
Map_Rehash<K, V>(m, nc);
|
|
}
|
|
Map_SetInsertOnly<K, V>(m, key, value);
|
|
}
|
|
|
|
func Map_Get<K, V>(m: *Map<K, V>, key: K) -> V {
|
|
if m.cap == 0 {
|
|
var zero: V = 0 as V;
|
|
return zero;
|
|
}
|
|
var keyPtr: *K = &key;
|
|
let hash: uint = bux_hash_bytes(keyPtr as *void, sizeof(K));
|
|
var idx: uint = hash % m.cap;
|
|
while m.entries[idx].occupied {
|
|
if m.entries[idx].key == key {
|
|
return m.entries[idx].value;
|
|
}
|
|
idx = (idx + 1) % m.cap;
|
|
}
|
|
// Return zero value for missing key
|
|
var zero: V = 0 as V;
|
|
return zero;
|
|
}
|
|
|
|
/// Like `Map_Get`, but returns `defaultVal` when the key is absent.
|
|
func Map_GetOr<K, V>(m: *Map<K, V>, key: K, defaultVal: V) -> V {
|
|
if Map_Has<K, V>(m, key) {
|
|
return Map_Get<K, V>(m, key);
|
|
}
|
|
return defaultVal;
|
|
}
|
|
|
|
func Map_Has<K, V>(m: *Map<K, V>, key: K) -> bool {
|
|
if m.cap == 0 {
|
|
return false;
|
|
}
|
|
var keyPtr: *K = &key;
|
|
let hash: uint = bux_hash_bytes(keyPtr as *void, sizeof(K));
|
|
var idx: uint = hash % m.cap;
|
|
while m.entries[idx].occupied {
|
|
if m.entries[idx].key == key {
|
|
return true;
|
|
}
|
|
idx = (idx + 1) % m.cap;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
func Map_Len<K, V>(m: *Map<K, V>) -> uint {
|
|
return m.len;
|
|
}
|
|
|
|
func Map_IsEmpty<K, V>(m: *Map<K, V>) -> bool {
|
|
return m.len == 0;
|
|
}
|
|
|
|
/* Remove key if present. Rebuilds the table to keep open-addressing correct. */
|
|
func Map_Remove<K, V>(m: *Map<K, V>, key: K) -> bool {
|
|
if !Map_Has<K, V>(m, key) {
|
|
return false;
|
|
}
|
|
var keepCap: uint = m.cap;
|
|
if keepCap == 0 {
|
|
keepCap = 8;
|
|
}
|
|
var fresh: Map<K, V> = Map_New<K, V>(keepCap);
|
|
var i: uint = 0;
|
|
while i < m.cap {
|
|
if m.entries[i].occupied {
|
|
if m.entries[i].key != key {
|
|
Map_SetInsertOnly<K, V>(&fresh, m.entries[i].key, m.entries[i].value);
|
|
}
|
|
}
|
|
i = i + 1;
|
|
}
|
|
bux_free(m.entries as *void);
|
|
m.entries = fresh.entries;
|
|
m.cap = fresh.cap;
|
|
m.len = fresh.len;
|
|
// Ownership transferred to `m` — clear `fresh` so auto-Drop does not free twice
|
|
fresh.entries = null as *MapEntry<K, V>;
|
|
fresh.cap = 0;
|
|
fresh.len = 0;
|
|
return true;
|
|
}
|
|
|
|
func Map_Clear<K, V>(m: *Map<K, V>) {
|
|
var i: uint = 0;
|
|
while i < m.cap {
|
|
m.entries[i].occupied = false;
|
|
i = i + 1;
|
|
}
|
|
m.len = 0;
|
|
}
|
|
|
|
func Map_Free<K, V>(m: *Map<K, V>) {
|
|
bux_free(m.entries as *void);
|
|
m.entries = null as *MapEntry<K, V>;
|
|
m.cap = 0;
|
|
m.len = 0;
|
|
}
|
|
|
|
func Map_Drop<K, V>(m: *Map<K, V>) {
|
|
Map_Free<K, V>(m);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// StringMap<V> — specialized Map for String keys, using strcmp
|
|
// ---------------------------------------------------------------------------
|
|
|
|
struct StringMapEntry<V> {
|
|
key: String,
|
|
value: V,
|
|
occupied: bool,
|
|
}
|
|
|
|
struct StringMap<V> {
|
|
entries: *StringMapEntry<V>,
|
|
cap: uint,
|
|
len: uint,
|
|
}
|
|
|
|
func StringMap_New<V>(cap: uint) -> StringMap<V> {
|
|
var c: uint = cap;
|
|
if c == 0 {
|
|
c = 8;
|
|
}
|
|
let total: uint = c * sizeof(StringMapEntry<V>);
|
|
let data: *StringMapEntry<V> = bux_alloc(total) as *StringMapEntry<V>;
|
|
var i: uint = 0;
|
|
while i < c {
|
|
data[i].occupied = false;
|
|
i = i + 1;
|
|
}
|
|
return StringMap<V> { entries: data, cap: c, len: 0 };
|
|
}
|
|
|
|
func StringMap_Rehash<V>(m: *StringMap<V>, newCap: uint) {
|
|
var nc: uint = newCap;
|
|
if nc == 0 {
|
|
nc = 8;
|
|
}
|
|
var fresh: StringMap<V> = StringMap_New<V>(nc);
|
|
var i: uint = 0;
|
|
while i < m.cap {
|
|
if m.entries[i].occupied {
|
|
StringMap_SetInsertOnly<V>(&fresh, m.entries[i].key, m.entries[i].value);
|
|
}
|
|
i = i + 1;
|
|
}
|
|
bux_free(m.entries as *void);
|
|
m.entries = fresh.entries;
|
|
m.cap = fresh.cap;
|
|
m.len = fresh.len;
|
|
fresh.entries = null as *StringMapEntry<V>;
|
|
fresh.cap = 0;
|
|
fresh.len = 0;
|
|
}
|
|
|
|
func StringMap_SetInsertOnly<V>(m: *StringMap<V>, key: String, value: V) {
|
|
let hash: uint = bux_hash_string(key);
|
|
var idx: uint = hash % m.cap;
|
|
while m.entries[idx].occupied {
|
|
if String_Eq(m.entries[idx].key, key) {
|
|
m.entries[idx].value = value;
|
|
return;
|
|
}
|
|
idx = (idx + 1) % m.cap;
|
|
}
|
|
m.entries[idx].key = key;
|
|
m.entries[idx].value = value;
|
|
m.entries[idx].occupied = true;
|
|
m.len = m.len + 1;
|
|
}
|
|
|
|
func StringMap_Set<V>(m: *StringMap<V>, key: String, value: V) {
|
|
if m.cap == 0 || m.len * 2 >= m.cap {
|
|
var nc: uint = m.cap * 2;
|
|
if nc < 8 {
|
|
nc = 8;
|
|
}
|
|
StringMap_Rehash<V>(m, nc);
|
|
}
|
|
StringMap_SetInsertOnly<V>(m, key, value);
|
|
}
|
|
|
|
func StringMap_Get<V>(m: *StringMap<V>, key: String) -> V {
|
|
if m.cap == 0 {
|
|
var zero: V = 0 as V;
|
|
return zero;
|
|
}
|
|
let hash: uint = bux_hash_string(key);
|
|
var idx: uint = hash % m.cap;
|
|
while m.entries[idx].occupied {
|
|
if String_Eq(m.entries[idx].key, key) {
|
|
return m.entries[idx].value;
|
|
}
|
|
idx = (idx + 1) % m.cap;
|
|
}
|
|
var zero: V = 0 as V;
|
|
return zero;
|
|
}
|
|
|
|
/// Like `StringMap_Get`, but returns `defaultVal` when the key is absent.
|
|
func StringMap_GetOr<V>(m: *StringMap<V>, key: String, defaultVal: V) -> V {
|
|
if StringMap_Has<V>(m, key) {
|
|
return StringMap_Get<V>(m, key);
|
|
}
|
|
return defaultVal;
|
|
}
|
|
|
|
func StringMap_Has<V>(m: *StringMap<V>, key: String) -> bool {
|
|
if m.cap == 0 {
|
|
return false;
|
|
}
|
|
let hash: uint = bux_hash_string(key);
|
|
var idx: uint = hash % m.cap;
|
|
while m.entries[idx].occupied {
|
|
if String_Eq(m.entries[idx].key, key) {
|
|
return true;
|
|
}
|
|
idx = (idx + 1) % m.cap;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
func StringMap_Len<V>(m: *StringMap<V>) -> uint {
|
|
return m.len;
|
|
}
|
|
|
|
func StringMap_IsEmpty<V>(m: *StringMap<V>) -> bool {
|
|
return m.len == 0;
|
|
}
|
|
|
|
func StringMap_Remove<V>(m: *StringMap<V>, key: String) -> bool {
|
|
if !StringMap_Has<V>(m, key) {
|
|
return false;
|
|
}
|
|
var keepCap: uint = m.cap;
|
|
if keepCap == 0 {
|
|
keepCap = 8;
|
|
}
|
|
var fresh: StringMap<V> = StringMap_New<V>(keepCap);
|
|
var i: uint = 0;
|
|
while i < m.cap {
|
|
if m.entries[i].occupied {
|
|
if !String_Eq(m.entries[i].key, key) {
|
|
StringMap_SetInsertOnly<V>(&fresh, m.entries[i].key, m.entries[i].value);
|
|
}
|
|
}
|
|
i = i + 1;
|
|
}
|
|
bux_free(m.entries as *void);
|
|
m.entries = fresh.entries;
|
|
m.cap = fresh.cap;
|
|
m.len = fresh.len;
|
|
// Ownership transferred to `m` — clear `fresh` so auto-Drop does not free twice
|
|
fresh.entries = null as *StringMapEntry<V>;
|
|
fresh.cap = 0;
|
|
fresh.len = 0;
|
|
return true;
|
|
}
|
|
|
|
func StringMap_Clear<V>(m: *StringMap<V>) {
|
|
var i: uint = 0;
|
|
while i < m.cap {
|
|
m.entries[i].occupied = false;
|
|
i = i + 1;
|
|
}
|
|
m.len = 0;
|
|
}
|
|
|
|
func StringMap_Free<V>(m: *StringMap<V>) {
|
|
bux_free(m.entries as *void);
|
|
m.entries = null as *StringMapEntry<V>;
|
|
m.cap = 0;
|
|
m.len = 0;
|
|
}
|
|
|
|
}
|