avl-tree-typed 2.2.5 → 2.2.6

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.
@@ -1024,8 +1032,6 @@ var Range = class {
1024
1032
  this.high = high;
1025
1033
  this.includeLow = includeLow;
1026
1034
  this.includeHigh = includeHigh;
1027
- if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
1028
- if (low > high) throw new RangeError("low must be less than or equal to high");
1029
1035
  }
1030
1036
  static {
1031
1037
  __name(this, "Range");
@@ -3171,77 +3177,63 @@ var BST = class extends BinaryTree {
3171
3177
  else _iterate();
3172
3178
  return inserted;
3173
3179
  }
3174
- /**
3175
- * Returns the first node with a key greater than or equal to the given key.
3176
- * This is equivalent to C++ std::lower_bound on a BST.
3177
- * Supports RECURSIVE and ITERATIVE implementations.
3178
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3179
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3180
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3181
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3182
- * @returns The first node with key >= given key, or undefined if no such node exists.
3183
- */
3184
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3185
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3186
- }
3187
- /**
3188
- * Returns the first node with a key strictly greater than the given key.
3189
- * This is equivalent to C++ std::upper_bound on a BST.
3190
- * Supports RECURSIVE and ITERATIVE implementations.
3191
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3192
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3193
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3194
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3195
- * @returns The first node with key > given key, or undefined if no such node exists.
3196
- */
3197
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3198
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3199
- }
3200
- /**
3201
- * Returns the first node with a key greater than or equal to the given key.
3202
- * This is equivalent to Java TreeMap.ceilingEntry().
3203
- * Supports RECURSIVE and ITERATIVE implementations.
3204
- * @remarks 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
- *
3207
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3208
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3209
- * @returns The first node with key >= given key, or undefined if no such node exists.
3210
- */
3211
- ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3212
- return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
3213
- }
3214
- /**
3215
- * Returns the first node with a key strictly greater than the given key.
3216
- * This is equivalent to Java TreeMap.higherEntry().
3217
- * Supports RECURSIVE and ITERATIVE implementations.
3218
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
3219
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3220
- *
3221
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3222
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3223
- * @returns The first node with key > given key, or undefined if no such node exists.
3224
- */
3225
- higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3226
- return this.upperBound(keyNodeEntryOrPredicate, iterationType);
3180
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3181
+ let actualCallback = void 0;
3182
+ let actualIterationType = this.iterationType;
3183
+ if (typeof callback === "string") {
3184
+ actualIterationType = callback;
3185
+ } else if (callback) {
3186
+ actualCallback = callback;
3187
+ if (iterationType) {
3188
+ actualIterationType = iterationType;
3189
+ }
3190
+ }
3191
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
3192
+ if (!actualCallback) {
3193
+ return node?.key;
3194
+ }
3195
+ return node ? actualCallback(node) : void 0;
3196
+ }
3197
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3198
+ let actualCallback = void 0;
3199
+ let actualIterationType = this.iterationType;
3200
+ if (typeof callback === "string") {
3201
+ actualIterationType = callback;
3202
+ } else if (callback) {
3203
+ actualCallback = callback;
3204
+ if (iterationType) {
3205
+ actualIterationType = iterationType;
3206
+ }
3207
+ }
3208
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
3209
+ if (!actualCallback) {
3210
+ return node?.key;
3211
+ }
3212
+ return node ? actualCallback(node) : void 0;
3227
3213
  }
3228
- /**
3229
- * Returns the first node with a key less than or equal to the given key.
3230
- * This is equivalent to Java TreeMap.floorEntry().
3231
- * Supports RECURSIVE and ITERATIVE implementations.
3232
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
3233
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3234
- *
3235
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3236
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3237
- * @returns The first node with key <= given key, or undefined if no such node exists.
3238
- */
3239
- floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3214
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3240
3215
  if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3216
+ if (typeof callback === "string" || !callback) {
3217
+ return void 0;
3218
+ }
3241
3219
  return void 0;
3242
3220
  }
