data-structure-typed 2.2.5 → 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.
@@ -157,6 +157,14 @@ var _IterableEntryBase = class _IterableEntryBase {
157
157
  }
158
158
  return accumulator;
159
159
  }
160
+ /**
161
+ * Converts data structure to `[key, value]` pairs.
162
+ * @returns Array of entries.
163
+ * @remarks Time O(n), Space O(n)
164
+ */
165
+ toArray() {
166
+ return [...this];
167
+ }
160
168
  /**
161
169
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
162
170
  * @returns Array of entries (default) or a string.
@@ -6812,8 +6820,6 @@ var _Range = class _Range {
6812
6820
  this.high = high;
6813
6821
  this.includeLow = includeLow;
6814
6822
  this.includeHigh = includeHigh;
6815
- if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
6816
- if (low > high) throw new RangeError("low must be less than or equal to high");
6817
6823
  }
6818
6824
  // Determine whether a key is within the range
6819
6825
  isInRange(key, comparator) {
@@ -8957,77 +8963,63 @@ var _BST = class _BST extends BinaryTree {
8957
8963
  else _iterate();
8958
8964
  return inserted;
8959
8965
  }
8960
- /**
8961
- * Returns the first node with a key greater than or equal to the given key.
8962
- * This is equivalent to C++ std::lower_bound on a BST.
8963
- * Supports RECURSIVE and ITERATIVE implementations.
8964
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
8965
- * Space Complexity: O(h) for recursion, O(1) for iteration.
8966
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8967
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
8968
- * @returns The first node with key >= given key, or undefined if no such node exists.
8969
- */
8970
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8971
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
8972
- }
8973
- /**
8974
- * Returns the first node with a key strictly greater than the given key.
8975
- * This is equivalent to C++ std::upper_bound on a BST.
8976
- * Supports RECURSIVE and ITERATIVE implementations.
8977
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
8978
- * Space Complexity: O(h) for recursion, O(1) for iteration.
8979
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8980
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
8981
- * @returns The first node with key > given key, or undefined if no such node exists.
8982
- */
8983
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8984
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
8985
- }
8986
- /**
8987
- * Returns the first node with a key greater than or equal to the given key.
8988
- * This is equivalent to Java TreeMap.ceilingEntry().
8989
- * Supports RECURSIVE and ITERATIVE implementations.
8990
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
8991
- * Space Complexity: O(h) for recursion, O(1) for iteration.
8992
- *
8993
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8994
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
8995
- * @returns The first node with key >= given key, or undefined if no such node exists.
8996
- */
8997
- ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8998
- return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
8966
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
8967
+ let actualCallback = void 0;
8968
+ let actualIterationType = this.iterationType;
8969
+ if (typeof callback === "string") {
8970
+ actualIterationType = callback;
8971
+ } else if (callback) {
8972
+ actualCallback = callback;
8973
+ if (iterationType) {
8974
+ actualIterationType = iterationType;
8975
+ }
8976
+ }
8977
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
8978
+ if (!actualCallback) {
8979
+ return node == null ? void 0 : node.key;
8980
+ }
8981
+ return node ? actualCallback(node) : void 0;
8999
8982
  }
9000
- /**
9001
- * Returns the first node with a key strictly greater than the given key.
9002
- * This is equivalent to Java TreeMap.higherEntry().
9003
- * Supports RECURSIVE and ITERATIVE implementations.
9004
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9005
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9006
- *
9007
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9008
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9009
- * @returns The first node with key > given key, or undefined if no such node exists.
9010
- */
9011
- higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9012
- return this.upperBound(keyNodeEntryOrPredicate, iterationType);
8983
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
8984
+ let actualCallback = void 0;
8985
+ let actualIterationType = this.iterationType;
8986
+ if (typeof callback === "string") {
8987
+ actualIterationType = callback;
8988
+ } else if (callback) {
8989
+ actualCallback = callback;
8990
+ if (iterationType) {
8991
+ actualIterationType = iterationType;
8992
+ }
8993
+ }
8994
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
8995
+ if (!actualCallback) {
8996
+ return node == null ? void 0 : node.key;
8997
+ }
8998
+ return node ? actualCallback(node) : void 0;
9013
8999
  }
9014
- /**
9015
- * Returns the first node with a key less than or equal to the given key.
9016
- * This is equivalent to Java TreeMap.floorEntry().
9017
- * Supports RECURSIVE and ITERATIVE implementations.
9018
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9019
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9020
- *
9021
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9022
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9023
- * @returns The first node with key <= given key, or undefined if no such node exists.
9024
- */
9025
- floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9000
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
9026
9001
  if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9002
+ if (typeof callback === "string" || !callback) {
9003
+ return void 0;
9004
+ }
9027
9005
  return void 0;
9028
9006
  }
