On this page

zena:ordered-map

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

Classes

OrderedMap

zena
final class OrderedMap<K extends Hashable, V> extends HashMap<K, V>
Extends HashMap<K, V>
Re-exported from zena:collections/hash-map.zena

A hash map that iterates in insertion order — exported as OrderedHashMap.

Order is maintained by a doubly-linked list through the entries, so it costs two pointers per entry and nothing per lookup. Replacing an existing key's value keeps its position; deleting a key and inserting it again moves it to the end.

Iteration follows the same rules as JavaScript's Map:

  • entries inserted during an iteration are visited by it;
  • entries deleted before an iteration reaches them are not visited;
  • deleting the entry an iteration is parked on does not disturb it;
  • once an iterator reports exhaustion it stays exhausted.

Example: let m = new OrderedHashMap<String, i32>(); m["b"] = 2; m["a"] = 1; // Iteration order: "b", "a"

Constructors
zena
new(capacity: i32 = 16)
#
Methods
zena
clear(): void
#

Removes every entry.

zena
keys(): Iterator<K>
#

Returns an iterator over the keys, in insertion order.

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

Calls callback once per entry, in insertion order, under the same rules as iteration.

14 inherited members
From HashMap
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.

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

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

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.