@@ -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 ) {
@@ -722,6 +726,8 @@ var BTree = /** @class */ (function () {
722
726
var depthBNormalized = levelIndicesB . length - ( heightB - heightMin ) ;
723
727
return depthANormalized - depthBNormalized ;
724
728
} ;
729
+ // End of helper methods for diffAgainst //////////////////////////////////
730
+ ///////////////////////////////////////////////////////////////////////////
725
731
/** Returns a new iterator for iterating the keys of each pair in ascending order.
726
732
* @param firstKey: Minimum key to include in the output. */
727
733
BTree . prototype . keys = function ( firstKey ) {
@@ -745,7 +751,8 @@ var BTree = /** @class */ (function () {
745
751
} ) ;
746
752
} ;
747
753
Object . defineProperty ( BTree . prototype , "maxNodeSize" , {
748
- // Additional methods /////////////////////////////////////////////////////
754
+ /////////////////////////////////////////////////////////////////////////////
755
+ // Additional methods ///////////////////////////////////////////////////////
749
756
/** Returns the maximum number of children/values before nodes will split. */
750
757
get : function ( ) {
751
758
return this . _maxNodeSize ;
@@ -811,32 +818,50 @@ var BTree = /** @class */ (function () {
811
818
return this . set ( key , value , false ) ;
812
819
} ;
813
820
/** Returns the next pair whose key is larger than the specified key (or undefined if there is none).
814
- * If key === undefined, this function returns the lowest pair.
821
+ * If key === undefined, this function returns the lowest pair.
822
+ * @param key The key to search for.
823
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
824
+ * avoid creating a new array on every iteration.
815
825
*/
816
- BTree . prototype . nextHigherPair = function ( key ) {
817
- var it = this . entries ( key , ReusedArray ) ;
818
- var r = it . next ( ) ;
819
- if ( ! r . done && key !== undefined && this . _compare ( r . value [ 0 ] , key ) <= 0 )
820
- r = it . next ( ) ;
821
- return r . value ;
826
+ BTree . prototype . nextHigherPair = function ( key , reusedArray ) {
827
+ reusedArray = reusedArray || [ ] ;
828
+ if ( key === undefined ) {
829
+ return this . _root . minPair ( reusedArray ) ;
830
+ }
831
+ return this . _root . getPairOrNextHigher ( key , this , false , reusedArray ) ;
822
832
} ;
823
833
/** Returns the next key larger than the specified key (or undefined if there is none) */
824
834
BTree . prototype . nextHigherKey = function ( key ) {
825
- var p = this . nextHigherPair ( key ) ;
835
+ var p = this . nextHigherPair ( key , ReusedArray ) ;
826
836
return p ? p [ 0 ] : p ;
827
837
} ;
828
838
/** Returns the next pair whose key is smaller than the specified key (or undefined if there is none).
829
839
* If key === undefined, this function returns the highest pair.
840
+ * @param key The key to search for.
841
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
842
+ * avoid creating a new array each time you call this method.
830
843
*/
831
- BTree . prototype . nextLowerPair = function ( key ) {
832
- var it = this . entriesReversed ( key , ReusedArray , true ) ;
833
- return it . next ( ) . value ;
844
+ BTree . prototype . nextLowerPair = function ( key , reusedArray ) {
845
+ reusedArray = reusedArray || [ ] ;
846
+ if ( key === undefined ) {
847
+ return this . _root . maxPair ( reusedArray ) ;
848
+ }
849
+ return this . _root . getPairOrNextLower ( key , this , false , reusedArray ) ;
834
850
} ;
835
851
/** Returns the next key smaller than the specified key (or undefined if there is none) */
836
852
BTree . prototype . nextLowerKey = function ( key ) {
837
- var p = this . nextLowerPair ( key ) ;
853
+ var p = this . nextLowerPair ( key , ReusedArray ) ;
838
854
return p ? p [ 0 ] : p ;
839
855
} ;
856
+ /** Returns the key-value pair associated with the supplied key if it exists
857
+ * and the next lower pair otherwise (or undefined if there is none)
858
+ * @param key The key to search for.
859
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
860
+ * avoid creating a new array each time you call this method.
861
+ * */
862
+ BTree . prototype . getPairOrNextLower = function ( key , reusedArray ) {
863
+ return this . _root . getPairOrNextLower ( key , this , true , reusedArray || [ ] ) ;
864
+ } ;
840
865
/** Edits the value associated with a key in the tree, if it already exists.
841
866
* @returns true if the key existed, false if not.
842
867
*/
@@ -1050,6 +1075,7 @@ var BNode = /** @class */ (function () {
1050
1075
enumerable : false ,
1051
1076
configurable : true
1052
1077
} ) ;
1078
+ ///////////////////////////////////////////////////////////////////////////
1053
1079
// Shared methods /////////////////////////////////////////////////////////
1054
1080
BNode . prototype . maxKey = function ( ) {
1055
1081
return this . keys [ this . keys . length - 1 ] ;
@@ -1122,10 +1148,26 @@ var BNode = /** @class */ (function () {
1122
1148
}
1123
1149
return c === 0 ? i : i ^ failXor;*/
1124
1150
} ;
1151
+ /////////////////////////////////////////////////////////////////////////////
1125
1152
// Leaf Node: misc //////////////////////////////////////////////////////////
1126
1153
BNode . prototype . minKey = function ( ) {
1127
1154
return this . keys [ 0 ] ;
1128
1155
} ;
1156
+ BNode . prototype . minPair = function ( reusedArray ) {
1157
+ if ( this . keys . length === 0 )
1158
+ return undefined ;
1159
+ reusedArray [ 0 ] = this . keys [ 0 ] ;
1160
+ reusedArray [ 1 ] = this . values [ 0 ] ;
1161
+ return reusedArray ;
1162
+ } ;
1163
+ BNode . prototype . maxPair = function ( reusedArray ) {
1164
+ if ( this . keys . length === 0 )
1165
+ return undefined ;
1166
+ var lastIndex = this . keys . length - 1 ;
1167
+ reusedArray [ 0 ] = this . keys [ lastIndex ] ;
1168
+ reusedArray [ 1 ] = this . values [ lastIndex ] ;
1169
+ return reusedArray ;
1170
+ } ;
1129
1171
BNode . prototype . clone = function ( ) {
1130
1172
var v = this . values ;
1131
1173
return new BNode ( this . keys . slice ( 0 ) , v === undefVals ? v : v . slice ( 0 ) ) ;
@@ -1137,6 +1179,27 @@ var BNode = /** @class */ (function () {
1137
1179
var i = this . indexOf ( key , - 1 , tree . _compare ) ;
1138
1180
return i < 0 ? defaultValue : this . values [ i ] ;
1139
1181
} ;
1182
+ BNode . prototype . getPairOrNextLower = function ( key , tree , inclusive , reusedArray ) {
1183
+ var i = this . indexOf ( key , - 1 , tree . _compare ) ;
1184
+ var indexOrLower = i < 0 ? ~ i - 1 : ( inclusive ? i : i - 1 ) ;
1185
+ if ( indexOrLower >= 0 ) {
1186
+ reusedArray [ 0 ] = this . keys [ indexOrLower ] ;
1187
+ reusedArray [ 1 ] = this . values [ indexOrLower ] ;
1188
+ return reusedArray ;
1189
+ }
1190
+ return undefined ;
1191
+ } ;
1192
+ BNode . prototype . getPairOrNextHigher = function ( key , tree , inclusive , reusedArray ) {
1193
+ var i = this . indexOf ( key , - 1 , tree . _compare ) ;
1194
+ var indexOrLower = i < 0 ? ~ i : ( inclusive ? i : i + 1 ) ;
1195
+ var keys = this . keys ;
1196
+ if ( indexOrLower < keys . length ) {
1197
+ reusedArray [ 0 ] = keys [ indexOrLower ] ;
1198
+ reusedArray [ 1 ] = this . values [ indexOrLower ] ;
1199
+ return reusedArray ;
1200
+ }
1201
+ return undefined ;
1202
+ } ;
1140
1203
BNode . prototype . checkValid = function ( depth , tree , baseIndex ) {
1141
1204
var kL = this . keys . length , vL = this . values . length ;
1142
1205
check ( this . values === undefVals ? kL <= vL : kL === vL , "keys/values length mismatch: depth" , depth , "with lengths" , kL , vL , "and baseIndex" , baseIndex ) ;
@@ -1148,6 +1211,7 @@ var BNode = /** @class */ (function () {
1148
1211
check ( depth == 0 || kL > 0 , "empty leaf at depth" , depth , "and baseIndex" , baseIndex ) ;
1149
1212
return kL ;
1150
1213
} ;
1214
+ /////////////////////////////////////////////////////////////////////////////
1151
1215
// Leaf Node: set & node splitting //////////////////////////////////////////
1152
1216
BNode . prototype . set = function ( key , value , overwrite , tree ) {
1153
1217
var i = this . indexOf ( key , - 1 , tree . _compare ) ;
@@ -1237,6 +1301,7 @@ var BNode = /** @class */ (function () {
1237
1301
var values = this . values === undefVals ? undefVals : this . values . splice ( half ) ;
1238
1302
return new BNode ( keys , values ) ;
1239
1303
} ;
1304
+ /////////////////////////////////////////////////////////////////////////////
1240
1305
// Leaf Node: scanning & deletions //////////////////////////////////////////
1241
1306
BNode . prototype . forRange = function ( low , high , includeHigh , editMode , tree , count , onFound ) {
1242
1307
var cmp = tree . _compare ;
@@ -1329,10 +1394,36 @@ var BNodeInternal = /** @class */ (function (_super) {
1329
1394
BNodeInternal . prototype . minKey = function ( ) {
1330
1395
return this . children [ 0 ] . minKey ( ) ;
1331
1396
} ;
1397
+ BNodeInternal . prototype . minPair = function ( reusedArray ) {
1398
+ return this . children [ 0 ] . minPair ( reusedArray ) ;
1399
+ } ;
1400
+ BNodeInternal . prototype . maxPair = function ( reusedArray ) {
1401
+ return this . children [ this . children . length - 1 ] . maxPair ( reusedArray ) ;
1402
+ } ;
1332
1403
BNodeInternal . prototype . get = function ( key , defaultValue , tree ) {
1333
1404
var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children ;
1334
1405
return i < children . length ? children [ i ] . get ( key , defaultValue , tree ) : undefined ;
1335
1406
} ;
1407
+ BNodeInternal . prototype . getPairOrNextLower = function ( key , tree , inclusive , reusedArray ) {
1408
+ var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children ;
1409
+ if ( i >= children . length )
1410
+ return undefined ;
1411
+ var result = children [ i ] . getPairOrNextLower ( key , tree , inclusive , reusedArray ) ;
1412
+ if ( result === undefined && i > 0 ) {
1413
+ return children [ i - 1 ] . maxPair ( reusedArray ) ;
1414
+ }
1415
+ return result ;
1416
+ } ;
1417
+ BNodeInternal . prototype . getPairOrNextHigher = function ( key , tree , inclusive , reusedArray ) {
1418
+ var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children , length = children . length ;
1419
+ if ( i >= length )
1420
+ return undefined ;
1421
+ var result = children [ i ] . getPairOrNextHigher ( key , tree , inclusive , reusedArray ) ;
1422
+ if ( result === undefined && i < length - 1 ) {
1423
+ return children [ i + 1 ] . minPair ( reusedArray ) ;
1424
+ }
1425
+ return result ;
1426
+ } ;
1336
1427
BNodeInternal . prototype . checkValid = function ( depth , tree , baseIndex ) {
1337
1428
var kL = this . keys . length , cL = this . children . length ;
1338
1429
check ( kL === cL , "keys/children length mismatch: depth" , depth , "lengths" , kL , cL , "baseIndex" , baseIndex ) ;
@@ -1355,6 +1446,7 @@ var BNodeInternal = /** @class */ (function (_super) {
1355
1446
check ( false , toofew ? "too few" : "too many" , "children (" , childSize , size , ") at depth" , depth , "maxNodeSize:" , tree . maxNodeSize , "children.length:" , cL , "baseIndex:" , baseIndex ) ;
1356
1447
return size ;
1357
1448
} ;
1449
+ /////////////////////////////////////////////////////////////////////////////
1358
1450
// Internal Node: set & node splitting //////////////////////////////////////
1359
1451
BNodeInternal . prototype . set = function ( key , value , overwrite , tree ) {
1360
1452
var c = this . children , max = tree . _maxNodeSize , cmp = tree . _compare ;
@@ -1423,6 +1515,7 @@ var BNodeInternal = /** @class */ (function (_super) {
1423
1515
this . keys . unshift ( lhs . keys . pop ( ) ) ;
1424
1516
this . children . unshift ( lhs . children . pop ( ) ) ;
1425
1517
} ;
1518
+ /////////////////////////////////////////////////////////////////////////////
1426
1519
// Internal Node: scanning & deletions //////////////////////////////////////
1427
1520
// Note: `count` is the next value of the third argument to `onFound`.
1428
1521
// A leaf node's `forRange` function returns a new value for this counter,
0 commit comments