Lyricdev
GitHub

class Map

pub class Map<K :: [Hashable], V, H :: [Hasher] = DefaultHasher> :: [Iterable]

Conforms to Iterable, IndexSet<K>, FromArrayLiteral<(K, V)>, Equatable, Debug, Display, FromIterator<(K, V)>, Contains<K>, Clone.

A hash map from keys to values: each key once, in no order of its own.

H hashes the keys: DefaultHasher under the process's key unless the map is given another with Map.withHasher; the hasher is a value, copied fresh for every key. map[k] gives the value under k or null; map[k] = v puts v there and map[k] = null removes k.

The table is a Swiss table: lookup, insertion and removal take expected constant time. The control words lie in an array of their own, so a lookup that misses reads no slot; a slot holds its key and value side by side, so a hit reads one. Not safe across threads.

Methods

pub static fn withHasher(seed: H): Map<K, V, H>

Creates an empty map whose keys are hashed with seed.

With a fixed hasher the order is the same in every run: Map<string, int, FixedHasher>.withHasher(FixedHasher.new()).

pub fn length(): int

Returns how many keys it holds.

pub fn isEmpty(): bool

Returns whether it holds no keys.

pub fn get(k: K): ?V

Returns the value under k, or null where k is not there.

pub fn containsKey(k: K): bool

Returns whether it holds k.

pub mut fn insert(k: K, v: V): ?V

Puts v under k and returns the value it replaces, or null where k was not there.

pub mut fn remove(k: K): ?V

Removes k and returns its value, or null where k was not there.

pub fn getOr(k: K, otherwise: V): V

Returns the value under k, or otherwise where k is not there.

pub mut fn getOrInsert<E :: [Error]>(k: K, make: fn() -> V throws E): V throws E

Returns the value under k; where k is not there, inserts and returns what make() gives.

make is called only when k is missing.

Errors

Throws what make throws; the map is then unchanged.

pub mut fn update<E :: [Error]>(k: K, f: fn(V) -> V throws E): bool throws E

Replaces the value under k with f of it and returns whether k was there.

Errors

Throws what f throws; the value is then unchanged.

pub mut fn retain<E :: [Error]>(p: fn(K, V) -> bool throws E): int throws E

Removes the entries for which p does not hold and returns how many were removed.

Errors

Throws what p throws; the entries already removed stay removed.

pub fn iter(): MapEntriesIter<K, V, H>

Returns an iterator over the entries, (K, V), in no order of the map's own.

Panics

The next pull panics (LYR-RT0016) after a key is inserted or removed, or the map cleared, while the iterator walks.

pub fn keys(): MapKeysIter<K, V, H>

Returns an iterator over the keys, in the order of Map.iter.

pub fn values(): MapValuesIter<K, V, H>

Returns an iterator over the values, in the order of Map.iter.

pub mut fn clear(): void

Removes every key and frees the table.

fn index(k: K): ?V
mut fn setIndex(k: K, v: ?V): void
pub fn entries(): MapEntriesIter<K, V, H>

Returns an iterator over the entries, (K, V), in no order of the map's own; the same as Map.iter.

static fn fromLiteral(items: (K, V)[]): Map<K, V, DefaultHasher>
pub static fn new(): Map<K, V, DefaultHasher>

Creates an empty map hashing with the process's key; its order differs from run to run.

pub static fn from(pairs: Slice<(K, V)>): Map<K, V, DefaultHasher>

Creates a map of the pairs: Map.from([("a", 1), ("b", 2)]).

A later value of a key replaces the earlier one.

pub static fn withCapacity(n: int): Map<K, V, DefaultHasher>

Creates an empty map with room for n keys before it grows.

Panics

When n is negative.

fn equals(o: Map<K, V, H>): bool
fn debug(): string
fn debugTo(out: StringBuilder): void
fn show(): string
fn showTo(out: StringBuilder): void
static fn fromIter<I :: [Iterator<Item = (K, V)>]>(
    it: I
): Map<K, V, DefaultHasher> throws I.Error
fn contains(k: K): bool

Returns whether k is one of the keys; this is k in map.

fn clone(): Map<K, V, H>