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
@@ -1475,6 +1475,12 @@ var dataStructureTyped = (() => {
1475
1475
  }
1476
1476
  return -1;
1477
1477
  }
1478
+ /**
1479
+ * Concatenate lists/elements preserving order.
1480
+ * @param items - Elements or `LinearBase` instances.
1481
+ * @returns New list with combined elements (`this` type).
1482
+ * @remarks Time O(sum(length)), Space O(sum(length))
1483
+ */
1478
1484
  concat(...items) {
1479
1485
  const newList = this.clone();
1480
1486
  for (const item of items) {
@@ -4276,6 +4282,12 @@ var dataStructureTyped = (() => {
4276
4282
  */
4277
4283
  _setBucketSize(size) {
4278
4284
  this._bucketSize = size;
4285
+ if (this._length === 0) {
4286
+ this._buckets = [new Array(this._bucketSize)];
4287
+ this._bucketCount = 1;
4288
+ this._bucketFirst = this._bucketLast = 0;
4289
+ this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
4290
+ }
4279
4291
  }
4280
4292
  /**
4281
4293
  * (Protected) Iterate elements from front to back.
@@ -6989,9 +7001,9 @@ var dataStructureTyped = (() => {
6989
7001
  var BinaryTree = class extends IterableEntryBase {
6990
7002
  /**
6991
7003
  * Creates an instance of BinaryTree.
6992
- * @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.
7004
+ * @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.
6993
7005
  *
6994
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
7006
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
6995
7007
  * @param [options] - Configuration options for the tree.
6996
7008
  */
6997
7009
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -7020,7 +7032,7 @@ var dataStructureTyped = (() => {
7020
7032
  if (typeof toEntryFn === "function") this._toEntryFn = toEntryFn;
7021
7033
  else if (toEntryFn) throw TypeError("toEntryFn must be a function type");
7022
7034
  }
7023
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
7035
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
7024
7036
  }
7025
7037
  /**
7026
7038
  * Gets whether the tree is in Map mode.
@@ -7227,10 +7239,20 @@ var dataStructureTyped = (() => {
7227
7239
  * @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).
7228
7240
  *
7229
7241
  * @param keyNodeOrEntry - The key, node, or entry to add.
7242
+ * @returns True if the addition was successful, false otherwise.
7243
+ */
7244
+ add(keyNodeOrEntry) {
7245
+ return this.set(keyNodeOrEntry);
7246
+ }
7247
+ /**
7248
+ * Adds or updates a new node to the tree.
7249
+ * @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).
7250
+ *
7251
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
7230
7252
  * @param [value] - The value, if providing just a key.
7231
7253
  * @returns True if the addition was successful, false otherwise.
7232
7254
  */
7233
- add(keyNodeOrEntry, value) {
7255
+ set(keyNodeOrEntry, value) {
7234
7256
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
7235
7257
  if (newNode === void 0) return false;
7236
7258
  if (!this._root) {
@@ -7274,25 +7296,24 @@ var dataStructureTyped = (() => {
7274
7296
  return false;
7275
7297
  }
7276
7298
  /**
7277
- * Adds or updates a new node to the tree.
7278
- * @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).
7299
+ * Adds multiple items to the tree.
7300
+ * @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).
7279
7301
  *
7280
- * @param keyNodeOrEntry - The key, node, or entry to add or update.
7281
- * @param [value] - The value, if providing just a key.
7282
- * @returns True if the addition was successful, false otherwise.
7302
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
7303
+ * @returns An array of booleans indicating the success of each individual `set` operation.
7283
7304
  */
7284
- set(keyNodeOrEntry, value) {
7285
- return this.add(keyNodeOrEntry, value);
7305
+ addMany(keysNodesEntriesOrRaws) {
7306
+ return this.setMany(keysNodesEntriesOrRaws);
7286
7307
  }
7287
7308
  /**
7288
- * Adds multiple items to the tree.
7289
- * @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).
7309
+ * Adds or updates multiple items to the tree.
7310
+ * @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).
7290
7311
  *
7291
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
7312
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
7292
7313
  * @param [values] - An optional parallel iterable of values.
7293
- * @returns An array of booleans indicating the success of each individual `add` operation.
7314
+ * @returns An array of booleans indicating the success of each individual `set` operation.
7294
7315
  */
7295
- addMany(keysNodesEntriesOrRaws, values) {
7316
+ setMany(keysNodesEntriesOrRaws, values) {
7296
7317
  const inserted = [];
7297
7318
  let valuesIterator;
7298
7319
  if (values) {
@@ -7307,52 +7328,41 @@ var dataStructureTyped = (() => {
7307
7328
  }
7308
7329
  }
7309
7330
  if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn(keyNodeEntryOrRaw);
7310
- inserted.push(this.add(keyNodeEntryOrRaw, value));
7331
+ inserted.push(this.set(keyNodeEntryOrRaw, value));
7311
7332
  }
7312
7333
  return inserted;
7313
7334
  }
7314
7335
  /**
7315
- * Adds or updates multiple items to the tree.
7316
- * @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).
7317
- *
7318
- * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
7319
- * @param [values] - An optional parallel iterable of values.
7320
- * @returns An array of booleans indicating the success of each individual `add` operation.
7321
- */
7322
- setMany(keysNodesEntriesOrRaws, values) {
7323
- return this.addMany(keysNodesEntriesOrRaws, values);
7324
- }
7325
- /**
7326
- * Merges another tree into this one by adding all its nodes.
7327
- * @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`).
7336
+ * Merges another tree into this one by seting all its nodes.
7337
+ * @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`).
7328
7338
  *
7329
7339
  * @param anotherTree - The tree to merge.
7330
7340
  */
7331
7341
  merge(anotherTree) {
7332
- this.addMany(anotherTree, []);
7342
+ this.setMany(anotherTree, []);
7333
7343
  }
7334
7344
  /**
7335
7345
  * Clears the tree and refills it with new items.
7336
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
7346
+ * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
7337
7347
  *
7338
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
7348
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
7339
7349
  * @param [values] - An optional parallel iterable of values.
7340
7350
  */
7341
7351
  refill(keysNodesEntriesOrRaws, values) {
7342
7352
  this.clear();
7343
- this.addMany(keysNodesEntriesOrRaws, values);
7353
+ this.setMany(keysNodesEntriesOrRaws, values);
7344
7354
  }
7345
7355
  /**
7346
7356
  * Deletes a node from the tree.
7347
7357
  * @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).
7348
7358
  *
7349
- * @param keyNodeOrEntry - The node to delete.
7359
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
7350
7360
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
7351
7361
  */
7352
- delete(keyNodeOrEntry) {
7362
+ delete(keyNodeEntryRawOrPredicate) {
7353
7363
  const deletedResult = [];
7354
7364
  if (!this._root) return deletedResult;
7355
- const curr = this.getNode(keyNodeOrEntry);
7365
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
7356
7366
  if (!curr) return deletedResult;
7357
7367
  const parent = curr == null ? void 0 : curr.parent;
7358
7368
  let needBalanced;
@@ -7536,7 +7546,7 @@ var dataStructureTyped = (() => {
7536
7546
  }
7537
7547
  return true;
7538
7548
  };
7539
- const isStandardBST = checkBST(false);
7549
+ const isStandardBST = checkBST();
7540
7550
  const isInverseBST = checkBST(true);
7541
7551
  return isStandardBST || isInverseBST;
7542
7552
  }
@@ -8006,7 +8016,7 @@ var dataStructureTyped = (() => {
8006
8016
  }
8007
8017
  /**
8008
8018
  * Clones the tree.
8009
- * @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.
8019
+ * @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.
8010
8020
  *
8011
8021
  * @returns A new, cloned instance of the tree.
8012
8022
  */
@@ -8017,7 +8027,7 @@ var dataStructureTyped = (() => {
8017
8027
  }
8018
8028
  /**
8019
8029
  * Creates a new tree containing only the entries that satisfy the predicate.
8020
- * @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.
8030
+ * @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.
8021
8031
  *
8022
8032
  * @param predicate - A function to test each [key, value] pair.
8023
8033
  * @param [thisArg] - `this` context for the predicate.
@@ -8026,7 +8036,7 @@ var dataStructureTyped = (() => {
8026
8036
  filter(predicate, thisArg) {
8027
8037
  const out = this._createInstance();
8028
8038
  let i = 0;
8029
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
8039
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
8030
8040
  return out;
8031
8041
  }
8032
8042
  /**
@@ -8044,7 +8054,7 @@ var dataStructureTyped = (() => {
8044
8054
  map(cb, options, thisArg) {
8045
8055
  const out = this._createLike([], options);
8046
8056
  let i = 0;
8047
- for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
8057
+ for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
8048
8058
  return out;
8049
8059
  }
8050
8060
  /**
@@ -8290,18 +8300,18 @@ var dataStructureTyped = (() => {
8290
8300
  return [this.createNode(keyNodeOrEntry, value), value];
8291
8301
  }
8292
8302
  /**
8293
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
8294
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
8303
+ * (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
8304
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
8295
8305
  *
8296
8306
  * @param cloned - The new, empty tree instance to populate.
8297
8307
  */
8298
8308
  _clone(cloned) {
8299
8309
  this.bfs(
8300
8310
  (node) => {
8301
- if (node === null) cloned.add(null);
8311
+ if (node === null) cloned.set(null);
8302
8312
  else {
8303
- if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
8304
- else cloned.add([node.key, node.value]);
8313
+ if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
8314
+ else cloned.set([node.key, node.value]);
8305
8315
  }
8306
8316
  },
8307
8317
  this._root,
@@ -8628,7 +8638,7 @@ var dataStructureTyped = (() => {
8628
8638
  * Creates an instance of BST.
8629
8639
  * @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
8630
8640
  *
8631
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
8641
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
8632
8642
  * @param [options] - Configuration options for the BST, including comparator.
8633
8643
  */
8634
8644
  constructor(keysNodesEntriesOrRaws = [], options) {
@@ -8649,7 +8659,7 @@ var dataStructureTyped = (() => {
8649
8659
  } else {
8650
8660
  this._comparator = this._createDefaultComparator();
8651
8661
  }
8652
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8662
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
8653
8663
  }
8654
8664
  /**
8655
8665
  * Gets the root node of the tree.
@@ -8763,8 +8773,40 @@ var dataStructureTyped = (() => {
8763
8773
  * @returns The first matching node, or undefined if not found.
8764
8774
  */
8765
8775
  getNode(keyNodeEntryOrPredicate, startNode = this._root, iterationType = this.iterationType) {
8766
- var _a;
8767
- return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
8776
+ var _a, _b;
8777
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) return void 0;
8778
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
8779
+ return (_a = this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0]) != null ? _a : void 0;
8780
+ }
8781
+ if (keyNodeEntryOrPredicate instanceof Range) {
8782
+ return (_b = this.getNodes(
8783
+ keyNodeEntryOrPredicate,
8784
+ true,
8785
+ startNode,
8786
+ iterationType
8787
+ )[0]) != null ? _b : void 0;
8788
+ }
8789
+ let targetKey;
8790
+ if (this.isNode(keyNodeEntryOrPredicate)) {
8791
+ targetKey = keyNodeEntryOrPredicate.key;
8792
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
8793
+ const k = keyNodeEntryOrPredicate[0];
8794
+ if (k === null || k === void 0) return void 0;
8795
+ targetKey = k;
8796
+ } else {
8797
+ targetKey = keyNodeEntryOrPredicate;
8798
+ }
8799
+ const start = this.ensureNode(startNode);
8800
+ if (!start) return void 0;
8801
+ const NIL = this._NIL;
8802
+ let cur = start;
8803
+ const cmpFn = this._comparator;
8804
+ while (cur && cur !== NIL) {
8805
+ const c = cmpFn(targetKey, cur.key);
8806
+ if (c === 0) return cur;
8807
+ cur = c < 0 ? cur._left : cur._right;
8808
+ }
8809
+ return void 0;
8768
8810
  }
8769
8811
  /**
8770
8812
  * Searches the tree for nodes matching a predicate, key, or range.
@@ -8785,8 +8827,30 @@ var dataStructureTyped = (() => {
8785
8827
  if (keyNodeEntryOrPredicate === null) return [];
8786
8828
  startNode = this.ensureNode(startNode);
8787
8829
  if (!startNode) return [];
8788
- let predicate;
8789
8830
  const isRange = this.isRange(keyNodeEntryOrPredicate);
8831
+ const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
8832
+ if (!isRange && !isPred) {
8833
+ let targetKey;
8834
+ if (this.isNode(keyNodeEntryOrPredicate)) {
8835
+ targetKey = keyNodeEntryOrPredicate.key;
8836
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
8837
+ const k = keyNodeEntryOrPredicate[0];
8838
+ if (k !== null && k !== void 0) targetKey = k;
8839
+ } else {
8840
+ targetKey = keyNodeEntryOrPredicate;
8841
+ }
8842
+ if (targetKey === void 0) return [];
8843
+ const NIL = this._NIL;
8844
+ const cmpFn = this._comparator;
8845
+ let cur = startNode;
8846
+ while (cur && cur !== NIL) {
8847
+ const c = cmpFn(targetKey, cur.key);
8848
+ if (c === 0) return [callback(cur)];
8849
+ cur = c < 0 ? cur._left : cur._right;
8850
+ }
8851
+ return [];
8852
+ }
8853
+ let predicate;
8790
8854
  if (isRange) {
8791
8855
  predicate = (node) => {
8792
8856
  if (!node) return false;
@@ -8860,11 +8924,11 @@ var dataStructureTyped = (() => {
8860
8924
  * Adds a new node to the BST based on key comparison.
8861
8925
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
8862
8926
  *
8863
- * @param keyNodeOrEntry - The key, node, or entry to add.
8927
+ * @param keyNodeOrEntry - The key, node, or entry to set.
8864
8928
  * @param [value] - The value, if providing just a key.
8865
8929
  * @returns True if the addition was successful, false otherwise.
8866
8930
  */
8867
- add(keyNodeOrEntry, value) {
8931
+ set(keyNodeOrEntry, value) {
8868
8932
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
8869
8933
  if (newNode === void 0) return false;
8870
8934
  if (this._root === void 0) {
@@ -8901,24 +8965,24 @@ var dataStructureTyped = (() => {
8901
8965
  }
8902
8966
  /**
8903
8967
  * Adds multiple items to the tree.
8904
- * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
8968
+ * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
8905
8969
  * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
8906
8970
  * Space O(N) for sorting and recursion/iteration stack.
8907
8971
  *
8908
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
8972
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
8909
8973
  * @param [values] - An optional parallel iterable of values.
8910
8974
  * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
8911
- * @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
8912
- * @returns An array of booleans indicating the success of each individual `add` operation.
8975
+ * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
8976
+ * @returns An array of booleans indicating the success of each individual `set` operation.
8913
8977
  */
8914
- addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
8978
+ setMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
8915
8979
  const inserted = [];
8916
8980
  const valuesIterator = values == null ? void 0 : values[Symbol.iterator]();
8917
8981
  if (!isBalanceAdd) {
8918
8982
  for (let kve of keysNodesEntriesOrRaws) {
8919
8983
  const val = valuesIterator == null ? void 0 : valuesIterator.next().value;
8920
8984
  if (this.isRaw(kve)) kve = this._toEntryFn(kve);
8921
- inserted.push(this.add(kve, val));
8985
+ inserted.push(this.set(kve, val));
8922
8986
  }
8923
8987
  return inserted;
8924
8988
  }
@@ -8946,9 +9010,9 @@ var dataStructureTyped = (() => {
8946
9010
  const { key, value, orgIndex } = arr[mid];
8947
9011
  if (this.isRaw(key)) {
8948
9012
  const entry = this._toEntryFn(key);
8949
- inserted[orgIndex] = this.add(entry);
9013
+ inserted[orgIndex] = this.set(entry);
8950
9014
  } else {
8951
- inserted[orgIndex] = this.add(key, value);
9015
+ inserted[orgIndex] = this.set(key, value);
8952
9016
  }
8953
9017
  _dfs(arr.slice(0, mid));
8954
9018
  _dfs(arr.slice(mid + 1));
@@ -8965,9 +9029,9 @@ var dataStructureTyped = (() => {
8965
9029
  const { key, value, orgIndex } = sorted[m];
8966
9030
  if (this.isRaw(key)) {
8967
9031
  const entry = this._toEntryFn(key);
8968
- inserted[orgIndex] = this.add(entry);
9032
+ inserted[orgIndex] = this.set(entry);
8969
9033
  } else {
8970
- inserted[orgIndex] = this.add(key, value);
9034
+ inserted[orgIndex] = this.set(key, value);
8971
9035
  }
8972
9036
  stack.push([m + 1, r]);
8973
9037
  stack.push([l, m - 1]);
@@ -9242,7 +9306,7 @@ var dataStructureTyped = (() => {
9242
9306
  const out = this._createLike([], options);
9243
9307
  let index = 0;
9244
9308
  for (const [key, value] of this) {
9245
- out.add(callback.call(thisArg, value, key, index++, this));
9309
+ out.set(callback.call(thisArg, value, key, index++, this));
9246
9310
  }
9247
9311
  return out;
9248
9312
  }
@@ -9673,7 +9737,6 @@ var dataStructureTyped = (() => {
9673
9737
  * @returns True if the node was found and deleted, false otherwise.
9674
9738
  */
9675
9739
  _deleteByKey(key) {
9676
- var _a;
9677
9740
  let node = this._root;
9678
9741
  while (node) {
9679
9742
  const cmp = this._compare(node.key, key);
@@ -9712,7 +9775,7 @@ var dataStructureTyped = (() => {
9712
9775
  succ.left = node.left;
9713
9776
  if (succ.left) succ.left.parent = succ;
9714
9777
  }
9715
- this._size = Math.max(0, ((_a = this._size) != null ? _a : 0) - 1);
9778
+ this._size = Math.max(0, this._size - 1);
9716
9779
  return true;
9717
9780
  }
9718
9781
  };
@@ -10418,14 +10481,14 @@ var dataStructureTyped = (() => {
10418
10481
  var AVLTree = class extends BST {
10419
10482
  /**
10420
10483
  * Creates an instance of AVLTree.
10421
- * @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
10484
+ * @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
10422
10485
  *
10423
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
10486
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
10424
10487
  * @param [options] - Configuration options for the AVL tree.
10425
10488
  */
10426
10489
  constructor(keysNodesEntriesOrRaws = [], options) {
10427
10490
  super([], options);
10428
- if (keysNodesEntriesOrRaws) super.addMany(keysNodesEntriesOrRaws);
10491
+ if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
10429
10492
  }
10430
10493
  /**
10431
10494
  * (Protected) Creates a new AVL tree node.
@@ -10449,16 +10512,16 @@ var dataStructureTyped = (() => {
10449
10512
  return keyNodeOrEntry instanceof AVLTreeNode;
10450
10513
  }
10451
10514
  /**
10452
- * Adds a new node to the AVL tree and balances the tree path.
10453
- * @remarks Time O(log N) (O(H) for BST add + O(H) for `_balancePath`). Space O(H) for path/recursion.
10515
+ * Sets a new node to the AVL tree and balances the tree path.
10516
+ * @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
10454
10517
  *
10455
- * @param keyNodeOrEntry - The key, node, or entry to add.
10518
+ * @param keyNodeOrEntry - The key, node, or entry to set.
10456
10519
  * @param [value] - The value, if providing just a key.
10457
10520
  * @returns True if the addition was successful, false otherwise.
10458
10521
  */
10459
- add(keyNodeOrEntry, value) {
10522
+ set(keyNodeOrEntry, value) {
10460
10523
  if (keyNodeOrEntry === null) return false;
10461
- const inserted = super.add(keyNodeOrEntry, value);
10524
+ const inserted = super.set(keyNodeOrEntry, value);
10462
10525
  if (inserted) this._balancePath(keyNodeOrEntry);
10463
10526
  return inserted;
10464
10527
  }
@@ -10510,7 +10573,7 @@ var dataStructureTyped = (() => {
10510
10573
  }
10511
10574
  /**
10512
10575
  * Creates a new AVLTree by mapping each [key, value] pair.
10513
- * @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.
10576
+ * @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.
10514
10577
  *
10515
10578
  * @template MK - New key type.
10516
10579
  * @template MV - New value type.
@@ -10524,7 +10587,7 @@ var dataStructureTyped = (() => {
10524
10587
  const out = this._createLike([], options);
10525
10588
  let index = 0;
10526
10589
  for (const [key, value] of this) {
10527
- out.add(callback.call(thisArg, value, key, index++, this));
10590
+ out.set(callback.call(thisArg, value, key, index++, this));
10528
10591
  }
10529
10592
  return out;
10530
10593
  }
@@ -10807,12 +10870,11 @@ var dataStructureTyped = (() => {
10807
10870
  // src/data-structures/binary-tree/red-black-tree.ts
10808
10871
  var RedBlackTreeNode = class {
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");
@@ -10943,9 +11005,31 @@ var dataStructureTyped = (() => {
10943
11005
  constructor(keysNodesEntriesOrRaws = [], options) {
10944
11006
  super([], options);
10945
11007
  __publicField(this, "_root");
11008
+ /**
11009
+ * (Internal) Header sentinel:
11010
+ * - header.parent -> root
11011
+ * - header._left -> min (or NIL)
11012
+ * - header._right -> max (or NIL)
11013
+ *
11014
+ * IMPORTANT:
11015
+ * - This header is NOT part of the actual tree.
11016
+ * - Do NOT use `header.left` / `header.right` accessors for wiring: those setters update `NIL.parent`
11017
+ * and can corrupt sentinel invariants / cause hangs. Only touch `header._left/_right`.
11018
+ */
11019
+ __publicField(this, "_header");
11020
+ /**
11021
+ * (Internal) Cache of the current minimum and maximum nodes.
11022
+ * Used for fast-path insert/update when keys are monotonic or near-boundary.
11023
+ */
11024
+ __publicField(this, "_minNode");
11025
+ __publicField(this, "_maxNode");
10946
11026
  this._root = this.NIL;
11027
+ this._header = new RedBlackTreeNode(void 0, void 0, "BLACK");
11028
+ this._header.parent = this.NIL;
11029
+ this._header._left = this.NIL;
11030
+ this._header._right = this.NIL;
10947
11031
  if (keysNodesEntriesOrRaws) {
10948
- this.addMany(keysNodesEntriesOrRaws);
11032
+ this.setMany(keysNodesEntriesOrRaws);
10949
11033
  }
10950
11034
  }
10951
11035
  /**
@@ -10981,18 +11065,390 @@ var dataStructureTyped = (() => {
10981
11065
  * @remarks Time O(n), Space O(1)
10982
11066
  * @returns void
10983
11067
  */
11068
+ /**
11069
+ * Remove all nodes and clear internal caches.
11070
+ * @remarks Time O(n) average, Space O(1)
11071
+ */
10984
11072
  clear() {
10985
11073
  super.clear();
10986
11074
  this._root = this.NIL;
11075
+ this._header.parent = this.NIL;
11076
+ this._setMinCache(void 0);
11077
+ this._setMaxCache(void 0);
10987
11078
  }
10988
11079
  /**
10989
- * Insert or replace an entry using BST order and red-black fix-up.
10990
- * @remarks Time O(log n), Space O(1)
10991
- * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
10992
- * @param [value]- See parameter type for details.
10993
- * @returns True if inserted or updated; false if ignored.
11080
+ * (Internal) Find a node by key using a tight BST walk (no allocations).
11081
+ *
11082
+ * NOTE: This uses `header.parent` as the canonical root pointer.
11083
+ * @remarks Time O(log n) average, Space O(1)
11084
+ */
11085
+ _findNodeByKey(key) {
11086
+ var _a, _b, _c;
11087
+ const NIL = this.NIL;
11088
+ const cmp = this._compare.bind(this);
11089
+ let cur = (_a = this._header.parent) != null ? _a : NIL;
11090
+ while (cur !== NIL) {
11091
+ const c = cmp(key, cur.key);
11092
+ if (c < 0) cur = (_b = cur.left) != null ? _b : NIL;
11093
+ else if (c > 0) cur = (_c = cur.right) != null ? _c : NIL;
11094
+ else return cur;
11095
+ }
11096
+ return void 0;
11097
+ }
11098
+ /**
11099
+ * (Internal) In-order predecessor of a node in a BST.
11100
+ * @remarks Time O(log n) average, Space O(1)
11101
+ */
11102
+ _predecessorOf(node) {
11103
+ const NIL = this.NIL;
11104
+ if (node.left && node.left !== NIL) {
11105
+ let cur2 = node.left;
11106
+ while (cur2.right && cur2.right !== NIL) cur2 = cur2.right;
11107
+ return cur2;
11108
+ }
11109
+ let cur = node;
11110
+ let p = node.parent;
11111
+ while (p && cur === p.left) {
11112
+ cur = p;
11113
+ p = p.parent;
11114
+ }
11115
+ return p;
11116
+ }
11117
+ /**
11118
+ * (Internal) In-order successor of a node in a BST.
11119
+ * @remarks Time O(log n) average, Space O(1)
11120
+ */
11121
+ _successorOf(node) {
11122
+ const NIL = this.NIL;
11123
+ if (node.right && node.right !== NIL) {
11124
+ let cur2 = node.right;
11125
+ while (cur2.left && cur2.left !== NIL) cur2 = cur2.left;
11126
+ return cur2;
11127
+ }
11128
+ let cur = node;
11129
+ let p = node.parent;
11130
+ while (p && cur === p.right) {
11131
+ cur = p;
11132
+ p = p.parent;
11133
+ }
11134
+ return p;
11135
+ }
11136
+ /**
11137
+ * (Internal) Attach a new node directly under a known parent/side (no search).
11138
+ *
11139
+ * This is a performance-oriented helper used by boundary fast paths and hinted insertion.
11140
+ * It will:
11141
+ * - wire parent/child pointers (using accessors, so parent pointers are updated)
11142
+ * - initialize children to NIL
11143
+ * - mark the new node RED, then run insert fix-up
11144
+ *
11145
+ * Precondition: the chosen slot (parent.left/parent.right) is empty (NIL/null/undefined).
11146
+ * @remarks Time O(log n) average, Space O(1)
11147
+ */
11148
+ _attachNewNode(parent, side, node) {
11149
+ const NIL = this.NIL;
11150
+ node.parent = parent;
11151
+ if (side === "left") parent.left = node;
11152
+ else parent.right = node;
11153
+ node.left = NIL;
11154
+ node.right = NIL;
11155
+ node.color = "RED";
11156
+ this._insertFixup(node);
11157
+ if (this.isRealNode(this._root)) this._root.color = "BLACK";
11158
+ }
11159
+ /**
11160
+ * (Internal) a single source of truth for min/max is header._left/_right.
11161
+ * Keep legacy _minNode/_maxNode mirrored for compatibility.
11162
+ * @remarks Time O(1), Space O(1)
11163
+ */
11164
+ /**
11165
+ * (Internal) Update min cache pointers (header._left is the canonical min pointer).
11166
+ * @remarks Time O(1), Space O(1)
10994
11167
  */
10995
- add(keyNodeOrEntry, value) {
11168
+ _setMinCache(node) {
11169
+ this._minNode = node;
11170
+ this._header._left = node != null ? node : this.NIL;
11171
+ }
11172
+ /**
11173
+ * (Internal) Update max cache pointers (header._right is the canonical max pointer).
11174
+ * @remarks Time O(1), Space O(1)
11175
+ */
11176
+ _setMaxCache(node) {
11177
+ this._maxNode = node;
11178
+ this._header._right = node != null ? node : this.NIL;
11179
+ }
11180
+ /**
11181
+ * (Internal) Core set implementation returning the affected node.
11182
+ *
11183
+ * Hot path goals:
11184
+ * - Avoid double walks (search+insert): do a single traversal that either updates or inserts.
11185
+ * - Use header min/max caches to fast-path boundary inserts.
11186
+ * - Keep header._left/_right as canonical min/max pointers.
11187
+ *
11188
+ * Return value:
11189
+ * - `{ node, created:false }` when an existing key is updated
11190
+ * - `{ node, created:true }` when a new node is inserted
11191
+ * - `undefined` only on unexpected internal failure.
11192
+ * @remarks Time O(log n) average, Space O(1)
11193
+ */
11194
+ _setKVNode(key, nextValue) {
11195
+ var _a, _b, _c, _d, _e, _f, _g;
11196
+ const NIL = this.NIL;
11197
+ const comparator = this._comparator;
11198
+ const header = this._header;
11199
+ const minN = (_a = header._left) != null ? _a : NIL;
11200
+ if (minN !== NIL) {
11201
+ const cMin = comparator(key, minN.key);
11202
+ if (cMin === 0) {
11203
+ if (this._isMapMode) {
11204
+ if (nextValue !== void 0) this._store.set(key, nextValue);
11205
+ else this._setValue(key, nextValue);
11206
+ } else minN.value = nextValue;
11207
+ return { node: minN, created: false };
11208
+ }
11209
+ const minL = minN.left;
11210
+ if (cMin < 0 && (minL === NIL || minL === null || minL === void 0)) {
11211
+ const newNode2 = this.createNode(key, nextValue);
11212
+ this._attachNewNode(minN, "left", newNode2);
11213
+ if (this._isMapMode) {
11214
+ if (nextValue !== void 0) this._store.set(newNode2.key, nextValue);
11215
+ else this._setValue(newNode2.key, nextValue);
11216
+ }
11217
+ this._size++;
11218
+ this._setMinCache(newNode2);
11219
+ if (header._right === NIL) this._setMaxCache(newNode2);
11220
+ return { node: newNode2, created: true };
11221
+ }
11222
+ if (cMin > 0) {
11223
+ const maxN = (_b = header._right) != null ? _b : NIL;
11224
+ const cMax = comparator(key, maxN.key);
11225
+ if (cMax === 0) {
11226
+ if (this._isMapMode) {
11227
+ if (nextValue !== void 0) this._store.set(key, nextValue);
11228
+ else this._setValue(key, nextValue);
11229
+ } else maxN.value = nextValue;
11230
+ return { node: maxN, created: false };
11231
+ }
11232
+ const maxR = maxN.right;
11233
+ if (cMax > 0 && (maxR === NIL || maxR === null || maxR === void 0)) {
11234
+ const newNode2 = this.createNode(key, nextValue);
11235
+ this._attachNewNode(maxN, "right", newNode2);
11236
+ if (this._isMapMode) {
11237
+ if (nextValue !== void 0) this._store.set(newNode2.key, nextValue);
11238
+ else this._setValue(newNode2.key, nextValue);
11239
+ }
11240
+ this._size++;
11241
+ this._setMaxCache(newNode2);
11242
+ if (header._left === NIL) this._setMinCache(newNode2);
11243
+ return { node: newNode2, created: true };
11244
+ }
11245
+ }
11246
+ }
11247
+ const cmp = comparator;
11248
+ const isMapMode = this._isMapMode;
11249
+ const store = this._store;
11250
+ let current = (_c = this._header.parent) != null ? _c : NIL;
11251
+ let parent;
11252
+ let lastCompared = 0;
11253
+ while (current !== NIL) {
11254
+ parent = current;
11255
+ lastCompared = cmp(key, current.key);
11256
+ if (lastCompared < 0) current = (_d = current.left) != null ? _d : NIL;
11257
+ else if (lastCompared > 0) current = (_e = current.right) != null ? _e : NIL;
11258
+ else {
11259
+ if (isMapMode) {
11260
+ if (nextValue !== void 0) store.set(key, nextValue);
11261
+ else this._setValue(key, nextValue);
11262
+ } else {
11263
+ current.value = nextValue;
11264
+ }
11265
+ return { node: current, created: false };
11266
+ }
11267
+ }
11268
+ const newNode = this.createNode(key, nextValue);
11269
+ newNode.parent = parent;
11270
+ if (!parent) {
11271
+ this._setRoot(newNode);
11272
+ } else if (lastCompared < 0) {
11273
+ parent.left = newNode;
11274
+ } else {
11275
+ parent.right = newNode;
11276
+ }
11277
+ newNode.left = NIL;
11278
+ newNode.right = NIL;
11279
+ newNode.color = "RED";
11280
+ this._insertFixup(newNode);
11281
+ if (this.isRealNode(this._root)) this._root.color = "BLACK";
11282
+ else return void 0;
11283
+ if (isMapMode) {
11284
+ if (nextValue !== void 0) store.set(newNode.key, nextValue);
11285
+ else this._setValue(newNode.key, nextValue);
11286
+ }
11287
+ this._size++;
11288
+ const hMin = (_f = this._header._left) != null ? _f : NIL;
11289
+ const hMax = (_g = this._header._right) != null ? _g : NIL;
11290
+ if (hMin === NIL || hMax === NIL) {
11291
+ this._setMinCache(newNode);
11292
+ this._setMaxCache(newNode);
11293
+ } else if (parent === hMax && lastCompared > 0) {
11294
+ this._setMaxCache(newNode);
11295
+ } else if (parent === hMin && lastCompared < 0) {
11296
+ this._setMinCache(newNode);
11297
+ } else {
11298
+ if (cmp(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
11299
+ if (cmp(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
11300
+ }
11301
+ return { node: newNode, created: true };
11302
+ }
11303
+ /**
11304
+ * (Internal) Boolean wrapper around `_setKVNode`.
11305
+ *
11306
+ * Includes a map-mode update fast-path:
11307
+ * - If `isMapMode=true` and the key already exists in `_store`, then updating the value does not
11308
+ * require any tree search/rotation (tree shape depends only on key).
11309
+ * - This path is intentionally limited to `nextValue !== undefined` to preserve existing
11310
+ * semantics for `undefined` values.
11311
+ * @remarks Time O(log n) average, Space O(1)
11312
+ */
11313
+ _setKV(key, nextValue) {
11314
+ if (this._isMapMode && nextValue !== void 0) {
11315
+ const store = this._store;
11316
+ if (store.has(key)) {
11317
+ store.set(key, nextValue);
11318
+ return true;
11319
+ }
11320
+ }
11321
+ return this._setKVNode(key, nextValue) !== void 0;
11322
+ }
11323
+ /**
11324
+ * Insert/update using a hint node to speed up nearby insertions.
11325
+ *
11326
+ * close to the expected insertion position (often the previously returned node in a loop).
11327
+ *
11328
+ * When the hint is a good fit (sorted / nearly-sorted insertion), this can avoid most of the
11329
+ * normal root-to-leaf search and reduce constant factors.
11330
+ *
11331
+ * When the hint does not match (random workloads), this will fall back to the normal set path.
11332
+ * @remarks Time O(log n) average, Space O(1)
11333
+ */
11334
+ setWithHintNode(key, value, hint) {
11335
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
11336
+ if (!hint || !this.isRealNode(hint)) {
11337
+ return (_a = this._setKVNode(key, value)) == null ? void 0 : _a.node;
11338
+ }
11339
+ const cmp = this._compare.bind(this);
11340
+ const c0 = cmp(key, hint.key);
11341
+ if (c0 === 0) {
11342
+ if (this._isMapMode) {
11343
+ if (value !== void 0) this._store.set(key, value);
11344
+ else this._setValue(key, value);
11345
+ } else hint.value = value;
11346
+ return hint;
11347
+ }
11348
+ if (c0 < 0) {
11349
+ if (!this.isRealNode(hint.left)) {
11350
+ const newNode = this.createNode(key, value);
11351
+ if (!this.isRealNode(newNode)) return void 0;
11352
+ this._attachNewNode(hint, "left", newNode);
11353
+ if (this._isMapMode) {
11354
+ if (value !== void 0) this._store.set(key, value);
11355
+ else this._setValue(key, value);
11356
+ }
11357
+ this._size++;
11358
+ const NIL = this.NIL;
11359
+ const hMin = (_b = this._header._left) != null ? _b : NIL;
11360
+ if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
11361
+ const hMax = (_c = this._header._right) != null ? _c : NIL;
11362
+ if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
11363
+ return newNode;
11364
+ }
11365
+ const pred = this._predecessorOf(hint);
11366
+ if (pred && cmp(pred.key, key) >= 0) {
11367
+ return (_d = this._setKVNode(key, value)) == null ? void 0 : _d.node;
11368
+ }
11369
+ if (pred && !this.isRealNode(pred.right)) {
11370
+ const newNode = this.createNode(key, value);
11371
+ if (!this.isRealNode(newNode)) return void 0;
11372
+ this._attachNewNode(pred, "right", newNode);
11373
+ if (this._isMapMode) {
11374
+ if (value !== void 0) this._store.set(key, value);
11375
+ else this._setValue(key, value);
11376
+ }
11377
+ this._size++;
11378
+ const NIL = this.NIL;
11379
+ const hMin = (_e = this._header._left) != null ? _e : NIL;
11380
+ if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
11381
+ const hMax = (_f = this._header._right) != null ? _f : NIL;
11382
+ if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
11383
+ return newNode;
11384
+ }
11385
+ return (_g = this._setKVNode(key, value)) == null ? void 0 : _g.node;
11386
+ }
11387
+ if (!this.isRealNode(hint.right)) {
11388
+ const newNode = this.createNode(key, value);
11389
+ if (!this.isRealNode(newNode)) return void 0;
11390
+ this._attachNewNode(hint, "right", newNode);
11391
+ if (this._isMapMode) {
11392
+ if (value !== void 0) this._store.set(key, value);
11393
+ else this._setValue(key, value);
11394
+ }
11395
+ this._size++;
11396
+ const NIL = this.NIL;
11397
+ const hMin = (_h = this._header._left) != null ? _h : NIL;
11398
+ if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
11399
+ const hMax = (_i = this._header._right) != null ? _i : NIL;
11400
+ if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
11401
+ return newNode;
11402
+ }
11403
+ const succ = this._successorOf(hint);
11404
+ if (succ && cmp(succ.key, key) <= 0) {
11405
+ return (_j = this._setKVNode(key, value)) == null ? void 0 : _j.node;
11406
+ }
11407
+ if (succ && !this.isRealNode(succ.left)) {
11408
+ const newNode = this.createNode(key, value);
11409
+ if (!this.isRealNode(newNode)) return void 0;
11410
+ this._attachNewNode(succ, "left", newNode);
11411
+ if (this._isMapMode) {
11412
+ if (value !== void 0) this._store.set(key, value);
11413
+ else this._setValue(key, value);
11414
+ }
11415
+ this._size++;
11416
+ const NIL = this.NIL;
11417
+ const hMin = (_k = this._header._left) != null ? _k : NIL;
11418
+ if (hMin === NIL || this._compare(newNode.key, hMin.key) < 0) this._setMinCache(newNode);
11419
+ const hMax = (_l = this._header._right) != null ? _l : NIL;
11420
+ if (hMax === NIL || this._compare(newNode.key, hMax.key) > 0) this._setMaxCache(newNode);
11421
+ return newNode;
11422
+ }
11423
+ return (_m = this._setKVNode(key, value)) == null ? void 0 : _m.node;
11424
+ }
11425
+ /**
11426
+ * Boolean wrapper for setWithHintNode.
11427
+ * @remarks Time O(log n) average, Space O(1)
11428
+ */
11429
+ setWithHint(key, value, hint) {
11430
+ return this.setWithHintNode(key, value, hint) !== void 0;
11431
+ }
11432
+ /**
11433
+ * Insert or update a key/value (map mode) or key-only (set mode).
11434
+ *
11435
+ * This method is optimized for:
11436
+ * - monotonic inserts via min/max boundary fast paths
11437
+ * - updates via a single-pass search (no double walk)
11438
+ *
11439
+ * @remarks Time O(log n) average, Space O(1)
11440
+ */
11441
+ set(keyNodeOrEntry, value) {
11442
+ if (!this.isNode(keyNodeOrEntry)) {
11443
+ if (keyNodeOrEntry === null || keyNodeOrEntry === void 0) return false;
11444
+ if (this.isEntry(keyNodeOrEntry)) {
11445
+ const key = keyNodeOrEntry[0];
11446
+ if (key === null || key === void 0) return false;
11447
+ const nextValue = value != null ? value : keyNodeOrEntry[1];
11448
+ return this._setKV(key, nextValue);
11449
+ }
11450
+ return this._setKV(keyNodeOrEntry, value);
11451
+ }
10996
11452
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
10997
11453
  if (!this.isRealNode(newNode)) return false;
10998
11454
  const insertStatus = this._insert(newNode);
@@ -11014,19 +11470,23 @@ var dataStructureTyped = (() => {
11014
11470
  }
11015
11471
  /**
11016
11472
  * Delete a node by key/node/entry and rebalance as needed.
11017
- * @remarks Time O(log n), Space O(1)
11018
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node to delete.
11473
+ * @remarks Time O(log n) average, Space O(1)
11474
+ * @param keyNodeEntryRawOrPredicate - Key, node, or [key, value] entry identifying the node to delete.
11019
11475
  * @returns Array with deletion metadata (removed node, rebalancing hint if any).
11020
11476
  */
11021
- delete(keyNodeOrEntry) {
11022
- if (keyNodeOrEntry === null) return [];
11477
+ delete(keyNodeEntryRawOrPredicate) {
11478
+ if (keyNodeEntryRawOrPredicate === null) return [];
11023
11479
  const results = [];
11024
11480
  let nodeToDelete;
11025
- if (this._isPredicate(keyNodeOrEntry)) nodeToDelete = this.getNode(keyNodeOrEntry);
11026
- else nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
11481
+ if (this._isPredicate(keyNodeEntryRawOrPredicate)) nodeToDelete = this.getNode(keyNodeEntryRawOrPredicate);
11482
+ else nodeToDelete = this.isRealNode(keyNodeEntryRawOrPredicate) ? keyNodeEntryRawOrPredicate : this.getNode(keyNodeEntryRawOrPredicate);
11027
11483
  if (!nodeToDelete) {
11028
11484
  return results;
11029
11485
  }
11486
+ const willDeleteMin = nodeToDelete === this._minNode;
11487
+ const willDeleteMax = nodeToDelete === this._maxNode;
11488
+ const nextMin = willDeleteMin ? this._successorOf(nodeToDelete) : void 0;
11489
+ const nextMax = willDeleteMax ? this._predecessorOf(nodeToDelete) : void 0;
11030
11490
  let originalColor = nodeToDelete.color;
11031
11491
  let replacementNode;
11032
11492
  if (!this.isRealNode(nodeToDelete.left)) {
@@ -11065,6 +11525,19 @@ var dataStructureTyped = (() => {
11065
11525
  }
11066
11526
  if (this._isMapMode) this._store.delete(nodeToDelete.key);
11067
11527
  this._size--;
11528
+ if (this._size <= 0) {
11529
+ this._setMinCache(void 0);
11530
+ this._setMaxCache(void 0);
11531
+ } else {
11532
+ if (willDeleteMin) this._setMinCache(nextMin);
11533
+ if (willDeleteMax) this._setMaxCache(nextMax);
11534
+ if (!this._minNode || !this.isRealNode(this._minNode)) {
11535
+ this._setMinCache(this.isRealNode(this._root) ? this.getLeftMost((n) => n, this._root) : void 0);
11536
+ }
11537
+ if (!this._maxNode || !this.isRealNode(this._maxNode)) {
11538
+ this._setMaxCache(this.isRealNode(this._root) ? this.getRightMost((n) => n, this._root) : void 0);
11539
+ }
11540
+ }
11068
11541
  if (originalColor === "BLACK") {
11069
11542
  this._deleteFixup(replacementNode);
11070
11543
  }
@@ -11073,7 +11546,7 @@ var dataStructureTyped = (() => {
11073
11546
  }
11074
11547
  /**
11075
11548
  * Transform entries into a like-kind red-black tree with possibly different key/value types.
11076
- * @remarks Time O(n), Space O(n)
11549
+ * @remarks Time O(n) average, Space O(n)
11077
11550
  * @template MK
11078
11551
  * @template MV
11079
11552
  * @template MR
@@ -11086,45 +11559,66 @@ var dataStructureTyped = (() => {
11086
11559
  const out = this._createLike([], options);
11087
11560
  let index = 0;
11088
11561
  for (const [key, value] of this) {
11089
- out.add(callback.call(thisArg, value, key, index++, this));
11562
+ out.set(callback.call(thisArg, value, key, index++, this));
11090
11563
  }
11091
11564
  return out;
11092
11565
  }
11566
+ /**
11567
+ * (Internal) Create an empty instance of the same concrete tree type.
11568
+ * @remarks Time O(1) average, Space O(1)
11569
+ */
11093
11570
  _createInstance(options) {
11094
11571
  const Ctor = this.constructor;
11095
11572
  return new Ctor([], { ...this._snapshotOptions(), ...options != null ? options : {} });
11096
11573
  }
11574
+ /**
11575
+ * (Internal) Create a like-kind tree (same concrete class) populated from an iterable.
11576
+ * @remarks Time O(m log m) average (m = iterable length), Space O(m)
11577
+ */
11097
11578
  _createLike(iter = [], options) {
11098
11579
  const Ctor = this.constructor;
11099
11580
  return new Ctor(iter, { ...this._snapshotOptions(), ...options != null ? options : {} });
11100
11581
  }
11582
+ /**
11583
+ * (Internal) Set the root pointer and keep header.parent in sync.
11584
+ * @remarks Time O(1), Space O(1)
11585
+ */
11101
11586
  _setRoot(v) {
11587
+ const NIL = this.NIL;
11102
11588
  if (v) {
11103
11589
  v.parent = void 0;
11104
11590
  }
11105
11591
  this._root = v;
11592
+ this._header.parent = v != null ? v : NIL;
11106
11593
  }
11594
+ /**
11595
+ * (Internal) Replace a node in place while preserving its color.
11596
+ * @remarks Time O(1) average, Space O(1)
11597
+ */
11107
11598
  _replaceNode(oldNode, newNode) {
11108
11599
  newNode.color = oldNode.color;
11109
11600
  return super._replaceNode(oldNode, newNode);
11110
11601
  }
11111
11602
  /**
11112
11603
  * (Protected) Standard BST insert followed by red-black fix-up.
11113
- * @remarks Time O(log n), Space O(1)
11604
+ * @remarks Time O(log n) average, Space O(1)
11114
11605
  * @param node - Node to insert.
11115
11606
  * @returns Status string: 'CREATED' or 'UPDATED'.
11116
11607
  */
11117
11608
  _insert(node) {
11118
11609
  var _a, _b, _c;
11119
- let current = (_a = this.root) != null ? _a : this.NIL;
11120
- let parent = void 0;
11121
- while (current !== this.NIL) {
11610
+ const NIL = this.NIL;
11611
+ const cmp = this._compare.bind(this);
11612
+ let current = (_a = this._header.parent) != null ? _a : NIL;
11613
+ let parent;
11614
+ let lastCompared = 0;
11615
+ while (current !== NIL) {
11122
11616
  parent = current;
11123
- const compared = this._compare(node.key, current.key);
11124
- if (compared < 0) {
11125
- current = (_b = current.left) != null ? _b : this.NIL;
11126
- } else if (compared > 0) {
11127
- current = (_c = current.right) != null ? _c : this.NIL;
11617
+ lastCompared = cmp(node.key, current.key);
11618
+ if (lastCompared < 0) {
11619
+ current = (_b = current.left) != null ? _b : NIL;
11620
+ } else if (lastCompared > 0) {
11621
+ current = (_c = current.right) != null ? _c : NIL;
11128
11622
  } else {
11129
11623
  this._replaceNode(current, node);
11130
11624
  return "UPDATED";
@@ -11133,13 +11627,13 @@ var dataStructureTyped = (() => {
11133
11627
  node.parent = parent;
11134
11628
  if (!parent) {
11135
11629
  this._setRoot(node);
11136
- } else if (this._compare(node.key, parent.key) < 0) {
11630
+ } else if (lastCompared < 0) {
11137
11631
  parent.left = node;
11138
11632
  } else {
11139
11633
  parent.right = node;
11140
11634
  }
11141
- node.left = this.NIL;
11142
- node.right = this.NIL;
11635
+ node.left = NIL;
11636
+ node.right = NIL;
11143
11637
  node.color = "RED";
11144
11638
  this._insertFixup(node);
11145
11639
  return "CREATED";
@@ -11165,56 +11659,66 @@ var dataStructureTyped = (() => {
11165
11659
  }
11166
11660
  /**
11167
11661
  * (Protected) Restore red-black properties after insertion (recolor/rotate).
11168
- * @remarks Time O(log n), Space O(1)
11662
+ * @remarks Time O(log n) average, Space O(1)
11169
11663
  * @param z - Recently inserted node.
11170
11664
  * @returns void
11171
11665
  */
11172
11666
  _insertFixup(z) {
11173
- var _a, _b, _c, _d, _e;
11174
- while (((_a = z == null ? void 0 : z.parent) == null ? void 0 : _a.color) === "RED") {
11175
- if (z.parent === ((_b = z.parent.parent) == null ? void 0 : _b.left)) {
11176
- const y = z.parent.parent.right;
11667
+ const leftRotate = this._leftRotate.bind(this);
11668
+ const rightRotate = this._rightRotate.bind(this);
11669
+ while (z) {
11670
+ const p = z.parent;
11671
+ if (!p || p.color !== "RED") break;
11672
+ const gp = p.parent;
11673
+ if (!gp) break;
11674
+ if (p === gp.left) {
11675
+ const y = gp.right;
11177
11676
  if ((y == null ? void 0 : y.color) === "RED") {
11178
- z.parent.color = "BLACK";
11677
+ p.color = "BLACK";
11179
11678
  y.color = "BLACK";
11180
- z.parent.parent.color = "RED";
11181
- z = z.parent.parent;
11182
- } else {
11183
- if (z === z.parent.right) {
11184
- z = z.parent;
11185
- this._leftRotate(z);
11186
- }
11187
- if (z && z.parent && z.parent.parent) {
11188
- z.parent.color = "BLACK";
11189
- z.parent.parent.color = "RED";
11190
- this._rightRotate(z.parent.parent);
11191
- }
11679
+ gp.color = "RED";
11680
+ z = gp;
11681
+ continue;
11682
+ }
11683
+ if (z === p.right) {
11684
+ z = p;
11685
+ leftRotate(z);
11686
+ }
11687
+ const p2 = z == null ? void 0 : z.parent;
11688
+ const gp2 = p2 == null ? void 0 : p2.parent;
11689
+ if (p2 && gp2) {
11690
+ p2.color = "BLACK";
11691
+ gp2.color = "RED";
11692
+ rightRotate(gp2);
11192
11693
  }
11193
11694
  } else {
11194
- const y = (_e = (_d = (_c = z == null ? void 0 : z.parent) == null ? void 0 : _c.parent) == null ? void 0 : _d.left) != null ? _e : void 0;
11695
+ const y = gp.left;
11195
11696
  if ((y == null ? void 0 : y.color) === "RED") {
11196
- z.parent.color = "BLACK";
11697
+ p.color = "BLACK";
11197
11698
  y.color = "BLACK";
11198
- z.parent.parent.color = "RED";
11199
- z = z.parent.parent;
11200
- } else {
11201
- if (z === z.parent.left) {
11202
- z = z.parent;
11203
- this._rightRotate(z);
11204
- }
11205
- if (z && z.parent && z.parent.parent) {
11206
- z.parent.color = "BLACK";
11207
- z.parent.parent.color = "RED";
11208
- this._leftRotate(z.parent.parent);
11209
- }
11699
+ gp.color = "RED";
11700
+ z = gp;
11701
+ continue;
11702
+ }
11703
+ if (z === p.left) {
11704
+ z = p;
11705
+ rightRotate(z);
11706
+ }
11707
+ const p2 = z == null ? void 0 : z.parent;
11708
+ const gp2 = p2 == null ? void 0 : p2.parent;
11709
+ if (p2 && gp2) {
11710
+ p2.color = "BLACK";
11711
+ gp2.color = "RED";
11712
+ leftRotate(gp2);
11210
11713
  }
11211
11714
  }
11715
+ break;
11212
11716
  }
11213
11717
  if (this.isRealNode(this._root)) this._root.color = "BLACK";
11214
11718
  }
11215
11719
  /**
11216
11720
  * (Protected) Restore red-black properties after deletion (recolor/rotate).
11217
- * @remarks Time O(log n), Space O(1)
11721
+ * @remarks Time O(log n) average, Space O(1)
11218
11722
  * @param node - Child that replaced the deleted node (may be undefined).
11219
11723
  * @returns void
11220
11724
  */
@@ -11471,7 +11975,7 @@ var dataStructureTyped = (() => {
11471
11975
  constructor(keysNodesEntriesOrRaws = [], options) {
11472
11976
  super([], { ...options, isMapMode: true });
11473
11977
  if (keysNodesEntriesOrRaws) {
11474
- this.addMany(keysNodesEntriesOrRaws);
11978
+ this.setMany(keysNodesEntriesOrRaws);
11475
11979
  }
11476
11980
  }
11477
11981
  createNode(key, value = []) {
@@ -11491,14 +11995,14 @@ var dataStructureTyped = (() => {
11491
11995
  * Insert a value or a list of values into the multimap. If the key exists, values are appended.
11492
11996
  * @remarks Time O(log N + M), Space O(1)
11493
11997
  * @param keyNodeOrEntry - Key, node, or [key, values] entry.
11494
- * @param [value] - Single value to add when a bare key is provided.
11998
+ * @param [value] - Single value to set when a bare key is provided.
11495
11999
  * @returns True if inserted or appended; false if ignored.
11496
12000
  */
11497
- add(keyNodeOrEntry, value) {
11498
- if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
12001
+ set(keyNodeOrEntry, value) {
12002
+ if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
11499
12003
  const _commonAdd = (key, values) => {
11500
12004
  if (key === void 0 || key === null) return false;
11501
- const _addToValues = () => {
12005
+ const _setToValues = () => {
11502
12006
  const existingValues = this.get(key);
11503
12007
  if (existingValues !== void 0 && values !== void 0) {
11504
12008
  for (const value2 of values) existingValues.push(value2);
@@ -11506,12 +12010,12 @@ var dataStructureTyped = (() => {
11506
12010
  }
11507
12011
  return false;
11508
12012
  };
11509
- const _addByNode = () => {
12013
+ const _setByNode = () => {
11510
12014
  const existingNode = this.getNode(key);
11511
12015
  if (this.isRealNode(existingNode)) {
11512
12016
  const existingValues = this.get(existingNode);
11513
12017
  if (existingValues === void 0) {
11514
- super.add(key, values);
12018
+ super.set(key, values);
11515
12019
  return true;
11516
12020
  }
11517
12021
  if (values !== void 0) {
@@ -11521,13 +12025,13 @@ var dataStructureTyped = (() => {
11521
12025
  return false;
11522
12026
  }
11523
12027
  } else {
11524
- return super.add(key, values);
12028
+ return super.set(key, values);
11525
12029
  }
11526
12030
  };
11527
12031
  if (this._isMapMode) {
11528
- return _addByNode() || _addToValues();
12032
+ return _setByNode() || _setToValues();
11529
12033
  }
11530
- return _addToValues() || _addByNode();
12034
+ return _setToValues() || _setByNode();
11531
12035
  };
11532
12036
  if (this.isEntry(keyNodeOrEntry)) {
11533
12037
  const [key, values] = keyNodeOrEntry;
@@ -11595,7 +12099,7 @@ var dataStructureTyped = (() => {
11595
12099
  map(callback, options, thisArg) {
11596
12100
  const out = this._createLike([], options);
11597
12101
  let i = 0;
11598
- for (const [k, v] of this) out.add(callback.call(thisArg, v, k, i++, this));
12102
+ for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
11599
12103
  return out;
11600
12104
  }
11601
12105
  /**
@@ -11774,7 +12278,7 @@ var dataStructureTyped = (() => {
11774
12278
  constructor(keysNodesEntriesOrRaws = [], options) {
11775
12279
  super([], { ...options });
11776
12280
  if (keysNodesEntriesOrRaws) {
11777
- this.addMany(keysNodesEntriesOrRaws);
12281
+ this.setMany(keysNodesEntriesOrRaws);
11778
12282
  }
11779
12283
  }
11780
12284
  createNode(key, value = []) {
@@ -11794,14 +12298,14 @@ var dataStructureTyped = (() => {
11794
12298
  * Insert a value or a list of values into the multimap. If the key exists, values are appended.
11795
12299
  * @remarks Time O(log N + M), Space O(1)
11796
12300
  * @param keyNodeOrEntry - Key, node, or [key, values] entry.
11797
- * @param [value] - Single value to add when a bare key is provided.
12301
+ * @param [value] - Single value to set when a bare key is provided.
11798
12302
  * @returns True if inserted or appended; false if ignored.
11799
12303
  */
11800
- add(keyNodeOrEntry, value) {
11801
- if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
12304
+ set(keyNodeOrEntry, value) {
12305
+ if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
11802
12306
  const _commonAdd = (key, values) => {
11803
12307
  if (key === void 0 || key === null) return false;
11804
- const _addToValues = () => {
12308
+ const _setToValues = () => {
11805
12309
  const existingValues = this.get(key);
11806
12310
  if (existingValues !== void 0 && values !== void 0) {
11807
12311
  for (const value2 of values) existingValues.push(value2);
@@ -11809,12 +12313,12 @@ var dataStructureTyped = (() => {
11809
12313
  }
11810
12314
  return false;
11811
12315
  };
11812
- const _addByNode = () => {
12316
+ const _setByNode = () => {
11813
12317
  const existingNode = this.getNode(key);
11814
12318
  if (this.isRealNode(existingNode)) {
11815
12319
  const existingValues = this.get(existingNode);
11816
12320
  if (existingValues === void 0) {
11817
- super.add(key, values);
12321
+ super.set(key, values);
11818
12322
  return true;
11819
12323
  }
11820
12324
  if (values !== void 0) {
@@ -11824,13 +12328,13 @@ var dataStructureTyped = (() => {
11824
12328
  return false;
11825
12329
  }
11826
12330
  } else {
11827
- return super.add(key, values);
12331
+ return super.set(key, values);
11828
12332
  }
11829
12333
  };
11830
12334
  if (this._isMapMode) {
11831
- return _addByNode() || _addToValues();
12335
+ return _setByNode() || _setToValues();
11832
12336
  }
11833
- return _addToValues() || _addByNode();
12337
+ return _setToValues() || _setByNode();
11834
12338
  };
11835
12339
  if (this.isEntry(keyNodeOrEntry)) {
11836
12340
  const [key, values] = keyNodeOrEntry;
@@ -11870,7 +12374,7 @@ var dataStructureTyped = (() => {
11870
12374
  map(callback, options, thisArg) {
11871
12375
  const out = this._createLike([], options);
11872
12376
  let i = 0;
11873
- for (const [k, v] of this) out.add(callback.call(thisArg, v, k, i++, this));
12377
+ for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
11874
12378
  return out;
11875
12379
  }
11876
12380
  /**
@@ -12052,7 +12556,7 @@ var dataStructureTyped = (() => {
12052
12556
  constructor(keysNodesEntriesOrRaws = [], options) {
12053
12557
  super([], options);
12054
12558
  __publicField(this, "_count", 0);
12055
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
12559
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
12056
12560
  }
12057
12561
  /**
12058
12562
  * Get the total aggregate count across all nodes.
@@ -12091,10 +12595,10 @@ var dataStructureTyped = (() => {
12091
12595
  * @param [count] - How much to increase the node's count (default 1).
12092
12596
  * @returns True if inserted/updated; false if ignored.
12093
12597
  */
12094
- add(keyNodeOrEntry, value, count = 1) {
12598
+ set(keyNodeOrEntry, value, count = 1) {
12095
12599
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
12096
12600
  const orgCount = (newNode == null ? void 0 : newNode.count) || 0;
12097
- const isSuccessAdded = super.add(newNode, newValue);
12601
+ const isSuccessAdded = super.set(newNode, newValue);
12098
12602
  if (isSuccessAdded) {
12099
12603
  this._count += orgCount;
12100
12604
  return true;
@@ -12105,16 +12609,16 @@ var dataStructureTyped = (() => {
12105
12609
  /**
12106
12610
  * Delete a node (or decrement its count) and rebalance if needed.
12107
12611
  * @remarks Time O(log N), Space O(1)
12108
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
12612
+ * @param keyNodeEntryRawOrPredicate - Key, node, or [key, value] entry identifying the node.
12109
12613
  * @param [ignoreCount] - If true, remove the node regardless of its count.
12110
12614
  * @returns Array of deletion results including deleted node and a rebalance hint when present.
12111
12615
  */
12112
- delete(keyNodeOrEntry, ignoreCount = false) {
12113
- if (keyNodeOrEntry === null) return [];
12616
+ delete(keyNodeEntryRawOrPredicate, ignoreCount = false) {
12617
+ if (keyNodeEntryRawOrPredicate === null) return [];
12114
12618
  const results = [];
12115
12619
  let nodeToDelete;
12116
- if (this._isPredicate(keyNodeOrEntry)) nodeToDelete = this.getNode(keyNodeOrEntry);
12117
- else nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
12620
+ if (this._isPredicate(keyNodeEntryRawOrPredicate)) nodeToDelete = this.getNode(keyNodeEntryRawOrPredicate);
12621
+ else nodeToDelete = this.isRealNode(keyNodeEntryRawOrPredicate) ? keyNodeEntryRawOrPredicate : this.getNode(keyNodeEntryRawOrPredicate);
12118
12622
  if (!nodeToDelete) {
12119
12623
  return results;
12120
12624
  }
@@ -12157,7 +12661,6 @@ var dataStructureTyped = (() => {
12157
12661
  if (ignoreCount || nodeToDelete.count <= 1) {
12158
12662
  if (successor.right !== null) {
12159
12663
  this._transplant(successor, successor.right);
12160
- this._count -= nodeToDelete.count;
12161
12664
  }
12162
12665
  } else {
12163
12666
  nodeToDelete.count--;
@@ -12247,7 +12750,7 @@ var dataStructureTyped = (() => {
12247
12750
  const out = this._createLike([], options);
12248
12751
  let index = 0;
12249
12752
  for (const [key, value] of this) {
12250
- out.add(callback.call(thisArg, value, key, index++, this));
12753
+ out.set(callback.call(thisArg, value, key, index++, this));
12251
12754
  }
12252
12755
  return out;
12253
12756
  }
@@ -12260,6 +12763,11 @@ var dataStructureTyped = (() => {
12260
12763
  const out = this._createInstance();
12261
12764
  this._clone(out);
12262
12765
  out._count = this._count;
12766
+ for (const node of this.dfs((n) => n, "IN")) {
12767
+ if (!node) continue;
12768
+ const outNode = out.getNode(node.key);
12769
+ if (outNode) outNode.count = node.count;
12770
+ }
12263
12771
  return out;
12264
12772
  }
12265
12773
  /**
@@ -12498,7 +13006,7 @@ var dataStructureTyped = (() => {
12498
13006
  constructor(keysNodesEntriesOrRaws = [], options) {
12499
13007
  super([], options);
12500
13008
  __publicField(this, "_count", 0);
12501
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
13009
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
12502
13010
  }
12503
13011
  get count() {
12504
13012
  return this._count;
@@ -12532,11 +13040,11 @@ var dataStructureTyped = (() => {
12532
13040
  * @param [count] - How much to increase the node's count (default 1).
12533
13041
  * @returns True if inserted/updated; false if ignored.
12534
13042
  */
12535
- add(keyNodeOrEntry, value, count = 1) {
13043
+ set(keyNodeOrEntry, value, count = 1) {
12536
13044
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
12537
13045
  if (newNode === void 0) return false;
12538
13046
  const orgNodeCount = (newNode == null ? void 0 : newNode.count) || 0;
12539
- const inserted = super.add(newNode, newValue);
13047
+ const inserted = super.set(newNode, newValue);
12540
13048
  if (inserted) {
12541
13049
  this._count += orgNodeCount;
12542
13050
  }
@@ -12645,9 +13153,9 @@ var dataStructureTyped = (() => {
12645
13153
  clone() {
12646
13154
  const out = this._createInstance();
12647
13155
  if (this._isMapMode) {
12648
- this.bfs((node) => out.add(node.key, void 0, node.count));
13156
+ this.bfs((node) => out.set(node.key, void 0, node.count));
12649
13157
  } else {
12650
- this.bfs((node) => out.add(node.key, node.value, node.count));
13158
+ this.bfs((node) => out.set(node.key, node.value, node.count));
12651
13159
  }
12652
13160
  if (this._isMapMode) out._store = this._store;
12653
13161
  return out;
@@ -12667,7 +13175,7 @@ var dataStructureTyped = (() => {
12667
13175
  const out = this._createLike([], options);
12668
13176
  let index = 0;
12669
13177
  for (const [key, value] of this) {
12670
- out.add(callback.call(thisArg, value, key, index++, this));
13178
+ out.set(callback.call(thisArg, value, key, index++, this));
12671
13179
  }
12672
13180
  return out;
12673
13181
  }