Skip to content

Commit 4bbf7fd

Browse files
committed
Bug fix: getPairOrNextLower returned undefined...
...if key was above maxKey and the tree had internal nodes
1 parent 062b0c1 commit 4bbf7fd

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

b+tree.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ var BNodeInternal = /** @class */ (function (_super) {
14221422
BNodeInternal.prototype.getPairOrNextLower = function (key, compare, inclusive, reusedArray) {
14231423
var i = this.indexOf(key, 0, compare), children = this.children;
14241424
if (i >= children.length)
1425-
return undefined;
1425+
return this.maxPair(reusedArray);
14261426
var result = children[i].getPairOrNextLower(key, compare, inclusive, reusedArray);
14271427
if (result === undefined && i > 0) {
14281428
return children[i - 1].maxPair(reusedArray);
@@ -1620,6 +1620,10 @@ var BNodeInternal = /** @class */ (function (_super) {
16201620
// increase, never decrease. Its type should be undefined[] but strangely
16211621
// TypeScript won't allow the comparison V[] === undefined[]. To prevent
16221622
// users from making this array too large, BTree has a maximum node size.
1623+
//
1624+
// FAQ: undefVals[i] is already undefined, so why increase the array size?
1625+
// Reading outside the bounds of an array is relatively slow because it
1626+
// has the side effect of scanning the prototype chain.
16231627
var undefVals = [];
16241628
var Delete = { delete: true }, DeleteRange = function () { return Delete; };
16251629
var Break = { break: true };

b+tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ class BNodeInternal<K,V> extends BNode<K,V> {
15671567
getPairOrNextLower(key: K, compare: (a: K, b: K) => number, inclusive: boolean, reusedArray: [K,V]): [K,V]|undefined {
15681568
var i = this.indexOf(key, 0, compare), children = this.children;
15691569
if (i >= children.length)
1570-
return undefined;
1570+
return this.maxPair(reusedArray);
15711571
const result = children[i].getPairOrNextLower(key, compare, inclusive, reusedArray);
15721572
if (result === undefined && i > 0) {
15731573
return children[i - 1].maxPair(reusedArray);

0 commit comments

Comments
 (0)