From a5024444119b63902fb1e600e2432b86ad2f1cf5 Mon Sep 17 00:00:00 2001 From: skilesare Date: Thu, 19 May 2022 20:00:47 -0500 Subject: [PATCH] Update StableRBTree.mo add keys, vals, keysRev and valsRev --- src/StableRBTree.mo | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/StableRBTree.mo b/src/StableRBTree.mo index 16b072b..2e45cd7 100644 --- a/src/StableRBTree.mo +++ b/src/StableRBTree.mo @@ -169,12 +169,38 @@ module { /// iterator is persistent, like the tree itself public func entries(tree: Tree) : I.Iter<(K, V)> { iter(tree, #fwd) }; + /// An `Iter` over the keys. + /// + /// Each iterator gets a _persistent view_ of the mapping, independent of concurrent updates to the iterated map. + public func keys(tree: Tree) : I.Iter + { I.map(entries(tree), func (kv : (K, V)) : K { kv.0 }) }; + + /// An `Iter` over the values. + /// + /// Each iterator gets a _persistent view_ of the mapping, independent of concurrent updates to the iterated map. + public func vals(tree: Tree) : I.Iter + { I.map(entries(tree), func (kv : (K, V)) : V { kv.1 }) }; + /// An iterator for the key-value entries of the map, in descending key order. /// /// iterator is persistent, like the tree itself public func entriesRev(tree: Tree) : I.Iter<(K, V)> { iter(tree, #bwd) }; +/// An `Iter` over the keys. + /// + /// Each iterator gets a _persistent view_ of the mapping, independent of concurrent updates to the iterated map. + public func keysRev(tree: Tree) : I.Iter + { I.map(entriesRev(tree), func (kv : (K, V)) : K { kv.0 }) }; + + /// An `Iter` over the values. + /// + /// Each iterator gets a _persistent view_ of the mapping, independent of concurrent updates to the iterated map. + public func valsRev(tree: Tree) : I.Iter + { I.map(entriesRev(tree), func (kv : (K, V)) : V { kv.1 }) }; + + + type IterRep = List.List<{ #tr:Tree; #kv:(K, ?V) }>;