Skip to content
FutureBlitz

    Install

    Import std.collections.map

    std.collections.map

    A hash map that keeps its entries in the order they were added.

    Its entries are in an array, each bucket starts a chain of the entries whose hashes land in it, and each entry links to the ones added before and after it, so removing one leaves the others in order. What a change takes out or replaces is let go of once the Map is whole again, so a key's or value's Destroy that it runs can use the Map.

    std/collections/map.fb

    Types

    Map class

    @DebugEntries Public Class Map<K: Hashable, V> Implements Iterable<(K, V)>, Cloneable, Defaultable

    A hash map from keys to values, which keeps its entries in the order they were added.

    Examples

    Local ages = {"Ada": 36, "Alan": 41}
    ages["Grace"] = 85
    Expect(ages.Get("Ada") == 36)
    Expect(ages.Get("Bob") == Null)
    Expect(ages.Keys() == ["Ada", "Alan", "Grace"])

    std/collections/map.fb, line 32

    • Public New() constructor #

      A new, empty Map.

    • Public New(default: V) constructor #

      A Map whose [] gives value for a key that isn't there, without adding the key. It's one value, which every such read gives: a default that's an object or an array is shared, so to give each key one of its own, use GetOrAdd.

    • Public Property Count: Int property #

      How many entries it holds.

    • Public Method Add(key: K, value: V) method #

      Adds an entry, or gives an existing key its new value.

    • Public Method TryAdd(key: K, value: V): Bool method #

      Adds an entry when the key isn't there, and tells whether it wasn't.

    • Public Method AddAll(other: Map<K, V>) method #

      Adds each of other's entries, in its order, as Add does.

    • Public Method Get(key: K): V? method #

      The key's value, or Null when it isn't there.

    • Public Method Get(key: K, fallback: V): V method #

      The key's value, or fallback when it isn't there.

    • Public Method GetOrAdd(key: K, make: Func(): V): V method #

      The key's value, adding what make gives for it first when it isn't there, which is the key's value though make added the key itself.

      Examples

      Local groups = Map<String, Int[]>()
      groups.GetOrAdd("odd", Function(): Int[] => []).Add(1)
      groups.GetOrAdd("odd", Function(): Int[] => []).Add(3)
      Expect(groups["odd"] == [1, 3])
    • Public Method Update(key: K, change: Func(V): V): Bool method #

      Gives the key's value what change gives for it, and tells whether the key was there. A key that change removed stays removed.

      Examples

      Local stock = {"apples": 3}
      Expect(stock.Update("apples", Function(n: Int) => n - 1))
      Expect(!stock.Update("pears", Function(n: Int) => n - 1))
      Expect(stock["apples"] == 2)
    • @PanicsAtCaller Operator [](key: K): V operator #

      The key's value. A key that isn't there gives the Map's default, without adding the key, or panics when the Map has none.

    • Operator []=(key: K, value: V) operator #

      Gives the key a value, adding it when it isn't there.

    • Public Method Contains(key: K): Bool method #

      Whether key has an entry.

    • Public Method Remove(key: K): Bool method #

      Removes the key's entry, and tells whether there was one. The others keep their order.

    • @PanicsAtCaller Public Method RemoveWhere(test: Func(K, V): Bool): Int method #

      Removes each entry that test says to, and tells how many went.

    • Public Method Clear() method #

      Takes every entry out.

    • @PanicsAtCaller Public Method Reserve(count: Int) method #

      Makes room for count entries in all, so adding up to that many doesn't move them again.

    • Public Method Keys(): K[] method #

      Its keys, in order, in a new array.

    • Public Method Values(): V[] method #

      Its values, in order, in a new array.

    • Public Static Method DefaultValue(): Map<K, V> static method #

      A new, empty Map.

    • Public Method Clone(): Map<K, V> method #

      A new Map with the same entries, in the same order, and the same default.

    • @PanicsAtCaller Public Method Filter(keep: Func(K, V): Bool): Map<K, V> method #

      A new Map of the entries that keep says to keep.

    • @PanicsAtCaller Public Method Find(test: Func(K, V): Bool): (K, V)? method #

      The first entry that test says is the one, or Null.

    • @PanicsAtCaller Public Method Any(test: Func(K, V): Bool): Bool method #

      Whether test says yes for any entry, going no further than the first.

    • @PanicsAtCaller Public Method MapValues<U>(change: Func(V): U): Map<K, U> method #

      A new Map of the same keys, in the same order, each with what change gives for its value.

    • Public Method Iterate(): MapIterator<K, V> method #

      Each key and its value, for 'For key, value In map'. Giving a key a new value while going over them is fine, but adding or removing one panics.

    MapIterator struct

    Public Struct MapIterator<K: Hashable, V> Implements Iterator<(K, V)>

    The entries of a Map, as (key, value), in order, which a For loop goes over.

    std/collections/map.fb, line 424

    • @PanicsAtCaller Public ByRef Method Next(): (K, V)? method #

      The next key and its value, in the order they were added, or Null after the last. Panics when the Map changed since the loop began.

    Extends

    Map extend

    Extend Map<K, V> Implements Equatable

    Compares Maps with == and !=, where their values can be compared.

    std/collections/map.fb, line 446

    • Operator ==(a: Map<K, V>, b: Map<K, V>): Bool operator #

      Whether the two have the same keys, each with an equal value, whatever order they were added in.

    Map extend

    Extend Map<K, V> Implements Hashable

    Hashes a Map, where its keys and values can be hashed, so it can be a Map's key or a Set's value.

    std/collections/map.fb, line 464

    • Public Method Hash(): UInt64 method #

      A hash of its entries, whatever order they were added in, which Maps that are equal share.

    Map extend

    Extend Map<K, V> Implements Printable

    Shows a Map in a string, as its ToString writes it.

    std/collections/map.fb, line 479

    • Public Method ToString(): String method #

      Its entries, in order, as "{a: 1, b: 2}".

    Report an issue with this page or with FutureBlitz on GitHub.