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