std1.0.0-beta-7 SafeC docs

Module collections::map

#include <std/collections/map.h> source

HashMap<K,V> / StrMap<V> (open addressing, linear probing)

struct HashMap<int, long long> m = std::HashMap::new();   // K,V inferred from the target
m.insert(7, 49LL);
match (m.get(7)) { case null: ... case some(v): *v = 50LL; }
?long long gone = m.remove(7);
m.free();

struct StrMap<int> sm = std::StrMap::new();           // C-string keys (copied)
sm.insert("one", 1);

HashMap keys are hashed and compared by their bytes, so K must be a type whose value is fully described by its bytes: integers, enums, pointers, and structs without padding. Use StrMap for text keys.

get() hands out '?&V' — a reference into the table, valid until the next insert()/remove()/clear()/free() (any of which may move or drop entries).

A map owns its values: it is a Drop type, and V may own something itself (String, Vec, ...). insert() takes the value by value — overwriting a key drops the old value, and a value it can't store (OOM) is dropped; remove() moves the value out to the caller; clear() and drop() drop every value still in the map. The table resizes at a 0.75 load factor; tombstones count toward it, so a remove-heavy workload rehashes instead of degrading every probe to O(cap).

Structs

HashMap
StrMap