Skip to content

Commit ea0af97

Browse files
committed
fix getInterval bug by replacing getIndex with getExpectedIndex to supports keys that are not in the btree
1 parent 555e3c7 commit ea0af97

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/MemoryBTree/Base.mo

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,12 +994,18 @@ module {
994994

995995
public func getInterval<K, V>(btree : MemoryBTree, btree_utils : BTreeUtils<K, V>, start : ?K, end : ?K) : (Nat, Nat) {
996996
let start_rank = switch (start) {
997-
case (?key) getIndex(btree, btree_utils, key);
997+
case (?key) switch (getExpectedIndex(btree, btree_utils, key)) {
998+
case (#Found(index)) index;
999+
case (#NotFound(index)) index;
1000+
};
9981001
case (null) 0;
9991002
};
10001003

10011004
let end_rank = switch (end) {
1002-
case (?key) getIndex(btree, btree_utils, key) + 1; // +1 because the end is exclusive
1005+
case (?key) switch (getIndex(btree, btree_utils, key)) {
1006+
case (#Found(index)) index + 1; // +1 because the end is exclusive
1007+
case (#NotFound(index)) index + 1; // +1 because the end is exclusive
1008+
};
10031009
case (null) btree.count;
10041010
};
10051011

0 commit comments

Comments
 (0)