diff --git a/src/Trie.mo b/src/Trie.mo index a3e19ae6..791b72d4 100644 --- a/src/Trie.mo +++ b/src/Trie.mo @@ -95,6 +95,16 @@ public type Key = { type List = List.List; +public func hashBit(bits: Hash.Hash, bitPos: Nat) : Bool { + if(bitPos <= Hash.length) { + Hash.bit(bits, bitPos) + } else { + // rather than assert false (as with Hash.bit), + // we simply give zero for all higher bits. + false + } +}; + /// Equality function for two `Key`s, in terms of equality of `K`'s. public func equalKey(keq:(K,K) -> Bool) : ((Key,Key) -> Bool) { func (key1:Key, key2:Key) : Bool { @@ -293,7 +303,7 @@ public func replace(t : Trie, k:Key, k_eq:(K,K)->Bool, v:?V) : (Tri (leaf(kvs, bitpos), null) }; case (#branch(b)) label profile_trie_replace_rec_branch : (Trie, ?V) { - let bit = Hash.bit(k.hash, bitpos); + let bit = hashBit(k.hash, bitpos); // rebuild either the left or right path with the (k,v) pair if (not bit) { let (l, v_) = rec(b.left, bitpos+1); @@ -336,7 +346,7 @@ public func find(t : Trie, k:Key, k_eq:(K,K) -> Bool) : ?V = label { AssocList.find,V>(l.keyvals, k, key_eq) } }; case (#branch(b)) { - let bit = Hash.bit(k.hash, bitpos); + let bit = hashBit(k.hash, bitpos); if (not bit) { label profile_trie_find_branch_left : (?V) { rec(b.left, bitpos+1) } @@ -360,7 +370,7 @@ func splitAssocList(al:AssocList,V>, bitpos:Nat) List.partition<(Key,V)>( al, func ((k : Key, v : V)) : Bool { - not Hash.bit(k.hash, bitpos) + not hashBit(k.hash, bitpos) } ) }; @@ -375,7 +385,7 @@ func splitList(l:AssocList,V>, bitpos:Nat) case null { (0, null, 0, null) }; case (?((k,v),t)) { let (cl, l, cr, r) = rec(t) ; - if (not Hash.bit(k.hash, bitpos)){ + if (not hashBit(k.hash, bitpos)){ (cl + 1, ?((k,v),l), cr, r) } else { (cl, l, cr + 1, ?((k,v),r))