From f10443f5b1a073a71830b0445a9d481373386c84 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Tue, 27 Apr 2021 14:11:45 -0600 Subject: [PATCH 1/3] Trie tries to avoid asserting false. --- src/Trie.mo | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Trie.mo b/src/Trie.mo index a3e19ae6..01b2a9ae 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. + 0 + } +}; + /// 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)) From 9934584172f320dc374df34faa7b3813ebf90f28 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Tue, 27 Apr 2021 14:21:14 -0600 Subject: [PATCH 2/3] bool type --- src/Trie.mo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Trie.mo b/src/Trie.mo index 01b2a9ae..f7fc7881 100644 --- a/src/Trie.mo +++ b/src/Trie.mo @@ -1,4 +1,4 @@ -/// Functional maps +s/// Functional maps /// /// Functional maps (and sets) whose representation is "canonical", and /// independent of their operation history (unlike other popular search trees). @@ -101,7 +101,7 @@ public func hashBit(bits: Hash.Hash, bitPos: Nat) : Bool { } else { // rather than assert false (as with Hash.bit), // we simply give zero for all higher bits. - 0 + false } }; From a3be754559aaecf86fc0d13f98f2382914bdba62 Mon Sep 17 00:00:00 2001 From: Matthew Hammer Date: Tue, 27 Apr 2021 14:44:08 -0600 Subject: [PATCH 3/3] thanks Yan --- src/Trie.mo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Trie.mo b/src/Trie.mo index f7fc7881..791b72d4 100644 --- a/src/Trie.mo +++ b/src/Trie.mo @@ -1,4 +1,4 @@ -s/// Functional maps +/// Functional maps /// /// Functional maps (and sets) whose representation is "canonical", and /// independent of their operation history (unlike other popular search trees).