Module: collections.hashmap
Source: ./collections/hashmap.reef
Overview
collections/hashmap - Generic Hash Map implementation
Provides a type-safe generic HashMap[V] with string keys. Uses open addressing with linear probing for collision handling. Grows and rehashes automatically when an insert would push load past 70% (double capacity, min 1). Updates of existing keys never rehash, even when load is already ≥ 70%. set returns true on success; false only on hard failure — not because the map was "full".
Usage: import collections.hashmap import core.option as option let m = hashmap.create:int // Create with default value 0 m.set("age", 42) let age = m.get("age") // Some(42) -- infers Option[int] if m.has("age") println("found!") end if
Available factory functions: create[:V](default_val: V) -> HashMap[V] - 256 slot capacity create_with_capacity[:V](default_val: V, cap) -> HashMap[V] - Custom capacity create_int_hashmap() -> HashMap[int] - Legacy factory create_string_hashmap() -> HashMap[string] - Legacy factory create_bool_hashmap() -> HashMap[bool] - Legacy factory
Methods: set(key, value) -> bool - Insert or update, returns true on success try_set(key, value) -> Result[bool, Error] Ok(true) = new key inserted; Ok(false) = existing key updated; Err = hard failure after grow/retry (not "full" under normal use) get(key) -> option.Option[V] - Get value; Some(value) if found, None if not has(key) -> bool - Check if key exists remove(key) -> bool - Remove key, returns true if existed len() -> int - Number of entries clear() - Remove all entries keys() -> [string] - Get array of all keys capacity() -> int - Get total capacity is_occupied_at(idx) -> bool - Check if slot is occupied key_at(idx) -> string - Get key at iteration index value_at(idx) -> V - Get value at iteration index
Performance:
- O(1) average for get/set/has/remove
- O(n) for keys() iteration / rehash
- Auto-rehash keeps load factor under 70%
Types
HashMap
Entry states: 0=empty, 1=occupied, 2=deleted (tombstone)
Fields:
| Name | Type |
|---|---|
keys |
[string] |
values |
[V] |
states |
[int] |
size |
int |
cap |
int |
default_value |
V |
Functions
fn hash_string(s: string): int
DJB2 hash function for strings - fast and good distribution
fn create(default_val: V): HashMap[V]
Generic factory - create HashMap[V] with 256-slot capacity
fn create_with_capacity(default_val: V, cap: int): HashMap[V]
Generic factory with custom capacity Note: Uses runtime array allocation via new T
fn create_int_hashmap(): HashMap[int]
Create a HashMap[int] with default capacity
fn create_string_hashmap(): HashMap[string]
Create a HashMap[string] with default capacity
fn create_bool_hashmap(): HashMap[bool]
Create a HashMap[bool] with default capacity
Generated by reefc doc