Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DMS-68] Use direct recursion in folding functions #12

Closed
wants to merge 2 commits into from
Closed
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
63 changes: 39 additions & 24 deletions src/PersistentOrderedMap.mo
Original file line number Diff line number Diff line change
Expand Up @@ -324,30 +324,39 @@ module {
///
/// Note: Full map iteration creates `O(n)` temporary objects that will be collected as garbage.
public func iter<K, V>(rbMap : Map<K, V>, direction : Direction) : I.Iter<(K, V)> {
object {
var trees : IterRep<K, V> = ?(#tr(rbMap), null);
public func next() : ?(K, V) {
switch (direction, trees) {
case (_, null) { null };
case (_, ?(#tr(#leaf), ts)) {
let turnLeftFirst : MapTraverser<K, V>
= func (l, xy, r, ts) { ?(#tr(l), ?(#xy(xy), ?(#tr(r), ts))) };

let turnRightFirst : MapTraverser<K, V>
= func (l, xy, r, ts) { ?(#tr(r), ?(#xy(xy), ?(#tr(l), ts))) };

switch direction {
case (#fwd) IterMap(rbMap, turnLeftFirst);
case (#bwd) IterMap(rbMap, turnRightFirst)
}
};

type MapTraverser<K, V> = (Map<K, V>, (K, V), Map<K, V>, IterRep<K, V>) -> IterRep<K, V>;

class IterMap<K, V>(rbMap : Map<K, V>, mapTraverser : MapTraverser<K, V>) {
var trees : IterRep<K, V> = ?(#tr(rbMap), null);
public func next() : ?(K, V) {
switch (trees) {
case (null) { null };
case (?(#tr(#leaf), ts)) {
trees := ts;
next()
};
case (_, ?(#xy(xy), ts)) {
case (?(#xy(xy), ts)) {
trees := ts;
?xy
}; // TODO: Let's float-out case on direction
case (#fwd, ?(#tr(#node(_, l, xy, r)), ts)) {
trees := ?(#tr(l), ?(#xy(xy), ?(#tr(r), ts)));
next()
};
case (#bwd, ?(#tr(#node(_, l, xy, r)), ts)) {
trees := ?(#tr(r), ?(#xy(xy), ?(#tr(l), ts)));
case (?(#tr(#node(_, l, xy, r)), ts)) {
trees := mapTraverser(l, xy, r, ts);
next()
}
}
}
}
};

/// Returns an Iterator (`Iter`) over the key-value pairs in the map.
Expand Down Expand Up @@ -527,11 +536,14 @@ module {
combine : (Key, Value, Accum) -> Accum
) : Accum
{
var acc = base;
for(val in iter(rbMap, #fwd)){
acc := combine(val.0, val.1, acc);
};
acc
switch (rbMap) {
case (#leaf) { base };
case (#node(_, l, (k, v), r)) {
let left = foldLeft(l, base, combine);
let middle = combine(k, v, left);
foldLeft(r, middle, combine)
}
}
};

/// Collapses the elements in `rbMap` into a single value by starting with `base`
Expand Down Expand Up @@ -567,11 +579,14 @@ module {
combine : (Key, Value, Accum) -> Accum
) : Accum
{
var acc = base;
for(val in iter(rbMap, #bwd)){
acc := combine(val.0, val.1, acc);
};
acc
switch (rbMap) {
case (#leaf) { base };
case (#node(_, l, (k, v), r)) {
let right = foldRight(r, base, combine);
let middle = combine(k, v, right);
foldRight(l, middle, combine)
}
}
};


Expand Down