@@ -160,7 +160,8 @@ var BTree = /** @class */ (function () {
160
160
this . setPairs ( entries ) ;
161
161
}
162
162
Object . defineProperty ( BTree . prototype , "size" , {
163
- // ES6 Map<K,V> methods ///////////////////////////////////////////////////
163
+ /////////////////////////////////////////////////////////////////////////////
164
+ // ES6 Map<K,V> methods /////////////////////////////////////////////////////
164
165
/** Gets the number of key-value pairs in the tree. */
165
166
get : function ( ) { return this . _size ; } ,
166
167
enumerable : false ,
@@ -345,7 +346,8 @@ var BTree = /** @class */ (function () {
345
346
p = callback ( p , next . value , i ++ , this ) ;
346
347
return p ;
347
348
} ;
348
- // Iterator methods ///////////////////////////////////////////////////////
349
+ /////////////////////////////////////////////////////////////////////////////
350
+ // Iterator methods /////////////////////////////////////////////////////////
349
351
/** Returns an iterator that provides items in order (ascending order if
350
352
* the collection's comparator uses ascending order, as is the default.)
351
353
* @param lowestKey First key to be iterated, or undefined to start at
@@ -418,7 +420,7 @@ var BTree = /** @class */ (function () {
418
420
var _a = this . findPath ( highestKey ) || this . findPath ( this . maxKey ( ) ) , nodequeue = _a . nodequeue , nodeindex = _a . nodeindex , leaf = _a . leaf ;
419
421
check ( ! nodequeue [ 0 ] || leaf === nodequeue [ 0 ] [ nodeindex [ 0 ] ] , "wat!" ) ;
420
422
var i = leaf . indexOf ( highestKey , 0 , this . _compare ) ;
421
- if ( ! ( skipHighest || this . _compare ( leaf . keys [ i ] , highestKey ) > 0 ) )
423
+ if ( ! skipHighest && i < leaf . keys . length && this . _compare ( leaf . keys [ i ] , highestKey ) <= 0 )
422
424
i ++ ;
423
425
var state = reusedArray !== undefined ? 1 : 0 ;
424
426
return iterator ( function ( ) {
@@ -603,6 +605,8 @@ var BTree = /** @class */ (function () {
603
605
if ( otherSuccess && onlyOther )
604
606
return BTree . finishCursorWalk ( otherCursor , thisCursor , _compare , onlyOther ) ;
605
607
} ;
608
+ ///////////////////////////////////////////////////////////////////////////
609
+ // Helper methods for diffAgainst /////////////////////////////////////////
606
610
BTree . finishCursorWalk = function ( cursor , cursorFinished , compareKeys , callback ) {
607
611
var compared = BTree . compare ( cursor , cursorFinished , compareKeys ) ;
608
612
if ( compared === 0 ) {
@@ -725,6 +729,8 @@ var BTree = /** @class */ (function () {
725
729
var depthBNormalized = levelIndicesB . length - ( heightB - heightMin ) ;
726
730
return depthANormalized - depthBNormalized ;
727
731
} ;
732
+ // End of helper methods for diffAgainst //////////////////////////////////
733
+ ///////////////////////////////////////////////////////////////////////////
728
734
/** Returns a new iterator for iterating the keys of each pair in ascending order.
729
735
* @param firstKey: Minimum key to include in the output. */
730
736
BTree . prototype . keys = function ( firstKey ) {
@@ -748,7 +754,8 @@ var BTree = /** @class */ (function () {
748
754
} ) ;
749
755
} ;
750
756
Object . defineProperty ( BTree . prototype , "maxNodeSize" , {
751
- // Additional methods /////////////////////////////////////////////////////
757
+ /////////////////////////////////////////////////////////////////////////////
758
+ // Additional methods ///////////////////////////////////////////////////////
752
759
/** Returns the maximum number of children/values before nodes will split. */
753
760
get : function ( ) {
754
761
return this . _maxNodeSize ;
@@ -832,14 +839,22 @@ var BTree = /** @class */ (function () {
832
839
* If key === undefined, this function returns the highest pair.
833
840
*/
834
841
BTree . prototype . nextLowerPair = function ( key ) {
835
- var it = this . entriesReversed ( key , ReusedArray , true ) ;
836
- return it . next ( ) . value ;
842
+ if ( key === undefined ) {
843
+ var maxKey = this . maxKey ( ) ;
844
+ if ( maxKey === undefined )
845
+ return undefined ;
846
+ return [ maxKey , this . get ( maxKey ) ] ;
847
+ }
848
+ return this . _root . getOrNextLower ( key , this , false ) ;
837
849
} ;
838
850
/** Returns the next key smaller than the specified key (or undefined if there is none) */
839
851
BTree . prototype . nextLowerKey = function ( key ) {
840
852
var p = this . nextLowerPair ( key ) ;
841
853
return p ? p [ 0 ] : p ;
842
854
} ;
855
+ BTree . prototype . getOrNextLower = function ( key ) {
856
+ return this . _root . getOrNextLower ( key , this , true ) ;
857
+ } ;
843
858
/** Edits the value associated with a key in the tree, if it already exists.
844
859
* @returns true if the key existed, false if not.
845
860
*/
@@ -1053,6 +1068,7 @@ var BNode = /** @class */ (function () {
1053
1068
enumerable : false ,
1054
1069
configurable : true
1055
1070
} ) ;
1071
+ ///////////////////////////////////////////////////////////////////////////
1056
1072
// Shared methods /////////////////////////////////////////////////////////
1057
1073
BNode . prototype . maxKey = function ( ) {
1058
1074
return this . keys [ this . keys . length - 1 ] ;
@@ -1125,6 +1141,7 @@ var BNode = /** @class */ (function () {
1125
1141
}
1126
1142
return c === 0 ? i : i ^ failXor;*/
1127
1143
} ;
1144
+ /////////////////////////////////////////////////////////////////////////////
1128
1145
// Leaf Node: misc //////////////////////////////////////////////////////////
1129
1146
BNode . prototype . minKey = function ( ) {
1130
1147
return this . keys [ 0 ] ;
@@ -1140,6 +1157,11 @@ var BNode = /** @class */ (function () {
1140
1157
var i = this . indexOf ( key , - 1 , tree . _compare ) ;
1141
1158
return i < 0 ? defaultValue : this . values [ i ] ;
1142
1159
} ;
1160
+ BNode . prototype . getOrNextLower = function ( key , tree , inclusive ) {
1161
+ var i = this . indexOf ( key , - 1 , tree . _compare ) ;
1162
+ var indexOrLower = i < 0 ? ( i ^ - 1 ) - 1 : ( inclusive ? i : i - 1 ) ;
1163
+ return indexOrLower >= 0 ? [ this . keys [ indexOrLower ] , this . values [ indexOrLower ] ] : undefined ;
1164
+ } ;
1143
1165
BNode . prototype . checkValid = function ( depth , tree , baseIndex ) {
1144
1166
var kL = this . keys . length , vL = this . values . length ;
1145
1167
check ( this . values === undefVals ? kL <= vL : kL === vL , "keys/values length mismatch: depth" , depth , "with lengths" , kL , vL , "and baseIndex" , baseIndex ) ;
@@ -1151,6 +1173,7 @@ var BNode = /** @class */ (function () {
1151
1173
check ( depth == 0 || kL > 0 , "empty leaf at depth" , depth , "and baseIndex" , baseIndex ) ;
1152
1174
return kL ;
1153
1175
} ;
1176
+ /////////////////////////////////////////////////////////////////////////////
1154
1177
// Leaf Node: set & node splitting //////////////////////////////////////////
1155
1178
BNode . prototype . set = function ( key , value , overwrite , tree ) {
1156
1179
var i = this . indexOf ( key , - 1 , tree . _compare ) ;
@@ -1240,6 +1263,7 @@ var BNode = /** @class */ (function () {
1240
1263
var values = this . values === undefVals ? undefVals : this . values . splice ( half ) ;
1241
1264
return new BNode ( keys , values ) ;
1242
1265
} ;
1266
+ /////////////////////////////////////////////////////////////////////////////
1243
1267
// Leaf Node: scanning & deletions //////////////////////////////////////////
1244
1268
BNode . prototype . forRange = function ( low , high , includeHigh , editMode , tree , count , onFound ) {
1245
1269
var cmp = tree . _compare ;
@@ -1336,6 +1360,18 @@ var BNodeInternal = /** @class */ (function (_super) {
1336
1360
var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children ;
1337
1361
return i < children . length ? children [ i ] . get ( key , defaultValue , tree ) : undefined ;
1338
1362
} ;
1363
+ BNodeInternal . prototype . getOrNextLower = function ( key , tree , inclusive ) {
1364
+ var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children ;
1365
+ if ( i > children . length )
1366
+ return undefined ;
1367
+ var result = children [ i ] . getOrNextLower ( key , tree , inclusive ) ;
1368
+ if ( result === undefined && i > 0 ) {
1369
+ var child = children [ i - 1 ] ;
1370
+ var maxKey = child . maxKey ( ) ;
1371
+ return [ maxKey , child . get ( maxKey , undefined , tree ) ] ;
1372
+ }
1373
+ return result ;
1374
+ } ;
1339
1375
BNodeInternal . prototype . checkValid = function ( depth , tree , baseIndex ) {
1340
1376
var kL = this . keys . length , cL = this . children . length ;
1341
1377
check ( kL === cL , "keys/children length mismatch: depth" , depth , "lengths" , kL , cL , "baseIndex" , baseIndex ) ;
@@ -1358,6 +1394,7 @@ var BNodeInternal = /** @class */ (function (_super) {
1358
1394
check ( false , toofew ? "too few" : "too many" , "children (" , childSize , size , ") at depth" , depth , "maxNodeSize:" , tree . maxNodeSize , "children.length:" , cL , "baseIndex:" , baseIndex ) ;
1359
1395
return size ;
1360
1396
} ;
1397
+ /////////////////////////////////////////////////////////////////////////////
1361
1398
// Internal Node: set & node splitting //////////////////////////////////////
1362
1399
BNodeInternal . prototype . set = function ( key , value , overwrite , tree ) {
1363
1400
var c = this . children , max = tree . _maxNodeSize , cmp = tree . _compare ;
@@ -1426,6 +1463,7 @@ var BNodeInternal = /** @class */ (function (_super) {
1426
1463
this . keys . unshift ( lhs . keys . pop ( ) ) ;
1427
1464
this . children . unshift ( lhs . children . pop ( ) ) ;
1428
1465
} ;
1466
+ /////////////////////////////////////////////////////////////////////////////
1429
1467
// Internal Node: scanning & deletions //////////////////////////////////////
1430
1468
// Note: `count` is the next value of the third argument to `onFound`.
1431
1469
// A leaf node's `forRange` function returns a new value for this counter,
0 commit comments