std.map
std.map: hash maps with byte-string keys (`StrMap(V)`), with keys that are integers (`IntMap(K, V)`), and with any key that `==` compares (`HashMap(K, V)`: structs, enums, optionals, ...).
Both use open addressing with linear probing in a power-of-two table that is at most 3/4 full, and remove by shifting later entries back (no tombstones). A pointer from `getPtr` or an iterator is valid until the next `put` or `remove`.
StrEntry
pub fn StrEntry(comptime V: type) type {
return struct {
key: []u8
value: *mut V
}
}
An entry of a `StrMap`, from its iterator: the key and a pointer to the value.
StrMapIterator
pub fn StrMapIterator(comptime V: type) type {
return struct {
slots: []mut StrSlot(V)
next_slot: usize
pub fn next(self: *mut Self) ?StrEntry(V)
}
}
Walks the entries of a `StrMap` in no particular order.
pub fn next(self: *mut Self) ?StrEntry(V)-
The next entry, in any order; null after the last one.
StrMap
pub fn StrMap(comptime V: type) type {
return struct {
slots: []mut StrSlot(V)
count: usize
alloc: Allocator
pub fn init(alloc: Allocator) Self
pub fn deinit(self: *mut Self)
pub fn len(self: *Self) usize
pub fn put(self: *mut Self, key: []u8, value: V) AllocError!void
pub fn get(self: *Self, key: []u8) ?V
pub fn getPtr(self: *Self, key: []u8) ?*mut V
pub fn contains(self: *Self, key: []u8) bool
pub fn remove(self: *mut Self, key: []u8) bool
pub fn iterator(self: *Self) StrMapIterator(V)
}
}
A map from `[]u8` keys to `V` values. Keys are not copied: each key's bytes must stay unchanged while it is in the map.
pub fn init(alloc: Allocator) Self-
An empty map; nothing is allocated until the first `put`.
pub fn deinit(self: *mut Self)-
Frees the table; the keys are not freed (the map never owned them).
pub fn len(self: *Self) usize-
The number of entries.
pub fn put(self: *mut Self, key: []u8, value: V) AllocError!void-
Sets the value of `key`, adding the entry if it is new.
pub fn get(self: *Self, key: []u8) ?V-
The value of `key`, or null.
pub fn getPtr(self: *Self, key: []u8) ?*mut V-
A pointer to the value of `key`, or null; valid until the next put or remove.
pub fn contains(self: *Self, key: []u8) bool-
Is `key` in the map?
pub fn remove(self: *mut Self, key: []u8) bool-
Removes the entry of `key`; false when there was none.
pub fn iterator(self: *Self) StrMapIterator(V)-
Walks the entries in any order.
IntEntry
pub fn IntEntry(comptime K: type, comptime V: type) type {
return struct {
key: K
value: *mut V
}
}
An entry of an `IntMap`, from its iterator.
IntMapIterator
pub fn IntMapIterator(comptime K: type, comptime V: type) type {
return struct {
slots: []mut IntSlot(K, V)
next_slot: usize
pub fn next(self: *mut Self) ?IntEntry(K, V)
}
}
Walks the entries of an `IntMap` in no particular order.
pub fn next(self: *mut Self) ?IntEntry(K, V)-
The next entry, in any order; null after the last one.
IntMap
pub fn IntMap(comptime K: type, comptime V: type) type where K: Integer {
return struct {
slots: []mut IntSlot(K, V)
count: usize
alloc: Allocator
pub fn init(alloc: Allocator) Self
pub fn deinit(self: *mut Self)
pub fn len(self: *Self) usize
pub fn put(self: *mut Self, key: K, value: V) AllocError!void
pub fn get(self: *Self, key: K) ?V
pub fn getPtr(self: *Self, key: K) ?*mut V
pub fn contains(self: *Self, key: K) bool
pub fn remove(self: *mut Self, key: K) bool
pub fn iterator(self: *Self) IntMapIterator(K, V)
}
}
A map from integer keys `K` (`IntMap(u32, V)`) to `V` values.
pub fn init(alloc: Allocator) Self-
An empty map; nothing is allocated until the first `put`.
pub fn deinit(self: *mut Self)-
Frees the table.
pub fn len(self: *Self) usize-
The number of entries.
pub fn put(self: *mut Self, key: K, value: V) AllocError!void-
Sets the value of `key`, adding the entry if it is new.
pub fn get(self: *Self, key: K) ?V-
The value of `key`, or null.
pub fn getPtr(self: *Self, key: K) ?*mut V-
A pointer to the value of `key`, or null; valid until the next put or remove.
pub fn contains(self: *Self, key: K) bool-
Is `key` in the map?
pub fn remove(self: *mut Self, key: K) bool-
Removes the entry of `key`; false when there was none.
pub fn iterator(self: *Self) IntMapIterator(K, V)-
Walks the entries in any order.
HashEntry
pub fn HashEntry(comptime K: type, comptime V: type) type {
return struct {
key: K
value: *mut V
}
}
An entry of a `HashMap`, from its iterator.
HashMapIterator
pub fn HashMapIterator(comptime K: type, comptime V: type) type {
return struct {
slots: []mut HashSlot(K, V)
next_slot: usize
pub fn next(self: *mut Self) ?HashEntry(K, V)
}
}
Walks the entries of a `HashMap` in no particular order.
pub fn next(self: *mut Self) ?HashEntry(K, V)-
The next entry, in any order; null after the last one.
HashMap
pub fn HashMap(comptime K: type, comptime V: type) type where K: Hash {
return struct {
slots: []mut HashSlot(K, V)
count: usize
alloc: Allocator
pub fn init(alloc: Allocator) Self
pub fn deinit(self: *mut Self)
pub fn len(self: *Self) usize
pub fn put(self: *mut Self, key: K, value: V) AllocError!void
pub fn get(self: *Self, key: K) ?V
pub fn getPtr(self: *Self, key: K) ?*mut V
pub fn contains(self: *Self, key: K) bool
pub fn remove(self: *mut Self, key: K) bool
pub fn iterator(self: *Self) HashMapIterator(K, V)
}
}
A map from keys `K` to `V` values, for any `K` that `==` compares: keys are found by `@hash(key)` and `==` (a struct field by field).
pub fn init(alloc: Allocator) Self-
An empty map; nothing is allocated until the first `put`.
pub fn deinit(self: *mut Self)-
Frees the table.
pub fn len(self: *Self) usize-
The number of entries.
pub fn put(self: *mut Self, key: K, value: V) AllocError!void-
Sets the value of `key`, adding the entry if it is new.
pub fn get(self: *Self, key: K) ?V-
The value of `key`, or null.
pub fn getPtr(self: *Self, key: K) ?*mut V-
A pointer to the value of `key`, or null; valid until the next put or remove.
pub fn contains(self: *Self, key: K) bool-
Is `key` in the map?
pub fn remove(self: *mut Self, key: K) bool-
Removes the entry of `key`; false when there was none.
pub fn iterator(self: *Self) HashMapIterator(K, V)-
Walks the entries in any order.