9007
+ let actualCallback = void 0;
9008
+ let actualIterationType = this.iterationType;
9009
+ if (typeof callback === "string") {
9010
+ actualIterationType = callback;
9011
+ } else if (callback) {
9012
+ actualCallback = callback;
9013
+ if (iterationType) {
9014
+ actualIterationType = iterationType;
9015
+ }
9016
+ }
9029
9017
  if (this._isPredicate(keyNodeEntryOrPredicate)) {
9030
- return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
9018
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9019
+ if (!actualCallback) {
9020
+ return node == null ? void 0 : node.key;
9021
+ }
9022
+ return node ? actualCallback(node) : void 0;
9031
9023
  }
9032
9024
  let targetKey;
9033
9025
  if (this.isNode(keyNodeEntryOrPredicate)) {
@@ -9035,6 +9027,9 @@ var _BST = class _BST extends BinaryTree {
9035
9027
  } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9036
9028
  const key = keyNodeEntryOrPredicate[0];
9037
9029
  if (key === null || key === void 0) {
9030
+ if (typeof callback === "string" || !callback) {
9031
+ return void 0;
9032
+ }
9038
9033
  return void 0;
9039
9034
  }
9040
9035
  targetKey = key;
@@ -9042,27 +9037,40 @@ var _BST = class _BST extends BinaryTree {
9042
9037
  targetKey = keyNodeEntryOrPredicate;
9043
9038
  }
9044
9039
  if (targetKey !== void 0) {
9045
- return this._floorByKey(targetKey, iterationType);
9040
+ const node = this._floorByKey(targetKey, actualIterationType);
9041
+ if (!actualCallback) {
9042
+ return node == null ? void 0 : node.key;
9043
+ }
9044
+ return node ? actualCallback(node) : void 0;
9045
+ }
9046
+ if (typeof callback === "string" || !callback) {
9047
+ return void 0;
9046
9048
  }
9047
9049
  return void 0;
9048
9050
  }
9049
- /**
9050
- * Returns the first node with a key strictly less than the given key.
9051
- * This is equivalent to Java TreeMap.lowerEntry().
9052
- * Supports RECURSIVE and ITERATIVE implementations.
9053
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9054
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9055
- *
9056
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9057
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9058
- * @returns The first node with key < given key, or undefined if no such node exists.
9059
- */
9060
- lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9051
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
9061
9052
  if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9053
+ if (typeof callback === "string" || !callback) {
9054
+ return void 0;
9055
+ }
9062
9056
  return void 0;
9063
9057
  }
9058
+ let actualCallback = void 0;
9059
+ let actualIterationType = this.iterationType;
9060
+ if (typeof callback === "string") {
9061
+ actualIterationType = callback;
9062
+ } else if (callback) {
9063
+ actualCallback = callback;
9064
+ if (iterationType) {
9065
+ actualIterationType = iterationType;
9066
+ }
9067
+ }
9064
9068
  if (this._isPredicate(keyNodeEntryOrPredicate)) {
9065
- return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
9069
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9070
+ if (!actualCallback) {
9071
+ return node == null ? void 0 : node.key;
9072
+ }
9073
+ return node ? actualCallback(node) : void 0;
9066
9074
  }
9067
9075
  let targetKey;
9068
9076
  if (this.isNode(keyNodeEntryOrPredicate)) {
@@ -9070,6 +9078,9 @@ var _BST = class _BST extends BinaryTree {
9070
9078
  } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9071
9079
  const key = keyNodeEntryOrPredicate[0];
9072
9080
  if (key === null || key === void 0) {
9081
+ if (typeof callback === "string" || !callback) {
9082
+ return void 0;
9083
+ }
9073
9084
  return void 0;
9074
9085
  }
9075
9086
  targetKey = key;
@@ -9077,7 +9088,14 @@ var _BST = class _BST extends BinaryTree {
9077
9088
  targetKey = keyNodeEntryOrPredicate;
9078
9089
  }
9079
9090
  if (targetKey !== void 0) {
9080
- return this._lowerByKey(targetKey, iterationType);
9091
+ const node = this._lowerByKey(targetKey, actualIterationType);
9092
+ if (!actualCallback) {
9093
+ return node == null ? void 0 : node.key;
9094
+ }
9095
+ return node ? actualCallback(node) : void 0;
9096
+ }
9097
+ if (typeof callback === "string" || !callback) {
9098
+ return void 0;
9081
9099
  }
9082
9100
  return void 0;
9083
9101
  }
@@ -9266,7 +9284,6 @@ var _BST = class _BST extends BinaryTree {
9266
9284
  */
9267
9285
  _createDefaultComparator() {
9268
9286
  return (a, b) => {
9269
- debugger;
9270
9287
  if (isComparable(a) && isComparable(b)) {
9271
9288
  if (a > b) return 1;
9272
9289
  if (a < b) return -1;