tree-multimap-typed 2.2.4 → 2.2.7

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.
@@ -996,6 +996,14 @@ var _IterableEntryBase = class _IterableEntryBase {
996
996
  }
997
997
  return accumulator;
998
998
  }
999
+ /**
1000
+ * Converts data structure to `[key, value]` pairs.
1001
+ * @returns Array of entries.
1002
+ * @remarks Time O(n), Space O(n)
1003
+ */
1004
+ toArray() {
1005
+ return [...this];
1006
+ }
999
1007
  /**
1000
1008
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
1001
1009
  * @returns Array of entries (default) or a string.
@@ -1027,8 +1035,6 @@ var _Range = class _Range {
1027
1035
  this.high = high;
1028
1036
  this.includeLow = includeLow;
1029
1037
  this.includeHigh = includeHigh;
1030
- if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
1031
- if (low > high) throw new RangeError("low must be less than or equal to high");
1032
1038
  }
1033
1039
  // Determine whether a key is within the range
1034
1040
  isInRange(key, comparator) {
@@ -2855,27 +2861,6 @@ var _BST = class _BST extends BinaryTree {
2855
2861
  get root() {
2856
2862
  return this._root;
2857
2863
  }
2858
- /**
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.
2862
- */
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
- };
2878
- }
2879
2864
  /**
2880
2865
  * Gets the comparator function used by the tree.
2881
2866
  * @remarks Time O(1)
@@ -3193,31 +3178,141 @@ var _BST = class _BST extends BinaryTree {
3193
3178
  else _iterate();
3194
3179
  return inserted;
3195
3180
  }
3196
- /**
3197
- * Returns the first node with a key greater than or equal to the given key.
3198
- * This is equivalent to C++ std::lower_bound on a BST.
3199
- * Supports RECURSIVE and ITERATIVE implementations.
3200
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3201
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3202
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3203
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3204
- * @returns The first node with key >= given key, or undefined if no such node exists.
3205
- */
3206
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3207
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3181
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3182
+ let actualCallback = void 0;
3183
+ let actualIterationType = this.iterationType;
3184
+ if (typeof callback === "string") {
3185
+ actualIterationType = callback;
3186
+ } else if (callback) {
3187
+ actualCallback = callback;
3188
+ if (iterationType) {
3189
+ actualIterationType = iterationType;
3190
+ }
3191
+ }
3192
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
3193
+ if (!actualCallback) {
3194
+ return node == null ? void 0 : node.key;
3195
+ }
3196
+ return node ? actualCallback(node) : void 0;
3197
+ }
3198
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3199
+ let actualCallback = void 0;
3200
+ let actualIterationType = this.iterationType;
3201
+ if (typeof callback === "string") {
3202
+ actualIterationType = callback;
3203
+ } else if (callback) {
3204
+ actualCallback = callback;
3205
+ if (iterationType) {
3206
+ actualIterationType = iterationType;
3207
+ }
3208
+ }
3209
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
3210
+ if (!actualCallback) {
3211
+ return node == null ? void 0 : node.key;
3212
+ }
3213
+ return node ? actualCallback(node) : void 0;
3208
3214
  }
