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