Module collections/hash_map

collections/hash_map

Hash map using SwissTable algorithm with SIMD-accelerated lookup.

Types

HashMap type-function
fn(K : Type, V : Type) -> Type

High-performance hash map using SwissTable algorithm. Keys must implement Eq and Hash. Provides O(1) average lookup, insert, and delete.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime

Trait Implementations

Dispose Index Clone Trace
impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V), ...)
_alloc_with_capacity : (fn(capacity : usize) -> Result(Self, HashMapError))

Allocate memory for HashMap with given capacity Initializes all control bytes to EMPTY

Returns: Result(Self, HashMapError)

new : (fn() -> Self)

Create a new empty HashMap with default capacity

Returns: Self

with_capacity : (fn(requested_capacity : usize) -> Self)

Create a HashMap with a specific initial capacity Capacity will be rounded up to next power of 2

Returns: Self

_ctrl_ptr : (fn(self : Self) -> *(u8))

Get ctrl pointer (unwrapped for internal use)

Returns: *(u8)

_data_ptr : (fn(self : Self) -> *(Bucket(K, V)))

Get data pointer (unwrapped for internal use)

Returns: *(Bucket(K, V))

_find_bucket : (fn(self : Self, key : K, hash : u64) -> Option(usize))

Find bucket index for a given key using quadratic probing Returns Some(index) if key is found, None otherwise

Returns: Option(usize)

_find_insert_bucket : (fn(self : Self, hash : u64) -> usize)

Find first available bucket (EMPTY or DELETED) for insertion Returns bucket index using quadratic probing

Returns: usize

_needs_resize : (fn(self : Self) -> bool)

Check if HashMap needs resizing based on load factor

Returns: bool

_resize : (fn(self : Self, new_capacity : usize) -> Result(unit, HashMapError))

Resize and rehash the HashMap to a new capacity

Returns: Result(unit, HashMapError)

set : (fn(self : Self, key : K, value : V) -> Result(Option(V), HashMapError))

Insert or update a key-value pair Returns Ok(Some(old_value)) if key existed, Ok(None) if new key

Returns: Result(Option(V), HashMapError)

get : (fn(self : Self, key : K) -> Option(V))

Get a value by key Returns Some(value) if found, None otherwise

Returns: Option(V)

contains_key : (fn(self : Self, key : K) -> bool)

Check if a key exists in the map

Returns: bool

remove : (fn(self : Self, key : K) -> Option(V))

Remove a key-value pair by key Returns Some(value) if the key was found and removed, None otherwise

Returns: Option(V)

len : (fn(self : Self) -> usize)

Get the number of key-value pairs in the map

Returns: usize

is_empty : (fn(self : Self) -> bool)

Check if the map is empty

Returns: bool

clear : (fn(self : Self) -> unit)

Clear all key-value pairs Resets all control bytes to EMPTY

Returns: unit

impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V), Dispose(...))
dispose : (fn(self : Self) -> unit)

RAII destructor - automatically called when HashMap goes out of scope

Returns: unit

impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V), ...)
into_iter : (fn(self : Self) -> HashMapIter(K, V))

Returns: HashMapIter(K, V)

impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V), ...)
iter_ptr : (fn(self : Self) -> HashMapIterPtr(K, V))

Pointer iterator — yields *(Bucket(K, V)) for each occupied bucket. For privileged/unsafe internal use; value iteration goes through into_iter().

Returns: HashMapIterPtr(K, V)

impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V), ...)
keys : (fn(self : Self) -> HashMapKeys(K, V))

Returns: HashMapKeys(K, V)

impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V), ...)
values : (fn(self : Self) -> HashMapValues(K, V))

Returns: HashMapValues(K, V)

impl(generic(K : Type, V : Type), HashMap(K, V), Index(K)(...))
Output : V
index : (fn(inout(self) : Self, idx : K, where(K <: (Eq(K), Hash))) -> *(Self.Output))

Parameters

NameTypeNotes
idxK

Returns: *(Self.Output)

impl(generic(K : Type, V : Type), where(K <: (Clone, Eq(K), Hash), V <: Clone), HashMap(K, V), Clone(...))
clone : (fn(inout(self) : Self) -> Self)

Returns: Self

impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMap(K, V), Trace(...))
trace : (fn(self : Self, tracer : GcTracer) -> unit)

Cycle-GC tracing. Buckets live in a malloc'd open-addressing buffer the compiler's auto-derived field walk cannot reach, so trace each FULL slot (its control byte is neither EMPTY nor DELETED). tracer.visit takes the Bucket(K, V) slot POINTER; the per-value traversal descends inline through the value struct, tracing BOTH the key and the value, WITHOUT touching any reference count (a by-value managed handle would be dup'd then dropped, freeing a live element mid-collection). Mirrors the ArrayList Trace impl + the SwissTable slot scan.

Parameters

NameTypeNotes
tracerGcTracer

Returns: unit

HashMapError

Error variants for HashMap operations.

Variants

VariantFieldsDescription
AllocErrorerror: AllocError

Memory allocation failed.

KeyNotFound

The requested key was not found.

CapacityOverflow

Capacity calculation overflowed.

Bucket type-function
fn(K : Type, V : Type) -> Type

Key-value pair stored in a hash map bucket.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime
HashMapIter type-function
fn(K : Type, V : Type) -> Type

Value iterator for HashMap - yields Bucket(K, V) by value Scans ctrl bytes to find occupied slots.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime

Trait Implementations

Iterator
impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMapIter(K, V), Iterator(...))
Item : Bucket(K, V)
next : (fn(inout(self) : Self) -> Option(Bucket(K, V)))

Returns: Option(Bucket(K, V))

HashMapIterPtr type-function
fn(K : Type, V : Type) -> Type

Pointer iterator for HashMap - yields pointers to Bucket(K, V) Pointers are valid as long as the map is not modified during iteration.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime

Trait Implementations

Iterator
impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMapIterPtr(K, V), Iterator(...))
Item : *(Bucket(K, V))
next : (fn(inout(self) : Self) -> Option(*(Bucket(K, V))))

Returns: Option(*(Bucket(K, V)))

HashMapKeys type-function
fn(K : Type, V : Type) -> Type

Keys iterator - scans ctrl bytes directly and yields only the keys.

NOTE: This held a wrapped _inner : HashMapIter(K, V) and delegated self._inner.next() — but a method call on a struct FIELD of inout(self) advances a COPY of the field, so _index never moved and manual it.next() loops yielded the first key forever (for(...) masked it). Scan directly instead, mirroring HashMapIterPtr.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime

Trait Implementations

Iterator
impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMapKeys(K, V), Iterator(...))
Item : K
next : (fn(inout(self) : Self) -> Option(K))

Returns: Option(K)

HashMapValues type-function
fn(K : Type, V : Type) -> Type

Values iterator - scans ctrl bytes directly and yields only the values.

NOTE: Same direct-scan structure as HashMapKeys — see the note there for why the wrapped-_inner delegation form was replaced.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime

Trait Implementations

Iterator
impl(generic(K : Type, V : Type), where(K <: (Eq(K), Hash)), HashMapValues(K, V), Iterator(...))
Item : V
next : (fn(inout(self) : Self) -> Option(V))

Returns: Option(V)

Functions

hash_map function
fn(...(quote(entries))) -> unquote(Expr)

hash_map macro — construct a HashMap(K, V) literal.

K and V are inferred from the first entry's key and value via typeof, so at least one entry is required.

Example

m := hash_map(`a` => i32(1), `b` => i32(2));

Returns: unquote(Expr)