3209
- /**
3210
- * Returns the first node with a key strictly greater than the given key.
3211
- * This is equivalent to C++ std::upper_bound on a BST.
3212
- * Supports RECURSIVE and ITERATIVE implementations.
3213
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3214
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3215
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3216
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3217
- * @returns The first node with key > given key, or undefined if no such node exists.
3218
- */
3219
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3220
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3215
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3216
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3217
+ if (typeof callback === "string" || !callback) {
3218
+ return void 0;
3219
+ }
3220
+ return void 0;
3221
+ }
3222
+ let actualCallback = void 0;
3223
+ let actualIterationType = this.iterationType;
3224
+ if (typeof callback === "string") {
3225
+ actualIterationType = callback;
3226
+ } else if (callback) {
3227
+ actualCallback = callback;
3228
+ if (iterationType) {
3229
+ actualIterationType = iterationType;
3230
+ }
3231
+ }
3232
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3233
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3234
+ if (!actualCallback) {
3235
+ return node == null ? void 0 : node.key;
3236
+ }
3237
+ return node ? actualCallback(node) : void 0;
3238
+ }
3239
+ let targetKey;
3240
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3241
+ targetKey = keyNodeEntryOrPredicate.key;
3242
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3243
+ const key = keyNodeEntryOrPredicate[0];
3244
+ if (key === null || key === void 0) {
3245
+ if (typeof callback === "string" || !callback) {
3246
+ return void 0;
3247
+ }
3248
+ return void 0;
3249
+ }
3250
+ targetKey = key;
3251
+ } else {
3252
+ targetKey = keyNodeEntryOrPredicate;
3253
+ }
3254
+ if (targetKey !== void 0) {
3255
+ const node = this._floorByKey(targetKey, actualIterationType);
3256
+ if (!actualCallback) {
3257
+ return node == null ? void 0 : node.key;
3258
+ }
3259
+ return node ? actualCallback(node) : void 0;
3260
+ }
3261
+ if (typeof callback === "string" || !callback) {
3262
+ return void 0;
3263
+ }
3264
+ return void 0;
3265
+ }
3266
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
3267
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3268
+ if (typeof callback === "string" || !callback) {
3269
+ return void 0;
3270
+ }
3271
+ return void 0;
3272
+ }
3273
+ let actualCallback = void 0;
3274
+ let actualIterationType = this.iterationType;
3275
+ if (typeof callback === "string") {
3276
+ actualIterationType = callback;
3277
+ } else if (callback) {
3278
+ actualCallback = callback;
3279
+ if (iterationType) {
3280
+ actualIterationType = iterationType;
3281
+ }
3282
+ }
3283
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3284
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3285
+ if (!actualCallback) {
3286
+ return node == null ? void 0 : node.key;
3287
+ }
3288
+ return node ? actualCallback(node) : void 0;
3289
+ }
3290
+ let targetKey;
3291
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3292
+ targetKey = keyNodeEntryOrPredicate.key;
3293
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3294
+ const key = keyNodeEntryOrPredicate[0];
3295
+ if (key === null || key === void 0) {
3296
+ if (typeof callback === "string" || !callback) {
3297
+ return void 0;
3298
+ }
3299
+ return void 0;
3300
+ }
3301
+ targetKey = key;
3302
+ } else {
3303
+ targetKey = keyNodeEntryOrPredicate;
3304
+ }
3305
+ if (targetKey !== void 0) {
3306
+ const node = this._lowerByKey(targetKey, actualIterationType);
3307
+ if (!actualCallback) {
3308
+ return node == null ? void 0 : node.key;
3309
+ }
3310
+ return node ? actualCallback(node) : void 0;
3311
+ }
3312
+ if (typeof callback === "string" || !callback) {
3313
+ return void 0;
3314
+ }
3315
+ return void 0;
3221
3316
  }
3222
3317
  /**
3223
3318
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
@@ -3389,13 +3484,7 @@ var _BST = class _BST extends BinaryTree {
3389
3484
  * - If no nodes match the search criteria, the returned map is empty.
3390
3485
  */
3391
3486
  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
- );
3487
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
3399
3488
  let results = [];
3400
3489
  for (const node of toDelete) {
3401
3490
  const deleteInfo = this.delete(node);
@@ -3403,6 +3492,193 @@ var _BST = class _BST extends BinaryTree {
3403
3492
  }
3404
3493
  return results;
3405
3494
  }
