Module imm/map
Persistent immutable hash map using a Hash Array Mapped Trie (HAMT).
Map(K, V) is an immutable hash map backed by atomic object nodes for
thread-safe structural sharing. All mutations return new maps, leaving the
original unchanged.
Keys must implement Eq, Hash, and Send. Values must implement Send.
Examples
{ Map } :: import "std/imm/map";
m := Map(i32, i32).new();
m = m.insert(i32(1), i32(100));
m = m.insert(i32(2), i32(200));
assert((m.get(i32(1)).unwrap() == i32(100)), "key 1 maps to 100");
assert((m.len() == usize(2)), "two entries");
Types
Persistent immutable hash map.
Type Parameters
| Name | Type | Notes |
|---|---|---|
K | Type | comptime |
V | Type | comptime |
Trait Implementations
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: Send), Map(K, V), ...)
new : (fn() -> Self)Create an empty map.
Returns: Self
len : (fn(self: Self) -> usize)Number of key-value entries.
Returns: usize
is_empty : (fn(self: Self) -> bool)Check if the map is empty.
Returns: bool
get : (fn(self: Self, key: K) -> Option(V))Look up the value associated with key.
Returns: Option(V)
contains_key : (fn(self: Self, key: K) -> bool)Check if the map contains key.
Returns: bool
insert : (fn(self: Self, key: K, value: V) -> Self)Return a new map with key mapped to value.
Returns: Self
remove : (fn(self: Self, key: K) -> Self)Return a new map without key.
Returns: Self
merge : (fn(self: Self, other: Self) -> Self)Merge another map into this one. Keys from other overwrite self.
Returns: Self
keys : (fn(self: Self) -> List(K))Collect all keys into a list.
Returns: List(K)
values : (fn(self: Self) -> List(V))Collect all values into a list.
Returns: List(V)
entries : (fn(self: Self) -> List(Pair(K, V)))map_values : (fn(forall(U : Type), self: Self, f: Impl(Fn(a: V) -> U), where(U <: Send)) -> Map(K, U))Apply a function to each value, producing a new map.
Returns: Map(K, U)
filter : (fn(self: Self, f: Impl(Fn(k: K, v: V) -> bool)) -> Self)Keep only entries where the predicate returns true.
Returns: Self
from_entries : (fn(pairs: Slice(Pair(K, V))) -> Self)Create a map from a slice of key-value pairs.
Returns: Self
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: (Send, Eq(V))), Map(K, V), Eq(Map(K, V))(...))
HAMT branch node -- bitmap + compact child array.
_children_ptr is *(void) to break the circular type dependency with
MapNode. It is cast to *(MapNode(K, V)) in methods.
Type Parameters
| Name | Type | Notes |
|---|---|---|
K | Type | comptime |
V | Type | comptime |
Trait Implementations
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: Send), MapBranch(K, V), Dispose(...))
dispose : (fn(self: Self) -> unit)Returns: unit
HAMT collision node -- multiple entries sharing the same hash.
Type Parameters
| Name | Type | Notes |
|---|---|---|
K | Type | comptime |
V | Type | comptime |
Trait Implementations
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: Send), MapCollision(K, V), Dispose(...))
dispose : (fn(self: Self) -> unit)Returns: unit