zena:map

zena
import {…} from 'zena:map';

Classes

MapEntry

zena
class MapEntry<K, V>
Re-exported from zena:collections/map.zena

A key-value pair from a Map.

Constructors
zena
new(nextEntry: MapEntry<K, V> | null)
#
Properties
zena
key: K
#

The key this entry was inserted with.

zena
var value: V
#

The value associated with this key. Mutable, so an entry obtained from iteration can be updated in place.

HashMap

zena
class HashMap<K extends Hashable, V> with IterableUtils<MapEntry<K, V>> implements Map<K, V>
Implements Map<K, V>
Mixes in IterableUtils<MapEntry<K, V>>
Re-exported from zena:collections/hash-map.zena

A hash map with separate chaining, growing at a 75% load factor.

Iteration order is unspecified and changes as the map grows; use OrderedHashMap when order matters. Lookup, insertion and deletion are O(1) amortized.

A subclass adapts the table through :newEntry, :entryRemoved and :rehash, which is how OrderedMap threads an insertion-order list through the entries without duplicating any of the lookup paths.

Constructors
zena
new(capacity: i32 = 16)
#
Properties
zena
size: i32 { get; }
#
Methods
zena
get(key: K): inline (true, V) | inline (false, _)
#

(true, value) if the key is present, (false, _) otherwise. An inline tuple, so this is the allocation-free way to test and read in one step.

zena
getOr(key: K, defaultValue: V): V
#

The value for key, or defaultValue if the key is absent.

zena
getOption(key: K): Option<V>
#

Some(value) if the key is present, None otherwise. Prefer get unless the result has to be stored or passed on.

zena
has(key: K): boolean
#
zena
delete(key: K): boolean
#

Removes key. Returns whether it was present.

zena
clear(): void
#

Removes every entry.

zena
keys(): Iterator<K>
#

Returns an iterator over the keys.

zena
forEach(callback: (key: K, value: V) => void): void
#

Calls callback once per entry.

Operators
zena
operator []=(key: K, value: V): void
#
zena
operator [](key: K): V
#

The value for key. Throws KeyNotFoundError if the key is absent.

6 inherited members
From Iterable
zena
contains(value: T): boolean
#

Returns true if the collection contains the specified value.

zena
all(predicate: (item: T) => boolean): boolean
#

Returns true if all elements match the given predicate. Returns true if the collection is empty.

zena
some(predicate: (item: T) => boolean): boolean
#

Returns true if at least one element matches the given predicate. Returns false if the collection is empty.

zena
fold<R>(initial: R, combine: (acc: R, item: T) => R): R
#

Reduces the collection to a single value by accumulating state.

zena
find(predicate: (item: T) => boolean): inline (true, T) | inline (false, _)
#

Finds the first element that matches the predicate. Returns an inline tuple (true, item) if found, or (false, _) if not.

zena
filter(predicate: (item: T) => boolean): Iterable<T>
#

Returns a new iterable containing only the elements that match the predicate.

Interfaces

Hashable

zena
interface Hashable
Re-exported from zena:hashable

A type that can be used as a key in hash-based collections like HashMap and HashSet.

The contract: if two values are equal (per ==, which is a virtual call to operator == when the class defines one, and reference equality otherwise), they must return the same hashCode. The reverse is not required — unequal values may share a hash code, though fewer collisions means better performance.

A class that uses reference equality (no operator ==) should return an identity hash: a value that is unique per instance and stable for the lifetime of the instance, e.g. a counter assigned in the constructor.

Case classes automatically satisfy Hashable: the compiler generates a structural hashCode() (and operator ==) from their parameters. Primitives (i32, boolean, enums, and distinct types over them) also satisfy the Hashable constraint; they hash to their own value. Strings hash with FNV-1a, cached after the first computation.

Methods
zena
hashCode(): i32
#

Map

zena
interface Map<K extends Hashable, V> extends Iterable<MapEntry<K, V>>
Extends Iterable<MapEntry<K, V>>
Re-exported from zena:collections/map.zena

A collection of key-value pairs with efficient lookup by key.

A key maps to at most one value; inserting over an existing key replaces the value.

Keys must not change while they are in the map. An implementation places an entry by the key's hash code and finds it again by equality, so a key that changes either leaves its entry unreachable — still held, still counted in size, but no longer findable — or collides with another. Prefer keys that cannot change: primitives, Strings, enums, and case classes over those.

Properties
zena
size: i32 { get; }
#

How many entries the map holds.

Methods
zena
get(key: K): inline (true, V) | inline (false, _)
#

(true, value) if the key is present, (false, _) otherwise.

The way to ask and read in one step, and the one to prefer: the result is an inline tuple, so it allocates nothing and is usually destructured straight away.

if (let (true, v) = m.get(k))

zena
getOr(key: K, defaultValue: V): V
#

The value for key, or defaultValue if the key is absent.

zena
getOption(key: K): Option<V>
#

Some(value) if the key is present, None otherwise.

Answers the same question as get, but as a value that can be stored, returned or passed on. That costs an allocation, so prefer get when the answer is used where it is asked for.

zena
has(key: K): boolean
#

Whether key is present.

zena
delete(key: K): boolean
#

Removes key. Returns whether it was present.

zena
clear(): void
#

Removes every entry.

zena
keys(): Iterator<K>
#

An iterator over the keys, in the map's iteration order.

zena
forEach(callback: (key: K, value: V) => void): void
#

Calls callback once per entry, in the map's iteration order.

Operators
zena
operator [](key: K): V
#

The value for key. Throws KeyNotFoundError if the key is absent, so reach for it only when the key's presence is an invariant rather than a question.

zena
operator []=(key: K, value: V): void
#

Associates value with key, replacing any value already there.

6 inherited members
From Iterable
zena
contains(value: T): boolean
#

Returns true if the collection contains the specified value.

zena
all(predicate: (item: T) => boolean): boolean
#

Returns true if all elements match the given predicate. Returns true if the collection is empty.

zena
some(predicate: (item: T) => boolean): boolean
#

Returns true if at least one element matches the given predicate. Returns false if the collection is empty.

zena
fold<R>(initial: R, combine: (acc: R, item: T) => R): R
#

Reduces the collection to a single value by accumulating state.

zena
find(predicate: (item: T) => boolean): inline (true, T) | inline (false, _)
#

Finds the first element that matches the predicate. Returns an inline tuple (true, item) if found, or (false, _) if not.

zena
filter(predicate: (item: T) => boolean): Iterable<T>
#

Returns a new iterable containing only the elements that match the predicate.

Functions

hash

zena
declare function hash<T>(value: T): i32
Re-exported from zena:hashable

The hash code of a value, dispatched on its static type: primitives hash by value, Strings by content, and classes through hashCode().

This is what hash-based collections call. Prefer it over hashCode() — it works for primitives, which have no methods.

equals

zena
declare function equals<T>(a: T, b: T): boolean
Re-exported from zena:hashable

Whether two values of the same type are equal, dispatched on their static type: primitives compare by value, Strings by content, and classes through operator == when the class defines one and by reference otherwise.