3495
+ /**
3496
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
3497
+ * @remarks Time O(1) Space O(1)
3498
+ * @returns The default comparator function.
3499
+ */
3500
+ _createDefaultComparator() {
3501
+ return (a, b) => {
3502
+ debugger;
3503
+ if (isComparable(a) && isComparable(b)) {
3504
+ if (a > b) return 1;
3505
+ if (a < b) return -1;
3506
+ return 0;
3507
+ }
3508
+ if (typeof a === "object" || typeof b === "object") {
3509
+ throw TypeError(
3510
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
3511
+ );
3512
+ }
3513
+ return 0;
3514
+ };
3515
+ }
3516
+ /**
3517
+ * (Protected) Binary search for floor by key with pruning optimization.
3518
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3519
+ * Finds first node where key <= target.
3520
+ * @remarks Time O(h) where h is tree height.
3521
+ *
3522
+ * @param key - The target key to search for.
3523
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3524
+ * @returns The first node with key <= target, or undefined if none exists.
3525
+ */
3526
+ _floorByKey(key, iterationType) {
3527
+ var _a, _b;
3528
+ if (iterationType === "RECURSIVE") {
3529
+ const dfs = /* @__PURE__ */ __name((cur) => {
3530
+ if (!this.isRealNode(cur)) return void 0;
3531
+ const cmp = this.comparator(cur.key, key);
3532
+ if (cmp <= 0) {
3533
+ const rightResult = dfs(cur.right);
3534
+ return rightResult != null ? rightResult : cur;
3535
+ } else {
3536
+ return dfs(cur.left);
3537
+ }
3538
+ }, "dfs");
3539
+ return dfs(this.root);
3540
+ } else {
3541
+ let current = this.root;
3542
+ let result = void 0;
3543
+ while (this.isRealNode(current)) {
3544
+ const cmp = this.comparator(current.key, key);
3545
+ if (cmp <= 0) {
3546
+ result = current;
3547
+ current = (_a = current.right) != null ? _a : void 0;
3548
+ } else {
3549
+ current = (_b = current.left) != null ? _b : void 0;
3550
+ }
3551
+ }
3552
+ return result;
3553
+ }
3554
+ }
3555
+ /**
3556
+ * (Protected) In-order traversal search for floor by predicate.
3557
+ * Falls back to linear in-order traversal when predicate-based search is required.
3558
+ * Returns the last node that satisfies the predicate function.
3559
+ * @remarks Time Complexity: O(n) since it may visit every node.
3560
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3561
+ *
3562
+ * @param predicate - The predicate function to test nodes.
3563
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3564
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
3565
+ */
3566
+ _floorByPredicate(predicate, iterationType) {
3567
+ if (iterationType === "RECURSIVE") {
3568
+ let result = void 0;
3569
+ const dfs = /* @__PURE__ */ __name((cur) => {
3570
+ if (!this.isRealNode(cur)) return;
3571
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3572
+ if (predicate(cur)) {
3573
+ result = cur;
3574
+ }
3575
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3576
+ }, "dfs");
3577
+ dfs(this.root);
3578
+ return result;
3579
+ } else {
3580
+ const stack = [];
3581
+ let current = this.root;
3582
+ let result = void 0;
3583
+ while (stack.length > 0 || this.isRealNode(current)) {
3584
+ if (this.isRealNode(current)) {
3585
+ stack.push(current);
3586
+ current = current.left;
3587
+ } else {
3588
+ const node = stack.pop();
3589
+ if (!this.isRealNode(node)) break;
3590
+ if (predicate(node)) {
3591
+ result = node;
3592
+ }
3593
+ current = node.right;
3594
+ }
3595
+ }
3596
+ return result;
3597
+ }
3598
+ }
3599
+ /**
3600
+ * (Protected) Binary search for lower by key with pruning optimization.
3601
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3602
+ * Finds first node where key < target.
3603
+ * @remarks Time O(h) where h is tree height.
3604
+ *
3605
+ * @param key - The target key to search for.
3606
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3607
+ * @returns The first node with key < target, or undefined if none exists.
3608
+ */
3609
+ _lowerByKey(key, iterationType) {
3610
+ var _a, _b;
3611
+ if (iterationType === "RECURSIVE") {
3612
+ const dfs = /* @__PURE__ */ __name((cur) => {
3613
+ if (!this.isRealNode(cur)) return void 0;
3614
+ const cmp = this.comparator(cur.key, key);
3615
+ if (cmp < 0) {
3616
+ const rightResult = dfs(cur.right);
3617
+ return rightResult != null ? rightResult : cur;
3618
+ } else {
3619
+ return dfs(cur.left);
3620
+ }
3621
+ }, "dfs");
3622
+ return dfs(this.root);
3623
+ } else {
3624
+ let current = this.root;
3625
+ let result = void 0;
3626
+ while (this.isRealNode(current)) {
3627
+ const cmp = this.comparator(current.key, key);
3628
+ if (cmp < 0) {
3629
+ result = current;
3630
+ current = (_a = current.right) != null ? _a : void 0;
3631
+ } else {
3632
+ current = (_b = current.left) != null ? _b : void 0;
3633
+ }
3634
+ }
3635
+ return result;
3636
+ }
3637
+ }
3638
+ /**
3639
+ * (Protected) In-order traversal search for lower by predicate.
3640
+ * Falls back to linear in-order traversal when predicate-based search is required.
3641
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
3642
+ * @remarks Time Complexity: O(n) since it may visit every node.
3643
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3644
+ *
3645
+ * @param predicate - The predicate function to test nodes.
3646
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3647
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
3648
+ */
3649
+ _lowerByPredicate(predicate, iterationType) {
3650
+ if (iterationType === "RECURSIVE") {
3651
+ let result = void 0;
3652
+ const dfs = /* @__PURE__ */ __name((cur) => {
3653
+ if (!this.isRealNode(cur)) return;
3654
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3655
+ if (predicate(cur)) {
3656
+ result = cur;
3657
+ }
3658
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3659
+ }, "dfs");
3660
+ dfs(this.root);
3661
+ return result;
3662
+ } else {
3663
+ const stack = [];
3664
+ let current = this.root;
3665
+ let result = void 0;
3666
+ while (stack.length > 0 || this.isRealNode(current)) {
3667
+ if (this.isRealNode(current)) {
3668
+ stack.push(current);
3669
+ current = current.left;
3670
+ } else {
3671
+ const node = stack.pop();
3672
+ if (!this.isRealNode(node)) break;
3673
+ if (predicate(node)) {
3674
+ result = node;
3675
+ }
3676
+ current = node.right;
3677
+ }
3678
+ }
3679
+ return result;
3680
+ }
3681
+ }
3406
3682
  /**
3407
3683
  * (Protected) Core bound search implementation supporting all parameter types.
3408
3684
  * Unified logic for both lowerBound and upperBound.