TrieMap

Functional map

This module defines an imperative hash map, with a general key and value type. It matches the interface and semantics of HashMap. Unlike HashMap, its internal representation uses a functional hash trie (see library Trie).

This class permits us to compare the performance of two representations of hash-based maps, where tries (as binary trees) permit more efficient, constant-time, cloning compared with ordinary tables. This property is nice for supporting transactional workflows where map mutations may be provisional, and where we may expect some mutations to be uncommitted, or to "roll back".

For now, this class does not permit a direct clone operation (neither does HashMap), but it does permit creating iterators via iter(). The effect is similar: Each iterator costs O(1) to create, but represents a fixed view of the mapping that does not interfere with mutations (it will not view subsequent insertions or mutations, if any).

TrieMap

class TrieMap<K, V>(isEq : (K, K) -> Bool, hashOf : K -> Hash.Hash)

size

func size() : Nat

Returns the number of entries in the map.

put

func put(k : K, v : V)

Associate a key and value, overwriting any prior association for the key.

replace

func replace(k : K, v : V) : ?V

Put the key and value, and return the (optional) prior value for the key.

get

func get(k : K) : ?V

Get the (optional) value associated with the given key.

delete

func delete(k : K)

Delete the (optional) value associated with the given key.

remove

func remove(k : K) : ?V

Delete and return the (optional) value associated with the given key.

entries

func entries() : I.Iter<(K, V)>

Returns an Iter over the entries.

Each iterator gets a persistent view of the mapping, independent of concurrent updates to the iterated map.

clone

func clone<K, V>(h : TrieMap<K, V>, keyEq : (K, K) -> Bool, keyHash : K -> Hash.Hash) : TrieMap<K, V>

Clone the map, given its key operations.

fromEntries

func fromEntries<K, V>(entries : I.Iter<(K, V)>, keyEq : (K, K) -> Bool, keyHash : K -> Hash.Hash) : TrieMap<K, V>

Clone an iterator of key-value pairs.

map

func map<K, V1, V2>(h : TrieMap<K, V1>, keyEq : (K, K) -> Bool, keyHash : K -> Hash.Hash, mapFn : (K, V1) -> V2) : TrieMap<K, V2>

Transform (map) the values of a map, retaining its keys.

mapFilter

func mapFilter<K, V1, V2>(h : TrieMap<K, V1>, keyEq : (K, K) -> Bool, keyHash : K -> Hash.Hash, mapFn : (K, V1) -> ?V2) : TrieMap<K, V2>

Transform and filter the values of a map, retaining its keys.