Module imm/map

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

Map type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_612) : Type)

Persistent immutable hash map.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime

Trait Implementations

Eq
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)))

Collect all entries into a list of pairs.

Returns: 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))(...))
MapNode type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_327) : Type)

Tagged union representing a single HAMT node.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime
MapBranch type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_269) : Type)

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

NameTypeNotes
KTypecomptime
VTypecomptime

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

MapLeaf type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_85) : Type)

HAMT leaf node -- single key-value entry.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime
MapCollision type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_134) : Type)

HAMT collision node -- multiple entries sharing the same hash.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime

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

Pair type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_36) : Type)

Key-value pair used in collision nodes and entries.

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime
InsertResult type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_2510) : Type)

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime
RemoveResult type-function
fn(comptime(K) : Type, comptime(V) : Type) -> (comptime(fn_return_yo2fd406f1_id_3016) : Type)

Type Parameters

NameTypeNotes
KTypecomptime
VTypecomptime