data-structure-typed 2.2.6 → 2.2.8
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/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +47 -1
- package/README.md +19 -8
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +109 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +109 -107
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +109 -107
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +109 -107
- 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/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 +22 -23
- package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +105 -103
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +48 -171
- 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 +53 -55
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- 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.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/tsup.config.js +50 -21
- package/tsup.leetcode.config.js +1 -1
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
package/dist/esm/index.mjs
CHANGED
|
@@ -179,6 +179,7 @@ var IterableEntryBase = class {
|
|
|
179
179
|
* @remarks Time O(n), Space O(n)
|
|
180
180
|
*/
|
|
181
181
|
print() {
|
|
182
|
+
console.log(this.toVisual());
|
|
182
183
|
}
|
|
183
184
|
};
|
|
184
185
|
|
|
@@ -401,6 +402,7 @@ var IterableElementBase = class {
|
|
|
401
402
|
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
402
403
|
*/
|
|
403
404
|
print() {
|
|
405
|
+
console.log(this.toVisual());
|
|
404
406
|
}
|
|
405
407
|
};
|
|
406
408
|
|
|
@@ -6991,9 +6993,9 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
6991
6993
|
iterationType = "ITERATIVE";
|
|
6992
6994
|
/**
|
|
6993
6995
|
* Creates an instance of BinaryTree.
|
|
6994
|
-
* @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) `
|
|
6996
|
+
* @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.
|
|
6995
6997
|
*
|
|
6996
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
6998
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
6997
6999
|
* @param [options] - Configuration options for the tree.
|
|
6998
7000
|
*/
|
|
6999
7001
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -7006,7 +7008,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
7006
7008
|
if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
|
|
7007
7009
|
else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
|
|
7008
7010
|
}
|
|
7009
|
-
if (keysNodesEntriesOrRaws) this.
|
|
7011
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
7010
7012
|
}
|
|
7011
7013
|
_isMapMode = true;
|
|
7012
7014
|
/**
|
|
@@ -7220,10 +7222,20 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
7220
7222
|
* @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).
|
|
7221
7223
|
*
|
|
7222
7224
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
7225
|
+
* @returns True if the addition was successful, false otherwise.
|
|
7226
|
+
*/
|
|
7227
|
+
add(keyNodeOrEntry) {
|
|
7228
|
+
return this.set(keyNodeOrEntry);
|
|
7229
|
+
}
|
|
7230
|
+
/**
|
|
7231
|
+
* Adds or updates a new node to the tree.
|
|
7232
|
+
* @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).
|
|
7233
|
+
*
|
|
7234
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
7223
7235
|
* @param [value] - The value, if providing just a key.
|
|
7224
7236
|
* @returns True if the addition was successful, false otherwise.
|
|
7225
7237
|
*/
|
|
7226
|
-
|
|
7238
|
+
set(keyNodeOrEntry, value) {
|
|
7227
7239
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
7228
7240
|
if (newNode === void 0) return false;
|
|
7229
7241
|
if (!this._root) {
|
|
@@ -7267,25 +7279,25 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
7267
7279
|
return false;
|
|
7268
7280
|
}
|
|
7269
7281
|
/**
|
|
7270
|
-
* Adds
|
|
7271
|
-
* @remarks Time O(
|
|
7282
|
+
* Adds multiple items to the tree.
|
|
7283
|
+
* @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
7284
|
*
|
|
7273
|
-
* @param
|
|
7274
|
-
* @param [
|
|
7275
|
-
* @returns
|
|
7285
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
7286
|
+
* @param [values] - An optional parallel iterable of values.
|
|
7287
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
7276
7288
|
*/
|
|
7277
|
-
|
|
7278
|
-
return this.
|
|
7289
|
+
addMany(keysNodesEntriesOrRaws) {
|
|
7290
|
+
return this.setMany(keysNodesEntriesOrRaws);
|
|
7279
7291
|
}
|
|
7280
7292
|
/**
|
|
7281
|
-
* Adds multiple items to the tree.
|
|
7282
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
7293
|
+
* Adds or updates multiple items to the tree.
|
|
7294
|
+
* @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).
|
|
7283
7295
|
*
|
|
7284
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
7296
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
7285
7297
|
* @param [values] - An optional parallel iterable of values.
|
|
7286
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
7298
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
7287
7299
|
*/
|
|
7288
|
-
|
|
7300
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
7289
7301
|
const inserted = [];
|
|
7290
7302
|
let valuesIterator;
|
|
7291
7303
|
if (values) {
|
|
@@ -7300,40 +7312,29 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
7300
7312
|
}
|
|
7301
7313
|
}
|
|
7302
7314
|
if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
|
|
7303
|
-
inserted.push(this.
|
|
7315
|
+
inserted.push(this.set(keyNodeEntryOrRaw, value));
|
|
7304
7316
|
}
|
|
7305
7317
|
return inserted;
|
|
7306
7318
|
}
|
|
7307
7319
|
/**
|
|
7308
|
-
*
|
|
7309
|
-
* @remarks Time O(N * M), where N is the
|
|
7310
|
-
*
|
|
7311
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
7312
|
-
* @param [values] - An optional parallel iterable of values.
|
|
7313
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
7314
|
-
*/
|
|
7315
|
-
setMany(keysNodesEntriesOrRaws, values) {
|
|
7316
|
-
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
7317
|
-
}
|
|
7318
|
-
/**
|
|
7319
|
-
* Merges another tree into this one by adding all its nodes.
|
|
7320
|
-
* @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`).
|
|
7320
|
+
* Merges another tree into this one by seting all its nodes.
|
|
7321
|
+
* @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`).
|
|
7321
7322
|
*
|
|
7322
7323
|
* @param anotherTree - The tree to merge.
|
|
7323
7324
|
*/
|
|
7324
7325
|
merge(anotherTree) {
|
|
7325
|
-
this.
|
|
7326
|
+
this.setMany(anotherTree, []);
|
|
7326
7327
|
}
|
|
7327
7328
|
/**
|
|
7328
7329
|
* Clears the tree and refills it with new items.
|
|
7329
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
7330
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
7330
7331
|
*
|
|
7331
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
7332
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
7332
7333
|
* @param [values] - An optional parallel iterable of values.
|
|
7333
7334
|
*/
|
|
7334
7335
|
refill(keysNodesEntriesOrRaws, values) {
|
|
7335
7336
|
this.clear();
|
|
7336
|
-
this.
|
|
7337
|
+
this.setMany(keysNodesEntriesOrRaws, values);
|
|
7337
7338
|
}
|
|
7338
7339
|
/**
|
|
7339
7340
|
* Deletes a node from the tree.
|
|
@@ -7998,7 +7999,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
7998
7999
|
}
|
|
7999
8000
|
/**
|
|
8000
8001
|
* Clones the tree.
|
|
8001
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
8002
|
+
* @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.
|
|
8002
8003
|
*
|
|
8003
8004
|
* @returns A new, cloned instance of the tree.
|
|
8004
8005
|
*/
|
|
@@ -8009,7 +8010,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
8009
8010
|
}
|
|
8010
8011
|
/**
|
|
8011
8012
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
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) `
|
|
8013
|
+
* @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.
|
|
8013
8014
|
*
|
|
8014
8015
|
* @param predicate - A function to test each [key, value] pair.
|
|
8015
8016
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -8018,7 +8019,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
8018
8019
|
filter(predicate, thisArg) {
|
|
8019
8020
|
const out = this._createInstance();
|
|
8020
8021
|
let i = 0;
|
|
8021
|
-
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.
|
|
8022
|
+
for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
|
|
8022
8023
|
return out;
|
|
8023
8024
|
}
|
|
8024
8025
|
/**
|
|
@@ -8036,7 +8037,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
8036
8037
|
map(cb, options, thisArg) {
|
|
8037
8038
|
const out = this._createLike([], options);
|
|
8038
8039
|
let i = 0;
|
|
8039
|
-
for (const [k, v] of this) out.
|
|
8040
|
+
for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
|
|
8040
8041
|
return out;
|
|
8041
8042
|
}
|
|
8042
8043
|
/**
|
|
@@ -8077,6 +8078,7 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
8077
8078
|
* @param [startNode=this._root] - The node to start printing from.
|
|
8078
8079
|
*/
|
|
8079
8080
|
print(options, startNode = this._root) {
|
|
8081
|
+
console.log(this.toVisual(startNode, options));
|
|
8080
8082
|
}
|
|
8081
8083
|
/**
|
|
8082
8084
|
* (Protected) Core DFS implementation.
|
|
@@ -8287,18 +8289,18 @@ var BinaryTree = class extends IterableEntryBase {
|
|
|
8287
8289
|
return [this.createNode(keyNodeOrEntry, value), value];
|
|
8288
8290
|
}
|
|
8289
8291
|
/**
|
|
8290
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
8291
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
8292
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
8293
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
8292
8294
|
*
|
|
8293
8295
|
* @param cloned - The new, empty tree instance to populate.
|
|
8294
8296
|
*/
|
|
8295
8297
|
_clone(cloned) {
|
|
8296
8298
|
this.bfs(
|
|
8297
8299
|
(node) => {
|
|
8298
|
-
if (node === null) cloned.
|
|
8300
|
+
if (node === null) cloned.set(null);
|
|
8299
8301
|
else {
|
|
8300
|
-
if (this._isMapMode) cloned.
|
|
8301
|
-
else cloned.
|
|
8302
|
+
if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
|
|
8303
|
+
else cloned.set([node.key, node.value]);
|
|
8302
8304
|
}
|
|
8303
8305
|
},
|
|
8304
8306
|
this._root,
|
|
@@ -8631,7 +8633,7 @@ var BST = class extends BinaryTree {
|
|
|
8631
8633
|
* Creates an instance of BST.
|
|
8632
8634
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
8633
8635
|
*
|
|
8634
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
8636
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
8635
8637
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
8636
8638
|
*/
|
|
8637
8639
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
@@ -8645,7 +8647,7 @@ var BST = class extends BinaryTree {
|
|
|
8645
8647
|
} else {
|
|
8646
8648
|
this._comparator = this._createDefaultComparator();
|
|
8647
8649
|
}
|
|
8648
|
-
if (keysNodesEntriesOrRaws) this.
|
|
8650
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
8649
8651
|
}
|
|
8650
8652
|
_root = void 0;
|
|
8651
8653
|
/**
|
|
@@ -8861,11 +8863,11 @@ var BST = class extends BinaryTree {
|
|
|
8861
8863
|
* Adds a new node to the BST based on key comparison.
|
|
8862
8864
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
8863
8865
|
*
|
|
8864
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
8866
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
8865
8867
|
* @param [value] - The value, if providing just a key.
|
|
8866
8868
|
* @returns True if the addition was successful, false otherwise.
|
|
8867
8869
|
*/
|
|
8868
|
-
|
|
8870
|
+
set(keyNodeOrEntry, value) {
|
|
8869
8871
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
8870
8872
|
if (newNode === void 0) return false;
|
|
8871
8873
|
if (this._root === void 0) {
|
|
@@ -8902,24 +8904,24 @@ var BST = class extends BinaryTree {
|
|
|
8902
8904
|
}
|
|
8903
8905
|
/**
|
|
8904
8906
|
* Adds multiple items to the tree.
|
|
8905
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced
|
|
8907
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
8906
8908
|
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
8907
8909
|
* Space O(N) for sorting and recursion/iteration stack.
|
|
8908
8910
|
*
|
|
8909
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
8911
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
8910
8912
|
* @param [values] - An optional parallel iterable of values.
|
|
8911
8913
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
8912
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced
|
|
8913
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
8914
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
8915
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
8914
8916
|
*/
|
|
8915
|
-
|
|
8917
|
+
setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
8916
8918
|
const inserted = [];
|
|
8917
8919
|
const valuesIterator = values?.[Symbol.iterator]();
|
|
8918
8920
|
if (!isBalanceAdd) {
|
|
8919
8921
|
for (let kve of keysNodesEntriesOrRaws) {
|
|
8920
8922
|
const val = valuesIterator?.next().value;
|
|
8921
8923
|
if (this.isRaw(kve)) kve = this._toEntryFn(kve);
|
|
8922
|
-
inserted.push(this.
|
|
8924
|
+
inserted.push(this.set(kve, val));
|
|
8923
8925
|
}
|
|
8924
8926
|
return inserted;
|
|
8925
8927
|
}
|
|
@@ -8947,9 +8949,9 @@ var BST = class extends BinaryTree {
|
|
|
8947
8949
|
const { key, value, orgIndex } = arr[mid];
|
|
8948
8950
|
if (this.isRaw(key)) {
|
|
8949
8951
|
const entry = this._toEntryFn(key);
|
|
8950
|
-
inserted[orgIndex] = this.
|
|
8952
|
+
inserted[orgIndex] = this.set(entry);
|
|
8951
8953
|
} else {
|
|
8952
|
-
inserted[orgIndex] = this.
|
|
8954
|
+
inserted[orgIndex] = this.set(key, value);
|
|
8953
8955
|
}
|
|
8954
8956
|
_dfs(arr.slice(0, mid));
|
|
8955
8957
|
_dfs(arr.slice(mid + 1));
|
|
@@ -8966,9 +8968,9 @@ var BST = class extends BinaryTree {
|
|
|
8966
8968
|
const { key, value, orgIndex } = sorted[m];
|
|
8967
8969
|
if (this.isRaw(key)) {
|
|
8968
8970
|
const entry = this._toEntryFn(key);
|
|
8969
|
-
inserted[orgIndex] = this.
|
|
8971
|
+
inserted[orgIndex] = this.set(entry);
|
|
8970
8972
|
} else {
|
|
8971
|
-
inserted[orgIndex] = this.
|
|
8973
|
+
inserted[orgIndex] = this.set(key, value);
|
|
8972
8974
|
}
|
|
8973
8975
|
stack.push([m + 1, r]);
|
|
8974
8976
|
stack.push([l, m - 1]);
|
|
@@ -9243,7 +9245,7 @@ var BST = class extends BinaryTree {
|
|
|
9243
9245
|
const out = this._createLike([], options);
|
|
9244
9246
|
let index = 0;
|
|
9245
9247
|
for (const [key, value] of this) {
|
|
9246
|
-
out.
|
|
9248
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
9247
9249
|
}
|
|
9248
9250
|
return out;
|
|
9249
9251
|
}
|
|
@@ -10430,14 +10432,14 @@ var AVLTree = class extends BST {
|
|
|
10430
10432
|
}
|
|
10431
10433
|
/**
|
|
10432
10434
|
* Creates an instance of AVLTree.
|
|
10433
|
-
* @remarks Time O(N log N) (from `
|
|
10435
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
10434
10436
|
*
|
|
10435
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
10437
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
10436
10438
|
* @param [options] - Configuration options for the AVL tree.
|
|
10437
10439
|
*/
|
|
10438
10440
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
10439
10441
|
super([], options);
|
|
10440
|
-
if (keysNodesEntriesOrRaws) super.
|
|
10442
|
+
if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
|
|
10441
10443
|
}
|
|
10442
10444
|
/**
|
|
10443
10445
|
* (Protected) Creates a new AVL tree node.
|
|
@@ -10461,16 +10463,16 @@ var AVLTree = class extends BST {
|
|
|
10461
10463
|
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
10462
10464
|
}
|
|
10463
10465
|
/**
|
|
10464
|
-
*
|
|
10465
|
-
* @remarks Time O(log N) (O(H) for BST
|
|
10466
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
10467
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
10466
10468
|
*
|
|
10467
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
10469
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
10468
10470
|
* @param [value] - The value, if providing just a key.
|
|
10469
10471
|
* @returns True if the addition was successful, false otherwise.
|
|
10470
10472
|
*/
|
|
10471
|
-
|
|
10473
|
+
set(keyNodeOrEntry, value) {
|
|
10472
10474
|
if (keyNodeOrEntry === null) return false;
|
|
10473
|
-
const inserted = super.
|
|
10475
|
+
const inserted = super.set(keyNodeOrEntry, value);
|
|
10474
10476
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
10475
10477
|
return inserted;
|
|
10476
10478
|
}
|
|
@@ -10522,7 +10524,7 @@ var AVLTree = class extends BST {
|
|
|
10522
10524
|
}
|
|
10523
10525
|
/**
|
|
10524
10526
|
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
10525
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `
|
|
10527
|
+
* @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.
|
|
10526
10528
|
*
|
|
10527
10529
|
* @template MK - New key type.
|
|
10528
10530
|
* @template MV - New value type.
|
|
@@ -10536,7 +10538,7 @@ var AVLTree = class extends BST {
|
|
|
10536
10538
|
const out = this._createLike([], options);
|
|
10537
10539
|
let index = 0;
|
|
10538
10540
|
for (const [key, value] of this) {
|
|
10539
|
-
out.
|
|
10541
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
10540
10542
|
}
|
|
10541
10543
|
return out;
|
|
10542
10544
|
}
|
|
@@ -10962,7 +10964,7 @@ var RedBlackTree = class extends BST {
|
|
|
10962
10964
|
super([], options);
|
|
10963
10965
|
this._root = this.NIL;
|
|
10964
10966
|
if (keysNodesEntriesOrRaws) {
|
|
10965
|
-
this.
|
|
10967
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
10966
10968
|
}
|
|
10967
10969
|
}
|
|
10968
10970
|
_root;
|
|
@@ -11010,7 +11012,7 @@ var RedBlackTree = class extends BST {
|
|
|
11010
11012
|
* @param [value]- See parameter type for details.
|
|
11011
11013
|
* @returns True if inserted or updated; false if ignored.
|
|
11012
11014
|
*/
|
|
11013
|
-
|
|
11015
|
+
set(keyNodeOrEntry, value) {
|
|
11014
11016
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
11015
11017
|
if (!this.isRealNode(newNode)) return false;
|
|
11016
11018
|
const insertStatus = this._insert(newNode);
|
|
@@ -11104,7 +11106,7 @@ var RedBlackTree = class extends BST {
|
|
|
11104
11106
|
const out = this._createLike([], options);
|
|
11105
11107
|
let index = 0;
|
|
11106
11108
|
for (const [key, value] of this) {
|
|
11107
|
-
out.
|
|
11109
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
11108
11110
|
}
|
|
11109
11111
|
return out;
|
|
11110
11112
|
}
|
|
@@ -11492,7 +11494,7 @@ var AVLTreeMultiMap = class extends AVLTree {
|
|
|
11492
11494
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11493
11495
|
super([], { ...options, isMapMode: true });
|
|
11494
11496
|
if (keysNodesEntriesOrRaws) {
|
|
11495
|
-
this.
|
|
11497
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
11496
11498
|
}
|
|
11497
11499
|
}
|
|
11498
11500
|
createNode(key, value = []) {
|
|
@@ -11512,27 +11514,27 @@ var AVLTreeMultiMap = class extends AVLTree {
|
|
|
11512
11514
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
11513
11515
|
* @remarks Time O(log N + M), Space O(1)
|
|
11514
11516
|
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
|
|
11515
|
-
* @param [value] - Single value to
|
|
11517
|
+
* @param [value] - Single value to set when a bare key is provided.
|
|
11516
11518
|
* @returns True if inserted or appended; false if ignored.
|
|
11517
11519
|
*/
|
|
11518
|
-
|
|
11519
|
-
if (this.isRealNode(keyNodeOrEntry)) return super.
|
|
11520
|
+
set(keyNodeOrEntry, value) {
|
|
11521
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
|
|
11520
11522
|
const _commonAdd = /* @__PURE__ */ __name((key, values) => {
|
|
11521
11523
|
if (key === void 0 || key === null) return false;
|
|
11522
|
-
const
|
|
11524
|
+
const _setToValues = /* @__PURE__ */ __name(() => {
|
|
11523
11525
|
const existingValues = this.get(key);
|
|
11524
11526
|
if (existingValues !== void 0 && values !== void 0) {
|
|
11525
11527
|
for (const value2 of values) existingValues.push(value2);
|
|
11526
11528
|
return true;
|
|
11527
11529
|
}
|
|
11528
11530
|
return false;
|
|
11529
|
-
}, "
|
|
11530
|
-
const
|
|
11531
|
+
}, "_setToValues");
|
|
11532
|
+
const _setByNode = /* @__PURE__ */ __name(() => {
|
|
11531
11533
|
const existingNode = this.getNode(key);
|
|
11532
11534
|
if (this.isRealNode(existingNode)) {
|
|
11533
11535
|
const existingValues = this.get(existingNode);
|
|
11534
11536
|
if (existingValues === void 0) {
|
|
11535
|
-
super.
|
|
11537
|
+
super.set(key, values);
|
|
11536
11538
|
return true;
|
|
11537
11539
|
}
|
|
11538
11540
|
if (values !== void 0) {
|
|
@@ -11542,13 +11544,13 @@ var AVLTreeMultiMap = class extends AVLTree {
|
|
|
11542
11544
|
return false;
|
|
11543
11545
|
}
|
|
11544
11546
|
} else {
|
|
11545
|
-
return super.
|
|
11547
|
+
return super.set(key, values);
|
|
11546
11548
|
}
|
|
11547
|
-
}, "
|
|
11549
|
+
}, "_setByNode");
|
|
11548
11550
|
if (this._isMapMode) {
|
|
11549
|
-
return
|
|
11551
|
+
return _setByNode() || _setToValues();
|
|
11550
11552
|
}
|
|
11551
|
-
return
|
|
11553
|
+
return _setToValues() || _setByNode();
|
|
11552
11554
|
}, "_commonAdd");
|
|
11553
11555
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11554
11556
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -11616,7 +11618,7 @@ var AVLTreeMultiMap = class extends AVLTree {
|
|
|
11616
11618
|
map(callback, options, thisArg) {
|
|
11617
11619
|
const out = this._createLike([], options);
|
|
11618
11620
|
let i = 0;
|
|
11619
|
-
for (const [k, v] of this) out.
|
|
11621
|
+
for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
|
|
11620
11622
|
return out;
|
|
11621
11623
|
}
|
|
11622
11624
|
/**
|
|
@@ -11799,7 +11801,7 @@ var TreeMultiMap = class extends RedBlackTree {
|
|
|
11799
11801
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11800
11802
|
super([], { ...options });
|
|
11801
11803
|
if (keysNodesEntriesOrRaws) {
|
|
11802
|
-
this.
|
|
11804
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
11803
11805
|
}
|
|
11804
11806
|
}
|
|
11805
11807
|
createNode(key, value = []) {
|
|
@@ -11819,27 +11821,27 @@ var TreeMultiMap = class extends RedBlackTree {
|
|
|
11819
11821
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
11820
11822
|
* @remarks Time O(log N + M), Space O(1)
|
|
11821
11823
|
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
|
|
11822
|
-
* @param [value] - Single value to
|
|
11824
|
+
* @param [value] - Single value to set when a bare key is provided.
|
|
11823
11825
|
* @returns True if inserted or appended; false if ignored.
|
|
11824
11826
|
*/
|
|
11825
|
-
|
|
11826
|
-
if (this.isRealNode(keyNodeOrEntry)) return super.
|
|
11827
|
+
set(keyNodeOrEntry, value) {
|
|
11828
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
|
|
11827
11829
|
const _commonAdd = /* @__PURE__ */ __name((key, values) => {
|
|
11828
11830
|
if (key === void 0 || key === null) return false;
|
|
11829
|
-
const
|
|
11831
|
+
const _setToValues = /* @__PURE__ */ __name(() => {
|
|
11830
11832
|
const existingValues = this.get(key);
|
|
11831
11833
|
if (existingValues !== void 0 && values !== void 0) {
|
|
11832
11834
|
for (const value2 of values) existingValues.push(value2);
|
|
11833
11835
|
return true;
|
|
11834
11836
|
}
|
|
11835
11837
|
return false;
|
|
11836
|
-
}, "
|
|
11837
|
-
const
|
|
11838
|
+
}, "_setToValues");
|
|
11839
|
+
const _setByNode = /* @__PURE__ */ __name(() => {
|
|
11838
11840
|
const existingNode = this.getNode(key);
|
|
11839
11841
|
if (this.isRealNode(existingNode)) {
|
|
11840
11842
|
const existingValues = this.get(existingNode);
|
|
11841
11843
|
if (existingValues === void 0) {
|
|
11842
|
-
super.
|
|
11844
|
+
super.set(key, values);
|
|
11843
11845
|
return true;
|
|
11844
11846
|
}
|
|
11845
11847
|
if (values !== void 0) {
|
|
@@ -11849,13 +11851,13 @@ var TreeMultiMap = class extends RedBlackTree {
|
|
|
11849
11851
|
return false;
|
|
11850
11852
|
}
|
|
11851
11853
|
} else {
|
|
11852
|
-
return super.
|
|
11854
|
+
return super.set(key, values);
|
|
11853
11855
|
}
|
|
11854
|
-
}, "
|
|
11856
|
+
}, "_setByNode");
|
|
11855
11857
|
if (this._isMapMode) {
|
|
11856
|
-
return
|
|
11858
|
+
return _setByNode() || _setToValues();
|
|
11857
11859
|
}
|
|
11858
|
-
return
|
|
11860
|
+
return _setToValues() || _setByNode();
|
|
11859
11861
|
}, "_commonAdd");
|
|
11860
11862
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11861
11863
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -11895,7 +11897,7 @@ var TreeMultiMap = class extends RedBlackTree {
|
|
|
11895
11897
|
map(callback, options, thisArg) {
|
|
11896
11898
|
const out = this._createLike([], options);
|
|
11897
11899
|
let i = 0;
|
|
11898
|
-
for (const [k, v] of this) out.
|
|
11900
|
+
for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
|
|
11899
11901
|
return out;
|
|
11900
11902
|
}
|
|
11901
11903
|
/**
|
|
@@ -12080,7 +12082,7 @@ var TreeCounter = class extends RedBlackTree {
|
|
|
12080
12082
|
*/
|
|
12081
12083
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
12082
12084
|
super([], options);
|
|
12083
|
-
if (keysNodesEntriesOrRaws) this.
|
|
12085
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
12084
12086
|
}
|
|
12085
12087
|
_count = 0;
|
|
12086
12088
|
/**
|
|
@@ -12120,10 +12122,10 @@ var TreeCounter = class extends RedBlackTree {
|
|
|
12120
12122
|
* @param [count] - How much to increase the node's count (default 1).
|
|
12121
12123
|
* @returns True if inserted/updated; false if ignored.
|
|
12122
12124
|
*/
|
|
12123
|
-
|
|
12125
|
+
set(keyNodeOrEntry, value, count = 1) {
|
|
12124
12126
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
12125
12127
|
const orgCount = newNode?.count || 0;
|
|
12126
|
-
const isSuccessAdded = super.
|
|
12128
|
+
const isSuccessAdded = super.set(newNode, newValue);
|
|
12127
12129
|
if (isSuccessAdded) {
|
|
12128
12130
|
this._count += orgCount;
|
|
12129
12131
|
return true;
|
|
@@ -12276,7 +12278,7 @@ var TreeCounter = class extends RedBlackTree {
|
|
|
12276
12278
|
const out = this._createLike([], options);
|
|
12277
12279
|
let index = 0;
|
|
12278
12280
|
for (const [key, value] of this) {
|
|
12279
|
-
out.
|
|
12281
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
12280
12282
|
}
|
|
12281
12283
|
return out;
|
|
12282
12284
|
}
|
|
@@ -12532,7 +12534,7 @@ var AVLTreeCounter = class extends AVLTree {
|
|
|
12532
12534
|
*/
|
|
12533
12535
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
12534
12536
|
super([], options);
|
|
12535
|
-
if (keysNodesEntriesOrRaws) this.
|
|
12537
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
12536
12538
|
}
|
|
12537
12539
|
_count = 0;
|
|
12538
12540
|
get count() {
|
|
@@ -12567,11 +12569,11 @@ var AVLTreeCounter = class extends AVLTree {
|
|
|
12567
12569
|
* @param [count] - How much to increase the node's count (default 1).
|
|
12568
12570
|
* @returns True if inserted/updated; false if ignored.
|
|
12569
12571
|
*/
|
|
12570
|
-
|
|
12572
|
+
set(keyNodeOrEntry, value, count = 1) {
|
|
12571
12573
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
12572
12574
|
if (newNode === void 0) return false;
|
|
12573
12575
|
const orgNodeCount = newNode?.count || 0;
|
|
12574
|
-
const inserted = super.
|
|
12576
|
+
const inserted = super.set(newNode, newValue);
|
|
12575
12577
|
if (inserted) {
|
|
12576
12578
|
this._count += orgNodeCount;
|
|
12577
12579
|
}
|
|
@@ -12679,9 +12681,9 @@ var AVLTreeCounter = class extends AVLTree {
|
|
|
12679
12681
|
clone() {
|
|
12680
12682
|
const out = this._createInstance();
|
|
12681
12683
|
if (this._isMapMode) {
|
|
12682
|
-
this.bfs((node) => out.
|
|
12684
|
+
this.bfs((node) => out.set(node.key, void 0, node.count));
|
|
12683
12685
|
} else {
|
|
12684
|
-
this.bfs((node) => out.
|
|
12686
|
+
this.bfs((node) => out.set(node.key, node.value, node.count));
|
|
12685
12687
|
}
|
|
12686
12688
|
if (this._isMapMode) out._store = this._store;
|
|
12687
12689
|
return out;
|
|
@@ -12701,7 +12703,7 @@ var AVLTreeCounter = class extends AVLTree {
|
|
|
12701
12703
|
const out = this._createLike([], options);
|
|
12702
12704
|
let index = 0;
|
|
12703
12705
|
for (const [key, value] of this) {
|
|
12704
|
-
out.
|
|
12706
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
12705
12707
|
}
|
|
12706
12708
|
return out;
|
|
12707
12709
|
}
|