Files
bux-lang/lib/Set.bux
T
dimgigov 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
fix(v1.0.2): Array grow from 0, Map/Set rehash, checked div/mod
- 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
2026-07-29 00:03:22 +03:00

162 lines
4.5 KiB
Plaintext

module Std::Set {
extern func bux_hash_bytes(ptr: *void, size: uint) -> uint;
extern func bux_mem_eq(a: *void, b: *void, size: uint) -> int;
extern func bux_alloc(size: uint) -> *void;
extern func bux_free(ptr: *void);
struct SetEntry<T> {
value: T,
occupied: bool,
}
struct Set<T> {
entries: *SetEntry<T>,
cap: uint,
len: uint,
}
/// Create a set with at least `cap` slots. `cap == 0` defaults to 8.
func Set_New<T>(cap: uint) -> Set<T> {
var c: uint = cap;
if c == 0 {
c = 8;
}
let total: uint = c * sizeof(SetEntry<T>);
let data: *SetEntry<T> = bux_alloc(total) as *SetEntry<T>;
var i: uint = 0;
while i < c {
data[i].occupied = false;
i = i + 1;
}
return Set<T> { entries: data, cap: c, len: 0 };
}
func Set_Rehash<T>(s: *Set<T>, newCap: uint) {
var nc: uint = newCap;
if nc == 0 {
nc = 8;
}
var fresh: Set<T> = Set_New<T>(nc);
var i: uint = 0;
while i < s.cap {
if s.entries[i].occupied {
Set_AddInsertOnly<T>(&fresh, s.entries[i].value);
}
i = i + 1;
}
bux_free(s.entries as *void);
s.entries = fresh.entries;
s.cap = fresh.cap;
s.len = fresh.len;
fresh.entries = null as *SetEntry<T>;
fresh.cap = 0;
fresh.len = 0;
}
func Set_AddInsertOnly<T>(s: *Set<T>, value: T) {
var valuePtr: *T = &value;
let hash: uint = bux_hash_bytes(valuePtr as *void, sizeof(T));
var idx: uint = hash % s.cap;
while s.entries[idx].occupied {
var entryPtr: *T = &s.entries[idx].value;
if bux_mem_eq(entryPtr as *void, valuePtr as *void, sizeof(T)) != 0 {
return;
}
idx = (idx + 1) % s.cap;
}
s.entries[idx].value = value;
s.entries[idx].occupied = true;
s.len = s.len + 1;
}
func Set_Add<T>(s: *Set<T>, value: T) {
if s.cap == 0 || s.len * 2 >= s.cap {
var nc: uint = s.cap * 2;
if nc < 8 {
nc = 8;
}
Set_Rehash<T>(s, nc);
}
Set_AddInsertOnly<T>(s, value);
}
func Set_Has<T>(s: *Set<T>, value: T) -> bool {
if s.cap == 0 {
return false;
}
var valuePtr: *T = &value;
let hash: uint = bux_hash_bytes(valuePtr as *void, sizeof(T));
var idx: uint = hash % s.cap;
while s.entries[idx].occupied {
var entryPtr: *T = &s.entries[idx].value;
if bux_mem_eq(entryPtr as *void, valuePtr as *void, sizeof(T)) != 0 {
return true;
}
idx = (idx + 1) % s.cap;
}
return false;
}
func Set_Len<T>(s: *Set<T>) -> uint {
return s.len;
}
func Set_IsEmpty<T>(s: *Set<T>) -> bool {
return s.len == 0;
}
/* Remove value if present. Rebuilds the table to keep open-addressing correct. */
func Set_Remove<T>(s: *Set<T>, value: T) -> bool {
if !Set_Has<T>(s, value) {
return false;
}
var keepCap: uint = s.cap;
if keepCap == 0 {
keepCap = 8;
}
var fresh: Set<T> = Set_New<T>(keepCap);
var i: uint = 0;
while i < s.cap {
if s.entries[i].occupied {
var entryPtr: *T = &s.entries[i].value;
var valuePtr: *T = &value;
if bux_mem_eq(entryPtr as *void, valuePtr as *void, sizeof(T)) == 0 {
Set_AddInsertOnly<T>(&fresh, s.entries[i].value);
}
}
i = i + 1;
}
bux_free(s.entries as *void);
s.entries = fresh.entries;
s.cap = fresh.cap;
s.len = fresh.len;
// Ownership transferred to `s` — clear `fresh` so auto-Drop does not free twice
fresh.entries = null as *SetEntry<T>;
fresh.cap = 0;
fresh.len = 0;
return true;
}
func Set_Clear<T>(s: *Set<T>) {
var i: uint = 0;
while i < s.cap {
s.entries[i].occupied = false;
i = i + 1;
}
s.len = 0;
}
func Set_Free<T>(s: *Set<T>) {
bux_free(s.entries as *void);
s.entries = null as *SetEntry<T>;
s.cap = 0;
s.len = 0;
}
func Set_Drop<T>(s: *Set<T>) {
Set_Free<T>(s);
}
}