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.
@@ -1000,6 +1000,14 @@ var IterableEntryBase = class {
1000
1000
  }
1001
1001
  return accumulator;
1002
1002
  }
1003
+ /**
1004
+ * Converts data structure to `[key, value]` pairs.
1005
+ * @returns Array of entries.
1006
+ * @remarks Time O(n), Space O(n)
1007
+ */
1008
+ toArray() {
1009
+ return [...this];
1010
+ }
1003
1011
  /**
1004
1012
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
1005
1013
  * @returns Array of entries (default) or a string.
@@ -1029,8 +1037,6 @@ var Range = class {
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
  static {
1036
1042
  __name(this, "Range");
@@ -2855,27 +2861,6 @@ var BST = class 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
  * The comparator function used to determine the order of keys in the tree.
2881
2866
 
@@ -3197,31 +3182,141 @@ var BST = class extends BinaryTree {
3197
3182
  else _iterate();
3198
3183
  return inserted;
3199
3184
  }
3200
- /**
3201
- * Returns the first node with a key greater than or equal to the given key.
3202
- * This is equivalent to C++ std::lower_bound on a BST.
3203
- * Supports RECURSIVE and ITERATIVE implementations.
3204
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3205
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3206
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3207
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3208
- * @returns The first node with key >= given key, or undefined if no such node exists.
3209
- */
3210
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3211
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3185
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3186
+ let actualCallback = void 0;
3187
+ let actualIterationType = this.iterationType;
3188
+ if (typeof callback === "string") {
3189
+ actualIterationType = callback;
3190
+ } else if (callback) {
3191
+ actualCallback = callback;
3192
+ if (iterationType) {
3193
+ actualIterationType = iterationType;
3194
+ }
3195
+ }
3196
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
3197
+ if (!actualCallback) {
3198
+ return node?.key;
3199
+ }
3200
+ return node ? actualCallback(node) : void 0;
3201
+ }
3202
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3203
+ let actualCallback = void 0;
3204
+ let actualIterationType = this.iterationType;
3205
+ if (typeof callback === "string") {
3206
+ actualIterationType = callback;
3207
+ } else if (callback) {
3208
+ actualCallback = callback;
3209
+ if (iterationType) {
3210
+ actualIterationType = iterationType;
3211
+ }
3212
+ }
3213
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
3214
+ if (!actualCallback) {
3215
+ return node?.key;
3216
+ }
3217
+ return node ? actualCallback(node) : void 0;
3212
3218
  }
3213
- /**
3214
- * Returns the first node with a key strictly greater than the given key.
3215
- * This is equivalent to C++ std::upper_bound on a BST.
3216
- * Supports RECURSIVE and ITERATIVE implementations.
3217
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3218
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3219
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3220
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3221
- * @returns The first node with key > given key, or undefined if no such node exists.
3222
- */
3223
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3224
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3219
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3220
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3221
+ if (typeof callback === "string" || !callback) {
3222
+ return void 0;
3223
+ }
3224
+ return void 0;
3225
+ }
3226
+ let actualCallback = void 0;
3227
+ let actualIterationType = this.iterationType;
3228
+ if (typeof callback === "string") {
3229
+ actualIterationType = callback;
3230
+ } else if (callback) {
3231
+ actualCallback = callback;
3232
+ if (iterationType) {
3233
+ actualIterationType = iterationType;
3234
+ }
3235
+ }
3236
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3237
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3238
+ if (!actualCallback) {
3239
+ return node?.key;
3240
+ }
3241
+ return node ? actualCallback(node) : void 0;
3242
+ }
3243
+ let targetKey;
3244
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3245
+ targetKey = keyNodeEntryOrPredicate.key;
3246
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3247
+ const key = keyNodeEntryOrPredicate[0];
3248
+ if (key === null || key === void 0) {
3249
+ if (typeof callback === "string" || !callback) {
3250
+ return void 0;
3251
+ }
3252
+ return void 0;
3253
+ }
3254
+ targetKey = key;
3255
+ } else {
3256
+ targetKey = keyNodeEntryOrPredicate;
3257
+ }
3258
+ if (targetKey !== void 0) {
3259
+ const node = this._floorByKey(targetKey, actualIterationType);
3260
+ if (!actualCallback) {
3261
+ return node?.key;
3262
+ }
3263
+ return node ? actualCallback(node) : void 0;
3264
+ }
3265
+ if (typeof callback === "string" || !callback) {
3266
+ return void 0;
3267
+ }
3268
+ return void 0;
3269
+ }
3270
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
3271
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3272
+ if (typeof callback === "string" || !callback) {
3273
+ return void 0;
3274
+ }
3275
+ return void 0;
3276
+ }
3277
+ let actualCallback = void 0;
3278
+ let actualIterationType = this.iterationType;
3279
+ if (typeof callback === "string") {
3280
+ actualIterationType = callback;
3281
+ } else if (callback) {
3282
+ actualCallback = callback;
3283
+ if (iterationType) {
3284
+ actualIterationType = iterationType;
3285
+ }
3286
+ }
3287
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3288
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3289
+ if (!actualCallback) {
3290
+ return node?.key;
3291
+ }
3292
+ return node ? actualCallback(node) : void 0;
3293
+ }
3294
+ let targetKey;
3295
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3296
+ targetKey = keyNodeEntryOrPredicate.key;
3297
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3298
+ const key = keyNodeEntryOrPredicate[0];
3299
+ if (key === null || key === void 0) {
3300
+ if (typeof callback === "string" || !callback) {
3301
+ return void 0;
3302
+ }
3303
+ return void 0;
3304
+ }
3305
+ targetKey = key;
3306
+ } else {
3307
+ targetKey = keyNodeEntryOrPredicate;
3308
+ }
3309
+ if (targetKey !== void 0) {
3310
+ const node = this._lowerByKey(targetKey, actualIterationType);
3311
+ if (!actualCallback) {
3312
+ return node?.key;
3313
+ }
3314
+ return node ? actualCallback(node) : void 0;
3315
+ }
3316
+ if (typeof callback === "string" || !callback) {
3317
+ return void 0;
3318
+ }
3319
+ return void 0;
3225
3320
  }
