HashMap¶
Module: Std::HashMap
An open-addressing hash map from integer keys to opaque pointer values.
Summary¶
struct HashMap {
data: *uint8;
cap: uint;
count: uint;
}
func Init(mapPtr: *HashMap)
func InitWithCap(mapPtr: *HashMap, capacity: uint)
extend HashMap {
func Len(self) -> uint
func Cap(self) -> uint
func Set(self, key: int, value: *opaque)
func Get(self, key: int) -> *opaque
func Contains(self, key: int) -> bool
func Remove(self, key: int) -> bool
func Clear(self)
func Free(self)
func Keys(self) -> *int
func Values(self) -> **opaque
}
Description¶
HashMap is an open-addressing hash table using linear probing. Keys are signed int values, values are *opaque pointers. The map automatically resizes when the load factor exceeds ⅞.
The hash function uses a splitmix64-derived mixing step for good distribution.
Initialization¶
A map must be initialized before use. Use Init for the default capacity (16) or InitWithCap for a custom minimum capacity. Maps should be freed with Free when no longer needed.
import Std::HashMap;
func Main() {
var map: HashMap;
HashMap::Init(&map);
map.Set(1, 42 as *opaque);
map.Free();
}
Functions¶
Set¶
Inserts or updates the value for key. Triggers a resize if the load factor exceeds ⅞.
Get¶
Returns the value for key, or null if not found.
Example¶
var map: HashMap;
HashMap::Init(&map);
map.Set(1, 42 as *opaque);
var val = map.Get(1); // 42 as *opaque
Contains¶
Returns true if key exists in the map.
Remove¶
Removes key from the map. Rehashes subsequent entries in the same probe chain to maintain correctness. Returns true if the key was found.
Clear¶
Removes all entries without deallocating the backing array — capacity is preserved.
Free¶
Frees the backing array and resets the map to zero capacity.
Keys¶
Returns a heap-allocated array of all keys. The caller must free it with Std::Memory::Free. Returns null if the map is empty.
Values¶
Returns a heap-allocated array of all values. The caller must free it with Std::Memory::Free. Returns null if the map is empty.