data-structure-typed 2.2.7 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +9 -0
- package/CHANGELOG.md +1 -1
- package/README.md +14 -3
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +689 -182
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -185
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +689 -182
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -185
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +25 -27
- package/dist/types/data-structures/binary-tree/bst.d.ts +13 -12
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +151 -21
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/interfaces/binary-tree.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +689 -181
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +50 -172
- package/src/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +57 -60
- package/src/data-structures/binary-tree/bst.ts +100 -26
- package/src/data-structures/binary-tree/red-black-tree.ts +586 -76
- package/src/data-structures/binary-tree/tree-counter.ts +25 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +1 -1
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/base/iterable-element-base.coverage.test.ts +106 -0
- package/test/unit/data-structures/base/iterable-element-base.more-branches.coverage.test.ts +61 -0
- package/test/unit/data-structures/base/linear-base.array.coverage.test.ts +168 -0
- package/test/unit/data-structures/base/linear-base.concat-else.coverage.test.ts +82 -0
- package/test/unit/data-structures/base/linear-base.coverage.test.ts +72 -0
- package/test/unit/data-structures/base/linear-base.more-branches.coverage.test.ts +417 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches-3.coverage.test.ts +146 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches.coverage.test.ts +93 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.coverage.test.ts +108 -0
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.more-branches-2.coverage.test.ts +85 -0
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree-node.familyPosition-root-left.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/avl-tree.more-branches-2.coverage.test.ts +99 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-indexed-tree.more-branches.coverage.test.ts +18 -0
- package/test/unit/data-structures/binary-tree/binary-tree.more-branches.coverage.test.ts +56 -0
- package/test/unit/data-structures/binary-tree/binary-tree.remaining-branches.coverage.test.ts +229 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.bound-by-predicate.coverage.test.ts +33 -0
- package/test/unit/data-structures/binary-tree/bst.coverage.test.ts +94 -0
- package/test/unit/data-structures/binary-tree/bst.deletebykey.coverage.test.ts +70 -0
- package/test/unit/data-structures/binary-tree/bst.deletewhere.coverage.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/bst.floor-lower-predicate.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/bst.floor-setmany.coverage.test.ts +72 -0
- package/test/unit/data-structures/binary-tree/bst.getnode.range-ensure.coverage.test.ts +22 -0
- package/test/unit/data-structures/binary-tree/bst.misc-branches.coverage.test.ts +100 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-2.coverage.test.ts +133 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-3.coverage.test.ts +45 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-4.coverage.test.ts +36 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-5.coverage.test.ts +40 -0
- package/test/unit/data-structures/binary-tree/bst.more.coverage.test.ts +39 -0
- package/test/unit/data-structures/binary-tree/bst.node-family.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/bst.range-pruning.coverage.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/bst.search-fastpath.coverage.test.ts +30 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +124 -154
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-corruption-repair.coverage.test.ts +66 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-max-update.coverage.test.ts +18 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-null.coverage.test.ts +53 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-stale-cache.coverage.test.ts +25 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-update.coverage.test.ts +23 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-delete.coverage.test.ts +49 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-edge.coverage.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-stale-insert.coverage.test.ts +39 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.coverage.test.ts +334 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-fixup.coverage.test.ts +68 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-successor.coverage.test.ts +75 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.factories.coverage.test.ts +26 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-compare-update.coverage.test.ts +74 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-no-update.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-nullish.coverage.test.ts +61 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-defined.coverage.test.ts +35 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-undefined.coverage.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-more.coverage.test.ts +99 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint.coverage.test.ts +60 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-cache-nullish.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-header-parent-nullish.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.internal-walk.coverage.test.ts +57 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.minmax-cache.test.ts +65 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.misc-inputs.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-2.coverage.test.ts +121 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-3.coverage.test.ts +55 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-4.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.predsucc.coverage.test.ts +40 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.remaining-branches.coverage.test.ts +123 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.set-inputs.coverage.test.ts +64 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-parent-cache.coverage.test.ts +79 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-remaining.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-uncovered.coverage.test.ts +74 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/red-black-tree.update-branches.coverage.test.ts +30 -0
- package/test/unit/data-structures/binary-tree/segment-tree.more-branches.coverage.test.ts +31 -0
- package/test/unit/data-structures/binary-tree/tree-counter.coverage.test.ts +115 -0
- package/test/unit/data-structures/binary-tree/tree-counter.more-branches.coverage.test.ts +244 -0
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +41 -39
- package/test/unit/data-structures/binary-tree/tree-multi-map.coverage.test.ts +104 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.more-branches-2.coverage.test.ts +59 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/test/unit/data-structures/graph/abstract-graph.more-branches-2.coverage.test.ts +40 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-3.coverage.test.ts +65 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-4.coverage.test.ts +98 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-5.coverage.test.ts +51 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches.coverage.test.ts +62 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches-2.coverage.test.ts +38 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches-3.coverage.test.ts +25 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches.coverage.test.ts +82 -0
- package/test/unit/data-structures/graph/map-graph.more-branches.coverage.test.ts +22 -0
- package/test/unit/data-structures/graph/undirected-graph.more-branches-2.coverage.test.ts +35 -0
- package/test/unit/data-structures/graph/undirected-graph.more-branches.coverage.test.ts +87 -0
- package/test/unit/data-structures/hash/hash-map.more-branches.coverage.test.ts +64 -0
- package/test/unit/data-structures/hash/hash-map.toEntryFn-branch.coverage.test.ts +9 -0
- package/test/unit/data-structures/heap/heap.misc-branches.coverage.test.ts +110 -0
- package/test/unit/data-structures/heap/heap.remaining-branches.coverage.test.ts +22 -0
- package/test/unit/data-structures/heap/max-heap.coverage.test.ts +29 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.more-branches.coverage.test.ts +72 -0
- package/test/unit/data-structures/linked-list/linked-list.unshiftMany-else.coverage.test.ts +15 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.coverage.test.ts +221 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.more-branches.coverage.test.ts +86 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.more-branches.coverage.test.ts +31 -0
- package/test/unit/data-structures/matrix/matrix.more-branches.coverage.test.ts +81 -0
- package/test/unit/data-structures/matrix/matrix.pivotElement-nullish.coverage.test.ts +28 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.more-branches.coverage.test.ts +10 -0
- package/test/unit/data-structures/priority-queue/priority-queue.coverage.test.ts +21 -0
- package/test/unit/data-structures/queue/deque.coverage.test.ts +173 -0
- package/test/unit/data-structures/queue/deque.more-branches-2.coverage.test.ts +39 -0
- package/test/unit/data-structures/queue/deque.more-branches-3.coverage.test.ts +9 -0
- package/test/unit/data-structures/queue/deque.more-branches.coverage.test.ts +95 -0
- package/test/unit/data-structures/queue/queue.coverage.test.ts +138 -0
- package/test/unit/data-structures/queue/queue.more-branches-2.coverage.test.ts +27 -0
- package/test/unit/data-structures/stack/stack.coverage.test.ts +112 -0
- package/test/unit/data-structures/tree/tree.more-branches.coverage.test.ts +9 -0
- package/test/unit/data-structures/trie/trie.more-branches-2.coverage.test.ts +51 -0
- package/test/utils/patch.ts +33 -0
- package/tsup.config.js +50 -21
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
|
@@ -1396,6 +1396,12 @@ var _LinearLinkedBase = class _LinearLinkedBase extends LinearBase {
|
|
|
1396
1396
|
}
|
|
1397
1397
|
return -1;
|
|
1398
1398
|
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Concatenate lists/elements preserving order.
|
|
1401
|
+
* @param items - Elements or `LinearBase` instances.
|
|
1402
|
+
* @returns New list with combined elements (`this` type).
|
|
1403
|
+
* @remarks Time O(sum(length)), Space O(sum(length))
|
|
1404
|
+
*/
|
|
1399
1405
|
concat(...items) {
|
|
1400
1406
|
const newList = this.clone();
|
|
1401
1407
|
for (const item of items) {
|
|
@@ -4218,6 +4224,12 @@ var _Deque = class _Deque extends LinearBase {
|
|
|
4218
4224
|
*/
|
|
4219
4225
|
_setBucketSize(size) {
|
|
4220
4226
|
this._bucketSize = size;
|
|
4227
|
+
if (this._length === 0) {
|
|
4228
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
4229
|
+
this._bucketCount = 1;
|
|
4230
|
+
this._bucketFirst = this._bucketLast = 0;
|
|
4231
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
4232
|
+
}
|
|
4221
4233
|
}
|
|
4222
4234
|
/**
|
|
4223
4235
|
* (Protected) Iterate elements from front to back.
|
|
@@ -6971,9 +6983,9 @@ var BinaryTreeNode = _BinaryTreeNode;
|
|
|
6971
6983
|
var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
6972
6984
|
/**
|
|
6973
6985
|
* Creates an instance of BinaryTree.
|
|
6974
|
-
* @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `
|
|
6986
|
+
* @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
|
|
6975
6987
|
*
|
|
6976
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
6988
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
6977
6989
|
* @param [options] - Configuration options for the tree.
|
|
6978
6990
|
*/
|
|
6979
6991
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -7002,7 +7014,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7002
7014
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
7003
7015
|
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
7004
7016
|
}
|
|
7005
|
-
if (keysNodesEntriesOrRaws) this.
|
|
7017
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
7006
7018
|
}
|
|
7007
7019
|
/**
|
|
7008
7020
|
* Gets whether the tree is in Map mode.
|
|
@@ -7209,10 +7221,20 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7209
7221
|
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
|
|
7210
7222
|
*
|
|
7211
7223
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
7224
|
+
* @returns True if the addition was successful, false otherwise.
|
|
7225
|
+
*/
|
|
7226
|
+
add(keyNodeOrEntry) {
|
|
7227
|
+
return this.set(keyNodeOrEntry);
|
|
7228
|
+
}
|
|
7229
|
+
/**
|
|
7230
|
+
* Adds or updates a new node to the tree.
|
|
7231
|
+
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
|
|
7232
|
+
*
|
|
7233
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
7212
7234
|
* @param [value] - The value, if providing just a key.
|
|
7213
7235
|
* @returns True if the addition was successful, false otherwise.
|
|
7214
7236
|
*/
|
|
7215
|
-
|
|
7237
|
+
set(keyNodeOrEntry, value) {
|
|
7216
7238
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
7217
7239
|
if (newNode === void 0) return false;
|
|
7218
7240
|
if (!this._root) {
|
|
@@ -7256,25 +7278,24 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7256
7278
|
return false;
|
|
7257
7279
|
}
|
|
7258
7280
|
/**
|
|
7259
|
-
* Adds
|
|
7260
|
-
* @remarks Time O(
|
|
7281
|
+
* Adds multiple items to the tree.
|
|
7282
|
+
* @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
|
|
7261
7283
|
*
|
|
7262
|
-
* @param
|
|
7263
|
-
* @
|
|
7264
|
-
* @returns True if the addition was successful, false otherwise.
|
|
7284
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
7285
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
7265
7286
|
*/
|
|
7266
|
-
|
|
7267
|
-
return this.
|
|
7287
|
+
addMany(keysNodesEntriesOrRaws) {
|
|
7288
|
+
return this.setMany(keysNodesEntriesOrRaws);
|
|
7268
7289
|
}
|
|
7269
7290
|
/**
|
|
7270
|
-
* Adds multiple items to the tree.
|
|
7271
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
7291
|
+
* Adds or updates multiple items to the tree.
|
|
7292
|
+
* @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
|
|
7272
7293
|
*
|
|
7273
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
7294
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
7274
7295
|
* @param [values] - An optional parallel iterable of values.
|
|
7275
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
7296
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
7276
7297
|
*/
|
|
7277
|
-
|
|
7298
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
7278
7299
|
const inserted = [];
|
|
7279
7300
|
let valuesIterator;
|
|
7280
7301
|
if (values) {
|
|
@@ -7289,52 +7310,41 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7289
7310
|
}
|
|
7290
7311
|
}
|
|
7291
7312
|
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7292
|
-
inserted.push(this.
|
|
7313
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
7293
7314
|
}
|
|
7294
7315
|
return inserted;
|
|
7295
7316
|
}
|
|
7296
7317
|
/**
|
|
7297
|
-
*
|
|
7298
|
-
* @remarks Time O(N * M), where N is the
|
|
7299
|
-
*
|
|
7300
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
7301
|
-
* @param [values] - An optional parallel iterable of values.
|
|
7302
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
7303
|
-
*/
|
|
7304
|
-
setMany(keysNodesEntriesOrRaws, values) {
|
|
7305
|
-
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
7306
|
-
}
|
|
7307
|
-
/**
|
|
7308
|
-
* Merges another tree into this one by adding all its nodes.
|
|
7309
|
-
* @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
|
|
7318
|
+
* Merges another tree into this one by seting all its nodes.
|
|
7319
|
+
* @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
|
|
7310
7320
|
*
|
|
7311
7321
|
* @param anotherTree - The tree to merge.
|
|
7312
7322
|
*/
|
|
7313
7323
|
merge(anotherTree) {
|
|
7314
|
-
this.
|
|
7324
|
+
this.setMany(anotherTree, []);
|
|
7315
7325
|
}
|
|
7316
7326
|
/**
|
|
7317
7327
|
* Clears the tree and refills it with new items.
|
|
7318
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
7328
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
7319
7329
|
*
|
|
7320
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
7330
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
7321
7331
|
* @param [values] - An optional parallel iterable of values.
|
|
7322
7332
|
*/
|
|
7323
7333
|
refill(keysNodesEntriesOrRaws, values) {
|
|
7324
7334
|
this.clear();
|
|
7325
|
-
this.
|
|
7335
|
+
this.setMany(keysNodesEntriesOrRaws, values);
|
|
7326
7336
|
}
|
|
7327
7337
|
/**
|
|
7328
7338
|
* Deletes a node from the tree.
|
|
7329
7339
|
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
|
|
7330
7340
|
*
|
|
7331
|
-
* @param
|
|
7341
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
7332
7342
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
7333
7343
|
*/
|
|
7334
|
-
delete(
|
|
7344
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
7335
7345
|
const deletedResult = [];
|
|
7336
7346
|
if (!this._root) return deletedResult;
|
|
7337
|
-
const curr = this.getNode(
|
|
7347
|
+
const curr = this.getNode(keyNodeEntryRawOrPredicate);
|
|
7338
7348
|
if (!curr) return deletedResult;
|
|
7339
7349
|
const parent = curr == null ? void 0 : curr.parent;
|
|
7340
7350
|
let needBalanced;
|
|
@@ -7518,7 +7528,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7518
7528
|
}
|
|
7519
7529
|
return true;
|
|
7520
7530
|
}, "checkBST");
|
|
7521
|
-
const isStandardBST = checkBST(
|
|
7531
|
+
const isStandardBST = checkBST();
|
|
7522
7532
|
const isInverseBST = checkBST(true);
|
|
7523
7533
|
return isStandardBST || isInverseBST;
|
|
7524
7534
|
}
|
|
@@ -7988,7 +7998,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7988
7998
|
}
|
|
7989
7999
|
/**
|
|
7990
8000
|
* Clones the tree.
|
|
7991
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
8001
|
+
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
|
|
7992
8002
|
*
|
|
7993
8003
|
* @returns A new, cloned instance of the tree.
|
|
7994
8004
|
*/
|
|
@@ -7999,7 +8009,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7999
8009
|
}
|
|
8000
8010
|
/**
|
|
8001
8011
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
8002
|
-
* @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `
|
|
8012
|
+
* @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
|
|
8003
8013
|
*
|
|
8004
8014
|
* @param predicate - A function to test each [key, value] pair.
|
|
8005
8015
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -8008,7 +8018,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8008
8018
|
filter(predicate, thisArg) {
|
|
8009
8019
|
const out = this._createInstance();
|
|
8010
8020
|
let i = 0;
|
|
8011
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.
|
|
8021
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
8012
8022
|
return out;
|
|
8013
8023
|
}
|
|
8014
8024
|
/**
|
|
@@ -8026,7 +8036,7 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8026
8036
|
map(cb, options, thisArg) {
|
|
8027
8037
|
const out = this._createLike([], options);
|
|
8028
8038
|
let i = 0;
|
|
8029
|
-
for (const [k, v] of this) out.
|
|
8039
|
+
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
8030
8040
|
return out;
|
|
8031
8041
|
}
|
|
8032
8042
|
/**
|
|
@@ -8272,18 +8282,18 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
8272
8282
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
8273
8283
|
}
|
|
8274
8284
|
/**
|
|
8275
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
8276
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
8285
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
8286
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
8277
8287
|
*
|
|
8278
8288
|
* @param cloned - The new, empty tree instance to populate.
|
|
8279
8289
|
*/
|
|
8280
8290
|
_clone(cloned) {
|
|
8281
8291
|
this.bfs(
|
|
8282
8292
|
(node) => {
|
|
8283
|
-
if (node === null) cloned.
|
|
8293
|
+
if (node === null) cloned.set(null);
|
|
8284
8294
|
else {
|
|
8285
|
-
if (this._isMapMode) cloned.
|
|
8286
|
-
else cloned.
|
|
8295
|
+
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
8296
|
+
else cloned.set([node.key, node.value]);
|
|
8287
8297
|
}
|
|
8288
8298
|
},
|
|
8289
8299
|
this._root,
|
|
@@ -8614,7 +8624,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8614
8624
|
* Creates an instance of BST.
|
|
8615
8625
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
8616
8626
|
*
|
|
8617
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
8627
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
8618
8628
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
8619
8629
|
*/
|
|
8620
8630
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -8635,7 +8645,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8635
8645
|
} else {
|
|
8636
8646
|
this._comparator = this._createDefaultComparator();
|
|
8637
8647
|
}
|
|
8638
|
-
if (keysNodesEntriesOrRaws) this.
|
|
8648
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
8639
8649
|
}
|
|
8640
8650
|
/**
|
|
8641
8651
|
* Gets the root node of the tree.
|
|
@@ -8749,8 +8759,40 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8749
8759
|
* @returns The first matching node, or undefined if not found.
|
|
8750
8760
|
*/
|
|
8751
8761
|
getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
8752
|
-
var _a;
|
|
8753
|
-
|
|
8762
|
+
var _a, _b;
|
|
8763
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
|
|
8764
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
8765
|
+
return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
|
|
8766
|
+
}
|
|
8767
|
+
if (keyNodeEntryOrPredicate instanceof Range) {
|
|
8768
|
+
return (_b = this.getNodes(
|
|
8769
|
+
keyNodeEntryOrPredicate,
|
|
8770
|
+
true,
|
|
8771
|
+
startNode,
|
|
8772
|
+
iterationType
|
|
8773
|
+
)[0]) != null ? _b : void 0;
|
|
8774
|
+
}
|
|
8775
|
+
let targetKey;
|
|
8776
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
8777
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
8778
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
8779
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
8780
|
+
if (k === null || k === void 0) return void 0;
|
|
8781
|
+
targetKey = k;
|
|
8782
|
+
} else {
|
|
8783
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
8784
|
+
}
|
|
8785
|
+
const start = this.ensureNode(startNode);
|
|
8786
|
+
if (!start) return void 0;
|
|
8787
|
+
const NIL = this._NIL;
|
|
8788
|
+
let cur = start;
|
|
8789
|
+
const cmpFn = this._comparator;
|
|
8790
|
+
while (cur && cur !== NIL) {
|
|
8791
|
+
const c = cmpFn(targetKey, cur.key);
|
|
8792
|
+
if (c === 0) return cur;
|
|
8793
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
8794
|
+
}
|
|
8795
|
+
return void 0;
|
|
8754
8796
|
}
|
|
8755
8797
|
/**
|
|
8756
8798
|
* Searches the tree for nodes matching a predicate, key, or range.
|
|
@@ -8771,8 +8813,30 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8771
8813
|
if (keyNodeEntryOrPredicate === null) return [];
|
|
8772
8814
|
startNode = this.ensureNode(startNode);
|
|
8773
8815
|
if (!startNode) return [];
|
|
8774
|
-
let predicate;
|
|
8775
8816
|
const isRange = this.isRange(keyNodeEntryOrPredicate);
|
|
8817
|
+
const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
|
|
8818
|
+
if (!isRange && !isPred) {
|
|
8819
|
+
let targetKey;
|
|
8820
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
8821
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
8822
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
8823
|
+
const k = keyNodeEntryOrPredicate[0];
|
|
8824
|
+
if (k !== null && k !== void 0) targetKey = k;
|
|
8825
|
+
} else {
|
|
8826
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
8827
|
+
}
|
|
8828
|
+
if (targetKey === void 0) return [];
|
|
8829
|
+
const NIL = this._NIL;
|
|
8830
|
+
const cmpFn = this._comparator;
|
|
8831
|
+
let cur = startNode;
|
|
8832
|
+
while (cur && cur !== NIL) {
|
|
8833
|
+
const c = cmpFn(targetKey, cur.key);
|
|
8834
|
+
if (c === 0) return [callback(cur)];
|
|
8835
|
+
cur = c < 0 ? cur._left : cur._right;
|
|
8836
|
+
}
|
|
8837
|
+
return [];
|
|
8838
|
+
}
|
|
8839
|
+
let predicate;
|
|
8776
8840
|
if (isRange) {
|
|
8777
8841
|
predicate = /* @__PURE__ */ __name((node) => {
|
|
8778
8842
|
if (!node) return false;
|
|
@@ -8846,11 +8910,11 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8846
8910
|
* Adds a new node to the BST based on key comparison.
|
|
8847
8911
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
8848
8912
|
*
|
|
8849
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
8913
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
8850
8914
|
* @param [value] - The value, if providing just a key.
|
|
8851
8915
|
* @returns True if the addition was successful, false otherwise.
|
|
8852
8916
|
*/
|
|
8853
|
-
|
|
8917
|
+
set(keyNodeOrEntry, value) {
|
|
8854
8918
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
8855
8919
|
if (newNode === void 0) return false;
|
|
8856
8920
|
if (this._root === void 0) {
|
|
@@ -8887,24 +8951,24 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8887
8951
|
}
|
|
8888
8952
|
/**
|
|
8889
8953
|
* Adds multiple items to the tree.
|
|
8890
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced
|
|
8954
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
8891
8955
|
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
8892
8956
|
* Space O(N) for sorting and recursion/iteration stack.
|
|
8893
8957
|
*
|
|
8894
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
8958
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
8895
8959
|
* @param [values] - An optional parallel iterable of values.
|
|
8896
8960
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
8897
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced
|
|
8898
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
8961
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
8962
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
8899
8963
|
*/
|
|
8900
|
-
|
|
8964
|
+
setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8901
8965
|
const inserted = [];
|
|
8902
8966
|
const valuesIterator = values == null ? void 0 : values[Symbol.iterator]();
|
|
8903
8967
|
if (!isBalanceAdd) {
|
|
8904
8968
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
8905
8969
|
const val = valuesIterator == null ? void 0 : valuesIterator.next().value;
|
|
8906
8970
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
8907
|
-
inserted.push(this.
|
|
8971
|
+
inserted.push(this.set(kve, val));
|
|
8908
8972
|
}
|
|
8909
8973
|
return inserted;
|
|
8910
8974
|
}
|
|
@@ -8932,9 +8996,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8932
8996
|
const { key, value, orgIndex } = arr[mid];
|
|
8933
8997
|
if (this.isRaw(key)) {
|
|
8934
8998
|
const entry = this._toEntryFn(key);
|
|
8935
|
-
inserted[orgIndex] = this.
|
|
8999
|
+
inserted[orgIndex] = this.set(entry);
|
|
8936
9000
|
} else {
|
|
8937
|
-
inserted[orgIndex] = this.
|
|
9001
|
+
inserted[orgIndex] = this.set(key, value);
|
|
8938
9002
|
}
|
|
8939
9003
|
_dfs(arr.slice(0, mid));
|
|
8940
9004
|
_dfs(arr.slice(mid + 1));
|
|
@@ -8951,9 +9015,9 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8951
9015
|
const { key, value, orgIndex } = sorted[m];
|
|
8952
9016
|
if (this.isRaw(key)) {
|
|
8953
9017
|
const entry = this._toEntryFn(key);
|
|
8954
|
-
inserted[orgIndex] = this.
|
|
9018
|
+
inserted[orgIndex] = this.set(entry);
|
|
8955
9019
|
} else {
|
|
8956
|
-
inserted[orgIndex] = this.
|
|
9020
|
+
inserted[orgIndex] = this.set(key, value);
|
|
8957
9021
|
}
|
|
8958
9022
|
stack.push([m + 1, r]);
|
|
8959
9023
|
stack.push([l, m - 1]);
|
|
@@ -9228,7 +9292,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9228
9292
|
const out = this._createLike([], options);
|
|
9229
9293
|
let index = 0;
|
|
9230
9294
|
for (const [key, value] of this) {
|
|
9231
|
-
out.
|
|
9295
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
9232
9296
|
}
|
|
9233
9297
|
return out;
|
|
9234
9298
|
}
|
|
@@ -9659,7 +9723,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9659
9723
|
* @returns True if the node was found and deleted, false otherwise.
|
|
9660
9724
|
*/
|
|
9661
9725
|
_deleteByKey(key) {
|
|
9662
|
-
var _a;
|
|
9663
9726
|
let node = this._root;
|
|
9664
9727
|
while (node) {
|
|
9665
9728
|
const cmp = this._compare(node.key, key);
|
|
@@ -9698,7 +9761,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9698
9761
|
succ.left = node.left;
|
|
9699
9762
|
if (succ.left) succ.left.parent = succ;
|
|
9700
9763
|
}
|
|
9701
|
-
this._size = Math.max(0,
|
|
9764
|
+
this._size = Math.max(0, this._size - 1);
|
|
9702
9765
|
return true;
|
|
9703
9766
|
}
|
|
9704
9767
|
};
|
|
@@ -10414,14 +10477,14 @@ var AVLTreeNode = _AVLTreeNode;
|
|
|
10414
10477
|
var _AVLTree = class _AVLTree extends BST {
|
|
10415
10478
|
/**
|
|
10416
10479
|
* Creates an instance of AVLTree.
|
|
10417
|
-
* @remarks Time O(N log N) (from `
|
|
10480
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
10418
10481
|
*
|
|
10419
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
10482
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
10420
10483
|
* @param [options] - Configuration options for the AVL tree.
|
|
10421
10484
|
*/
|
|
10422
10485
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
10423
10486
|
super([], options);
|
|
10424
|
-
if (keysNodesEntriesOrRaws) super.
|
|
10487
|
+
if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
|
|
10425
10488
|
}
|
|
10426
10489
|
/**
|
|
10427
10490
|
* (Protected) Creates a new AVL tree node.
|
|
@@ -10445,16 +10508,16 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
10445
10508
|
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
10446
10509
|
}
|
|
10447
10510
|
/**
|
|
10448
|
-
*
|
|
10449
|
-
* @remarks Time O(log N) (O(H) for BST
|
|
10511
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
10512
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
10450
10513
|
*
|
|
10451
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
10514
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
10452
10515
|
* @param [value] - The value, if providing just a key.
|
|
10453
10516
|
* @returns True if the addition was successful, false otherwise.
|
|
10454
10517
|
*/
|
|
10455
|
-
|
|
10518
|
+
set(keyNodeOrEntry, value) {
|
|
10456
10519
|
if (keyNodeOrEntry === null) return false;
|
|
10457
|
-
const inserted = super.
|
|
10520
|
+
const inserted = super.set(keyNodeOrEntry, value);
|
|
10458
10521
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
10459
10522
|
return inserted;
|
|
10460
10523
|
}
|
|
@@ -10506,7 +10569,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
10506
10569
|
}
|
|
10507
10570
|
/**
|
|
10508
10571
|
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
10509
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `
|
|
10572
|
+
* @remarks Time O(N log N) (O(N) iteration + O(log M) `set` for each item into the new tree). Space O(N) for the new tree.
|
|
10510
10573
|
*
|
|
10511
10574
|
* @template MK - New key type.
|
|
10512
10575
|
* @template MV - New value type.
|
|
@@ -10520,7 +10583,7 @@ var _AVLTree = class _AVLTree extends BST {
|
|
|
10520
10583
|
const out = this._createLike([], options);
|
|
10521
10584
|
let index = 0;
|
|
10522
10585
|
for (const [key, value] of this) {
|
|
10523
|
-
out.
|
|
10586
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
10524
10587
|
}
|
|
10525
10588
|
return out;
|
|
10526
10589
|
}
|
|
@@ -10805,12 +10868,11 @@ var AVLTree = _AVLTree;
|
|
|
10805
10868
|
// src/data-structures/binary-tree/red-black-tree.ts
|
|
10806
10869
|
var _RedBlackTreeNode = class _RedBlackTreeNode {
|
|
10807
10870
|
/**
|
|
10808
|
-
* Create a Red-Black Tree
|
|
10809
|
-
* @remarks Time O(
|
|
10810
|
-
* @param key -
|
|
10811
|
-
* @param [value]-
|
|
10812
|
-
* @param color -
|
|
10813
|
-
* @returns New RedBlackTree instance.
|
|
10871
|
+
* Create a Red-Black Tree node.
|
|
10872
|
+
* @remarks Time O(1), Space O(1)
|
|
10873
|
+
* @param key - Node key.
|
|
10874
|
+
* @param [value] - Node value (unused in map mode trees).
|
|
10875
|
+
* @param color - Node color.
|
|
10814
10876
|
*/
|
|
10815
10877
|
constructor(key, value, color = "BLACK") {
|
|
10816
10878
|
__publicField(this, "key");
|
|
@@ -10943,9 +11005,31 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10943
11005
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
10944
11006
|
super([], options);
|
|
10945
11007
|
__publicField(this, "_root");
|
|
11008
|
+
/**
|
|
11009
|
+
* (Internal) Header sentinel:
|
|
11010
|
+
* - header.parent -> root
|
|
11011
|
+
* - header._left -> min (or NIL)
|
|
11012
|
+
* - header._right -> max (or NIL)
|
|
11013
|
+
*
|
|
11014
|
+
* IMPORTANT:
|
|
11015
|
+
* - This header is NOT part of the actual tree.
|
|
11016
|
+
* - Do NOT use `header.left` / `header.right` accessors for wiring: those setters update `NIL.parent`
|
|
11017
|
+
* and can corrupt sentinel invariants / cause hangs. Only touch `header._left/_right`.
|
|
11018
|
+
*/
|
|
11019
|
+
__publicField(this, "_header");
|
|
11020
|
+
/**
|
|
11021
|
+
* (Internal) Cache of the current minimum and maximum nodes.
|
|
11022
|
+
* Used for fast-path insert/update when keys are monotonic or near-boundary.
|
|
11023
|
+
*/
|
|
11024
|
+
__publicField(this, "_minNode");
|
|
11025
|
+
__publicField(this, "_maxNode");
|
|
10946
11026
|
this._root = this.NIL;
|
|
11027
|
+
this._header = new RedBlackTreeNode(void 0, void 0, "BLACK");
|
|
11028
|
+
this._header.parent = this.NIL;
|
|
11029
|
+
this._header._left = this.NIL;
|
|
11030
|
+
this._header._right = this.NIL;
|
|
10947
11031
|
if (keysNodesEntriesOrRaws) {
|
|
10948
|
-
this.
|
|
11032
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
10949
11033
|
}
|
|
10950
11034
|
}
|
|
10951
11035
|
/**
|
|
@@ -10981,18 +11065,390 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
10981
11065
|
* @remarks Time O(n), Space O(1)
|
|
10982
11066
|
* @returns void
|
|
10983
11067
|
*/
|
|
11068
|
+
/**
|
|
11069
|
+
* Remove all nodes and clear internal caches.
|
|
11070
|
+
* @remarks Time O(n) average, Space O(1)
|
|
11071
|
+
*/
|
|
10984
11072
|
clear() {
|
|
10985
11073
|
super.clear();
|
|
10986
11074
|
this._root = this.NIL;
|
|
11075
|
+
this._header.parent = this.NIL;
|
|
11076
|
+
this._setMinCache(void 0);
|
|
11077
|
+
this._setMaxCache(void 0);
|
|
10987
11078
|
}
|
|
10988
11079
|
/**
|
|
10989
|
-
*
|
|
10990
|
-
*
|
|
10991
|
-
*
|
|
10992
|
-
* @
|
|
10993
|
-
|
|
11080
|
+
* (Internal) Find a node by key using a tight BST walk (no allocations).
|
|
11081
|
+
*
|
|
11082
|
+
* NOTE: This uses `header.parent` as the canonical root pointer.
|
|
11083
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11084
|
+
*/
|
|
11085
|
+
_findNodeByKey(key) {
|
|
11086
|
+
var _a, _b, _c;
|
|
11087
|
+
const NIL = this.NIL;
|
|
11088
|
+
const cmp = this._compare.bind(this);
|
|
11089
|
+
let cur = (_a = this._header.parent) != null ? _a : NIL;
|
|
11090
|
+
while (cur !== NIL) {
|
|
11091
|
+
const c = cmp(key, cur.key);
|
|
11092
|
+
if (c < 0) cur = (_b = cur.left) != null ? _b : NIL;
|
|
11093
|
+
else if (c > 0) cur = (_c = cur.right) != null ? _c : NIL;
|
|
11094
|
+
else return cur;
|
|
11095
|
+
}
|
|
11096
|
+
return void 0;
|
|
11097
|
+
}
|
|
11098
|
+
/**
|
|
11099
|
+
* (Internal) In-order predecessor of a node in a BST.
|
|
11100
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11101
|
+
*/
|
|
11102
|
+
_predecessorOf(node) {
|
|
11103
|
+
const NIL = this.NIL;
|
|
11104
|
+
if (node.left && node.left !== NIL) {
|
|
11105
|
+
let cur2 = node.left;
|
|
11106
|
+
while (cur2.right && cur2.right !== NIL) cur2 = cur2.right;
|
|
11107
|
+
return cur2;
|
|
11108
|
+
}
|
|
11109
|
+
let cur = node;
|
|
11110
|
+
let p = node.parent;
|
|
11111
|
+
while (p && cur === p.left) {
|
|
11112
|
+
cur = p;
|
|
11113
|
+
p = p.parent;
|
|
11114
|
+
}
|
|
11115
|
+
return p;
|
|
11116
|
+
}
|
|
11117
|
+
/**
|
|
11118
|
+
* (Internal) In-order successor of a node in a BST.
|
|
11119
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11120
|
+
*/
|
|
11121
|
+
_successorOf(node) {
|
|
11122
|
+
const NIL = this.NIL;
|
|
11123
|
+
if (node.right && node.right !== NIL) {
|
|
11124
|
+
let cur2 = node.right;
|
|
11125
|
+
while (cur2.left && cur2.left !== NIL) cur2 = cur2.left;
|
|
11126
|
+
return cur2;
|
|
11127
|
+
}
|
|
11128
|
+
let cur = node;
|
|
11129
|
+
let p = node.parent;
|
|
11130
|
+
while (p && cur === p.right) {
|
|
11131
|
+
cur = p;
|
|
11132
|
+
p = p.parent;
|
|
11133
|
+
}
|
|
11134
|
+
return p;
|
|
11135
|
+
}
|
|
11136
|
+
/**
|
|
11137
|
+
* (Internal) Attach a new node directly under a known parent/side (no search).
|
|
11138
|
+
*
|
|
11139
|
+
* This is a performance-oriented helper used by boundary fast paths and hinted insertion.
|
|
11140
|
+
* It will:
|
|
11141
|
+
* - wire parent/child pointers (using accessors, so parent pointers are updated)
|
|
11142
|
+
* - initialize children to NIL
|
|
11143
|
+
* - mark the new node RED, then run insert fix-up
|
|
11144
|
+
*
|
|
11145
|
+
* Precondition: the chosen slot (parent.left/parent.right) is empty (NIL/null/undefined).
|
|
11146
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11147
|
+
*/
|
|
11148
|
+
_attachNewNode(parent, side, node) {
|
|
11149
|
+
const NIL = this.NIL;
|
|
11150
|
+
node.parent = parent;
|
|
11151
|
+
if (side === "left") parent.left = node;
|
|
11152
|
+
else parent.right = node;
|
|
11153
|
+
node.left = NIL;
|
|
11154
|
+
node.right = NIL;
|
|
11155
|
+
node.color = "RED";
|
|
11156
|
+
this._insertFixup(node);
|
|
11157
|
+
if (this.isRealNode(this._root)) this._root.color = "BLACK";
|
|
11158
|
+
}
|
|
11159
|
+
/**
|
|
11160
|
+
* (Internal) a single source of truth for min/max is header._left/_right.
|
|
11161
|
+
* Keep legacy _minNode/_maxNode mirrored for compatibility.
|
|
11162
|
+
* @remarks Time O(1), Space O(1)
|
|
11163
|
+
*/
|
|
11164
|
+
/**
|
|
11165
|
+
* (Internal) Update min cache pointers (header._left is the canonical min pointer).
|
|
11166
|
+
* @remarks Time O(1), Space O(1)
|
|
10994
11167
|
*/
|
|
10995
|
-
|
|
11168
|
+
_setMinCache(node) {
|
|
11169
|
+
this._minNode = node;
|
|
11170
|
+
this._header._left = node != null ? node : this.NIL;
|
|
11171
|
+
}
|
|
11172
|
+
/**
|
|
11173
|
+
* (Internal) Update max cache pointers (header._right is the canonical max pointer).
|
|
11174
|
+
* @remarks Time O(1), Space O(1)
|
|
11175
|
+
*/
|
|
11176
|
+
_setMaxCache(node) {
|
|
11177
|
+
this._maxNode = node;
|
|
11178
|
+
this._header._right = node != null ? node : this.NIL;
|
|
11179
|
+
}
|
|
11180
|
+
/**
|
|
11181
|
+
* (Internal) Core set implementation returning the affected node.
|
|
11182
|
+
*
|
|
11183
|
+
* Hot path goals:
|
|
11184
|
+
* - Avoid double walks (search+insert): do a single traversal that either updates or inserts.
|
|
11185
|
+
* - Use header min/max caches to fast-path boundary inserts.
|
|
11186
|
+
* - Keep header._left/_right as canonical min/max pointers.
|
|
11187
|
+
*
|
|
11188
|
+
* Return value:
|
|
11189
|
+
* - `{ node, created:false }` when an existing key is updated
|
|
11190
|
+
* - `{ node, created:true }` when a new node is inserted
|
|
11191
|
+
* - `undefined` only on unexpected internal failure.
|
|
11192
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11193
|
+
*/
|
|
11194
|
+
_setKVNode(key, nextValue) {
|
|
11195
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
11196
|
+
const NIL = this.NIL;
|
|
11197
|
+
const comparator = this._comparator;
|
|
11198
|
+
const header = this._header;
|
|
11199
|
+
const minN = (_a = header._left) != null ? _a : NIL;
|
|
11200
|
+
if (minN !== NIL) {
|
|
11201
|
+
const cMin = comparator(key, minN.key);
|
|
11202
|
+
if (cMin === 0) {
|
|
11203
|
+
if (this._isMapMode) {
|
|
11204
|
+
if (nextValue !== void 0) this._store.set(key, nextValue);
|
|
11205
|
+
else this._setValue(key, nextValue);
|
|
11206
|
+
} else minN.value = nextValue;
|
|
11207
|
+
return { node: minN, created: false };
|
|
11208
|
+
}
|
|
11209
|
+
const minL = minN.left;
|
|
11210
|
+
if (cMin < 0 && (minL === NIL || minL === null || minL === void 0)) {
|
|
11211
|
+
const newNode2 = this.createNode(key, nextValue);
|
|
11212
|
+
this._attachNewNode(minN, "left", newNode2);
|
|
11213
|
+
if (this._isMapMode) {
|
|
11214
|
+
if (nextValue !== void 0) this._store.set(newNode2.key, nextValue);
|
|
11215
|
+
else this._setValue(newNode2.key, nextValue);
|
|
11216
|
+
}
|
|
11217
|
+
this._size++;
|
|
11218
|
+
this._setMinCache(newNode2);
|
|
11219
|
+
if (header._right === NIL) this._setMaxCache(newNode2);
|
|
11220
|
+
return { node: newNode2, created: true };
|
|
11221
|
+
}
|
|
11222
|
+
if (cMin > 0) {
|
|
11223
|
+
const maxN = (_b = header._right) != null ? _b : NIL;
|
|
11224
|
+
const cMax = comparator(key, maxN.key);
|
|
11225
|
+
if (cMax === 0) {
|
|
11226
|
+
if (this._isMapMode) {
|
|
11227
|
+
if (nextValue !== void 0) this._store.set(key, nextValue);
|
|
11228
|
+
else this._setValue(key, nextValue);
|
|
11229
|
+
} else maxN.value = nextValue;
|
|
11230
|
+
return { node: maxN, created: false };
|
|
11231
|
+
}
|
|
11232
|
+
const maxR = maxN.right;
|
|
11233
|
+
if (cMax > 0 && (maxR === NIL || maxR === null || maxR === void 0)) {
|
|
11234
|
+
const newNode2 = this.createNode(key, nextValue);
|
|
11235
|
+
this._attachNewNode(maxN, "right", newNode2);
|
|
11236
|
+
if (this._isMapMode) {
|
|
11237
|
+
if (nextValue !== void 0) this._store.set(newNode2.key, nextValue);
|
|
11238
|
+
else this._setValue(newNode2.key, nextValue);
|
|
11239
|
+
}
|
|
11240
|
+
this._size++;
|
|
11241
|
+
this._setMaxCache(newNode2);
|
|
11242
|
+
if (header._left === NIL) this._setMinCache(newNode2);
|
|
11243
|
+
return { node: newNode2, created: true };
|
|
11244
|
+
}
|
|
11245
|
+
}
|
|
11246
|
+
}
|
|
11247
|
+
const cmp = comparator;
|
|
11248
|
+
const isMapMode = this._isMapMode;
|
|
11249
|
+
const store = this._store;
|
|
11250
|
+
let current = (_c = this._header.parent) != null ? _c : NIL;
|
|
11251
|
+
let parent;
|
|
11252
|
+
let lastCompared = 0;
|
|
11253
|
+
while (current !== NIL) {
|
|
11254
|
+
parent = current;
|
|
11255
|
+
lastCompared = cmp(key, current.key);
|
|
11256
|
+
if (lastCompared < 0) current = (_d = current.left) != null ? _d : NIL;
|
|
11257
|
+
else if (lastCompared > 0) current = (_e = current.right) != null ? _e : NIL;
|
|
11258
|
+
else {
|
|
11259
|
+
if (isMapMode) {
|
|
11260
|
+
if (nextValue !== void 0) store.set(key, nextValue);
|
|
11261
|
+
else this._setValue(key, nextValue);
|
|
11262
|
+
} else {
|
|
11263
|
+
current.value = nextValue;
|
|
11264
|
+
}
|
|
11265
|
+
return { node: current, created: false };
|
|
11266
|
+
}
|
|
11267
|
+
}
|
|
11268
|
+
const newNode = this.createNode(key, nextValue);
|
|
11269
|
+
newNode.parent = parent;
|
|
11270
|
+
if (!parent) {
|
|
11271
|
+
this._setRoot(newNode);
|
|
11272
|
+
} else if (lastCompared < 0) {
|
|
11273
|
+
parent.left = newNode;
|
|
11274
|
+
} else {
|
|
11275
|
+
parent.right = newNode;
|
|
11276
|
+
}
|
|
11277
|
+
newNode.left = NIL;
|
|
11278
|
+
newNode.right = NIL;
|
|
11279
|
+
newNode.color = "RED";
|
|
11280
|
+
this._insertFixup(newNode);
|
|
11281
|
+
if (this.isRealNode(this._root)) this._root.color = "BLACK";
|
|
11282
|
+
else return void 0;
|
|
11283
|
+
if (isMapMode) {
|
|
11284
|
+
if (nextValue !== void 0) store.set(newNode.key, nextValue);
|
|
11285
|
+
else this._setValue(newNode.key, nextValue);
|
|
11286
|
+
}
|
|
11287
|
+
this._size++;
|
|
11288
|
+
const hMin = (_f = this._header._left) != null ? _f : NIL;
|
|
11289
|
+
const hMax = (_g = this._header._right) != null ? _g : NIL;
|
|
11290
|
+
if (hMin === NIL || hMax === NIL) {
|
|
11291
|
+
this._setMinCache(newNode);
|
|
11292
|
+
this._setMaxCache(newNode);
|
|
11293
|
+
} else if (parent === hMax && lastCompared > 0) {
|
|
11294
|
+
this._setMaxCache(newNode);
|
|
11295
|
+
} else if (parent === hMin && lastCompared < 0) {
|
|
11296
|
+
this._setMinCache(newNode);
|
|
11297
|
+
} else {
|
|
11298
|
+
if (cmp(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
|
|
11299
|
+
if (cmp(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
|
|
11300
|
+
}
|
|
11301
|
+
return { node: newNode, created: true };
|
|
11302
|
+
}
|
|
11303
|
+
/**
|
|
11304
|
+
* (Internal) Boolean wrapper around `_setKVNode`.
|
|
11305
|
+
*
|
|
11306
|
+
* Includes a map-mode update fast-path:
|
|
11307
|
+
* - If `isMapMode=true` and the key already exists in `_store`, then updating the value does not
|
|
11308
|
+
* require any tree search/rotation (tree shape depends only on key).
|
|
11309
|
+
* - This path is intentionally limited to `nextValue !== undefined` to preserve existing
|
|
11310
|
+
* semantics for `undefined` values.
|
|
11311
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11312
|
+
*/
|
|
11313
|
+
_setKV(key, nextValue) {
|
|
11314
|
+
if (this._isMapMode && nextValue !== void 0) {
|
|
11315
|
+
const store = this._store;
|
|
11316
|
+
if (store.has(key)) {
|
|
11317
|
+
store.set(key, nextValue);
|
|
11318
|
+
return true;
|
|
11319
|
+
}
|
|
11320
|
+
}
|
|
11321
|
+
return this._setKVNode(key, nextValue) !== void 0;
|
|
11322
|
+
}
|
|
11323
|
+
/**
|
|
11324
|
+
* Insert/update using a hint node to speed up nearby insertions.
|
|
11325
|
+
*
|
|
11326
|
+
* close to the expected insertion position (often the previously returned node in a loop).
|
|
11327
|
+
*
|
|
11328
|
+
* When the hint is a good fit (sorted / nearly-sorted insertion), this can avoid most of the
|
|
11329
|
+
* normal root-to-leaf search and reduce constant factors.
|
|
11330
|
+
*
|
|
11331
|
+
* When the hint does not match (random workloads), this will fall back to the normal set path.
|
|
11332
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11333
|
+
*/
|
|
11334
|
+
setWithHintNode(key, value, hint) {
|
|
11335
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
11336
|
+
if (!hint || !this.isRealNode(hint)) {
|
|
11337
|
+
return (_a = this._setKVNode(key, value)) == null ? void 0 : _a.node;
|
|
11338
|
+
}
|
|
11339
|
+
const cmp = this._compare.bind(this);
|
|
11340
|
+
const c0 = cmp(key, hint.key);
|
|
11341
|
+
if (c0 === 0) {
|
|
11342
|
+
if (this._isMapMode) {
|
|
11343
|
+
if (value !== void 0) this._store.set(key, value);
|
|
11344
|
+
else this._setValue(key, value);
|
|
11345
|
+
} else hint.value = value;
|
|
11346
|
+
return hint;
|
|
11347
|
+
}
|
|
11348
|
+
if (c0 < 0) {
|
|
11349
|
+
if (!this.isRealNode(hint.left)) {
|
|
11350
|
+
const newNode = this.createNode(key, value);
|
|
11351
|
+
if (!this.isRealNode(newNode)) return void 0;
|
|
11352
|
+
this._attachNewNode(hint, "left", newNode);
|
|
11353
|
+
if (this._isMapMode) {
|
|
11354
|
+
if (value !== void 0) this._store.set(key, value);
|
|
11355
|
+
else this._setValue(key, value);
|
|
11356
|
+
}
|
|
11357
|
+
this._size++;
|
|
11358
|
+
const NIL = this.NIL;
|
|
11359
|
+
const hMin = (_b = this._header._left) != null ? _b : NIL;
|
|
11360
|
+
if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
|
|
11361
|
+
const hMax = (_c = this._header._right) != null ? _c : NIL;
|
|
11362
|
+
if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
|
|
11363
|
+
return newNode;
|
|
11364
|
+
}
|
|
11365
|
+
const pred = this._predecessorOf(hint);
|
|
11366
|
+
if (pred && cmp(pred.key, key) >= 0) {
|
|
11367
|
+
return (_d = this._setKVNode(key, value)) == null ? void 0 : _d.node;
|
|
11368
|
+
}
|
|
11369
|
+
if (pred && !this.isRealNode(pred.right)) {
|
|
11370
|
+
const newNode = this.createNode(key, value);
|
|
11371
|
+
if (!this.isRealNode(newNode)) return void 0;
|
|
11372
|
+
this._attachNewNode(pred, "right", newNode);
|
|
11373
|
+
if (this._isMapMode) {
|
|
11374
|
+
if (value !== void 0) this._store.set(key, value);
|
|
11375
|
+
else this._setValue(key, value);
|
|
11376
|
+
}
|
|
11377
|
+
this._size++;
|
|
11378
|
+
const NIL = this.NIL;
|
|
11379
|
+
const hMin = (_e = this._header._left) != null ? _e : NIL;
|
|
11380
|
+
if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
|
|
11381
|
+
const hMax = (_f = this._header._right) != null ? _f : NIL;
|
|
11382
|
+
if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
|
|
11383
|
+
return newNode;
|
|
11384
|
+
}
|
|
11385
|
+
return (_g = this._setKVNode(key, value)) == null ? void 0 : _g.node;
|
|
11386
|
+
}
|
|
11387
|
+
if (!this.isRealNode(hint.right)) {
|
|
11388
|
+
const newNode = this.createNode(key, value);
|
|
11389
|
+
if (!this.isRealNode(newNode)) return void 0;
|
|
11390
|
+
this._attachNewNode(hint, "right", newNode);
|
|
11391
|
+
if (this._isMapMode) {
|
|
11392
|
+
if (value !== void 0) this._store.set(key, value);
|
|
11393
|
+
else this._setValue(key, value);
|
|
11394
|
+
}
|
|
11395
|
+
this._size++;
|
|
11396
|
+
const NIL = this.NIL;
|
|
11397
|
+
const hMin = (_h = this._header._left) != null ? _h : NIL;
|
|
11398
|
+
if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
|
|
11399
|
+
const hMax = (_i = this._header._right) != null ? _i : NIL;
|
|
11400
|
+
if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
|
|
11401
|
+
return newNode;
|
|
11402
|
+
}
|
|
11403
|
+
const succ = this._successorOf(hint);
|
|
11404
|
+
if (succ && cmp(succ.key, key) <= 0) {
|
|
11405
|
+
return (_j = this._setKVNode(key, value)) == null ? void 0 : _j.node;
|
|
11406
|
+
}
|
|
11407
|
+
if (succ && !this.isRealNode(succ.left)) {
|
|
11408
|
+
const newNode = this.createNode(key, value);
|
|
11409
|
+
if (!this.isRealNode(newNode)) return void 0;
|
|
11410
|
+
this._attachNewNode(succ, "left", newNode);
|
|
11411
|
+
if (this._isMapMode) {
|
|
11412
|
+
if (value !== void 0) this._store.set(key, value);
|
|
11413
|
+
else this._setValue(key, value);
|
|
11414
|
+
}
|
|
11415
|
+
this._size++;
|
|
11416
|
+
const NIL = this.NIL;
|
|
11417
|
+
const hMin = (_k = this._header._left) != null ? _k : NIL;
|
|
11418
|
+
if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
|
|
11419
|
+
const hMax = (_l = this._header._right) != null ? _l : NIL;
|
|
11420
|
+
if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
|
|
11421
|
+
return newNode;
|
|
11422
|
+
}
|
|
11423
|
+
return (_m = this._setKVNode(key, value)) == null ? void 0 : _m.node;
|
|
11424
|
+
}
|
|
11425
|
+
/**
|
|
11426
|
+
* Boolean wrapper for setWithHintNode.
|
|
11427
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11428
|
+
*/
|
|
11429
|
+
setWithHint(key, value, hint) {
|
|
11430
|
+
return this.setWithHintNode(key, value, hint) !== void 0;
|
|
11431
|
+
}
|
|
11432
|
+
/**
|
|
11433
|
+
* Insert or update a key/value (map mode) or key-only (set mode).
|
|
11434
|
+
*
|
|
11435
|
+
* This method is optimized for:
|
|
11436
|
+
* - monotonic inserts via min/max boundary fast paths
|
|
11437
|
+
* - updates via a single-pass search (no double walk)
|
|
11438
|
+
*
|
|
11439
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11440
|
+
*/
|
|
11441
|
+
set(keyNodeOrEntry, value) {
|
|
11442
|
+
if (!this.isNode(keyNodeOrEntry)) {
|
|
11443
|
+
if (keyNodeOrEntry === null || keyNodeOrEntry === void 0) return false;
|
|
11444
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
11445
|
+
const key = keyNodeOrEntry[0];
|
|
11446
|
+
if (key === null || key === void 0) return false;
|
|
11447
|
+
const nextValue = value != null ? value : keyNodeOrEntry[1];
|
|
11448
|
+
return this._setKV(key, nextValue);
|
|
11449
|
+
}
|
|
11450
|
+
return this._setKV(keyNodeOrEntry, value);
|
|
11451
|
+
}
|
|
10996
11452
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
10997
11453
|
if (!this.isRealNode(newNode)) return false;
|
|
10998
11454
|
const insertStatus = this._insert(newNode);
|
|
@@ -11014,19 +11470,23 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
11014
11470
|
}
|
|
11015
11471
|
/**
|
|
11016
11472
|
* Delete a node by key/node/entry and rebalance as needed.
|
|
11017
|
-
* @remarks Time O(log n), Space O(1)
|
|
11018
|
-
* @param
|
|
11473
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11474
|
+
* @param keyNodeEntryRawOrPredicate - Key, node, or [key, value] entry identifying the node to delete.
|
|
11019
11475
|
* @returns Array with deletion metadata (removed node, rebalancing hint if any).
|
|
11020
11476
|
*/
|
|
11021
|
-
delete(
|
|
11022
|
-
if (
|
|
11477
|
+
delete(keyNodeEntryRawOrPredicate) {
|
|
11478
|
+
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
11023
11479
|
const results = [];
|
|
11024
11480
|
let nodeToDelete;
|
|
11025
|
-
if (this._isPredicate(
|
|
11026
|
-
else nodeToDelete = this.isRealNode(
|
|
11481
|
+
if (this._isPredicate(keyNodeEntryRawOrPredicate)) nodeToDelete = this.getNode(keyNodeEntryRawOrPredicate);
|
|
11482
|
+
else nodeToDelete = this.isRealNode(keyNodeEntryRawOrPredicate) ? keyNodeEntryRawOrPredicate : this.getNode(keyNodeEntryRawOrPredicate);
|
|
11027
11483
|
if (!nodeToDelete) {
|
|
11028
11484
|
return results;
|
|
11029
11485
|
}
|
|
11486
|
+
const willDeleteMin = nodeToDelete === this._minNode;
|
|
11487
|
+
const willDeleteMax = nodeToDelete === this._maxNode;
|
|
11488
|
+
const nextMin = willDeleteMin ? this._successorOf(nodeToDelete) : void 0;
|
|
11489
|
+
const nextMax = willDeleteMax ? this._predecessorOf(nodeToDelete) : void 0;
|
|
11030
11490
|
let originalColor = nodeToDelete.color;
|
|
11031
11491
|
let replacementNode;
|
|
11032
11492
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
@@ -11065,6 +11525,19 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
11065
11525
|
}
|
|
11066
11526
|
if (this._isMapMode) this._store.delete(nodeToDelete.key);
|
|
11067
11527
|
this._size--;
|
|
11528
|
+
if (this._size <= 0) {
|
|
11529
|
+
this._setMinCache(void 0);
|
|
11530
|
+
this._setMaxCache(void 0);
|
|
11531
|
+
} else {
|
|
11532
|
+
if (willDeleteMin) this._setMinCache(nextMin);
|
|
11533
|
+
if (willDeleteMax) this._setMaxCache(nextMax);
|
|
11534
|
+
if (!this._minNode || !this.isRealNode(this._minNode)) {
|
|
11535
|
+
this._setMinCache(this.isRealNode(this._root) ? this.getLeftMost((n) => n, this._root) : void 0);
|
|
11536
|
+
}
|
|
11537
|
+
if (!this._maxNode || !this.isRealNode(this._maxNode)) {
|
|
11538
|
+
this._setMaxCache(this.isRealNode(this._root) ? this.getRightMost((n) => n, this._root) : void 0);
|
|
11539
|
+
}
|
|
11540
|
+
}
|
|
11068
11541
|
if (originalColor === "BLACK") {
|
|
11069
11542
|
this._deleteFixup(replacementNode);
|
|
11070
11543
|
}
|
|
@@ -11073,7 +11546,7 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
11073
11546
|
}
|
|
11074
11547
|
/**
|
|
11075
11548
|
* Transform entries into a like-kind red-black tree with possibly different key/value types.
|
|
11076
|
-
* @remarks Time O(n), Space O(n)
|
|
11549
|
+
* @remarks Time O(n) average, Space O(n)
|
|
11077
11550
|
* @template MK
|
|
11078
11551
|
* @template MV
|
|
11079
11552
|
* @template MR
|
|
@@ -11086,45 +11559,66 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
11086
11559
|
const out = this._createLike([], options);
|
|
11087
11560
|
let index = 0;
|
|
11088
11561
|
for (const [key, value] of this) {
|
|
11089
|
-
out.
|
|
11562
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
11090
11563
|
}
|
|
11091
11564
|
return out;
|
|
11092
11565
|
}
|
|
11566
|
+
/**
|
|
11567
|
+
* (Internal) Create an empty instance of the same concrete tree type.
|
|
11568
|
+
* @remarks Time O(1) average, Space O(1)
|
|
11569
|
+
*/
|
|
11093
11570
|
_createInstance(options) {
|
|
11094
11571
|
const Ctor = this.constructor;
|
|
11095
11572
|
return new Ctor([], { ...this._snapshotOptions(), ...options != null ? options : {} });
|
|
11096
11573
|
}
|
|
11574
|
+
/**
|
|
11575
|
+
* (Internal) Create a like-kind tree (same concrete class) populated from an iterable.
|
|
11576
|
+
* @remarks Time O(m log m) average (m = iterable length), Space O(m)
|
|
11577
|
+
*/
|
|
11097
11578
|
_createLike(iter = [], options) {
|
|
11098
11579
|
const Ctor = this.constructor;
|
|
11099
11580
|
return new Ctor(iter, { ...this._snapshotOptions(), ...options != null ? options : {} });
|
|
11100
11581
|
}
|
|
11582
|
+
/**
|
|
11583
|
+
* (Internal) Set the root pointer and keep header.parent in sync.
|
|
11584
|
+
* @remarks Time O(1), Space O(1)
|
|
11585
|
+
*/
|
|
11101
11586
|
_setRoot(v) {
|
|
11587
|
+
const NIL = this.NIL;
|
|
11102
11588
|
if (v) {
|
|
11103
11589
|
v.parent = void 0;
|
|
11104
11590
|
}
|
|
11105
11591
|
this._root = v;
|
|
11592
|
+
this._header.parent = v != null ? v : NIL;
|
|
11106
11593
|
}
|
|
11594
|
+
/**
|
|
11595
|
+
* (Internal) Replace a node in place while preserving its color.
|
|
11596
|
+
* @remarks Time O(1) average, Space O(1)
|
|
11597
|
+
*/
|
|
11107
11598
|
_replaceNode(oldNode, newNode) {
|
|
11108
11599
|
newNode.color = oldNode.color;
|
|
11109
11600
|
return super._replaceNode(oldNode, newNode);
|
|
11110
11601
|
}
|
|
11111
11602
|
/**
|
|
11112
11603
|
* (Protected) Standard BST insert followed by red-black fix-up.
|
|
11113
|
-
* @remarks Time O(log n), Space O(1)
|
|
11604
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11114
11605
|
* @param node - Node to insert.
|
|
11115
11606
|
* @returns Status string: 'CREATED' or 'UPDATED'.
|
|
11116
11607
|
*/
|
|
11117
11608
|
_insert(node) {
|
|
11118
11609
|
var _a, _b, _c;
|
|
11119
|
-
|
|
11120
|
-
|
|
11121
|
-
|
|
11610
|
+
const NIL = this.NIL;
|
|
11611
|
+
const cmp = this._compare.bind(this);
|
|
11612
|
+
let current = (_a = this._header.parent) != null ? _a : NIL;
|
|
11613
|
+
let parent;
|
|
11614
|
+
let lastCompared = 0;
|
|
11615
|
+
while (current !== NIL) {
|
|
11122
11616
|
parent = current;
|
|
11123
|
-
|
|
11124
|
-
if (
|
|
11125
|
-
current = (_b = current.left) != null ? _b :
|
|
11126
|
-
} else if (
|
|
11127
|
-
current = (_c = current.right) != null ? _c :
|
|
11617
|
+
lastCompared = cmp(node.key, current.key);
|
|
11618
|
+
if (lastCompared < 0) {
|
|
11619
|
+
current = (_b = current.left) != null ? _b : NIL;
|
|
11620
|
+
} else if (lastCompared > 0) {
|
|
11621
|
+
current = (_c = current.right) != null ? _c : NIL;
|
|
11128
11622
|
} else {
|
|
11129
11623
|
this._replaceNode(current, node);
|
|
11130
11624
|
return "UPDATED";
|
|
@@ -11133,13 +11627,13 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
11133
11627
|
node.parent = parent;
|
|
11134
11628
|
if (!parent) {
|
|
11135
11629
|
this._setRoot(node);
|
|
11136
|
-
} else if (
|
|
11630
|
+
} else if (lastCompared < 0) {
|
|
11137
11631
|
parent.left = node;
|
|
11138
11632
|
} else {
|
|
11139
11633
|
parent.right = node;
|
|
11140
11634
|
}
|
|
11141
|
-
node.left =
|
|
11142
|
-
node.right =
|
|
11635
|
+
node.left = NIL;
|
|
11636
|
+
node.right = NIL;
|
|
11143
11637
|
node.color = "RED";
|
|
11144
11638
|
this._insertFixup(node);
|
|
11145
11639
|
return "CREATED";
|
|
@@ -11165,56 +11659,66 @@ var _RedBlackTree = class _RedBlackTree extends BST {
|
|
|
11165
11659
|
}
|
|
11166
11660
|
/**
|
|
11167
11661
|
* (Protected) Restore red-black properties after insertion (recolor/rotate).
|
|
11168
|
-
* @remarks Time O(log n), Space O(1)
|
|
11662
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11169
11663
|
* @param z - Recently inserted node.
|
|
11170
11664
|
* @returns void
|
|
11171
11665
|
*/
|
|
11172
11666
|
_insertFixup(z) {
|
|
11173
|
-
|
|
11174
|
-
|
|
11175
|
-
|
|
11176
|
-
|
|
11667
|
+
const leftRotate = this._leftRotate.bind(this);
|
|
11668
|
+
const rightRotate = this._rightRotate.bind(this);
|
|
11669
|
+
while (z) {
|
|
11670
|
+
const p = z.parent;
|
|
11671
|
+
if (!p || p.color !== "RED") break;
|
|
11672
|
+
const gp = p.parent;
|
|
11673
|
+
if (!gp) break;
|
|
11674
|
+
if (p === gp.left) {
|
|
11675
|
+
const y = gp.right;
|
|
11177
11676
|
if ((y == null ? void 0 : y.color) === "RED") {
|
|
11178
|
-
|
|
11677
|
+
p.color = "BLACK";
|
|
11179
11678
|
y.color = "BLACK";
|
|
11180
|
-
|
|
11181
|
-
z =
|
|
11182
|
-
|
|
11183
|
-
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
|
|
11187
|
-
|
|
11188
|
-
|
|
11189
|
-
|
|
11190
|
-
|
|
11191
|
-
|
|
11679
|
+
gp.color = "RED";
|
|
11680
|
+
z = gp;
|
|
11681
|
+
continue;
|
|
11682
|
+
}
|
|
11683
|
+
if (z === p.right) {
|
|
11684
|
+
z = p;
|
|
11685
|
+
leftRotate(z);
|
|
11686
|
+
}
|
|
11687
|
+
const p2 = z == null ? void 0 : z.parent;
|
|
11688
|
+
const gp2 = p2 == null ? void 0 : p2.parent;
|
|
11689
|
+
if (p2 && gp2) {
|
|
11690
|
+
p2.color = "BLACK";
|
|
11691
|
+
gp2.color = "RED";
|
|
11692
|
+
rightRotate(gp2);
|
|
11192
11693
|
}
|
|
11193
11694
|
} else {
|
|
11194
|
-
const y =
|
|
11695
|
+
const y = gp.left;
|
|
11195
11696
|
if ((y == null ? void 0 : y.color) === "RED") {
|
|
11196
|
-
|
|
11697
|
+
p.color = "BLACK";
|
|
11197
11698
|
y.color = "BLACK";
|
|
11198
|
-
|
|
11199
|
-
z =
|
|
11200
|
-
|
|
11201
|
-
|
|
11202
|
-
|
|
11203
|
-
|
|
11204
|
-
|
|
11205
|
-
|
|
11206
|
-
|
|
11207
|
-
|
|
11208
|
-
|
|
11209
|
-
|
|
11699
|
+
gp.color = "RED";
|
|
11700
|
+
z = gp;
|
|
11701
|
+
continue;
|
|
11702
|
+
}
|
|
11703
|
+
if (z === p.left) {
|
|
11704
|
+
z = p;
|
|
11705
|
+
rightRotate(z);
|
|
11706
|
+
}
|
|
11707
|
+
const p2 = z == null ? void 0 : z.parent;
|
|
11708
|
+
const gp2 = p2 == null ? void 0 : p2.parent;
|
|
11709
|
+
if (p2 && gp2) {
|
|
11710
|
+
p2.color = "BLACK";
|
|
11711
|
+
gp2.color = "RED";
|
|
11712
|
+
leftRotate(gp2);
|
|
11210
11713
|
}
|
|
11211
11714
|
}
|
|
11715
|
+
break;
|
|
11212
11716
|
}
|
|
11213
11717
|
if (this.isRealNode(this._root)) this._root.color = "BLACK";
|
|
11214
11718
|
}
|
|
11215
11719
|
/**
|
|
11216
11720
|
* (Protected) Restore red-black properties after deletion (recolor/rotate).
|
|
11217
|
-
* @remarks Time O(log n), Space O(1)
|
|
11721
|
+
* @remarks Time O(log n) average, Space O(1)
|
|
11218
11722
|
* @param node - Child that replaced the deleted node (may be undefined).
|
|
11219
11723
|
* @returns void
|
|
11220
11724
|
*/
|
|
@@ -11475,7 +11979,7 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
11475
11979
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11476
11980
|
super([], { ...options, isMapMode: true });
|
|
11477
11981
|
if (keysNodesEntriesOrRaws) {
|
|
11478
|
-
this.
|
|
11982
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
11479
11983
|
}
|
|
11480
11984
|
}
|
|
11481
11985
|
createNode(key, value = []) {
|
|
@@ -11495,27 +11999,27 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
11495
11999
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
11496
12000
|
* @remarks Time O(log N + M), Space O(1)
|
|
11497
12001
|
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
|
|
11498
|
-
* @param [value] - Single value to
|
|
12002
|
+
* @param [value] - Single value to set when a bare key is provided.
|
|
11499
12003
|
* @returns True if inserted or appended; false if ignored.
|
|
11500
12004
|
*/
|
|
11501
|
-
|
|
11502
|
-
if (this.isRealNode(keyNodeOrEntry)) return super.
|
|
12005
|
+
set(keyNodeOrEntry, value) {
|
|
12006
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
|
|
11503
12007
|
const _commonAdd = /* @__PURE__ */ __name((key, values) => {
|
|
11504
12008
|
if (key === void 0 || key === null) return false;
|
|
11505
|
-
const
|
|
12009
|
+
const _setToValues = /* @__PURE__ */ __name(() => {
|
|
11506
12010
|
const existingValues = this.get(key);
|
|
11507
12011
|
if (existingValues !== void 0 && values !== void 0) {
|
|
11508
12012
|
for (const value2 of values) existingValues.push(value2);
|
|
11509
12013
|
return true;
|
|
11510
12014
|
}
|
|
11511
12015
|
return false;
|
|
11512
|
-
}, "
|
|
11513
|
-
const
|
|
12016
|
+
}, "_setToValues");
|
|
12017
|
+
const _setByNode = /* @__PURE__ */ __name(() => {
|
|
11514
12018
|
const existingNode = this.getNode(key);
|
|
11515
12019
|
if (this.isRealNode(existingNode)) {
|
|
11516
12020
|
const existingValues = this.get(existingNode);
|
|
11517
12021
|
if (existingValues === void 0) {
|
|
11518
|
-
super.
|
|
12022
|
+
super.set(key, values);
|
|
11519
12023
|
return true;
|
|
11520
12024
|
}
|
|
11521
12025
|
if (values !== void 0) {
|
|
@@ -11525,13 +12029,13 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
11525
12029
|
return false;
|
|
11526
12030
|
}
|
|
11527
12031
|
} else {
|
|
11528
|
-
return super.
|
|
12032
|
+
return super.set(key, values);
|
|
11529
12033
|
}
|
|
11530
|
-
}, "
|
|
12034
|
+
}, "_setByNode");
|
|
11531
12035
|
if (this._isMapMode) {
|
|
11532
|
-
return
|
|
12036
|
+
return _setByNode() || _setToValues();
|
|
11533
12037
|
}
|
|
11534
|
-
return
|
|
12038
|
+
return _setToValues() || _setByNode();
|
|
11535
12039
|
}, "_commonAdd");
|
|
11536
12040
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11537
12041
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -11599,7 +12103,7 @@ var _AVLTreeMultiMap = class _AVLTreeMultiMap extends AVLTree {
|
|
|
11599
12103
|
map(callback, options, thisArg) {
|
|
11600
12104
|
const out = this._createLike([], options);
|
|
11601
12105
|
let i = 0;
|
|
11602
|
-
for (const [k, v] of this) out.
|
|
12106
|
+
for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
|
|
11603
12107
|
return out;
|
|
11604
12108
|
}
|
|
11605
12109
|
/**
|
|
@@ -11782,7 +12286,7 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
11782
12286
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11783
12287
|
super([], { ...options });
|
|
11784
12288
|
if (keysNodesEntriesOrRaws) {
|
|
11785
|
-
this.
|
|
12289
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
11786
12290
|
}
|
|
11787
12291
|
}
|
|
11788
12292
|
createNode(key, value = []) {
|
|
@@ -11802,27 +12306,27 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
11802
12306
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
11803
12307
|
* @remarks Time O(log N + M), Space O(1)
|
|
11804
12308
|
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
|
|
11805
|
-
* @param [value] - Single value to
|
|
12309
|
+
* @param [value] - Single value to set when a bare key is provided.
|
|
11806
12310
|
* @returns True if inserted or appended; false if ignored.
|
|
11807
12311
|
*/
|
|
11808
|
-
|
|
11809
|
-
if (this.isRealNode(keyNodeOrEntry)) return super.
|
|
12312
|
+
set(keyNodeOrEntry, value) {
|
|
12313
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
|
|
11810
12314
|
const _commonAdd = /* @__PURE__ */ __name((key, values) => {
|
|
11811
12315
|
if (key === void 0 || key === null) return false;
|
|
11812
|
-
const
|
|
12316
|
+
const _setToValues = /* @__PURE__ */ __name(() => {
|
|
11813
12317
|
const existingValues = this.get(key);
|
|
11814
12318
|
if (existingValues !== void 0 && values !== void 0) {
|
|
11815
12319
|
for (const value2 of values) existingValues.push(value2);
|
|
11816
12320
|
return true;
|
|
11817
12321
|
}
|
|
11818
12322
|
return false;
|
|
11819
|
-
}, "
|
|
11820
|
-
const
|
|
12323
|
+
}, "_setToValues");
|
|
12324
|
+
const _setByNode = /* @__PURE__ */ __name(() => {
|
|
11821
12325
|
const existingNode = this.getNode(key);
|
|
11822
12326
|
if (this.isRealNode(existingNode)) {
|
|
11823
12327
|
const existingValues = this.get(existingNode);
|
|
11824
12328
|
if (existingValues === void 0) {
|
|
11825
|
-
super.
|
|
12329
|
+
super.set(key, values);
|
|
11826
12330
|
return true;
|
|
11827
12331
|
}
|
|
11828
12332
|
if (values !== void 0) {
|
|
@@ -11832,13 +12336,13 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
11832
12336
|
return false;
|
|
11833
12337
|
}
|
|
11834
12338
|
} else {
|
|
11835
|
-
return super.
|
|
12339
|
+
return super.set(key, values);
|
|
11836
12340
|
}
|
|
11837
|
-
}, "
|
|
12341
|
+
}, "_setByNode");
|
|
11838
12342
|
if (this._isMapMode) {
|
|
11839
|
-
return
|
|
12343
|
+
return _setByNode() || _setToValues();
|
|
11840
12344
|
}
|
|
11841
|
-
return
|
|
12345
|
+
return _setToValues() || _setByNode();
|
|
11842
12346
|
}, "_commonAdd");
|
|
11843
12347
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11844
12348
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -11878,7 +12382,7 @@ var _TreeMultiMap = class _TreeMultiMap extends RedBlackTree {
|
|
|
11878
12382
|
map(callback, options, thisArg) {
|
|
11879
12383
|
const out = this._createLike([], options);
|
|
11880
12384
|
let i = 0;
|
|
11881
|
-
for (const [k, v] of this) out.
|
|
12385
|
+
for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
|
|
11882
12386
|
return out;
|
|
11883
12387
|
}
|
|
11884
12388
|
/**
|
|
@@ -12064,7 +12568,7 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
12064
12568
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
12065
12569
|
super([], options);
|
|
12066
12570
|
__publicField(this, "_count", 0);
|
|
12067
|
-
if (keysNodesEntriesOrRaws) this.
|
|
12571
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
12068
12572
|
}
|
|
12069
12573
|
/**
|
|
12070
12574
|
* Get the total aggregate count across all nodes.
|
|
@@ -12103,10 +12607,10 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
12103
12607
|
* @param [count] - How much to increase the node's count (default 1).
|
|
12104
12608
|
* @returns True if inserted/updated; false if ignored.
|
|
12105
12609
|
*/
|
|
12106
|
-
|
|
12610
|
+
set(keyNodeOrEntry, value, count = 1) {
|
|
12107
12611
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
12108
12612
|
const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
12109
|
-
const isSuccessAdded = super.
|
|
12613
|
+
const isSuccessAdded = super.set(newNode, newValue);
|
|
12110
12614
|
if (isSuccessAdded) {
|
|
12111
12615
|
this._count += orgCount;
|
|
12112
12616
|
return true;
|
|
@@ -12117,16 +12621,16 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
12117
12621
|
/**
|
|
12118
12622
|
* Delete a node (or decrement its count) and rebalance if needed.
|
|
12119
12623
|
* @remarks Time O(log N), Space O(1)
|
|
12120
|
-
* @param
|
|
12624
|
+
* @param keyNodeEntryRawOrPredicate - Key, node, or [key, value] entry identifying the node.
|
|
12121
12625
|
* @param [ignoreCount] - If true, remove the node regardless of its count.
|
|
12122
12626
|
* @returns Array of deletion results including deleted node and a rebalance hint when present.
|
|
12123
12627
|
*/
|
|
12124
|
-
delete(
|
|
12125
|
-
if (
|
|
12628
|
+
delete(keyNodeEntryRawOrPredicate, ignoreCount = false) {
|
|
12629
|
+
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
12126
12630
|
const results = [];
|
|
12127
12631
|
let nodeToDelete;
|
|
12128
|
-
if (this._isPredicate(
|
|
12129
|
-
else nodeToDelete = this.isRealNode(
|
|
12632
|
+
if (this._isPredicate(keyNodeEntryRawOrPredicate)) nodeToDelete = this.getNode(keyNodeEntryRawOrPredicate);
|
|
12633
|
+
else nodeToDelete = this.isRealNode(keyNodeEntryRawOrPredicate) ? keyNodeEntryRawOrPredicate : this.getNode(keyNodeEntryRawOrPredicate);
|
|
12130
12634
|
if (!nodeToDelete) {
|
|
12131
12635
|
return results;
|
|
12132
12636
|
}
|
|
@@ -12169,7 +12673,6 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
12169
12673
|
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
12170
12674
|
if (successor.right !== null) {
|
|
12171
12675
|
this._transplant(successor, successor.right);
|
|
12172
|
-
this._count -= nodeToDelete.count;
|
|
12173
12676
|
}
|
|
12174
12677
|
} else {
|
|
12175
12678
|
nodeToDelete.count--;
|
|
@@ -12259,7 +12762,7 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
12259
12762
|
const out = this._createLike([], options);
|
|
12260
12763
|
let index = 0;
|
|
12261
12764
|
for (const [key, value] of this) {
|
|
12262
|
-
out.
|
|
12765
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
12263
12766
|
}
|
|
12264
12767
|
return out;
|
|
12265
12768
|
}
|
|
@@ -12272,6 +12775,11 @@ var _TreeCounter = class _TreeCounter extends RedBlackTree {
|
|
|
12272
12775
|
const out = this._createInstance();
|
|
12273
12776
|
this._clone(out);
|
|
12274
12777
|
out._count = this._count;
|
|
12778
|
+
for (const node of this.dfs((n) => n, "IN")) {
|
|
12779
|
+
if (!node) continue;
|
|
12780
|
+
const outNode = out.getNode(node.key);
|
|
12781
|
+
if (outNode) outNode.count = node.count;
|
|
12782
|
+
}
|
|
12275
12783
|
return out;
|
|
12276
12784
|
}
|
|
12277
12785
|
/**
|
|
@@ -12514,7 +13022,7 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
12514
13022
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
12515
13023
|
super([], options);
|
|
12516
13024
|
__publicField(this, "_count", 0);
|
|
12517
|
-
if (keysNodesEntriesOrRaws) this.
|
|
13025
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
12518
13026
|
}
|
|
12519
13027
|
get count() {
|
|
12520
13028
|
return this._count;
|
|
@@ -12548,11 +13056,11 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
12548
13056
|
* @param [count] - How much to increase the node's count (default 1).
|
|
12549
13057
|
* @returns True if inserted/updated; false if ignored.
|
|
12550
13058
|
*/
|
|
12551
|
-
|
|
13059
|
+
set(keyNodeOrEntry, value, count = 1) {
|
|
12552
13060
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
12553
13061
|
if (newNode === void 0) return false;
|
|
12554
13062
|
const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
|
|
12555
|
-
const inserted = super.
|
|
13063
|
+
const inserted = super.set(newNode, newValue);
|
|
12556
13064
|
if (inserted) {
|
|
12557
13065
|
this._count += orgNodeCount;
|
|
12558
13066
|
}
|
|
@@ -12661,9 +13169,9 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
12661
13169
|
clone() {
|
|
12662
13170
|
const out = this._createInstance();
|
|
12663
13171
|
if (this._isMapMode) {
|
|
12664
|
-
this.bfs((node) => out.
|
|
13172
|
+
this.bfs((node) => out.set(node.key, void 0, node.count));
|
|
12665
13173
|
} else {
|
|
12666
|
-
this.bfs((node) => out.
|
|
13174
|
+
this.bfs((node) => out.set(node.key, node.value, node.count));
|
|
12667
13175
|
}
|
|
12668
13176
|
if (this._isMapMode) out._store = this._store;
|
|
12669
13177
|
return out;
|
|
@@ -12683,7 +13191,7 @@ var _AVLTreeCounter = class _AVLTreeCounter extends AVLTree {
|
|
|
12683
13191
|
const out = this._createLike([], options);
|
|
12684
13192
|
let index = 0;
|
|
12685
13193
|
for (const [key, value] of this) {
|
|
12686
|
-
out.
|
|
13194
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
12687
13195
|
}
|
|
12688
13196
|
return out;
|
|
12689
13197
|
}
|