3226
3321
  /**
3227
3322
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
@@ -3393,13 +3488,7 @@ var BST = class extends BinaryTree {
3393
3488
  * - If no nodes match the search criteria, the returned map is empty.
3394
3489
  */
3395
3490
  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
- );
3491
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
3403
3492
  let results = [];
3404
3493
  for (const node of toDelete) {
3405
3494
  const deleteInfo = this.delete(node);
@@ -3407,6 +3496,191 @@ var BST = class extends BinaryTree {
3407
3496
  }
3408
3497
  return results;
3409
3498
  }
3499
+ /**
3500
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
3501
+ * @remarks Time O(1) Space O(1)
3502
+ * @returns The default comparator function.
3503
+ */
3504
+ _createDefaultComparator() {
3505
+ return (a, b) => {
3506
+ debugger;
3507
+ if (isComparable(a) && isComparable(b)) {
3508
+ if (a > b) return 1;
3509
+ if (a < b) return -1;
3510
+ return 0;
3511
+ }
3512
+ if (typeof a === "object" || typeof b === "object") {
3513
+ throw TypeError(
3514
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
3515
+ );
3516
+ }
3517
+ return 0;
3518
+ };
3519
+ }
3520
+ /**
3521
+ * (Protected) Binary search for floor by key with pruning optimization.
3522
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3523
+ * Finds first node where key <= target.
3524
+ * @remarks Time O(h) where h is tree height.
3525
+ *
3526
+ * @param key - The target key to search for.
3527
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3528
+ * @returns The first node with key <= target, or undefined if none exists.
3529
+ */
3530
+ _floorByKey(key, iterationType) {
3531
+ if (iterationType === "RECURSIVE") {
3532
+ const dfs = /* @__PURE__ */ __name((cur) => {
3533
+ if (!this.isRealNode(cur)) return void 0;
3534
+ const cmp = this.comparator(cur.key, key);
3535
+ if (cmp <= 0) {
3536
+ const rightResult = dfs(cur.right);
3537
+ return rightResult ?? cur;
3538
+ } else {
3539
+ return dfs(cur.left);
3540
+ }
3541
+ }, "dfs");
3542
+ return dfs(this.root);
3543
+ } else {
3544
+ let current = this.root;
3545
+ let result = void 0;
3546
+ while (this.isRealNode(current)) {
3547
+ const cmp = this.comparator(current.key, key);
3548
+ if (cmp <= 0) {
3549
+ result = current;
3550
+ current = current.right ?? void 0;
3551
+ } else {
3552
+ current = current.left ?? void 0;
3553
+ }
3554
+ }
3555
+ return result;
3556
+ }
3557
+ }
3558
+ /**
3559
+ * (Protected) In-order traversal search for floor by predicate.
3560
+ * Falls back to linear in-order traversal when predicate-based search is required.
3561
+ * Returns the last node that satisfies the predicate function.
3562
+ * @remarks Time Complexity: O(n) since it may visit every node.
3563
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3564
+ *
3565
+ * @param predicate - The predicate function to test nodes.
3566
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3567
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
3568
+ */
3569
+ _floorByPredicate(predicate, iterationType) {
3570
+ if (iterationType === "RECURSIVE") {
3571
+ let result = void 0;
3572
+ const dfs = /* @__PURE__ */ __name((cur) => {
3573
+ if (!this.isRealNode(cur)) return;
3574
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3575
+ if (predicate(cur)) {
3576
+ result = cur;
3577
+ }
3578
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3579
+ }, "dfs");
3580
+ dfs(this.root);
3581
+ return result;
3582
+ } else {
3583
+ const stack = [];
3584
+ let current = this.root;
3585
+ let result = void 0;
3586
+ while (stack.length > 0 || this.isRealNode(current)) {
3587
+ if (this.isRealNode(current)) {
3588
+ stack.push(current);
3589
+ current = current.left;
3590
+ } else {
3591
+ const node = stack.pop();
3592
+ if (!this.isRealNode(node)) break;
3593
+ if (predicate(node)) {
3594
+ result = node;
3595
+ }
3596
+ current = node.right;
3597
+ }
3598
+ }
3599
+ return result;
3600
+ }
3601
+ }
3602
+ /**
3603
+ * (Protected) Binary search for lower by key with pruning optimization.
3604
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3605
+ * Finds first node where key < target.
3606
+ * @remarks Time O(h) where h is tree height.
3607
+ *
3608
+ * @param key - The target key to search for.
3609
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3610
+ * @returns The first node with key < target, or undefined if none exists.
3611
+ */
3612
+ _lowerByKey(key, iterationType) {
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 ?? 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 = current.right ?? void 0;
3633
+ } else {
3634
+ current = current.left ?? 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
+ }
3410
3684
  /**
3411
3685
  * (Protected) Core bound search implementation supporting all parameter types.
3412
3686
  * Unified logic for both lowerBound and upperBound.