3221
+ let actualCallback = void 0;
3222
+ let actualIterationType = this.iterationType;
3223
+ if (typeof callback === "string") {
3224
+ actualIterationType = callback;
3225
+ } else if (callback) {
3226
+ actualCallback = callback;
3227
+ if (iterationType) {
3228
+ actualIterationType = iterationType;
3229
+ }
3230
+ }
3243
3231
  if (this._isPredicate(keyNodeEntryOrPredicate)) {
3244
- return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
3232
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3233
+ if (!actualCallback) {
3234
+ return node?.key;
3235
+ }
3236
+ return node ? actualCallback(node) : void 0;
3245
3237
  }
3246
3238
  let targetKey;
3247
3239
  if (this.isNode(keyNodeEntryOrPredicate)) {
@@ -3249,6 +3241,9 @@ var BST = class extends BinaryTree {
3249
3241
  } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3250
3242
  const key = keyNodeEntryOrPredicate[0];
3251
3243
  if (key === null || key === void 0) {
3244
+ if (typeof callback === "string" || !callback) {
3245
+ return void 0;
3246
+ }
3252
3247
  return void 0;
3253
3248
  }
3254
3249
  targetKey = key;
@@ -3256,27 +3251,40 @@ var BST = class extends BinaryTree {
3256
3251
  targetKey = keyNodeEntryOrPredicate;
3257
3252
  }
3258
3253
  if (targetKey !== void 0) {
3259
- return this._floorByKey(targetKey, iterationType);
3254
+ const node = this._floorByKey(targetKey, actualIterationType);
3255
+ if (!actualCallback) {
3256
+ return node?.key;
3257
+ }
3258
+ return node ? actualCallback(node) : void 0;
3259
+ }
3260
+ if (typeof callback === "string" || !callback) {
3261
+ return void 0;
3260
3262
  }
3261
3263
  return void 0;
3262
3264
  }
3263
- /**
3264
- * Returns the first node with a key strictly less than the given key.
3265
- * This is equivalent to Java TreeMap.lowerEntry().
3266
- * Supports RECURSIVE and ITERATIVE implementations.
3267
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
3268
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3269
- *
3270
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3271
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3272
- * @returns The first node with key < given key, or undefined if no such node exists.
3273
- */
3274
- lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3265
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
3275
3266
  if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3267
+ if (typeof callback === "string" || !callback) {
3268
+ return void 0;
3269
+ }
3276
3270
  return void 0;
3277
3271
  }
3272
+ let actualCallback = void 0;
3273
+ let actualIterationType = this.iterationType;
3274
+ if (typeof callback === "string") {
3275
+ actualIterationType = callback;
3276
+ } else if (callback) {
3277
+ actualCallback = callback;
3278
+ if (iterationType) {
3279
+ actualIterationType = iterationType;
3280
+ }
3281
+ }
3278
3282
  if (this._isPredicate(keyNodeEntryOrPredicate)) {
3279
- return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
3283
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3284
+ if (!actualCallback) {
3285
+ return node?.key;
3286
+ }
3287
+ return node ? actualCallback(node) : void 0;
3280
3288
  }
3281
3289
  let targetKey;
3282
3290
  if (this.isNode(keyNodeEntryOrPredicate)) {
@@ -3284,6 +3292,9 @@ var BST = class extends BinaryTree {
3284
3292
  } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3285
3293
  const key = keyNodeEntryOrPredicate[0];
3286
3294
  if (key === null || key === void 0) {
3295
+ if (typeof callback === "string" || !callback) {
3296
+ return void 0;
3297
+ }
3287
3298
  return void 0;
3288
3299
  }
3289
3300
  targetKey = key;
@@ -3291,7 +3302,14 @@ var BST = class extends BinaryTree {
3291
3302
  targetKey = keyNodeEntryOrPredicate;
3292
3303
  }
3293
3304
  if (targetKey !== void 0) {
3294
- return this._lowerByKey(targetKey, iterationType);
3305
+ const node = this._lowerByKey(targetKey, actualIterationType);
3306
+ if (!actualCallback) {
3307
+ return node?.key;
3308
+ }
3309
+ return node ? actualCallback(node) : void 0;
3310
+ }
3311
+ if (typeof callback === "string" || !callback) {
3312
+ return void 0;
3295
3313
  }
3296
3314
  return void 0;
3297
3315
  }