Skip to content

std.map

open-addressed maps/sets and SortedMap, all in an Arena; no per-entry frees

pub ByteKey :: constraint($K)

keys whose bytes are their value: no floats, slices, unions or padding

pub AutoHashMap :: struct($K: ByteKey, $V: type) {
region: *Arena,
states: []u8,
keys: []K,
vals: []V,
count: usize,
tombs: usize,
}

keys hashed and compared by their raw bytes

  • count: live entries
  • tombs: tombstone slots (count toward the load factor)
pub len :: fn(self: *Self) -> usize
pub cap :: fn(self: *Self) -> usize
pub is_empty :: fn(self: *Self) -> bool
pub put :: fn(self: *Self, k: K, v: V)

overwrites; grows past 0.75 load (live + tombstones)

pub get :: fn(self: *Self, k: K) -> ?V
pub contains :: fn(self: *Self, k: K) -> bool
pub remove :: fn(self: *Self, k: K) -> bool

false if absent; leaves a tombstone

pub clear :: fn(self: *Self)

keeps the tables for reuse

pub entries :: fn(self: *Self) -> AutoEntries(K, V)

no particular order; put/remove while iterating may skip or repeat

pub Entry :: struct($K: type, $V: type) {
key: K,
value: V,
}
pub AutoEntries :: struct($K: ByteKey, $V: type) {
m: *AutoHashMap(K, V),
i: usize,
}

AutoHashMap.entries iterator

  • i: the next slot to look at
pub next :: fn(self: *Self) -> ?Entry(K, V)
pub auto :: fn($K: ByteKey, $V: type, a: *Arena) -> AutoHashMap(K, V)

empty map in a

pub StrMap :: struct($V: type) {
region: *Arena,
states: []u8,
keys: [][]const u8,
vals: []V,
count: usize,
tombs: usize,
}

string keys by content; keys are copied into the arena

pub len :: fn(self: *Self) -> usize
pub cap :: fn(self: *Self) -> usize
pub is_empty :: fn(self: *Self) -> bool
pub put :: fn(self: *Self, k: []const u8, v: V)
pub get :: fn(self: *Self, k: []const u8) -> ?V
pub contains :: fn(self: *Self, k: []const u8) -> bool
pub remove :: fn(self: *Self, k: []const u8) -> bool
pub clear :: fn(self: *Self)
pub entries :: fn(self: *Self) -> StrEntries(V)

no particular order; put/remove while iterating may skip or repeat

pub StrEntries :: struct($V: type) {
m: *StrMap(V),
i: usize,
}

StrMap.entries iterator; keys are the map’s own copies

pub next :: fn(self: *Self) -> ?Entry([]const u8, V)
pub strmap :: fn($V: type, a: *Arena) -> StrMap(V)

empty map in a

pub AutoSet :: struct($K: ByteKey) {
m: AutoHashMap(K, u8),
}

AutoHashMap(K, u8) with nothing stored

pub len :: fn(self: *Self) -> usize
pub is_empty :: fn(self: *Self) -> bool
pub contains :: fn(self: *Self, k: K) -> bool
pub add :: fn(self: *Self, k: K) -> bool

true if newly added

pub remove :: fn(self: *Self, k: K) -> bool

true if it was there

pub clear :: fn(self: *Self)
pub each :: fn(self: *Self) -> AutoSetKeys(K)

no particular order

pub AutoSetKeys :: struct($K: ByteKey) {
it: AutoEntries(K, u8),
}

AutoSet.each iterator

pub next :: fn(self: *Self) -> ?K
pub StrSet :: struct {
m: StrMap(u8),
}

strings by content; keeps its own copies

pub len :: fn(self: *Self) -> usize
pub is_empty :: fn(self: *Self) -> bool
pub contains :: fn(self: *Self, k: []const u8) -> bool
pub add :: fn(self: *Self, k: []const u8) -> bool

true if newly added

pub remove :: fn(self: *Self, k: []const u8) -> bool

true if it was there

pub clear :: fn(self: *Self)
pub each :: fn(self: *Self) -> StrSetKeys

no particular order

pub StrSetKeys :: struct {
it: StrEntries(u8),
}

StrSet.each iterator

pub next :: fn(self: *Self) -> ?[]const u8
pub autoset :: fn($K: ByteKey, a: *Arena) -> AutoSet(K)

empty set in a

pub strset :: fn(a: *Arena) -> StrSet

empty set in a

pub SortedMap :: struct($K: type, $V: type, $C: type) {
region: *Arena,
keys: []K,
vals: []V,
count: usize,
order: C,
}

sorted arrays in the arena; binary-search get, put/remove shift (fine to ~10k entries) C has less(self, a, b), like sort::Asc(K) or sort::By(K)

  • keys: first count in use
pub len :: fn(self: *Self) -> usize
pub is_empty :: fn(self: *Self) -> bool
pub put :: fn(self: *Self, k: K, v: V)

replaces an existing value

pub get :: fn(self: *Self, k: K) -> ?V
pub contains :: fn(self: *Self, k: K) -> bool
pub remove :: fn(self: *Self, k: K) -> bool

true if it was there

pub first :: fn(self: *Self) -> ?Entry(K, V)

smallest key; null if empty

pub last :: fn(self: *Self) -> ?Entry(K, V)

largest key; null if empty

pub entries :: fn(self: *Self) -> SortedEntries(K, V, C)

smallest first; put/remove while iterating may skip or repeat

pub from :: fn(self: *Self, k: K) -> SortedEntries(K, V, C)

from the first key not before k

pub clear :: fn(self: *Self)
pub SortedEntries :: struct($K: type, $V: type, $C: type) {
m: *SortedMap(K, V, C),
i: usize,
}

SortedMap.entries/from iterator, key order

pub next :: fn(self: *Self) -> ?Entry(K, V)
pub sorted_with :: fn($K: type, $V: type, $C: type, a: *Arena, order: C) -> SortedMap(K, V, C)

map in a ordered by order

pub sorted :: fn($K: type, $V: type, a: *Arena) -> SortedMap(K, V, sort::Asc(K))

keys ordered by <

pub sorted_by :: fn($K: type, $V: type, a: *Arena, less: fn(K, K) -> bool) -> SortedMap(K, V, sort::By(K))

keys ordered by less (mem::less_bytes for text)