Module imm/sorted_map
Persistent immutable sorted map using a left-leaning red-black tree.
SortedMap(K, V) stores key-value pairs in sorted order by key.
All mutations return new maps, leaving the original unchanged.
Backed by atomic object nodes for thread-safe structural sharing.
Keys must implement Eq, Ord, and Send. Values must implement Send.
Examples
{ SortedMap } :: import "std/imm/sorted_map";
m := SortedMap(i32, i32).new();
m = m.insert(i32(3), i32(30));
m = m.insert(i32(1), i32(10));
m = m.insert(i32(2), i32(20));
// keys are always sorted: 1, 2, 3
Types
Persistent immutable sorted map backed by a left-leaning red-black tree.
Type Parameters
| Name | Type | Notes |
|---|---|---|
K | Type | comptime |
V | Type | comptime |
Trait Implementations
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Ord(K), Send), V <: Send), SortedMap(K, V), ...)
new : (fn() -> Self)Create an empty sorted map.
Returns: Self
len : (fn(self: Self) -> usize)Number of 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 a value by key.
Returns: Option(V)
contains_key : (fn(self: Self, key: K) -> bool)Check if a key exists.
Returns: bool
insert : (fn(self: Self, key: K, value: V) -> Self)Return a new map with the key-value pair inserted (or updated).
Returns: Self
remove : (fn(self: Self, key: K) -> Self)Return a new map with the key removed.
Returns: Self
min_key : (fn(self: Self) -> Option(K))Get the minimum key in the map.
Returns: Option(K)
max_key : (fn(self: Self) -> Option(K))Get the maximum key by going right.
Returns: Option(K)
keys : (fn(self: Self) -> List(K))Return in-order list of keys.
Returns: List(K)
values : (fn(self: Self) -> List(V))Return list of values in key order.
Returns: List(V)
from_entries : (fn(pairs: Slice(Pair(K, V))) -> Self)Create a sorted map from a slice of key-value pairs.
Returns: Self
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Ord(K), Send), V <: (Eq(V), Send)), SortedMap(K, V), Eq(SortedMap(K, V))(...))
Internal LLRB tree node — an atomic object for thread-safe sharing.
Type Parameters
| Name | Type | Notes |
|---|---|---|
K | Type | comptime |
V | Type | comptime |
Trait Implementations
impl(forall(K : Type, V : Type), where(K <: (Eq(K), Ord(K), Send), V <: Send), RBNode(K, V), Acyclic())
Color of a red-black tree node.
Variants
| Variant | Fields | Description |
|---|---|---|
Red | ||
Black |