zena:ordered-map
import {…} from 'zena:ordered-map';
Classes
OrderedMap
final class OrderedMap<K extends Hashable, V> extends HashMap<K, V>
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"
new(capacity: i32 = 16)
14 inherited members
HashMap
size: i32 { get; }
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.
getOr(key: K, defaultValue: V): V
The value for key, or defaultValue if the key is absent.
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.
has(key: K): boolean
delete(key: K): boolean
Removes key. Returns whether it was present.
Iterable
contains(value: T): boolean
Returns true if the collection contains the specified value.
all(predicate: (item: T) => boolean): boolean
Returns true if all elements match the given predicate. Returns true if the collection is empty.
some(predicate: (item: T) => boolean): boolean
Returns true if at least one element matches the given predicate. Returns false if the collection is empty.
fold<R>(initial: R, combine: (acc: R, item: T) => R): R
Reduces the collection to a single value by accumulating state.
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.
filter(predicate: (item: T) => boolean): Iterable<T>
Returns a new iterable containing only the elements that match the predicate.