Skip to content

Commit 3fb0df5

Browse files
committed
Tweak the new code
Avoid ?? because the target is ES5. `~i` is shorter than `i ^ -1`. I assume package-lock.json changed a bunch because I upgraded node.js
1 parent d763d2e commit 3fb0df5

File tree

4 files changed

+7987
-28
lines changed

4 files changed

+7987
-28
lines changed

b+tree.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export default class BTree<K = any, V = any> implements ISortedMapF<K, V>, ISort
339339
* If key === undefined, this function returns the highest pair.
340340
* @param key The key to search for.
341341
* @param reusedArray Optional array used repeatedly to store key-value pairs, to
342-
* avoid creating a new array on every iteration.
342+
* avoid creating a new array each time you call this method.
343343
*/
344344
nextLowerPair(key: K | undefined, reusedArray?: [K, V]): [K, V] | undefined;
345345
/** Returns the next key smaller than the specified key (or undefined if there is none) */
@@ -348,7 +348,7 @@ export default class BTree<K = any, V = any> implements ISortedMapF<K, V>, ISort
348348
* and the next lower pair otherwise (or undefined if there is none)
349349
* @param key The key to search for.
350350
* @param reusedArray Optional array used repeatedly to store key-value pairs, to
351-
* avoid creating a new array on every iteration.
351+
* avoid creating a new array each time you call this method.
352352
* */
353353
getPairOrNextLower(key: K, reusedArray?: [K, V]): [K, V] | undefined;
354354
/** Edits the value associated with a key in the tree, if it already exists.

b+tree.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -827,11 +827,11 @@ var BTree = /** @class */ (function () {
827827
* avoid creating a new array on every iteration.
828828
*/
829829
BTree.prototype.nextHigherPair = function (key, reusedArray) {
830-
var pair = reusedArray !== null && reusedArray !== void 0 ? reusedArray : [];
830+
reusedArray = reusedArray || [];
831831
if (key === undefined) {
832-
return this._root.minPair(pair);
832+
return this._root.minPair(reusedArray);
833833
}
834-
return this._root.getPairOrNextHigher(key, this, false, pair);
834+
return this._root.getPairOrNextHigher(key, this, false, reusedArray);
835835
};
836836
/** Returns the next key larger than the specified key (or undefined if there is none) */
837837
BTree.prototype.nextHigherKey = function (key) {
@@ -842,14 +842,14 @@ var BTree = /** @class */ (function () {
842842
* If key === undefined, this function returns the highest pair.
843843
* @param key The key to search for.
844844
* @param reusedArray Optional array used repeatedly to store key-value pairs, to
845-
* avoid creating a new array on every iteration.
845+
* avoid creating a new array each time you call this method.
846846
*/
847847
BTree.prototype.nextLowerPair = function (key, reusedArray) {
848-
var pair = reusedArray !== null && reusedArray !== void 0 ? reusedArray : [];
848+
reusedArray = reusedArray || [];
849849
if (key === undefined) {
850-
return this._root.maxPair(pair);
850+
return this._root.maxPair(reusedArray);
851851
}
852-
return this._root.getPairOrNextLower(key, this, false, pair);
852+
return this._root.getPairOrNextLower(key, this, false, reusedArray);
853853
};
854854
/** Returns the next key smaller than the specified key (or undefined if there is none) */
855855
BTree.prototype.nextLowerKey = function (key) {
@@ -860,10 +860,10 @@ var BTree = /** @class */ (function () {
860860
* and the next lower pair otherwise (or undefined if there is none)
861861
* @param key The key to search for.
862862
* @param reusedArray Optional array used repeatedly to store key-value pairs, to
863-
* avoid creating a new array on every iteration.
863+
* avoid creating a new array each time you call this method.
864864
* */
865865
BTree.prototype.getPairOrNextLower = function (key, reusedArray) {
866-
return this._root.getPairOrNextLower(key, this, true, reusedArray !== null && reusedArray !== void 0 ? reusedArray : []);
866+
return this._root.getPairOrNextLower(key, this, true, reusedArray || []);
867867
};
868868
/** Edits the value associated with a key in the tree, if it already exists.
869869
* @returns true if the key existed, false if not.
@@ -1184,7 +1184,7 @@ var BNode = /** @class */ (function () {
11841184
};
11851185
BNode.prototype.getPairOrNextLower = function (key, tree, inclusive, reusedArray) {
11861186
var i = this.indexOf(key, -1, tree._compare);
1187-
var indexOrLower = i < 0 ? (i ^ -1) - 1 : (inclusive ? i : i - 1);
1187+
var indexOrLower = i < 0 ? ~i - 1 : (inclusive ? i : i - 1);
11881188
if (indexOrLower >= 0) {
11891189
reusedArray[0] = this.keys[indexOrLower];
11901190
reusedArray[1] = this.values[indexOrLower];
@@ -1194,7 +1194,7 @@ var BNode = /** @class */ (function () {
11941194
};
11951195
BNode.prototype.getPairOrNextHigher = function (key, tree, inclusive, reusedArray) {
11961196
var i = this.indexOf(key, -1, tree._compare);
1197-
var indexOrLower = i < 0 ? i ^ -1 : (inclusive ? i : i + 1);
1197+
var indexOrLower = i < 0 ? ~i : (inclusive ? i : i + 1);
11981198
var keys = this.keys;
11991199
if (indexOrLower < keys.length) {
12001200
reusedArray[0] = keys[indexOrLower];

b+tree.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -931,11 +931,11 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
931931
* avoid creating a new array on every iteration.
932932
*/
933933
nextHigherPair(key: K|undefined, reusedArray?: [K,V]): [K,V]|undefined {
934-
const pair = reusedArray ?? ([] as unknown as [K,V]);
934+
reusedArray = reusedArray || ([] as unknown as [K,V]);
935935
if (key === undefined) {
936-
return this._root.minPair(pair);
936+
return this._root.minPair(reusedArray);
937937
}
938-
return this._root.getPairOrNextHigher(key, this, false, pair);
938+
return this._root.getPairOrNextHigher(key, this, false, reusedArray);
939939
}
940940

941941
/** Returns the next key larger than the specified key (or undefined if there is none) */
@@ -948,14 +948,14 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
948948
* If key === undefined, this function returns the highest pair.
949949
* @param key The key to search for.
950950
* @param reusedArray Optional array used repeatedly to store key-value pairs, to
951-
* avoid creating a new array on every iteration.
951+
* avoid creating a new array each time you call this method.
952952
*/
953953
nextLowerPair(key: K|undefined, reusedArray?: [K,V]): [K,V]|undefined {
954-
const pair = reusedArray ?? ([] as unknown as [K,V]);
954+
reusedArray = reusedArray || ([] as unknown as [K,V]);
955955
if (key === undefined) {
956-
return this._root.maxPair(pair);
956+
return this._root.maxPair(reusedArray);
957957
}
958-
return this._root.getPairOrNextLower(key, this, false, pair);
958+
return this._root.getPairOrNextLower(key, this, false, reusedArray);
959959
}
960960

961961
/** Returns the next key smaller than the specified key (or undefined if there is none) */
@@ -968,10 +968,10 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
968968
* and the next lower pair otherwise (or undefined if there is none)
969969
* @param key The key to search for.
970970
* @param reusedArray Optional array used repeatedly to store key-value pairs, to
971-
* avoid creating a new array on every iteration.
971+
* avoid creating a new array each time you call this method.
972972
* */
973973
getPairOrNextLower(key: K, reusedArray?: [K,V]): [K,V]|undefined {
974-
return this._root.getPairOrNextLower(key, this, true, reusedArray ?? ([] as unknown as [K,V]));
974+
return this._root.getPairOrNextLower(key, this, true, reusedArray || ([] as unknown as [K,V]));
975975
}
976976

977977
/** Edits the value associated with a key in the tree, if it already exists.
@@ -1311,7 +1311,7 @@ class BNode<K,V> {
13111311

13121312
getPairOrNextLower(key: K, tree: BTree<K,V>, inclusive: boolean, reusedArray: [K,V]): [K,V]|undefined {
13131313
var i = this.indexOf(key, -1, tree._compare);
1314-
const indexOrLower = i < 0 ? (i ^ -1) - 1 : (inclusive ? i : i - 1);
1314+
const indexOrLower = i < 0 ? ~i - 1 : (inclusive ? i : i - 1);
13151315
if (indexOrLower >= 0) {
13161316
reusedArray[0] = this.keys[indexOrLower];
13171317
reusedArray[1] = this.values[indexOrLower];
@@ -1322,7 +1322,7 @@ class BNode<K,V> {
13221322

13231323
getPairOrNextHigher(key: K, tree: BTree<K,V>, inclusive: boolean, reusedArray: [K,V]): [K,V]|undefined {
13241324
var i = this.indexOf(key, -1, tree._compare);
1325-
const indexOrLower = i < 0 ? i ^ -1 : (inclusive ? i : i + 1);
1325+
const indexOrLower = i < 0 ? ~i : (inclusive ? i : i + 1);
13261326
const keys = this.keys;
13271327
if (indexOrLower < keys.length) {
13281328
reusedArray[0] = keys[indexOrLower];

0 commit comments

Comments
 (0)