collections/core

sourcecollections/core.sx

copy_cstr_in

private
fn copy_cstr_in[A: allocators.GeneralAllocator](ac: A, src: ref u8) option[ref u8]

ListAllocError

pub enum ListAllocError = { OutOfMemory }

Variants

  • OutOfMemory

PtrList

type PtrList = { ptr: ref void, len: usize, cap: usize }

An unsafe list for storing values of any type as raw pointers (ref void).

Fields

NameType
ptrref void
lenusize
capusize

new

pub fn (PtrList) new[A: allocators.GeneralAllocator](ac: A, initial_cap: usize) result[ref void, ListAllocError]

Allocate a new PtrList with the given initial capacity.

free

pub fn (PtrList) free[A: allocators.GeneralAllocator](ac: A, list: mut PtrList) void

Free a PtrList and its backing buffer.

len

pub fn (PtrList) len(list: PtrList) usize

Return the number of elements in the list.

cap

pub fn (PtrList) cap(list: PtrList) usize

Return the current capacity.

grow

private
fn (PtrList) grow[A: allocators.GeneralAllocator](ac: A, list: mut PtrList, new_cap: usize) result[void, ListAllocError]

Grow the backing buffer to at least new_cap elements. Requires new_cap > 0 to avoid a zero-size allocation.

push

pub fn (PtrList) push[A: allocators.GeneralAllocator](ac: A, list: mut PtrList, value: usize) result[void, ListAllocError]

Append a value to the end of the list. Requires the list pointer to be non-null (non-zero).

get

pub fn (PtrList) get(list: PtrList, i: usize) option[usize]

Get the element at index i. Requires i < len to guarantee in-bounds access. Returns none if out of bounds (defensive, belt-and-suspenders).

set

pub fn (PtrList) set(list: PtrList, i: usize, value: usize) usize

Set the element at index i. Requires i < len to guarantee in-bounds write. Returns 1 on success, 0 if out of bounds (defensive).

pop

pub fn (PtrList) pop(list: PtrList) option[usize]

Pop the last element. Requires the list to be non-empty.

Map

pub type Map = { header: ref void }

Fields

NameType
headerref void

StringMap

pub type StringMap = { header: ref void }

Fields

NameType
headerref void

LongMap

pub type LongMap = {  }

A compact open-addressing map with i64 keys and i64 values.

hash_identity

pub fn hash_identity(key: usize) usize

Default identity hash for integer keys.

new

pub fn (Map) new[A: allocators.GeneralAllocator](ac: A, initial_cap: usize, hash_fn: ref void) Map

Allocate a new Map with the given initial capacity and hash function. initial_cap must be > 0.

free

pub fn (Map) free[A: allocators.GeneralAllocator](ac: A, map: Map) void

Free the map and its backing bucket array.

len

pub fn (Map) len(map: Map) usize

Return the number of live entries.

cap

pub fn (Map) cap(map: Map) usize = trust { ... }

Return the current bucket capacity.

grow

private
fn (Map) grow[A: allocators.GeneralAllocator](ac: A, map: mut Map, new_cap: usize) void

Internal: grow the map to new_cap buckets and rehash all live entries.

set

pub fn (Map) set[A: allocators.GeneralAllocator](ac: A, map: mut Map, key: usize, value: usize) void

Insert or update a key-value pair. Grows the map (2x + 1) when load factor exceeds 70%.

get

pub fn (Map) get(map: Map, key: usize) option[usize]

Look up a key. Returns some(value) if found, none otherwise.

delete

pub fn (Map) delete(map: Map, key: usize) usize = trust { ... }

Remove a key. Returns 1 if the key was present, 0 otherwise.

contains

pub fn (Map) contains(map: Map, key: usize) bool

Check if a key exists in the map.

clear

pub fn (Map) clear(map: Map) void

Clear all entries (reset to empty, keep allocation).

hash_string

pub fn hash_string(key: ref u8) usize

Casts the string pointer to usize for use as a Map key seed, then hashes the actual bytes so distinct strings with the same address never collide.

cstr_eq

private
fn cstr_eq(a: ref u8, b: ref u8) bool = trust { ... }

Compare two null-terminated C strings for equality.

This is intentionally local to the collections module: collections is a dependency of string, so importing std/string here would create a cycle.

new

pub fn (StringMap) new[A: allocators.GeneralAllocator](ac: A, initial_cap: usize) StringMap

Allocate a new StringMap with the given initial capacity.

set

pub fn (StringMap) set[A: allocators.GeneralAllocator](ac: A, map: StringMap, key: ref u8, value: usize) void

Insert or update a string key with a usize value.

The key is copied onto the heap; the copy is owned by the map.

get

pub fn (StringMap) get(map: StringMap, key: ref u8) option[usize]

Look up a string key. Returns some(value) or none.

contains

