std/map

March 13, 2026 ยท View on GitHub

Map<V> is a generic hash map implementation mapping string keys to values of type V.

Usage

import "std/map.zc"

fn main() {
    let m = Map<int>::new();
    
    m.put("one", 1);
    m.put("two", 2);
    
    if (m.contains("one")) {
        let val = m.get("one");
        println "{val.unwrap()}";
    }
    
    m.remove("two");
} // m is freed automatically here

Struct Definition

struct Map<V> {
    keys: char**;
    vals: V*;
    // ... internal fields
}

Methods

Construction

MethodSignatureDescription
newMap<V>::new() -> Map<V>Creates a new, empty map.

Iteration

You can iterate over the map's key-value pairs using a for loop.

let m = Map<int>::new();
m.put("a", 1);

for entry in m {
    println "Key: {entry.key}, Val: {entry.val}";
}

The iterator yields a MapEntry<V> struct:

struct MapEntry<V> {
    key: char*;
    val: V;
}

Modification

MethodSignatureDescription
putput(self, key: char*, val: V)Inserts or updates a key-value pair.
removeremove(self, key: char*)Removes a key and its value from the map.

Access & Query

MethodSignatureDescription
getget(self, key: char*) -> Option<V>Retrieves the value associated with the key.
containscontains(self, key: char*) -> boolReturns true if the key exists.
lengthlength(self) -> usizeReturns the number of items in the map.
is_emptyis_empty(self) -> boolReturns true if the map is empty.
capacitycapacity(self) -> usizeReturns the current capacity of the map.

Iteration Helpers

MethodSignatureDescription
is_slot_occupiedis_slot_occupied(self, idx: usize) -> boolChecks if a raw slot index is occupied.
key_atkey_at(self, idx: usize) -> char*Gets key at raw slot index.
val_atval_at(self, idx: usize) -> VGets value at raw slot index.

Memory Management

MethodSignatureDescription
freefree(self)Frees the map's internal storage. Note: This does not free the values if they are pointers/objects.
Traitimpl Drop for MapAutomatically calls free() when out of scope.