Skip to content

Commit

Permalink
Merge pull request #24 from serokell/wip/s-and-witch/reorder-some-bra…
Browse files Browse the repository at this point in the history
…nches

[DMS-68] Move leaf branch to the end of switch expression, as the most unlikely (Second attempt)
  • Loading branch information
s-and-witch authored Oct 16, 2024
2 parents 2d87094 + 86b876f commit d364a94
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/PersistentOrderedMap.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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

0 comments on commit d364a94

Please sign in to comment.