Tin
Standard library · Map and Set

On this page

Standard library

Map and Set

A Map<K, V> looks values up by key; a Set<T> holds each value once. Both are values: assigning one copies it.

Example

var stock: Map<string, int> = newMap()
stock.set("tea", 4)
stock.set("coffee", 0)
stock["milk"] = 2
stock["milk"] += 1
print("{stock.get("tea")} {stock.get("milk")} {stock.has("coffee")} {stock.size()}")
print(stock.keys())
for item, n in stock { print("{item}: {n}") }
var seen = setOf(["tea", "tea", "jam"])
seen.add("bun")
print("{seen} {seen.has("jam")} {seen.size()}")
Some(4) Some(3) true 3
["tea", "coffee", "milk"]
tea: 4
coffee: 0
milk: 3
{"tea", "jam", "bun"} true 3

All 27 symbols

Set

type Set<T> = {  }

A set of distinct values, in the order they were first added. Built on Map, so lookups hash.

Map

type Map<K, V> = {  }

An insertion-ordered hash map: `get`, `set` and `has` take constant time on average (from 8 entries on; smaller maps are scanned). Keys are compared with `==`; a type with its own `equals` should also define `hash` (an `int` that is equal for equal keys), or lookups fall back to a scan. `m[k]` reads (panics if k is missing) and `m[k] = v` sets. `for k, v in m` walks it in insertion order; `keys()`, `values()` and `entries()` copy it out.

newMap

fn newMap<K, V>(): Map<K, V>

An empty map. The key and value types come from the annotation: `var m: Map<string, int> = newMap()`.

Map.set

fn Map.set<K, V>(m: inout Map<K, V>, key: K, value: V)

Stores `value` under `key`, replacing any earlier value. Changes the map, so it needs a `var`.

Map.get

fn Map.get<K, V>(m: Map<K, V>, key: K): Option<V>

The value stored under `key`, or None.

Map.at

fn Map.at<K, V>(m: Map<K, V>, key: K): V

The value stored under `key`; panics if there is none. `m[key]` is the same, and `m[key] = v` is `m.set(key, v)`.

Map.has

fn Map.has<K, V>(m: Map<K, V>, key: K): bool

Whether `key` is present.

Map.size

fn Map.size<K, V>(m: Map<K, V>): int

How many keys the map holds.

Map.keys

fn Map.keys<K, V>(m: Map<K, V>): K[]

The keys, in the order they were first set (a new array).

Map.values

fn Map.values<K, V>(m: Map<K, V>): V[]

The values, in the same order as keys() (a new array).

Map.entries

fn Map.entries<K, V>(m: Map<K, V>): ()<K, V>[]

(key, value) pairs in insertion order.

Map.getOr

fn Map.getOr<K, V>(m: Map<K, V>, key: K, fallback: V): V

The value for key, or fallback.

Map.remove

fn Map.remove<K, V>(m: inout Map<K, V>, key: K): Option<V>

Removes key and gives back its value, or None. Keeps the order of the rest; takes time in the size of the map.

Map.equals

fn Map.equals<K, V>(a: Map<K, V>, b: Map<K, V>): bool

Two maps are equal when they hold the same keys with equal values, in any order.

Map.hash

fn Map.hash<K, V>(m: Map<K, V>): int

Agrees with equals: the entries are combined in a way that ignores their order.

mapOf

fn mapOf<K, V>(pairs: ()<K, V>[]): Map<K, V>

A map holding these (key, value) pairs; a later pair replaces an earlier one with the same key.

newSet

fn newSet<T>(): Set<T>

No summary yet; the signature is the whole description.

setOf

fn setOf<T>(xs: T[]): Set<T>

A set holding these values.

Set.equals

fn Set.equals<T>(a: Set<T>, b: Set<T>): bool

Two sets are equal when they hold the same values, in any order.

Set.hash

fn Set.hash<T>(s: Set<T>): int

Agrees with equals, whatever the order the values were added in.

Set.add

fn Set.add<T>(s: inout Set<T>, x: T)

Adds x; adding it again changes nothing.

Set.has

fn Set.has<T>(s: Set<T>, x: T): bool

No summary yet; the signature is the whole description.

Set.remove

fn Set.remove<T>(s: inout Set<T>, x: T): bool

Removes x; gives whether it was there.

Set.size

fn Set.size<T>(s: Set<T>): int

No summary yet; the signature is the whole description.

Set.values

fn Set.values<T>(s: Set<T>): T[]

The values in the order they were added (a new array).

Set.union

fn Set.union<T>(a: Set<T>, b: Set<T>): Set<T>

The values in both sets.

Set.intersect

fn Set.intersect<T>(a: Set<T>, b: Set<T>): Set<T>

The values of a that are also in b.

See also