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