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