Module: collections.map

Source: ./collections/map.reef


Overview

collections/map - Hash map implementations

Provides hash map functionality for common key types. Due to language limitations with generics and hashing, we provide type-specific implementations rather than a fully generic Map[K,V].

Available maps:

  • StringIntMap: string keys -> int values
  • StringStringMap: string keys -> string values
  • IntIntMap: int keys -> int values
  • IntStringMap: int keys -> string values
  • StringAnyMap: string keys -> Any values (heterogeneous)

Implementation: Open addressing with linear probing. Capacity: FIXED at create (default 32; create_cap accepts 16/32/64). Does NOT auto-grow (unlike collections.hashmap). map_set returns false when load would exceed ~70% or when no free slot remains; updates of existing keys still succeed. Prefer collections.hashmap for unbounded maps.

Usage: let m: StringIntMap = create_string_int_map() map_set_si(m, "key", 42) let val: int = option.unwrap_or(map_get_si(m, "key"), 0)


Types

StringIntMap

Entry states: 0=empty, 1=occupied, 2=deleted (tombstone)

Fields:

Name Type
keys [string]
values [int]
states [int]
size int
cap int

StringStringMap

Fields:

Name Type
keys [string]
values [string]
states [int]
size int
cap int

IntIntMap

Fields:

Name Type
keys [int]
values [int]
states [int]
size int
cap int

IntStringMap

Fields:

Name Type
keys [int]
values [string]
states [int]
size int
cap int

StringAnyMap

Fields:

Name Type
keys [string]
values [Any]
states [int]
size int
cap int

MapBuilder

Fields:

Name Type
result StringAnyMap

Map

Generic map with string keys and any value type V Uses monomorphization - Map[int], Map[string], Map[float] are separate types

Fields:

Name Type
keys [string]
values [V]
states [int]
size int
cap int

Functions

fn hash_string(s: string): int

DJB2 hash function for strings

fn hash_int(n: int): int

Simple hash for integers (mixing function)

fn create_string_int_map(): StringIntMap

fn create_string_int_map_cap(cap: int): StringIntMap

fn map_set_si(m: StringIntMap, key: string, value: int): bool

fn map_get_si(m: StringIntMap, key: string): option.Option[int]

fn map_has_si(m: StringIntMap, key: string): bool

fn map_remove_si(m: StringIntMap, key: string): bool

fn map_size_si(m: StringIntMap): int

fn create_string_string_map(): StringStringMap

fn map_set_ss(m: StringStringMap, key: string, value: string): bool

fn map_get_ss(m: StringStringMap, key: string): option.Option[string]

fn map_has_ss(m: StringStringMap, key: string): bool

fn map_remove_ss(m: StringStringMap, key: string): bool

fn map_size_ss(m: StringStringMap): int

fn create_int_int_map(): IntIntMap

fn map_set_ii(m: IntIntMap, key: int, value: int): bool

fn map_get_ii(m: IntIntMap, key: int): option.Option[int]

fn map_has_ii(m: IntIntMap, key: int): bool

fn map_remove_ii(m: IntIntMap, key: int): bool

fn map_size_ii(m: IntIntMap): int

fn create_int_string_map(): IntStringMap

fn map_set_is(m: IntStringMap, key: int, value: string): bool

fn map_get_is(m: IntStringMap, key: int): option.Option[string]

fn map_has_is(m: IntStringMap, key: int): bool

fn map_remove_is(m: IntStringMap, key: int): bool

fn map_size_is(m: IntStringMap): int

fn create_string_any_map(): StringAnyMap

fn map_set_sa(m: StringAnyMap, key: string, value: Any): bool

fn map_get_sa(m: StringAnyMap, key: string): option.Option[Any]

fn map_has_sa(m: StringAnyMap, key: string): bool

fn map_remove_sa(m: StringAnyMap, key: string): bool

fn map_size_sa(m: StringAnyMap): int

fn build_map(): MapBuilder

Create a new MapBuilder

fn map_add(b: MapBuilder, key: string, value: Any): MapBuilder

Add a key-value pair to the builder and return it (for chaining)

fn map_done(b: MapBuilder): StringAnyMap

Finish building and return the completed map

fn map_keys_si(m: StringIntMap, result: [string], max: int): int

Copy all keys from StringIntMap to result array Returns the number of keys copied

fn map_values_si(m: StringIntMap, result: [int], max: int): int

Copy all values from StringIntMap to result array

fn map_keys_ss(m: StringStringMap, result: [string], max: int): int

Copy all keys from StringStringMap to result array

fn map_values_ss(m: StringStringMap, result: [string], max: int): int

Copy all values from StringStringMap to result array

fn map_keys_ii(m: IntIntMap, result: [int], max: int): int

Copy all keys from IntIntMap to result array

fn map_values_ii(m: IntIntMap, result: [int], max: int): int

Copy all values from IntIntMap to result array

fn create_map(): Map[V]

Create a new Map with default capacity (32)

fn map_set(m: Map[V], key: string, value: V): bool

Set a key-value pair in the map

fn map_get(m: Map[V], key: string): option.Option[V]

Get a value from the map. Returns Some(value) if key is present, None otherwise. For a default fallback, use option.unwrap_or(map_get(m, key), default).

fn map_has(m: Map[V], key: string): bool

Check if a key exists in the map

fn map_remove(m: Map[V], key: string): bool

Remove a key from the map

fn map_size(m: Map[V]): int

Get the number of entries in the map

fn map_keys(m: Map[V], result: [string], max: int): int

Copy all keys to result array


Procedures

proc map_clear_si(m: StringIntMap)

proc map_clear_ss(m: StringStringMap)

proc map_clear_ii(m: IntIntMap)

proc map_clear_is(m: IntStringMap)

proc map_clear_sa(m: StringAnyMap)

proc map_clear(m: Map[V])

Clear all entries from the map


Generated by reefc doc