Skip to content

Map

Source: std/map.mw.

LinearMap scans linearly and is meant for tiny maps; HashMap is a chained hash map.

struct LinearMapEntry {
key: *String;
value: rawptr;
}
struct LinearMap {
entries: *VecPtr; // Stocke des *LinearMapEntry
}
struct HashNode {
key: *String;
value: rawptr;
hash: i64;
next: *HashNode;
}
struct HashMap {
buckets: *VecPtr;
capacity: i64;
count: i64;
}
fn linear_map_new(initial_cap: i64) -> ^LinearMap
fn linear_map_put(map: *LinearMap, key: *String, value: rawptr)
fn linear_map_get(map: *LinearMap, key: *String) -> rawptr
fn linear_map_free(map: ^LinearMap)
fn linear_map_destroy(map: *LinearMap)

The map owns its entries: a ^LinearMap frees them when dropped. Keys and values are left to the caller.

fn hash_djb2(s: *String) -> i64
fn hash_map_new(capacity: i64) -> ^HashMap
fn hash_map_grow(map: *HashMap)

Doubles the number of buckets and relinks every node.

fn hash_map_put(map: *HashMap, key: *String, value: rawptr)
fn hash_map_get(map: *HashMap, key: *String) -> rawptr
fn hash_map_contains(map: *HashMap, key: *String) -> bool
fn hash_map_len(map: *HashMap) -> i64
fn hash_map_remove(map: *HashMap, key: *String) -> rawptr

Removes key and returns its value (0 when absent). The key string itself is not freed.

fn hash_map_keys(map: *HashMap) -> *VecPtr

Keys as a vector of *String (the keys are shared, not copied).

fn hash_map_free(map: ^HashMap)
fn hash_map_destroy(map: *HashMap)

Frees the map and its nodes; keys and values are left to the caller. A ^HashMap does it by itself when it goes out of scope; hash_map_destroy is for a map held by a plain pointer.