pub fn (StringMap) contains(map: StringMap, key: ref u8) bool

Check if a string key exists.

delete

pub fn (StringMap) delete[A: allocators.GeneralAllocator](ac: A, map: StringMap, key: ref u8) usize

Remove a string key. Returns 1 if found and removed, 0 otherwise.

len

pub fn (StringMap) len(map: StringMap) usize

Return the number of live entries.

free

pub fn (StringMap) free[A: allocators.GeneralAllocator](ac: A, map: StringMap) void

Free all heap-copied keys, then free the map itself.

IndexMap

type IndexMap = { inner_header: ref void, keys: ref void }

An insertion-order preserving map with ref u8 keys and usize values.

Implemented as a StringMap for O(1) lookup plus a PtrList of keys to track insertion order.

Fields

NameType
inner_headerref void
keysref void

RawIndexMap

type RawIndexMap = {  }

new

pub fn (IndexMap) new[A: allocators.GeneralAllocator](ac: A, initial_cap: usize) IndexMap

Allocate a new IndexMap with the given initial capacity.

set

pub fn (IndexMap) set[A: allocators.GeneralAllocator](ac: A, map: IndexMap, key: ref u8, value: usize) void

Insert or update a key-value pair, preserving insertion order for new keys.

get

pub fn (IndexMap) get(map: IndexMap, key: ref u8) option[usize] = trust { ... }

Look up a key. Returns some(value) or none.

contains

pub fn (IndexMap) contains(map: IndexMap, key: ref u8) bool

Check if a key exists.

delete

pub fn (IndexMap) delete[A: allocators.GeneralAllocator](ac: A, map: IndexMap, key: ref u8) usize

Remove a key. Returns 1 if found, 0 otherwise. Note: removal marks the key as a tombstone in the ordered list (sets to 0).

len

pub fn (IndexMap) len(map: IndexMap) usize

Return the number of live entries.

each

pub fn (IndexMap) each(map: IndexMap, cb: fn(ref u8, usize) void) void = trust { ... }

Iterate in insertion order. Calls callback(key, value) for each live entry.

key_at

pub fn (IndexMap) key_at(map: IndexMap, i: usize) option[ref u8]

Get the i-th key in insertion order (skipping tombstones). Returns none if out of range.

free

pub fn (IndexMap) free[A: allocators.GeneralAllocator](ac: A, map: IndexMap) void

Free the IndexMap and all owned key copies.

new

pub fn (RawIndexMap) new(initial_cap: usize) ref void = trust { ... }

set

pub fn (RawIndexMap) set(map: ref void, key: ref u8, value: usize) void = trust { ... }

get

pub fn (RawIndexMap) get(map: ref void, key: ref u8) option[usize] = trust { ... }

contains

pub fn (RawIndexMap) contains(map: ref void, key: ref u8) bool

delete

pub fn (RawIndexMap) delete(map: ref void, key: ref u8) usize = trust { ... }

len

pub fn (RawIndexMap) len(map: ref void) usize

each

pub fn (RawIndexMap) each(map: ref void, cb: fn(ref u8, usize) void) void = trust { ... }

key_at

pub fn (RawIndexMap) key_at(map: ref void, i: usize) option[ref u8]

free

pub fn (RawIndexMap) free(map: ref void) void = trust { ... }

new

pub fn (LongMap) new[A: allocators.GeneralAllocator](ac: A, initial_cap: usize) ref void

Allocate a new integer map. initial_cap must be a power of two and > 0.

len

pub fn (LongMap) len(map: ref void) usize = trust { ... }

Return the number of entries in the map.

cap

pub fn (LongMap) cap(map: ref void) usize = trust { ... }

Return the current capacity.

lookup

private
fn (LongMap) lookup(map: ref void, key: i64) i64 = trust { ... }

Find the slot for key, or -1 when the key is absent.

grow

private
fn (LongMap) grow[A: allocators.GeneralAllocator](ac: A, map: ref void) void

Double the bucket array and rehash all live entries.

set

pub fn (LongMap) set[A: allocators.GeneralAllocator](ac: A, map: ref void, key: i64, value: i64) void

Insert or update a key/value pair.

get_or

pub fn (LongMap) get_or(map: ref void, key: i64, fallback: i64) i64 = trust { ... }

Look up a key and return its value, or fallback when absent.

get

pub fn (LongMap) get(map: ref void, key: i64) option[i64] = trust { ... }

Look up a key. Returns some(value) or none.

add

pub fn (LongMap) add[A: allocators.GeneralAllocator](ac: A, map: ref void, key: i64, delta: i64) i64

Add delta to the value stored at key (starting from zero) and return the new value. The key is hashed only once.

free

pub fn (LongMap) free[A: allocators.GeneralAllocator](ac: A, map: ref void) void

Free the map and its backing storage.