Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/StableRBTree.mo
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,39 @@ module {
/// iterator is persistent, like the tree itself
public func entries<K, V>(tree: Tree<K, V>) : 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<K, V>(tree: Tree<K,V>) : I.Iter<K>
{ 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<K, V>(tree: Tree<K,V>) : I.Iter<V>
{ 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<K, V>(tree: Tree<K, V>) : 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<K, V>(tree: Tree<K,V>) : I.Iter<K>
{ 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<K, V>(tree: Tree<K,V>) : I.Iter<V>
{ I.map(entriesRev(tree), func (kv : (K, V)) : V { kv.1 }) };




type IterRep<K, V> = List.List<{ #tr:Tree<K, V>; #kv:(K, ?V) }>;

/// Direction of iteration (forwards or backwards)
Expand Down