diff --git a/src/StableRBTree.mo b/src/StableRBTree.mo index b42b04e..3f33fbe 100644 --- a/src/StableRBTree.mo +++ b/src/StableRBTree.mo @@ -178,11 +178,39 @@ 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) }>; /// Direction of iteration (forwards or backwards)