ClassStableHashMap

The below is the reference HashMap.mo file from https://github.com/dfinity/motoko-base/blob/master/src/HashMap.mo that was used to create StableHashMap.mo

Mutable stable hash map (aka Hashtable)

This module defines an imperative hash map (hash table), with a general key and value type. exportProps() and importProps() functions were added to the original implementation in order to allow the hashtable and it's item count to be retrievable and therefore persistable across upgrades.

It has a minimal object-oriented interface: get, set, delete, count and entries.

The class is parameterized by the key's equality and hash functions, and an initial capacity. However, as with the Buffer class, no array allocation happens until the first set.

Internally, table growth policy is very simple, for now: Double the current capacity when the expected bucket list size grows beyond a certain constant.

type HashTableProps<K, V> = { table : HashTable<K, V>; _count : Nat }

class StableHashMap<K, V>(
  initCapacity : Nat,
  keyEq : (K, K) -> Bool,
  keyHash : K -> Hash.Hash
)

public func importProps(props : HashTableProps<K, V>) : ()

public func exportProps() : HashTableProps<K, V>

public func size() : Nat

Returns the number of entries in this StableHashMap.

public func delete(k : K)

Deletes the entry with the key k. Doesn't do anything if the key doesn't exist.

public func remove(k : K) : ?V

Removes the entry with the key k and returns the associated value if it existed or null otherwise.

public func get(k : K) : ?V

Gets the entry with the key k and returns its associated value if it existed or null otherwise.

public func put(k : K, v : V)

Insert the value v at key k. Overwrites an existing entry with key k

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

Insert the value v at key k and returns the previous value stored at k or null if it didn't exist.

public func keys() : Iter.Iter<K>

An Iter over the keys.

public func vals() : Iter.Iter<V>

An Iter over the values.

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

Returns an iterator over the key value pairs in this StableHashMap. Does not modify the StableHashMap.

An imperative StableHashMap with a minimal object-oriented interface. Maps keys of type K to values of type V.

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

clone cannot be an efficient object method, ...but is still useful in tests, and beyond.

public func fromIter<K, V>(
  iter : Iter.Iter<(K, V)>,
  initCapacity : Nat,
  keyEq : (K, K) -> Bool,
  keyHash : K -> Hash.Hash
) : StableHashMap<K, V>

Clone from any iterator of key-value pairs

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

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