Mutable hash map (aka Hashtable)
This module is a direct deconstruction of the object oriented HashMap.mo class in motoko-base (https://github.com/dfinity/motoko-base/blob/master/src/HashMap.mo) into a series of functions and is meant to be persistent across updates, with the tradeoff being larger function signatures.
One of the main additions/difference between the two modules at this time besides class deconstruction is the differing initialization methods if an initialCapacity is known (to prevent array doubling slowdown during map initialization)
The rest of this documentation and code therefore follows directly from HashMap.mo, with minor modifications that do not attempt to change implementation. Please raise and issue if a discrepancy in implementation is found.
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.
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
andentries
.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 firstset
.Internally, table growth policy is very simple, for now: Double the current capacity when the expected bucket list size grows beyond a certain constant.