Skip to reference

std.map

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.map — the documented API surface of the prelude's Map[K, V]: the keyed store the wordcount exemplar is written against. Stability tier: core.

WHAT IS THE LANGUAGE'S AND WHAT IS STD'S — RULED, and this header stops being a filing. F-0011 asked three questions when this module was written (sc02) and s152 ([type.map], [type.map.key], [mem.map.absent]; wolf-lang#11 item 4, #154) answers two of them and leaves the third exactly as it was:

- **The key protocol: RULED, and there is no Hash half.** This header used to say "hashing lives inside the implementations and is unspecified … the Hash half is the filing's open question". [type.map.key] retires that sentence: Map[K, V] admits str, int, char and bool keys this edition — the four whose equality the LANGUAGE defines — and [K: Eq] is satisfied by the four through std.cmp's ordinary impls. So the bound the five functions below already carried is not "the honest half" of a protocol; it is the whole of it. A struct key is E0418 and waits on derived equality, which is named upstream and unruled. impl Eq for char landed in std.cmp at sc44 for this clause: the ruling names four keys and that module shipped three impls. - **tally: RULED, and it is std's.** The filed question was "is absent-key-defaulting a language rule or a std function?" and it closes the way the filing recommended. The LANGUAGE rules the absence — m[k] is V ! {none}, never (); m[k] = v inserts; m[k] op= v is E0417, whose note names map.tally(mut m, k) BY NAME — and STD owns the defaulting. tally's body below is the clause's own spelling, unchanged since sc02 and now ruled rather than proposed. - Iteration order: unchanged, and deliberately. keys, values and pairs promise the SAME order as each other for an unmodified map, and nothing more. Both machines answer insertion order at these pins and neither promise is made here.

Map itself stays the language's. The sc02 recommendation — that it become a std-defined generic type — is answered in the other direction and the answer is better than the recommendation: Map is TyKind::Map in the compiler, a wolf_rt::map seam in the native runtime and a served type in the checked machine, so it is a language type with a spec clause instead of scaffolding with a deadline. This module is the API surface over it, which is what §8's container rule has said all along.

get copies only the value selected by the language's index read m[k]; has asks the same read for presence. Both keep its none row for an absent key. Neither walks a pairs() snapshot or calls Eq.eq for every entry. get_or and tally keep their std defaulting behaviour and inherit get's lookup. The runtime owns the lookup algorithm; a hashed runtime needs no further std change.

The key functions keep their [K: cmp.Eq] bounds: that is the same public key protocol, not a new user-defined equality hook. For all four admitted key types, std.cmp's impl is exactly the language's ==. The four tests/map/get_*_agreement.lu rows compare the old walk with get on each key type. Checked, native and the local release witness run all four; lupin 0.1.58 cannot resolve the old walk's Eq.eq for char. Its exact refusal is gated, while the other small rows run. The full 10k row runs on checked/native; lupin has a 1k companion and a loud slow cell for 10k at its 60-second ceiling.

Absence is spelled none, the convention's own spelling — F-0003 is dead (sc05 pins, renamed at sc06) and nothing here waits on it.

Types and traits

No public declarations in this section.

Functions and methods

len

fn · Source line 76

pub fn len[K, V](m: Map[K, V]) -> int

The number of key-value entries. Total: no error row, no traps.

var m = Map[str, int]()
map.set(mut m, "a", 1)
map.len(m) == 1

is_empty

fn · Source line 88

pub fn is_empty[K, V](m: Map[K, V]) -> bool

Whether the map holds no entries. Total: no error row, no traps.

var m = Map[str, int]()
map.is_empty(m) == true
map.set(mut m, "a", 1)
map.is_empty(m) == false

set

fn · Source line 119

pub fn set[K, V](mut m: Map[K, V], k: K, v: V)

Binds k to v, replacing any previous binding. Total: no error row, no traps beyond allocation.

**The body is m[k] = copy v and the copy is not decoration.** This function's body was m[k] = v from sc02 until sc44 and nothing read it, because Map was untyped on both compiler rungs. At the pin that types it the mem tier read it for the first time and answered E1004 — "this stores a value from v's region into m's region — parameter regions are independent by default" — which took the WHOLE module down on both rungs, not just this function. copy is the fix-it the diagnostic itself suggests and it is in the grammar now; the value is copied into the container's region, which for the four key types and every V this edition admits is what a caller means by set.

The asymmetry behind it is filed upstream: (mut xs).push(v) under [T] needs no copy and m[k] = v under [K, V] does, and the same body monomorphic at Map[str, int] runs on all three lanes. Filed as wolf-lang#333; if it is ruled a bug the copy comes back out and no caller notices.

var m = Map[str, int]()
map.set(mut m, "a", 1)
map.set(mut m, "a", 2)
map.len(m) == 1

keys

fn · Source line 140

pub fn keys[K, V](m: Map[K, V]) -> List[K]

Every key, in the map's unspecified-but-consistent iteration order. Allocates a fresh list (to_-style, §1). Total: no error row, no traps beyond allocation.

The example populates through the language's m[k] = v rather than set: a mut-mode call followed by an allocating read-mode call is a false ub(mem.ub) under lupin's provenance model (F-0013), and a doc example never ships around a bug quietly.

var m = Map[str, int]()
m["a"] = 1
m["b"] = 2
var seen = 0
for k in map.keys(m) { seen = seen + 1 }
seen == 2

values

fn · Source line 158

pub fn values[K, V](m: Map[K, V]) -> List[V]

Every value, in the same order keys reports. Total: no error row, no traps beyond allocation.

var m = Map[str, int]()
m["a"] = 7
var total = 0
for v in map.values(m) { total = total + v }
total == 7

pairs

fn · Source line 177

pub fn pairs[K, V](m: Map[K, V]) -> List[(K, V)]

Every entry as a (key, value) pair, in the map's iteration order — the shape for (k, v) in map.pairs(m) destructures. Total: no error row, no traps beyond allocation.

var m = Map[str, int]()
m["a"] = 1
var count = 0
for pair in map.pairs(m) { count = count + 1 }
count == 1

clear

fn · Source line 189

pub fn clear[K, V](mut m: Map[K, V])

Empties the map, keeping it usable. Total: no error row, no traps.

var m = Map[str, int]()
map.set(mut m, "a", 1)
map.clear(mut m)
map.is_empty(m) == true

extend

fn · Source line 205

pub fn extend[K, V](mut m: Map[K, V], other: Map[K, V])

Copies every entry of other into m. Right wins: a key present in both ends up bound to other's value. Total: no error row, no traps beyond allocation.

var a = Map[str, int]()
var b = Map[str, int]()
map.set(mut a, "k", 1)
map.set(mut b, "k", 2)
map.extend(mut a, b)
map.len(a) == 1

get

fn · Source line 235

pub fn get[K: cmp.Eq, V](m: Map[K, V], k: K) -> V ! {none}

An independent copy of the value bound to k; none when the key is absent — the absence row, never a zero-value sentinel (§2). No traps beyond allocation.

Reads through the language's m[k], copying only the selected value rather than materializing an entry list or calling a trait per entry. Lookup uses the runtime's algorithm. The absence row is handled before copying, so List values do not ask native to copy a heap-bearing row. The cmp.Eq bound stays part of the signature; its four allowed key impls agree with index equality. get_or and tally inherit this lookup without changing their bodies.

Until sc78, this function walked m.pairs() (wolf-std#109). On the compiler tiers a List result could alias the stored List, contrary to [mem.tier0.excl.1]; lupin already returned independent values. Copying the selected value now prevents a caller's mutation from changing the stored value on every machine.

var m = Map[str, int]()
m["a"] = 1
(map.get(m, "a") else 0) == 1
(map.get(m, "z") else 0 - 1) == 0 - 1

get_or

fn · Source line 250

pub fn get_or[K: cmp.Eq, V](m: Map[K, V], k: K, d: V) -> V

An independent copy of the value bound to k, or of d when the key is absent — the eager default (a lazy variant waits on closures, c05). Total: no error row, no traps beyond allocation.

var m = Map[str, int]()
m["a"] = 1
map.get_or(m, "a", 0) == 1
map.get_or(m, "z", 0) == 0

has

fn · Source line 270

pub fn has[K: cmp.Eq, V](m: Map[K, V], k: K) -> bool

Whether k is bound. Total: no error row, no traps.

It asks the map itself — the index read m[k], whose none row IS "absent" ([mem.map.absent]) — and allocates nothing. Until sc57 this body walked m.pairs(), which builds a fresh List of every entry in the caller's region on every call: about 6.4 KB a call at 200 entries, never freed in a long-running loop (wolf-std#54, measured on pelt's variable lookups). tests/map/has_allocates_nothing.lu holds the region's ledger at zero across a hit and a miss.

var m = Map[str, int]()
m["a"] = 1
map.has(m, "a") == true
map.has(m, "z") == false

remove

fn · Source line 299

pub fn remove[K: cmp.Eq, V](mut m: Map[K, V], k: K) -> V ! {none}

Removes k's entry and returns the value it was bound to; none when the key was absent. No traps.

var m = Map[str, int]()
m["a"] = 1
(map.remove(mut m, "a") else 0) == 1
map.has(m, "a") == false
(map.remove(mut m, "a") else 0 - 1) == 0 - 1

Delegates to the language's m.remove(k) ([type.map]; D50 on wolf-lang#11, landed as wolf-lang#344), whose semantics are therefore this function's contract. The erase happens in place. The survivors keep their order, and a miss leaves the map unchanged. Its cost is the clause's: one scan to the key and a shift of the entries after it, with nothing allocated and nothing freed. Until sc52 this body rebuilt the whole map through pairs(), about N^2 entry copies to empty an N-entry map one key at a time. That ran past lupin's and the checked tier's step budgets at 3000 keys (tests/map/remove_many.lu), and the language had shipped the erase a release earlier (B117). The [K: cmp.Eq] bound stays, because it is the module's key protocol ([type.map.key]) and removing it would change the signature. The body no longer calls it.

tally

fn · Source line 318

pub fn tally[K: cmp.Eq](mut m: Map[K, int], k: K) -> int

Increments the count k is bound to and returns the new count, treating an absent key as zero — the wordcount idiom as a named operation instead of a defaulting index (tally[w] += 1, see the module header's filed question). Traps overflow when the count is at int's maximum: arithmetic is checked in every profile (X3).

The body below is the one [mem.map.absent]'s E0417 note names by name — a reader who writes m[k] += 1 is sent here by the compiler.

var m = Map[str, int]()
map.tally(mut m, "a") == 1
map.tally(mut m, "a") == 2
(map.get(m, "a") else 0) == 2

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: none. Their conditions are documented on the functions that return them.

No public declarations in this section.