tree-multimap-typed 2.2.3 → 2.2.4

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 (29) hide show
  1. package/dist/cjs/index.cjs +85 -75
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +85 -75
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +85 -75
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +85 -75
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  14. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  16. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  17. package/dist/umd/tree-multimap-typed.js +85 -75
  18. package/dist/umd/tree-multimap-typed.js.map +1 -1
  19. package/dist/umd/tree-multimap-typed.min.js +3 -3
  20. package/dist/umd/tree-multimap-typed.min.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  23. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  24. package/src/data-structures/binary-tree/avl-tree.ts +4 -5
  25. package/src/data-structures/binary-tree/bst.ts +111 -82
  26. package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
  27. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  28. package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
  29. package/src/types/data-structures/binary-tree/bst.ts +5 -5
@@ -2833,9 +2833,13 @@ var BST = class extends BinaryTree {
2833
2833
  constructor(keysNodesEntriesOrRaws = [], options) {
2834
2834
  super([], options);
2835
2835
  if (options) {
2836
- const { specifyComparable, isReverse } = options;
2837
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2838
- if (isReverse !== void 0) this._isReverse = isReverse;
2836
+ if ("comparator" in options && options.comparator !== void 0) {
2837
+ this._comparator = options.comparator;
2838
+ } else {
2839
+ this._comparator = this._createDefaultComparator();
2840
+ }
2841
+ } else {
2842
+ this._comparator = this._createDefaultComparator();
2839
2843
  }
2840
2844
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2841
2845
  }
@@ -2849,40 +2853,33 @@ var BST = class extends BinaryTree {
2849
2853
  get root() {
2850
2854
  return this._root;
2851
2855
  }
2852
- _isReverse = false;
2853
2856
  /**
2854
- * Gets whether the tree's comparison logic is reversed.
2855
- * @remarks Time O(1)
2856
- *
2857
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2857
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2858
+ * @remarks Time O(1) Space O(1)
2859
+ * @returns The default comparator function.
2858
2860
  */
2859
- get isReverse() {
2860
- return this._isReverse;
2861
+ _createDefaultComparator() {
2862
+ return (a, b) => {
2863
+ debugger;
2864
+ if (isComparable(a) && isComparable(b)) {
2865
+ if (a > b) return 1;
2866
+ if (a < b) return -1;
2867
+ return 0;
2868
+ }
2869
+ if (typeof a === "object" || typeof b === "object") {
2870
+ throw TypeError(
2871
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2872
+ );
2873
+ }
2874
+ return 0;
2875
+ };
2861
2876
  }
2862
2877
  /**
2863
- * The default comparator function.
2864
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2865
- */
2866
- _comparator = /* @__PURE__ */ __name((a, b) => {
2867
- if (isComparable(a) && isComparable(b)) {
2868
- if (a > b) return 1;
2869
- if (a < b) return -1;
2870
- return 0;
2871
- }
2872
- if (this._specifyComparable) {
2873
- const va = this._specifyComparable(a);
2874
- const vb = this._specifyComparable(b);
2875
- if (va > vb) return 1;
2876
- if (va < vb) return -1;
2877
- return 0;
2878
- }
2879
- if (typeof a === "object" || typeof b === "object") {
2880
- throw TypeError(
2881
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2882
- );
2883
- }
2884
- return 0;
2885
- }, "_comparator");
2878
+ * The comparator function used to determine the order of keys in the tree.
2879
+
2880
+ * @remarks Time O(1) Space O(1)
2881
+ */
2882
+ _comparator;
2886
2883
  /**
2887
2884
  * Gets the comparator function used by the tree.
2888
2885
  * @remarks Time O(1)
@@ -2892,16 +2889,6 @@ var BST = class extends BinaryTree {
2892
2889
  get comparator() {
2893
2890
  return this._comparator;
2894
2891
  }
2895
- _specifyComparable;
2896
- /**
2897
- * Gets the function used to extract a comparable value from a complex key.
2898
- * @remarks Time O(1)
2899
- *
2900
- * @returns The key-to-comparable conversion function.
2901
- */
2902
- get specifyComparable() {
2903
- return this._specifyComparable;
2904
- }
2905
2892
  /**
2906
2893
  * (Protected) Creates a new BST node.
2907
2894
  * @remarks Time O(1), Space O(1)
@@ -2942,7 +2929,7 @@ var BST = class extends BinaryTree {
2942
2929
  * @returns True if the key is valid, false otherwise.
2943
2930
  */
2944
2931
  isValidKey(key) {
2945
- return isComparable(key, this._specifyComparable !== void 0);
2932
+ return isComparable(key);
2946
2933
  }
2947
2934
  /**
2948
2935
  * Performs a Depth-First Search (DFS) traversal.
@@ -3031,8 +3018,8 @@ var BST = class extends BinaryTree {
3031
3018
  if (!this.isRealNode(cur.left)) return false;
3032
3019
  if (isRange) {
3033
3020
  const range = keyNodeEntryOrPredicate;
3034
- const leftS = this.isReverse ? range.high : range.low;
3035
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
3021
+ const leftS = range.low;
3022
+ const leftI = range.includeLow;
3036
3023
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
3037
3024
  }
3038
3025
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3046,8 +3033,8 @@ var BST = class extends BinaryTree {
3046
3033
  if (!this.isRealNode(cur.right)) return false;
3047
3034
  if (isRange) {
3048
3035
  const range = keyNodeEntryOrPredicate;
3049
- const rightS = this.isReverse ? range.low : range.high;
3050
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
3036
+ const rightS = range.high;
3037
+ const rightI = range.includeHigh;
3051
3038
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
3052
3039
  }
3053
3040
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3368,31 +3355,55 @@ var BST = class extends BinaryTree {
3368
3355
  return out;
3369
3356
  }
3370
3357
  /**
3371
- * Deletes the first node found that satisfies the predicate.
3372
- * @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
3358
+ * Deletes nodes that match a key, node, entry, predicate, or range.
3373
3359
  *
3374
- * @param predicate - A function to test each [key, value] pair.
3375
- * @returns True if a node was deleted, false otherwise.
3376
- */
3377
- deleteWhere(predicate) {
3378
- const stack = [];
3379
- let cur = this._root;
3380
- let index = 0;
3381
- while (stack.length > 0 || cur !== void 0) {
3382
- while (cur !== void 0 && cur !== null) {
3383
- stack.push(cur);
3384
- cur = cur.left;
3385
- }
3386
- const node = stack.pop();
3387
- if (!node) break;
3388
- const key = node.key;
3389
- const val = node.value;
3390
- if (predicate(key, val, index++, this)) {
3391
- return this._deleteByKey(key);
3392
- }
3393
- cur = node.right;
3360
+ * @remarks
3361
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
3362
+ * Space Complexity: O(M) for storing matched nodes and result map.
3363
+ *
3364
+ * @template K - The key type.
3365
+ * @template V - The value type.
3366
+ *
3367
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
3368
+ * - A key (type K): searches for exact key match using the comparator.
3369
+ * - A BSTNode: searches for the matching node in the tree.
3370
+ * - An entry tuple: searches for the key-value pair.
3371
+ * - A NodePredicate function: tests each node and returns true for matches.
3372
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
3373
+ * - null or undefined: treated as no match, returns empty results.
3374
+ *
3375
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
3376
+ * If false (default), searches for and deletes all matching nodes.
3377
+ *
3378
+ * @param startNode - The node to start the search from. Can be:
3379
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
3380
+ * - null or undefined: defaults to the root, searching the entire tree.
3381
+ * - Default value: this._root (the tree's root).
3382
+ *
3383
+ * @param iterationType - Controls the internal traversal implementation:
3384
+ * - 'RECURSIVE': uses recursive function calls for traversal.
3385
+ * - 'ITERATIVE': uses explicit stack-based iteration.
3386
+ * - Default: this.iterationType (the tree's default iteration mode).
3387
+ *
3388
+ * @returns A Map<K, boolean> containing the deletion results:
3389
+ * - Key: the matched node's key.
3390
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
3391
+ * - If no nodes match the search criteria, the returned map is empty.
3392
+ */
3393
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3394
+ const toDelete = this.search(
3395
+ keyNodeEntryOrPredicate,
3396
+ onlyOne,
3397
+ (node) => node,
3398
+ startNode,
3399
+ iterationType
3400
+ );
3401
+ let results = [];
3402
+ for (const node of toDelete) {
3403
+ const deleteInfo = this.delete(node);
3404
+ results = results.concat(deleteInfo);
3394
3405
  }
3395
- return false;
3406
+ return results;
3396
3407
  }
3397
3408
  /**
3398
3409
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -3544,8 +3555,7 @@ var BST = class extends BinaryTree {
3544
3555
  _snapshotOptions() {
3545
3556
  return {
3546
3557
  ...super._snapshotOptions(),
3547
- specifyComparable: this.specifyComparable,
3548
- isReverse: this.isReverse
3558
+ comparator: this._comparator
3549
3559
  };
3550
3560
  }
3551
3561
  /**
@@ -3573,14 +3583,14 @@ var BST = class extends BinaryTree {
3573
3583
  }
3574
3584
  /**
3575
3585
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3576
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3586
+ * @remarks Time O(1) Space O(1)
3577
3587
  *
3578
3588
  * @param a - The first key.
3579
3589
  * @param b - The second key.
3580
3590
  * @returns A number (1, -1, or 0) representing the comparison.
3581
3591
  */
3582
3592
  _compare(a, b) {
3583
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3593
+ return this._comparator(a, b);
3584
3594
  }
3585
3595
  /**
3586
3596
  * (Private) Deletes a node by its key.