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] Move leaf branch to the end of switch expression, as the most unlikely #23

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
30 changes: 15 additions & 15 deletions src/PersistentOrderedMap.mo
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,13 @@ module {
public func map<K, V1, V2>(rbMap : Map<K, V1>, f : (K, V1) -> V2) : Map<K, V2> {
func mapRec(m : Map<K, V1>) : Map<K, V2> {
switch m {
case (#leaf) { #leaf };
case (#red(l, xy, r)) {
#red(mapRec l, (xy.0, f xy), mapRec r)
};
case (#black(l, xy, r)) {
#black(mapRec l, (xy.0, f xy), mapRec r)
};
case (#leaf) { #leaf }
}
};
mapRec(rbMap)
Expand All @@ -501,13 +501,13 @@ module {
/// where `n` denotes the number of key-value entries stored in the tree.
public func size<K, V>(t : Map<K, V>) : Nat {
switch t {
case (#leaf) { 0 };
case (#red(l, _, r)) {
size(l) + size(r) + 1
};
case (#black(l, _, r)) {
size(l) + size(r) + 1
}
};
case (#leaf) { 0 }
}
};

Expand Down Expand Up @@ -545,7 +545,6 @@ module {
) : Accum
{
switch (rbMap) {
case (#leaf) { base };
case (#red(l, (k, v), r)) {
let left = foldLeft(l, base, combine);
let middle = combine(k, v, left);
Expand All @@ -555,7 +554,8 @@ module {
let left = foldLeft(l, base, combine);
let middle = combine(k, v, left);
foldLeft(r, middle, combine)
}
};
case (#leaf) { base }
}
};

Expand Down Expand Up @@ -593,7 +593,6 @@ module {
) : Accum
{
switch (rbMap) {
case (#leaf) { base };
case (#red(l, (k, v), r)) {
let right = foldRight(r, base, combine);
let middle = combine(k, v, right);
Expand All @@ -603,7 +602,8 @@ module {
let right = foldRight(r, base, combine);
let middle = combine(k, v, right);
foldRight(l, middle, combine)
}
};
case (#leaf) { base }
}
};

Expand Down Expand Up @@ -633,7 +633,6 @@ module {

public func get<K, V>(t : Map<K, V>, compare : (K, K) -> O.Order, x : K) : ?V {
switch t {
case (#leaf) { null };
case (#red(l, xy, r)) {
switch (compare(x, xy.0)) {
case (#less) { get(l, compare, x) };
Expand All @@ -647,7 +646,8 @@ module {
case (#equal) { ?xy.1 };
case (#greater) { get(r, compare, x) }
}
}
};
case (#leaf) { null }
}
};

Expand Down Expand Up @@ -714,9 +714,6 @@ module {
: Map<K, V>{
func ins(tree : Map<K,V>) : Map<K,V> {
switch tree {
case (#leaf) {
#red(#leaf, (key,val), #leaf)
};
case (#black(left, xy, right)) {
switch (compare (key, xy.0)) {
case (#less) {
Expand Down Expand Up @@ -744,6 +741,9 @@ module {
#red(left, (key,newVal), right)
}
}
};
case (#leaf) {
#red(#leaf, (key,val), #leaf)
}
};
};
Expand Down Expand Up @@ -903,14 +903,14 @@ module {
};
func del(tree : Map<K,V>) : Map<K,V> {
switch tree {
case (#leaf) {
tree
};
case (#red(left, xy, right)) {
delNode(left, xy, right)
};
case (#black(left, xy, right)) {
delNode(left, xy, right)
};
case (#leaf) {
tree
}
};
};
Expand Down