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
@@ -2831,36 +2831,20 @@ var _BST = class _BST extends BinaryTree {
2831
2831
  constructor(keysNodesEntriesOrRaws = [], options) {
2832
2832
  super([], options);
2833
2833
  __publicField(this, "_root");
2834
- __publicField(this, "_isReverse", false);
2835
2834
  /**
2836
- * The default comparator function.
2837
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2838
- */
2839
- __publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
2840
- if (isComparable(a) && isComparable(b)) {
2841
- if (a > b) return 1;
2842
- if (a < b) return -1;
2843
- return 0;
2844
- }
2845
- if (this._specifyComparable) {
2846
- const va = this._specifyComparable(a);
2847
- const vb = this._specifyComparable(b);
2848
- if (va > vb) return 1;
2849
- if (va < vb) return -1;
2850
- return 0;
2851
- }
2852
- if (typeof a === "object" || typeof b === "object") {
2853
- throw TypeError(
2854
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2855
- );
2856
- }
2857
- return 0;
2858
- }, "_comparator"));
2859
- __publicField(this, "_specifyComparable");
2835
+ * The comparator function used to determine the order of keys in the tree.
2836
+
2837
+ * @remarks Time O(1) Space O(1)
2838
+ */
2839
+ __publicField(this, "_comparator");
2860
2840
  if (options) {
2861
- const { specifyComparable, isReverse } = options;
2862
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2863
- if (isReverse !== void 0) this._isReverse = isReverse;
2841
+ if ("comparator" in options && options.comparator !== void 0) {
2842
+ this._comparator = options.comparator;
2843
+ } else {
2844
+ this._comparator = this._createDefaultComparator();
2845
+ }
2846
+ } else {
2847
+ this._comparator = this._createDefaultComparator();
2864
2848
  }
2865
2849
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2866
2850
  }
@@ -2874,13 +2858,25 @@ var _BST = class _BST extends BinaryTree {
2874
2858
  return this._root;
2875
2859
  }
2876
2860
  /**
2877
- * Gets whether the tree's comparison logic is reversed.
2878
- * @remarks Time O(1)
2879
- *
2880
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2861
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2862
+ * @remarks Time O(1) Space O(1)
2863
+ * @returns The default comparator function.
2881
2864
  */
2882
- get isReverse() {
2883
- return this._isReverse;
2865
+ _createDefaultComparator() {
2866
+ return (a, b) => {
2867
+ debugger;
2868
+ if (isComparable(a) && isComparable(b)) {
2869
+ if (a > b) return 1;
2870
+ if (a < b) return -1;
2871
+ return 0;
2872
+ }
2873
+ if (typeof a === "object" || typeof b === "object") {
2874
+ throw TypeError(
2875
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2876
+ );
2877
+ }
2878
+ return 0;
2879
+ };
2884
2880
  }
2885
2881
  /**
2886
2882
  * Gets the comparator function used by the tree.
@@ -2891,15 +2887,6 @@ var _BST = class _BST extends BinaryTree {
2891
2887
  get comparator() {
2892
2888
  return this._comparator;
2893
2889
  }
2894
- /**
2895
- * Gets the function used to extract a comparable value from a complex key.
2896
- * @remarks Time O(1)
2897
- *
2898
- * @returns The key-to-comparable conversion function.
2899
- */
2900
- get specifyComparable() {
2901
- return this._specifyComparable;
2902
- }
2903
2890
  /**
2904
2891
  * (Protected) Creates a new BST node.
2905
2892
  * @remarks Time O(1), Space O(1)
@@ -2941,7 +2928,7 @@ var _BST = class _BST extends BinaryTree {
2941
2928
  * @returns True if the key is valid, false otherwise.
2942
2929
  */
2943
2930
  isValidKey(key) {
2944
- return isComparable(key, this._specifyComparable !== void 0);
2931
+ return isComparable(key);
2945
2932
  }
2946
2933
  /**
2947
2934
  * Performs a Depth-First Search (DFS) traversal.
@@ -3031,8 +3018,8 @@ var _BST = class _BST 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 _BST 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 _BST 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.
@@ -3545,8 +3556,7 @@ var _BST = class _BST extends BinaryTree {
3545
3556
  _snapshotOptions() {
3546
3557
  return {
3547
3558
  ...super._snapshotOptions(),
3548
- specifyComparable: this.specifyComparable,
3549
- isReverse: this.isReverse
3559
+ comparator: this._comparator
3550
3560
  };
3551
3561
  }
3552
3562
  /**
@@ -3574,14 +3584,14 @@ var _BST = class _BST extends BinaryTree {
3574
3584
  }
3575
3585
  /**
3576
3586
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3577
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3587
+ * @remarks Time O(1) Space O(1)
3578
3588
  *
3579
3589
  * @param a - The first key.
3580
3590
  * @param b - The second key.
3581
3591
  * @returns A number (1, -1, or 0) representing the comparison.
3582
3592
  */
3583
3593
  _compare(a, b) {
3584
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3594
+ return this._comparator(a, b);
3585
3595
  }
3586
3596
  /**
3587
3597
  * (Private) Deletes a node by its key.