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