@@ -925,44 +925,53 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
925
925
}
926
926
927
927
/** Returns the next pair whose key is larger than the specified key (or undefined if there is none).
928
- * If key === undefined, this function returns the lowest pair.
928
+ * If key === undefined, this function returns the lowest pair.
929
+ * @param key The key to search for.
930
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
931
+ * avoid creating a new array on every iteration.
929
932
*/
930
- nextHigherPair ( key : K | undefined ) : [ K , V ] | undefined {
931
- var it = this . entries ( key , ReusedArray ) ;
932
- var r = it . next ( ) ;
933
- if ( ! r . done && key !== undefined && this . _compare ( r . value [ 0 ] , key ) <= 0 )
934
- r = it . next ( ) ;
935
- return r . value ;
933
+ nextHigherPair ( key : K | undefined , reusedArray ?: [ K , V ] ) : [ K , V ] | undefined {
934
+ const pair = reusedArray ?? ( [ ] as unknown as [ K , V ] ) ;
935
+ if ( key === undefined ) {
936
+ return this . _root . minPair ( pair ) ;
937
+ }
938
+ return this . _root . getPairOrNextHigher ( key , this , false , pair ) ;
936
939
}
937
940
938
941
/** Returns the next key larger than the specified key (or undefined if there is none) */
939
942
nextHigherKey ( key : K | undefined ) : K | undefined {
940
- var p = this . nextHigherPair ( key ) ;
943
+ var p = this . nextHigherPair ( key , ReusedArray as [ K , V ] ) ;
941
944
return p ? p [ 0 ] : p ;
942
945
}
943
946
944
947
/** Returns the next pair whose key is smaller than the specified key (or undefined if there is none).
945
948
* If key === undefined, this function returns the highest pair.
949
+ * @param key The key to search for.
950
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
951
+ * avoid creating a new array on every iteration.
946
952
*/
947
- nextLowerPair ( key : K | undefined ) : [ K , V ] | undefined {
953
+ nextLowerPair ( key : K | undefined , reusedArray ?: [ K , V ] ) : [ K , V ] | undefined {
954
+ const pair = reusedArray ?? ( [ ] as unknown as [ K , V ] ) ;
948
955
if ( key === undefined ) {
949
- const maxKey = this . maxKey ( ) ;
950
- if ( maxKey === undefined )
951
- return undefined ;
952
- return [ maxKey , this . get ( maxKey ) as V ] ;
956
+ return this . _root . maxPair ( pair ) ;
953
957
}
954
- return this . _root . getOrNextLower ( key , this , false ) ;
958
+ return this . _root . getPairOrNextLower ( key , this , false , pair ) ;
955
959
}
956
960
957
961
/** Returns the next key smaller than the specified key (or undefined if there is none) */
958
962
nextLowerKey ( key : K | undefined ) : K | undefined {
959
- var p = this . nextLowerPair ( key ) ;
963
+ var p = this . nextLowerPair ( key , ReusedArray as [ K , V ] ) ;
960
964
return p ? p [ 0 ] : p ;
961
965
}
962
966
963
- /** Returns the key-value pair associated with the supplied key if it exists and the next lower pair otherwise (or undefined if there is none) */
964
- getOrNextLower ( key : K ) : [ K , V ] | undefined {
965
- return this . _root . getOrNextLower ( key , this , true ) ;
967
+ /** Returns the key-value pair associated with the supplied key if it exists
968
+ * and the next lower pair otherwise (or undefined if there is none)
969
+ * @param key The key to search for.
970
+ * @param reusedArray Optional array used repeatedly to store key-value pairs, to
971
+ * avoid creating a new array on every iteration.
972
+ * */
973
+ getPairOrNextLower ( key : K , reusedArray ?: [ K , V ] ) : [ K , V ] | undefined {
974
+ return this . _root . getPairOrNextLower ( key , this , true , reusedArray ?? ( [ ] as unknown as [ K , V ] ) ) ;
966
975
}
967
976
968
977
/** Edits the value associated with a key in the tree, if it already exists.
@@ -1265,10 +1274,27 @@ class BNode<K,V> {
1265
1274
/////////////////////////////////////////////////////////////////////////////
1266
1275
// Leaf Node: misc //////////////////////////////////////////////////////////
1267
1276
1268
- minKey ( ) {
1277
+ minKey ( ) : K | undefined {
1269
1278
return this . keys [ 0 ] ;
1270
1279
}
1271
1280
1281
+ minPair ( reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1282
+ if ( this . keys . length === 0 )
1283
+ return undefined ;
1284
+ reusedArray [ 0 ] = this . keys [ 0 ] ;
1285
+ reusedArray [ 1 ] = this . values [ 0 ] ;
1286
+ return reusedArray ;
1287
+ }
1288
+
1289
+ maxPair ( reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1290
+ if ( this . keys . length === 0 )
1291
+ return undefined ;
1292
+ const lastIndex = this . keys . length - 1 ;
1293
+ reusedArray [ 0 ] = this . keys [ lastIndex ] ;
1294
+ reusedArray [ 1 ] = this . values [ lastIndex ] ;
1295
+ return reusedArray ;
1296
+ }
1297
+
1272
1298
clone ( ) : BNode < K , V > {
1273
1299
var v = this . values ;
1274
1300
return new BNode < K , V > ( this . keys . slice ( 0 ) , v === undefVals ? v : v . slice ( 0 ) ) ;
@@ -1283,10 +1309,27 @@ class BNode<K,V> {
1283
1309
return i < 0 ? defaultValue : this . values [ i ] ;
1284
1310
}
1285
1311
1286
- getOrNextLower ( key : K , tree : BTree < K , V > , inclusive : boolean ) : [ K , V ] | undefined {
1312
+ getPairOrNextLower ( key : K , tree : BTree < K , V > , inclusive : boolean , reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1287
1313
var i = this . indexOf ( key , - 1 , tree . _compare ) ;
1288
1314
const indexOrLower = i < 0 ? ( i ^ - 1 ) - 1 : ( inclusive ? i : i - 1 ) ;
1289
- return indexOrLower >= 0 ? [ this . keys [ indexOrLower ] , this . values [ indexOrLower ] ] : undefined ;
1315
+ if ( indexOrLower >= 0 ) {
1316
+ reusedArray [ 0 ] = this . keys [ indexOrLower ] ;
1317
+ reusedArray [ 1 ] = this . values [ indexOrLower ] ;
1318
+ return reusedArray ;
1319
+ }
1320
+ return undefined ;
1321
+ }
1322
+
1323
+ getPairOrNextHigher ( key : K , tree : BTree < K , V > , inclusive : boolean , reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1324
+ var i = this . indexOf ( key , - 1 , tree . _compare ) ;
1325
+ const indexOrLower = i < 0 ? i ^ - 1 : ( inclusive ? i : i + 1 ) ;
1326
+ const keys = this . keys ;
1327
+ if ( indexOrLower < keys . length ) {
1328
+ reusedArray [ 0 ] = keys [ indexOrLower ] ;
1329
+ reusedArray [ 1 ] = this . values [ indexOrLower ] ;
1330
+ return reusedArray ;
1331
+ }
1332
+ return undefined ;
1290
1333
}
1291
1334
1292
1335
checkValid ( depth : number , tree : BTree < K , V > , baseIndex : number ) : number {
@@ -1494,20 +1537,37 @@ class BNodeInternal<K,V> extends BNode<K,V> {
1494
1537
return this . children [ 0 ] . minKey ( ) ;
1495
1538
}
1496
1539
1540
+ minPair ( reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1541
+ return this . children [ 0 ] . minPair ( reusedArray ) ;
1542
+ }
1543
+
1544
+ maxPair ( reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1545
+ return this . children [ this . children . length - 1 ] . maxPair ( reusedArray ) ;
1546
+ }
1547
+
1497
1548
get ( key : K , defaultValue : V | undefined , tree : BTree < K , V > ) : V | undefined {
1498
1549
var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children ;
1499
1550
return i < children . length ? children [ i ] . get ( key , defaultValue , tree ) : undefined ;
1500
1551
}
1501
1552
1502
- getOrNextLower ( key : K , tree : BTree < K , V > , inclusive : boolean ) : [ K , V ] | undefined {
1553
+ getPairOrNextLower ( key : K , tree : BTree < K , V > , inclusive : boolean , reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1503
1554
var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children ;
1504
- if ( i > children . length )
1555
+ if ( i >= children . length )
1505
1556
return undefined ;
1506
- const result = children [ i ] . getOrNextLower ( key , tree , inclusive ) ;
1557
+ const result = children [ i ] . getPairOrNextLower ( key , tree , inclusive , reusedArray ) ;
1507
1558
if ( result === undefined && i > 0 ) {
1508
- const child = children [ i - 1 ] ;
1509
- const maxKey = child . maxKey ( ) ;
1510
- return [ maxKey , child . get ( maxKey , undefined , tree ) as V ] ;
1559
+ return children [ i - 1 ] . maxPair ( reusedArray ) ;
1560
+ }
1561
+ return result ;
1562
+ }
1563
+
1564
+ getPairOrNextHigher ( key : K , tree : BTree < K , V > , inclusive : boolean , reusedArray : [ K , V ] ) : [ K , V ] | undefined {
1565
+ var i = this . indexOf ( key , 0 , tree . _compare ) , children = this . children , length = children . length ;
1566
+ if ( i >= length )
1567
+ return undefined ;
1568
+ const result = children [ i ] . getPairOrNextHigher ( key , tree , inclusive , reusedArray ) ;
1569
+ if ( result === undefined && i < length - 1 ) {
1570
+ return children [ i + 1 ] . minPair ( reusedArray ) ;
1511
1571
}
1512
1572
return result ;
1513
1573
}
0 commit comments