std/collections
Hash-based mutable collections.
m :: MutableMap<string, i32> {}
m["answer"] = 42
s :: MutableSet<i32> {}
s.add(10)Java-inspired API : struct-literal construction + UFCS methods. Method
names like put / get / len / is_empty are deliberately shared
across the two collections; UFCS dispatch picks the right overload by
receiver type (cf. SPEC §6 Function overloading).
Sequence collections live in the language : [T] arrays support push,
len, indexed access, mutation, and for x in arr: no MutableList
wrapper is needed at MVP.
MutableMap is a COMPACT (open-addressed) hash map : dense parallel
ekeys / evals arrays in insertion order, plus a sparse i32 index
(hash slot → entry position). No per-entry node, no next pointer, no
nullable bucket array: ~3-4× less memory than a chained map, faster
insert / iterate, and far faster string-key lookups. The index doubles and
rehashes past a 0.75 load factor. MutableSet wraps it (each member a
T → true).
Types
MutableMap :: struct<K: Hash & Equals, V> Compact hash map keyed by any K that implements Hash & Equals. Entries
are stored in insertion order in dense ekeys / evals; a sparse i32
index resolves a key's hash to its entry position. K's hash() is
queried on insert / lookup / rehash, equals() on probe comparisons.
m[k] is V | null, null for an absent key: when V admits null itself,
tell the two apart with k in m.
Functions
get_or_put :: fn<K: Hash & Equals, V>(self: MutableMap<K, V>, key: K, value: V) -> V The value stored for key, after storing value there when key was
absent. One probe either way. The caller evaluates value even when key
is present: for a default that allocates, take get_or_put_with.
Parameters
| self | MutableMap<K, V> | the map receiver. |
| key | K | key to look up. |
| value | V | stored when key is absent. |
Returns
V the value now stored for key.Example
index_of :: MutableMap<string, i64> {}
at :: index_of.get_or_put("x", index_of.len())get_or_put_with :: fn<K: Hash & Equals, V>(self: MutableMap<K, V>, key: K, make: fn() -> V) -> V The value stored for key, after storing what make returns there when
key was absent. make runs only on a miss, so a default that allocates
costs nothing when key is present. A hit takes one probe; a miss takes a
second one after make, which may itself change the map.
Parameters
| self | MutableMap<K, V> | the map receiver. |
| key | K | key to look up. |
| make | fn() -> V | builds the value to store when key is absent. |
Returns
V the value now stored for key.Example
by_module :: MutableMap<string, string[]> {}
by_module.get_or_put_with("std/io", () -> []).push("println")clear :: fn<K, V>(self: MutableMap<K, V>) -> void Logically empty the map while KEEPING its backing arrays (ekeys / evals /
index) allocated, so a subsequent refill reuses them instead of allocating
a fresh map. For hot reuse loops, e.g. a dataflow fixpoint's scratch set
that would otherwise mint a new map every round. The dense entries past
size are stale but unreachable (size = 0), and put overwrites them
from slot 0 on the next insert. O(index slots).
Parameters
| self | MutableMap<K, V> | the map receiver. |
clear :: fn<T>(self: MutableSet<T>) -> void Logically empty the set, keeping the backing arrays for reuse: for hot
reuse loops (e.g. a dataflow fixpoint's scratch set). Delegates to the
backing map's clear. O(index slots).
Parameters
| self | MutableSet<T> | No description. |
remove :: fn<K: Hash & Equals, V>(self: MutableMap<K, V>, key: K) -> bool Remove the entry for key, if present. The matching entry is overwritten by
the last dense entry (which is re-pointed in the index), the freed dense slot
becomes spare, and the key's index slot is tombstoned. A no-op when absent.
Parameters
| self | MutableMap<K, V> | the map receiver. |
| key | K | key whose entry to drop. |
Returns
bool true when an entry was removed, false when key was absent.remove :: fn<T: Hash & Equals>(self: MutableSet<T>, value: T) -> bool Remove value from the set, if present. Delegates to the backing map's
remove. A no-op when value is not a member.
Parameters
| self | MutableSet<T> | the set receiver. |
| value | T | the element to drop. |
Returns
bool true when value was removed, false when it was absent.len :: fn<K, V>(self: MutableMap<K, V>) -> i64 Number of (key, value) entries currently stored in self. O(1).
Parameters
| self | MutableMap<K, V> | No description. |
Returns
i64 len :: fn<T>(self: MutableSet<T>) -> i64 Number of members in the set. O(1).
Parameters
| self | MutableSet<T> | No description. |
Returns
i64 is_empty :: fn<K, V>(self: MutableMap<K, V>) -> bool True iff no entries have been inserted (or all were removed). O(1).
Parameters
| self | MutableMap<K, V> | No description. |
Returns
bool is_empty :: fn<T>(self: MutableSet<T>) -> bool True iff no members have been added (or all were removed). O(1).
Parameters
| self | MutableSet<T> | No description. |
Returns
bool keys :: fn<K, V>(self: MutableMap<K, V>) -> K[] Snapshot of every key in insertion order. Allocates a fresh [K].
Parameters
| self | MutableMap<K, V> | the map receiver. |
Returns
K[] a new array of every stored key.values :: fn<K, V>(self: MutableMap<K, V>) -> V[] Snapshot of every value in insertion order, matching keys(). Allocates a
fresh [V]. Pairing keys and values index-by-index gives a stable view.
Parameters
| self | MutableMap<K, V> | the map receiver. |
Returns
V[] a new array of every stored value.add :: fn<T: Hash & Equals>(self: MutableSet<T>, value: T) -> bool Insert value. Returns true if value was newly added, false if it
was already present (the set is unchanged in the latter case).
Parameters
| self | MutableSet<T> | the set receiver. |
| value | T | the element to add. |
Returns
bool true on insertion, false on duplicate.