@itwin/core-bentley 4.0.0-dev.7 → 4.0.0-dev.72

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.
Files changed (211) hide show
  1. package/CHANGELOG.md +50 -1
  2. package/lib/cjs/AccessToken.d.ts +10 -10
  3. package/lib/cjs/AccessToken.d.ts.map +1 -1
  4. package/lib/cjs/AccessToken.js +9 -9
  5. package/lib/cjs/Assert.d.ts +25 -25
  6. package/lib/cjs/Assert.js +45 -45
  7. package/lib/cjs/Assert.js.map +1 -1
  8. package/lib/cjs/BeEvent.d.ts +81 -81
  9. package/lib/cjs/BeEvent.d.ts.map +1 -1
  10. package/lib/cjs/BeEvent.js +156 -156
  11. package/lib/cjs/BeSQLite.d.ts +172 -170
  12. package/lib/cjs/BeSQLite.d.ts.map +1 -1
  13. package/lib/cjs/BeSQLite.js +185 -183
  14. package/lib/cjs/BeSQLite.js.map +1 -1
  15. package/lib/cjs/BentleyError.d.ts +378 -378
  16. package/lib/cjs/BentleyError.d.ts.map +1 -1
  17. package/lib/cjs/BentleyError.js +703 -702
  18. package/lib/cjs/BentleyError.js.map +1 -1
  19. package/lib/cjs/BentleyLoggerCategory.d.ts +11 -11
  20. package/lib/cjs/BentleyLoggerCategory.js +19 -19
  21. package/lib/cjs/ByteStream.d.ts +110 -110
  22. package/lib/cjs/ByteStream.js +159 -159
  23. package/lib/cjs/ClassUtils.d.ts +14 -14
  24. package/lib/cjs/ClassUtils.js +27 -27
  25. package/lib/cjs/Compare.d.ts +47 -47
  26. package/lib/cjs/Compare.d.ts.map +1 -1
  27. package/lib/cjs/Compare.js +75 -75
  28. package/lib/cjs/CompressedId64Set.d.ts +134 -134
  29. package/lib/cjs/CompressedId64Set.d.ts.map +1 -1
  30. package/lib/cjs/CompressedId64Set.js +428 -428
  31. package/lib/cjs/CompressedId64Set.js.map +1 -1
  32. package/lib/cjs/Dictionary.d.ts +125 -125
  33. package/lib/cjs/Dictionary.js +203 -203
  34. package/lib/cjs/Disposable.d.ts +80 -80
  35. package/lib/cjs/Disposable.d.ts.map +1 -1
  36. package/lib/cjs/Disposable.js +120 -120
  37. package/lib/cjs/Id.d.ts +285 -285
  38. package/lib/cjs/Id.d.ts.map +1 -1
  39. package/lib/cjs/Id.js +643 -643
  40. package/lib/cjs/IndexMap.d.ts +65 -65
  41. package/lib/cjs/IndexMap.js +91 -91
  42. package/lib/cjs/JsonSchema.d.ts +77 -77
  43. package/lib/cjs/JsonSchema.d.ts.map +1 -1
  44. package/lib/cjs/JsonSchema.js +9 -9
  45. package/lib/cjs/JsonUtils.d.ts +78 -78
  46. package/lib/cjs/JsonUtils.js +151 -151
  47. package/lib/cjs/LRUMap.d.ts +129 -129
  48. package/lib/cjs/LRUMap.js +333 -333
  49. package/lib/cjs/LRUMap.js.map +1 -1
  50. package/lib/cjs/Logger.d.ts +143 -143
  51. package/lib/cjs/Logger.d.ts.map +1 -1
  52. package/lib/cjs/Logger.js +256 -258
  53. package/lib/cjs/Logger.js.map +1 -1
  54. package/lib/cjs/ObservableSet.d.ts +23 -23
  55. package/lib/cjs/ObservableSet.js +51 -51
  56. package/lib/cjs/OneAtATimeAction.d.ts +31 -31
  57. package/lib/cjs/OneAtATimeAction.js +94 -94
  58. package/lib/cjs/OneAtATimeAction.js.map +1 -1
  59. package/lib/cjs/OrderedId64Iterable.d.ts +74 -74
  60. package/lib/cjs/OrderedId64Iterable.d.ts.map +1 -1
  61. package/lib/cjs/OrderedId64Iterable.js +235 -235
  62. package/lib/cjs/OrderedSet.d.ts +40 -40
  63. package/lib/cjs/OrderedSet.js +64 -64
  64. package/lib/cjs/PriorityQueue.d.ts +70 -70
  65. package/lib/cjs/PriorityQueue.d.ts.map +1 -1
  66. package/lib/cjs/PriorityQueue.js +140 -140
  67. package/lib/cjs/ProcessDetector.d.ts +59 -59
  68. package/lib/cjs/ProcessDetector.js +71 -71
  69. package/lib/cjs/SortedArray.d.ts +236 -232
  70. package/lib/cjs/SortedArray.d.ts.map +1 -1
  71. package/lib/cjs/SortedArray.js +315 -303
  72. package/lib/cjs/SortedArray.js.map +1 -1
  73. package/lib/cjs/StatusCategory.d.ts +30 -30
  74. package/lib/cjs/StatusCategory.d.ts.map +1 -1
  75. package/lib/cjs/StatusCategory.js +460 -460
  76. package/lib/cjs/StatusCategory.js.map +1 -1
  77. package/lib/cjs/StringUtils.d.ts +22 -22
  78. package/lib/cjs/StringUtils.js +148 -148
  79. package/lib/cjs/Time.d.ts +122 -122
  80. package/lib/cjs/Time.js +152 -152
  81. package/lib/cjs/Time.js.map +1 -1
  82. package/lib/cjs/Tracing.d.ts +43 -40
  83. package/lib/cjs/Tracing.d.ts.map +1 -1
  84. package/lib/cjs/Tracing.js +134 -130
  85. package/lib/cjs/Tracing.js.map +1 -1
  86. package/lib/cjs/TupleKeyedMap.d.ts +36 -36
  87. package/lib/cjs/TupleKeyedMap.js +102 -102
  88. package/lib/cjs/TypedArrayBuilder.d.ts +155 -155
  89. package/lib/cjs/TypedArrayBuilder.d.ts.map +1 -1
  90. package/lib/cjs/TypedArrayBuilder.js +206 -208
  91. package/lib/cjs/TypedArrayBuilder.js.map +1 -1
  92. package/lib/cjs/UnexpectedErrors.d.ts +43 -43
  93. package/lib/cjs/UnexpectedErrors.d.ts.map +1 -1
  94. package/lib/cjs/UnexpectedErrors.js +68 -68
  95. package/lib/cjs/UnexpectedErrors.js.map +1 -1
  96. package/lib/cjs/UtilityTypes.d.ts +112 -96
  97. package/lib/cjs/UtilityTypes.d.ts.map +1 -1
  98. package/lib/cjs/UtilityTypes.js +40 -40
  99. package/lib/cjs/UtilityTypes.js.map +1 -1
  100. package/lib/cjs/YieldManager.d.ts +18 -18
  101. package/lib/cjs/YieldManager.js +34 -34
  102. package/lib/cjs/core-bentley.d.ts +74 -74
  103. package/lib/cjs/core-bentley.js +94 -90
  104. package/lib/cjs/core-bentley.js.map +1 -1
  105. package/lib/cjs/partitionArray.d.ts +21 -21
  106. package/lib/cjs/partitionArray.js +43 -43
  107. package/lib/esm/AccessToken.d.ts +10 -10
  108. package/lib/esm/AccessToken.d.ts.map +1 -1
  109. package/lib/esm/AccessToken.js +8 -8
  110. package/lib/esm/Assert.d.ts +25 -25
  111. package/lib/esm/Assert.js +41 -41
  112. package/lib/esm/Assert.js.map +1 -1
  113. package/lib/esm/BeEvent.d.ts +81 -81
  114. package/lib/esm/BeEvent.d.ts.map +1 -1
  115. package/lib/esm/BeEvent.js +150 -150
  116. package/lib/esm/BeSQLite.d.ts +172 -170
  117. package/lib/esm/BeSQLite.d.ts.map +1 -1
  118. package/lib/esm/BeSQLite.js +182 -180
  119. package/lib/esm/BeSQLite.js.map +1 -1
  120. package/lib/esm/BentleyError.d.ts +378 -378
  121. package/lib/esm/BentleyError.d.ts.map +1 -1
  122. package/lib/esm/BentleyError.js +699 -698
  123. package/lib/esm/BentleyError.js.map +1 -1
  124. package/lib/esm/BentleyLoggerCategory.d.ts +11 -11
  125. package/lib/esm/BentleyLoggerCategory.js +16 -16
  126. package/lib/esm/ByteStream.d.ts +110 -110
  127. package/lib/esm/ByteStream.js +155 -155
  128. package/lib/esm/ClassUtils.d.ts +14 -14
  129. package/lib/esm/ClassUtils.js +22 -22
  130. package/lib/esm/Compare.d.ts +47 -47
  131. package/lib/esm/Compare.d.ts.map +1 -1
  132. package/lib/esm/Compare.js +63 -63
  133. package/lib/esm/CompressedId64Set.d.ts +134 -134
  134. package/lib/esm/CompressedId64Set.d.ts.map +1 -1
  135. package/lib/esm/CompressedId64Set.js +423 -423
  136. package/lib/esm/CompressedId64Set.js.map +1 -1
  137. package/lib/esm/Dictionary.d.ts +125 -125
  138. package/lib/esm/Dictionary.js +199 -199
  139. package/lib/esm/Disposable.d.ts +80 -80
  140. package/lib/esm/Disposable.d.ts.map +1 -1
  141. package/lib/esm/Disposable.js +112 -112
  142. package/lib/esm/Id.d.ts +285 -285
  143. package/lib/esm/Id.d.ts.map +1 -1
  144. package/lib/esm/Id.js +639 -639
  145. package/lib/esm/IndexMap.d.ts +65 -65
  146. package/lib/esm/IndexMap.js +86 -86
  147. package/lib/esm/JsonSchema.d.ts +77 -77
  148. package/lib/esm/JsonSchema.d.ts.map +1 -1
  149. package/lib/esm/JsonSchema.js +8 -8
  150. package/lib/esm/JsonUtils.d.ts +78 -78
  151. package/lib/esm/JsonUtils.js +148 -148
  152. package/lib/esm/LRUMap.d.ts +129 -129
  153. package/lib/esm/LRUMap.js +326 -326
  154. package/lib/esm/LRUMap.js.map +1 -1
  155. package/lib/esm/Logger.d.ts +143 -143
  156. package/lib/esm/Logger.d.ts.map +1 -1
  157. package/lib/esm/Logger.js +253 -253
  158. package/lib/esm/Logger.js.map +1 -1
  159. package/lib/esm/ObservableSet.d.ts +23 -23
  160. package/lib/esm/ObservableSet.js +47 -47
  161. package/lib/esm/OneAtATimeAction.d.ts +31 -31
  162. package/lib/esm/OneAtATimeAction.js +89 -89
  163. package/lib/esm/OneAtATimeAction.js.map +1 -1
  164. package/lib/esm/OrderedId64Iterable.d.ts +74 -74
  165. package/lib/esm/OrderedId64Iterable.d.ts.map +1 -1
  166. package/lib/esm/OrderedId64Iterable.js +232 -232
  167. package/lib/esm/OrderedSet.d.ts +40 -40
  168. package/lib/esm/OrderedSet.js +59 -59
  169. package/lib/esm/PriorityQueue.d.ts +70 -70
  170. package/lib/esm/PriorityQueue.d.ts.map +1 -1
  171. package/lib/esm/PriorityQueue.js +136 -136
  172. package/lib/esm/ProcessDetector.d.ts +59 -59
  173. package/lib/esm/ProcessDetector.js +67 -67
  174. package/lib/esm/SortedArray.d.ts +236 -232
  175. package/lib/esm/SortedArray.d.ts.map +1 -1
  176. package/lib/esm/SortedArray.js +308 -296
  177. package/lib/esm/SortedArray.js.map +1 -1
  178. package/lib/esm/StatusCategory.d.ts +30 -30
  179. package/lib/esm/StatusCategory.d.ts.map +1 -1
  180. package/lib/esm/StatusCategory.js +455 -454
  181. package/lib/esm/StatusCategory.js.map +1 -1
  182. package/lib/esm/StringUtils.d.ts +22 -22
  183. package/lib/esm/StringUtils.js +142 -142
  184. package/lib/esm/Time.d.ts +122 -122
  185. package/lib/esm/Time.js +146 -146
  186. package/lib/esm/Time.js.map +1 -1
  187. package/lib/esm/Tracing.d.ts +43 -40
  188. package/lib/esm/Tracing.d.ts.map +1 -1
  189. package/lib/esm/Tracing.js +130 -126
  190. package/lib/esm/Tracing.js.map +1 -1
  191. package/lib/esm/TupleKeyedMap.d.ts +36 -36
  192. package/lib/esm/TupleKeyedMap.js +98 -98
  193. package/lib/esm/TypedArrayBuilder.d.ts +155 -155
  194. package/lib/esm/TypedArrayBuilder.d.ts.map +1 -1
  195. package/lib/esm/TypedArrayBuilder.js +198 -200
  196. package/lib/esm/TypedArrayBuilder.js.map +1 -1
  197. package/lib/esm/UnexpectedErrors.d.ts +43 -43
  198. package/lib/esm/UnexpectedErrors.d.ts.map +1 -1
  199. package/lib/esm/UnexpectedErrors.js +65 -64
  200. package/lib/esm/UnexpectedErrors.js.map +1 -1
  201. package/lib/esm/UtilityTypes.d.ts +112 -96
  202. package/lib/esm/UtilityTypes.d.ts.map +1 -1
  203. package/lib/esm/UtilityTypes.js +34 -34
  204. package/lib/esm/UtilityTypes.js.map +1 -1
  205. package/lib/esm/YieldManager.d.ts +18 -18
  206. package/lib/esm/YieldManager.js +30 -30
  207. package/lib/esm/core-bentley.d.ts +74 -74
  208. package/lib/esm/core-bentley.js +78 -78
  209. package/lib/esm/partitionArray.d.ts +21 -21
  210. package/lib/esm/partitionArray.js +39 -39
  211. package/package.json +9 -9
@@ -1,304 +1,316 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module Collections
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.SortedArray = exports.ReadonlySortedArray = exports.DuplicatePolicy = exports.lowerBound = exports.shallowClone = void 0;
11
- /**
12
- * A [[CloneFunction]] that, given a value of type T, returns the same value.
13
- * Useful as a default argument for functions that can alternatively accept custom logic for cloning values of object type.
14
- * @param value The value to clone.
15
- * @returns the input value.
16
- * @public
17
- */
18
- function shallowClone(value) {
19
- return value;
20
- }
21
- exports.shallowClone = shallowClone;
22
- /**
23
- * Given a sorted array, computes the position at which the specified value should be inserted into the array so that the array remains sorted.
24
- * @param value The value whose position is to be computed.
25
- * @param list An array of U already sorted according to the comparison criterion.
26
- * @param compare The function used to compare the value with elements in `list`.
27
- * @returns an object with 'index' corresponding to the computed position and 'equal' set to true if an equivalent element already exists at that index.
28
- * @public
29
- */
30
- function lowerBound(value, list, compare) {
31
- return lowerBoundOfEquivalent(list, (element) => compare(value, element));
32
- }
33
- exports.lowerBound = lowerBound;
34
- function lowerBoundOfEquivalent(list, criterion) {
35
- let low = 0;
36
- let high = list.length;
37
- while (low < high) {
38
- const mid = Math.floor((low + high) / 2);
39
- const comp = criterion(list[mid]);
40
- if (0 === comp)
41
- return { index: mid, equal: true };
42
- else if (comp < 0)
43
- high = mid;
44
- else
45
- low = mid + 1;
46
- }
47
- return { index: low, equal: false };
48
- }
49
- /** Describes how duplicate values are handled when inserting into a [[SortedArray]].
50
- * A "duplicate" value is one that compares equal to a value already present in the array, per the array's comparison function.
51
- * @public
52
- */
53
- var DuplicatePolicy;
54
- (function (DuplicatePolicy) {
55
- /** The array allows duplicate values to be inserted. All duplicate values will be adjacent in the array, but the ordering between duplicate values is unspecified.
56
- * @note In the presence of duplicate values, functions like [[SortedArray.indexOf]] and [[SortedArray.findEqual]] will return one of the values - exactly which one is unspecified.
57
- */
58
- DuplicatePolicy[DuplicatePolicy["Allow"] = 0] = "Allow";
59
- /** Duplicate values are forbidden - when attempting to insert a value equivalent to one already present, the already-present value is retained. */
60
- DuplicatePolicy[DuplicatePolicy["Retain"] = 1] = "Retain";
61
- /** Duplicate values are forbidden - when attempting to insert a value equivalent to one already present, the already-present value is replaced by the new value.
62
- * This can be useful when the value type carries additional data that is not evaluated by the comparison function.
63
- */
64
- DuplicatePolicy[DuplicatePolicy["Replace"] = 2] = "Replace";
65
- })(DuplicatePolicy = exports.DuplicatePolicy || (exports.DuplicatePolicy = {}));
66
- /**
67
- * A read-only view of an array of some type T sorted according to some user-supplied criterion.
68
- * Duplicate elements may be present, though sub-types may enforce uniqueness of elements.
69
- * In the absence of duplicates, a ReadonlySortedArray<T> can behave like a Set<T> where T is an object and equality is determined
70
- * by some criterion other than object identity.
71
- *
72
- * Because the array is always sorted, querying for the presence of an element is performed using binary
73
- * search, which is more efficient than a linear search for reasonably large arrays.
74
- *
75
- * The comparison function must meet the following criteria, given 'lhs' and 'rhs' of type T:
76
- * - If lhs is equal to rhs, returns 0
77
- * - If lhs is less than rhs, returns a negative value
78
- * - If lhs is greater than rhs, returns a positive value
79
- * - If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0
80
- * - If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa.
81
- *
82
- * Note that the array is read-only only from the perspective of its public interface. Mutation methods are defined for internal use by sub-types.
83
- *
84
- * @see [[SortedArray]] for a general-purpose mutable sorted array.
85
- * @public
86
- */
87
- class ReadonlySortedArray {
88
- /**
89
- * Construct a new ReadonlySortedArray<T>.
90
- * @param compare The function used to compare elements within the array.
91
- * @param duplicatePolicy Policy for handling attempts to insert a value when an equivalent value already exists. If the input is a boolean, then `true` indicates [[DuplicatePolicy.Allow]], and `false` indicates [[DuplicatePolicy.Retain]].
92
- * @param clone The function invoked to clone a new element for insertion into the array. The default implementation simply returns its input.
93
- */
94
- constructor(compare, duplicatePolicy = false, clone = shallowClone) {
95
- this._array = [];
96
- this._compare = compare;
97
- this._clone = clone;
98
- if (typeof duplicatePolicy === "boolean")
99
- duplicatePolicy = duplicatePolicy ? DuplicatePolicy.Allow : DuplicatePolicy.Retain;
100
- this._duplicatePolicy = duplicatePolicy;
101
- }
102
- /** The number of elements in the array */
103
- get length() { return this._array.length; }
104
- /** Returns true if the array contains no elements. */
105
- get isEmpty() { return 0 === this.length; }
106
- /** Returns an iterator over the contents of the array in sorted order, suitable for use in `for-of` loops. */
107
- [Symbol.iterator]() { return this._array[Symbol.iterator](); }
108
- /**
109
- * Looks up the index of an element comparing equal to the specified value using binary search.
110
- * @param value The value to search for
111
- * @returns the index of the first equivalent element found in the array, or -1 if no such element exists.
112
- */
113
- indexOf(value) {
114
- const bound = this.lowerBound(value);
115
- return bound.equal ? bound.index : -1;
116
- }
117
- /**
118
- * Returns true if this array contains at least one value comparing equal to the specified value.
119
- * @param value The value to search for
120
- * @returns true if an equivalent element exists in the array.
121
- */
122
- contains(value) {
123
- return -1 !== this.indexOf(value);
124
- }
125
- /**
126
- * Looks up an element comparing equal to the specified value using binary search.
127
- * @param value The value to search for
128
- * @returns the first equivalent element found in the array, or undefined if no such element exists.
129
- */
130
- findEqual(value) {
131
- const index = this.indexOf(value);
132
- return -1 !== index ? this._array[index] : undefined;
133
- }
134
- /** Find an element that compares as equivalent based on some criterion. If multiple elements are equivalent, the specific one returned is unspecified.
135
- * As an example, consider a `SortedArray<ModelState>` which uses `ModelState.id` as its ordering criterion. To find a model by its Id,
136
- * use `sortedArray.findEquivalent((element) => compareStrings(element.id, modelId))` where `modelId` is an [[Id64String]].
137
- * @param criterion A function accepting an element and returning 0 if it compares as equivalent, a negative number if it compares as "less-than", or a positive value if it compares as "greater-than".
138
- * @returns The first element found that meets the criterion, or `undefined` if no elements meet the criterion.
139
- * @see [[indexOfEquivalent]].
140
- * @public
141
- */
142
- findEquivalent(criterion) {
143
- const index = this.indexOfEquivalent(criterion);
144
- return -1 !== index ? this._array[index] : undefined;
145
- }
146
- /** Find the index of an element that compares as equivalent based on some criterion. If multiple elements are equivalent, the specific one returned is unspecified.
147
- * As an example, consider a `SortedArray<ModelState>` which uses `ModelState.id` as its ordering criterion. To find the index of a model by its Id,
148
- * use `sortedArray.indexOfEquivalent((element) => compareStrings(element.id, modelId))` where `modelId` is an [[Id64String]].
149
- * @param criterion A function accepting an element and returning 0 if it compares as equivalent, a negative number if the element compares as "less-than", or a positive value if the element compares as "greater-than".
150
- * @returns The index of the first element found that meets the criterion, or -1 if no elements meet the criterion.
151
- * @public
152
- */
153
- indexOfEquivalent(criterion) {
154
- // NB: Must invert the ordering.
155
- const bound = lowerBoundOfEquivalent(this._array, (elem) => 0 - criterion(elem));
156
- return bound.equal ? bound.index : -1;
157
- }
158
- /**
159
- * Looks up an element by its index in the array.
160
- * @param index The array index
161
- * @returns the element corresponding to that position in the array, or undefined if the supplied index exceeds the length of the array.
162
- */
163
- get(index) { return index < this.length ? this._array[index] : undefined; }
164
- /** Apply a function to each element in the array, in sorted order.
165
- * @param func The function to be applied.
166
- */
167
- forEach(func) {
168
- for (let i = 0; i < this.length; i++)
169
- func(this._array[i]);
170
- }
171
- /**
172
- * Computes the position at which the specified value should be inserted to maintain sorted order.
173
- * @param value The value whose position is to be computed.
174
- * @returns an object with 'index' corresponding to the computed position and 'equal' set to true if an equivalent element already exists at that index.
175
- */
176
- lowerBound(value) { return lowerBound(value, this._array, this._compare); }
177
- /** Clears the contents of the sorted array. */
178
- _clear() { this._array.length = 0; }
179
- /** Extracts the sorted array as a T[] and empties the contents of this ReadonlySortedArray.
180
- * @returns the contents of this ReadonlySortedArray as a T[].
181
- */
182
- _extractArray() {
183
- const result = this._array;
184
- this._array = [];
185
- return result;
186
- }
187
- /**
188
- * Attempts to insert a new value into the array at a position determined by the ordering.
189
- * The behavior differs based on the array's [[DuplicatePolicy]]:
190
- * If duplicates are **not** permitted, then:
191
- * - If an equivalent element already exists in the array:
192
- * - [[DuplicatePolicy.Retain]]: nothing will be inserted and the index of the existing element will be returned.
193
- * - [[DuplicatePolicy.Replace]]: the input value will overwrite the existing element at the same index and that index will be returned.
194
- * - Otherwise, the element is inserted and its index is returned.
195
- * If duplicates **are** permitted, then:
196
- * - The element will be inserted in a correct position based on the sorting criterion;
197
- * - The position of the element relative to other elements comparing as equal to it is unspecified; and
198
- * - The actual index of the newly-inserted element is returned.
199
- * If the element is to be inserted, then the supplied value will be passed to the clone function supplied to the constructor and the result will be inserted into the array.
200
- * @param value The value to insert
201
- * @param onInsert The optional callback method to call if insertion occurs with the inserted value
202
- * @returns the index in the array of the newly-inserted value, or, if duplicates are not permitted and an equivalent value already exists, the index of the equivalent value.
203
- */
204
- _insert(value, onInsert) {
205
- const bound = this.lowerBound(value);
206
- if (bound.equal) {
207
- switch (this._duplicatePolicy) {
208
- case DuplicatePolicy.Retain:
209
- return bound.index;
210
- case DuplicatePolicy.Replace:
211
- this._array[bound.index] = this._clone(value);
212
- if (onInsert)
213
- onInsert(value);
214
- return bound.index;
215
- }
216
- }
217
- this._array.splice(bound.index, 0, this._clone(value));
218
- if (undefined !== onInsert)
219
- onInsert(value);
220
- return bound.index;
221
- }
222
- /**
223
- * Removes the first occurrence of a value comparing equal to the specified value from the array.
224
- * @param value The value of the element to delete
225
- * @returns the index of the deleted value, or -1 if no such element exists.
226
- */
227
- _remove(value) {
228
- const bound = this.lowerBound(value);
229
- if (bound.equal) {
230
- this._array.splice(bound.index, 1);
231
- return bound.index;
232
- }
233
- else {
234
- return -1;
235
- }
236
- }
237
- }
238
- exports.ReadonlySortedArray = ReadonlySortedArray;
239
- /**
240
- * Maintains an array of some type T in sorted order. The ordering is specified by a function supplied
241
- * by the user.
242
- * By default, only unique elements are permitted; attempting to insert a new element that compares
243
- * as equal to an element already in the array will not modify the contents of the array.
244
- *
245
- * This allows a SortedArray<T> to behave like a Set<T> where T is an object and equality is determined
246
- * by some criterion other than object identity.
247
- *
248
- * Because the array is always sorted, querying for the presence of an element is performed using binary
249
- * search, which is more efficient than a linear search for reasonably large arrays.
250
- *
251
- * The user can also specify how the SortedArray takes ownership of inserted values, e.g., by cloning them.
252
- *
253
- * The comparison function must meet the following criteria, given 'lhs' and 'rhs' of type T:
254
- * - If lhs is equal to rhs, returns 0
255
- * - If lhs is less than rhs, returns a negative value
256
- * - If lhs is greater than rhs, returns a positive value
257
- * - If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0
258
- * - If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa.
259
- *
260
- * Modifying an element in a way that affects the comparison function will produce unpredictable results, the
261
- * most likely of which is that the array will cease to be sorted.
262
- * @public
263
- */
264
- class SortedArray extends ReadonlySortedArray {
265
- /**
266
- * Construct a new SortedArray<T>.
267
- * @param compare The function used to compare elements within the array.
268
- * @param duplicatePolicy Policy for handling attempts to insert a value when an equivalent value already exists. If the input is a boolean, then `true` indicates [[DuplicatePolicy.Allow]], and `false` indicates [[DuplicatePolicy.Retain]].
269
- * @param clone The function invoked to clone a new element for insertion into the array. The default implementation simply returns its input.
270
- */
271
- constructor(compare, duplicatePolicy = false, clone = shallowClone) {
272
- super(compare, duplicatePolicy, clone);
273
- }
274
- /** Clears the contents of the sorted array. */
275
- clear() { this._clear(); }
276
- /** Extracts the sorted array as a T[] and empties the contents of this SortedArray.
277
- * @returns the contents of this SortedArray as a T[].
278
- */
279
- extractArray() { return this._extractArray(); }
280
- /**
281
- * Attempts to insert a new value into the array at a position determined by the ordering.
282
- * The behavior differs based on whether or not duplicate elements are permitted.
283
- * If duplicates are **not** permitted, then:
284
- * - If an equivalent element already exists in the array, nothing will be inserted and the index of the existing element will be returned.
285
- * - Otherwise, the element is inserted and its index is returned.
286
- * If duplicates **are** permitted, then:
287
- * - The element will be inserted in a correct position based on the sorting criterion;
288
- * - The position of the element relative to other elements comparing as equal to it is unspecified; and
289
- * - The actual index of the newly-inserted element is returned.
290
- * If the element is to be inserted, then the supplied value will be passed to the clone function supplied to the constructor and the result will be inserted into the array.
291
- * @param value The value to insert
292
- * @param onInsert The optional callback method to call if insertion occurs with the inserted value
293
- * @returns the index in the array of the newly-inserted value, or, if duplicates are not permitted and an equivalent value already exists, the index of the equivalent value.
294
- */
295
- insert(value, onInsert) { return this._insert(value, onInsert); }
296
- /**
297
- * Removes the first occurrence of a value comparing equal to the specified value from the array.
298
- * @param value The value of the element to delete
299
- * @returns the index of the deleted value, or -1 if no such element exists.
300
- */
301
- remove(value) { return this._remove(value); }
302
- }
303
- exports.SortedArray = SortedArray;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Collections
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.SortedArray = exports.ReadonlySortedArray = exports.DuplicatePolicy = exports.lowerBound = exports.shallowClone = void 0;
11
+ /**
12
+ * A [[CloneFunction]] that, given a value of type T, returns the same value.
13
+ * Useful as a default argument for functions that can alternatively accept custom logic for cloning values of object type.
14
+ * @param value The value to clone.
15
+ * @returns the input value.
16
+ * @public
17
+ */
18
+ function shallowClone(value) {
19
+ return value;
20
+ }
21
+ exports.shallowClone = shallowClone;
22
+ /**
23
+ * Given a sorted array, computes the position at which the specified value should be inserted into the array so that the array remains sorted.
24
+ * @param value The value whose position is to be computed.
25
+ * @param list An array of U already sorted according to the comparison criterion.
26
+ * @param compare The function used to compare the value with elements in `list`.
27
+ * @returns an object with 'index' corresponding to the computed position and 'equal' set to true if an equivalent element already exists at that index.
28
+ * @public
29
+ */
30
+ function lowerBound(value, list, compare) {
31
+ return lowerBoundOfEquivalent(list, (element) => compare(value, element));
32
+ }
33
+ exports.lowerBound = lowerBound;
34
+ function lowerBoundOfEquivalent(list, criterion) {
35
+ let low = 0;
36
+ let high = list.length;
37
+ while (low < high) {
38
+ const mid = Math.floor((low + high) / 2);
39
+ const comp = criterion(list[mid]);
40
+ if (0 === comp)
41
+ return { index: mid, equal: true };
42
+ else if (comp < 0)
43
+ high = mid;
44
+ else
45
+ low = mid + 1;
46
+ }
47
+ return { index: low, equal: false };
48
+ }
49
+ /** Describes how duplicate values are handled when inserting into a [[SortedArray]].
50
+ * A "duplicate" value is one that compares equal to a value already present in the array, per the array's comparison function.
51
+ * @public
52
+ */
53
+ var DuplicatePolicy;
54
+ (function (DuplicatePolicy) {
55
+ /** The array allows duplicate values to be inserted. All duplicate values will be adjacent in the array, but the ordering between duplicate values is unspecified.
56
+ * @note In the presence of duplicate values, functions like [[SortedArray.indexOf]] and [[SortedArray.findEqual]] will return one of the values - exactly which one is unspecified.
57
+ */
58
+ DuplicatePolicy[DuplicatePolicy["Allow"] = 0] = "Allow";
59
+ /** Duplicate values are forbidden - when attempting to insert a value equivalent to one already present, the already-present value is retained. */
60
+ DuplicatePolicy[DuplicatePolicy["Retain"] = 1] = "Retain";
61
+ /** Duplicate values are forbidden - when attempting to insert a value equivalent to one already present, the already-present value is replaced by the new value.
62
+ * This can be useful when the value type carries additional data that is not evaluated by the comparison function.
63
+ */
64
+ DuplicatePolicy[DuplicatePolicy["Replace"] = 2] = "Replace";
65
+ })(DuplicatePolicy = exports.DuplicatePolicy || (exports.DuplicatePolicy = {}));
66
+ /**
67
+ * A read-only view of an array of some type T sorted according to some user-supplied criterion.
68
+ * Duplicate elements may be present, though sub-types may enforce uniqueness of elements.
69
+ * In the absence of duplicates, a ReadonlySortedArray<T> can behave like a Set<T> where T is an object and equality is determined
70
+ * by some criterion other than object identity.
71
+ *
72
+ * Because the array is always sorted, querying for the presence of an element is performed using binary
73
+ * search, which is more efficient than a linear search for reasonably large arrays.
74
+ *
75
+ * The comparison function must meet the following criteria, given 'lhs' and 'rhs' of type T:
76
+ * - If lhs is equal to rhs, returns 0
77
+ * - If lhs is less than rhs, returns a negative value
78
+ * - If lhs is greater than rhs, returns a positive value
79
+ * - If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0
80
+ * - If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa.
81
+ *
82
+ * Note that the array is read-only only from the perspective of its public interface. Mutation methods are defined for internal use by sub-types.
83
+ *
84
+ * @see [[SortedArray]] for a general-purpose mutable sorted array.
85
+ * @public
86
+ */
87
+ class ReadonlySortedArray {
88
+ /**
89
+ * Construct a new ReadonlySortedArray<T>.
90
+ * @param compare The function used to compare elements within the array.
91
+ * @param duplicatePolicy Policy for handling attempts to insert a value when an equivalent value already exists. If the input is a boolean, then `true` indicates [[DuplicatePolicy.Allow]], and `false` indicates [[DuplicatePolicy.Retain]].
92
+ * @param clone The function invoked to clone a new element for insertion into the array. The default implementation simply returns its input.
93
+ */
94
+ constructor(compare, duplicatePolicy = false, clone = shallowClone) {
95
+ this._array = [];
96
+ this._compare = compare;
97
+ this._clone = clone;
98
+ if (typeof duplicatePolicy === "boolean")
99
+ duplicatePolicy = duplicatePolicy ? DuplicatePolicy.Allow : DuplicatePolicy.Retain;
100
+ this._duplicatePolicy = duplicatePolicy;
101
+ }
102
+ /** The number of elements in the array */
103
+ get length() { return this._array.length; }
104
+ /** Returns true if the array contains no elements. */
105
+ get isEmpty() { return 0 === this.length; }
106
+ /** Returns an iterator over the contents of the array in sorted order, suitable for use in `for-of` loops. */
107
+ [Symbol.iterator]() { return this._array[Symbol.iterator](); }
108
+ /**
109
+ * Looks up the index of an element comparing equal to the specified value using binary search.
110
+ * @param value The value to search for
111
+ * @returns the index of the first equivalent element found in the array, or -1 if no such element exists.
112
+ */
113
+ indexOf(value) {
114
+ const bound = this.lowerBound(value);
115
+ return bound.equal ? bound.index : -1;
116
+ }
117
+ /**
118
+ * Returns true if this array contains at least one value comparing equal to the specified value.
119
+ * @param value The value to search for
120
+ * @returns true if an equivalent element exists in the array.
121
+ */
122
+ contains(value) {
123
+ return -1 !== this.indexOf(value);
124
+ }
125
+ /**
126
+ * Looks up an element comparing equal to the specified value using binary search.
127
+ * @param value The value to search for
128
+ * @returns the first equivalent element found in the array, or undefined if no such element exists.
129
+ */
130
+ findEqual(value) {
131
+ const index = this.indexOf(value);
132
+ return -1 !== index ? this._array[index] : undefined;
133
+ }
134
+ /** Find an element that compares as equivalent based on some criterion. If multiple elements are equivalent, the specific one returned is unspecified.
135
+ * As an example, consider a `SortedArray<ModelState>` which uses `ModelState.id` as its ordering criterion. To find a model by its Id,
136
+ * use `sortedArray.findEquivalent((element) => compareStrings(element.id, modelId))` where `modelId` is an [[Id64String]].
137
+ * @param criterion A function accepting an element and returning 0 if it compares as equivalent, a negative number if it compares as "less-than", or a positive value if it compares as "greater-than".
138
+ * @returns The first element found that meets the criterion, or `undefined` if no elements meet the criterion.
139
+ * @see [[indexOfEquivalent]].
140
+ * @public
141
+ */
142
+ findEquivalent(criterion) {
143
+ const index = this.indexOfEquivalent(criterion);
144
+ return -1 !== index ? this._array[index] : undefined;
145
+ }
146
+ /** Find the index of an element that compares as equivalent based on some criterion. If multiple elements are equivalent, the specific one returned is unspecified.
147
+ * As an example, consider a `SortedArray<ModelState>` which uses `ModelState.id` as its ordering criterion. To find the index of a model by its Id,
148
+ * use `sortedArray.indexOfEquivalent((element) => compareStrings(element.id, modelId))` where `modelId` is an [[Id64String]].
149
+ * @param criterion A function accepting an element and returning 0 if it compares as equivalent, a negative number if the element compares as "less-than", or a positive value if the element compares as "greater-than".
150
+ * @returns The index of the first element found that meets the criterion, or -1 if no elements meet the criterion.
151
+ * @public
152
+ */
153
+ indexOfEquivalent(criterion) {
154
+ // NB: Must invert the ordering.
155
+ const bound = lowerBoundOfEquivalent(this._array, (elem) => 0 - criterion(elem));
156
+ return bound.equal ? bound.index : -1;
157
+ }
158
+ /**
159
+ * Looks up an element by its index in the array.
160
+ * @param index The array index
161
+ * @returns the element corresponding to that position in the array, or undefined if the supplied index exceeds the length of the array.
162
+ */
163
+ get(index) { return index < this.length ? this._array[index] : undefined; }
164
+ /** Apply a function to each element in the array, in sorted order.
165
+ * @param func The function to be applied.
166
+ */
167
+ forEach(func) {
168
+ for (let i = 0; i < this.length; i++)
169
+ func(this._array[i]);
170
+ }
171
+ /** The equivalent of [Array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice). */
172
+ slice(start, end) {
173
+ const slice = new ReadonlySortedArray(this._compare, this._duplicatePolicy, this._clone);
174
+ slice._array = this._array.slice(start, end);
175
+ return slice;
176
+ }
177
+ /**
178
+ * Computes the position at which the specified value should be inserted to maintain sorted order.
179
+ * @param value The value whose position is to be computed.
180
+ * @returns an object with 'index' corresponding to the computed position and 'equal' set to true if an equivalent element already exists at that index.
181
+ */
182
+ lowerBound(value) { return lowerBound(value, this._array, this._compare); }
183
+ /** Clears the contents of the sorted array. */
184
+ _clear() { this._array.length = 0; }
185
+ /** Extracts the sorted array as a T[] and empties the contents of this ReadonlySortedArray.
186
+ * @returns the contents of this ReadonlySortedArray as a T[].
187
+ */
188
+ _extractArray() {
189
+ const result = this._array;
190
+ this._array = [];
191
+ return result;
192
+ }
193
+ /**
194
+ * Attempts to insert a new value into the array at a position determined by the ordering.
195
+ * The behavior differs based on the array's [[DuplicatePolicy]]:
196
+ * If duplicates are **not** permitted, then:
197
+ * - If an equivalent element already exists in the array:
198
+ * - [[DuplicatePolicy.Retain]]: nothing will be inserted and the index of the existing element will be returned.
199
+ * - [[DuplicatePolicy.Replace]]: the input value will overwrite the existing element at the same index and that index will be returned.
200
+ * - Otherwise, the element is inserted and its index is returned.
201
+ * If duplicates **are** permitted, then:
202
+ * - The element will be inserted in a correct position based on the sorting criterion;
203
+ * - The position of the element relative to other elements comparing as equal to it is unspecified; and
204
+ * - The actual index of the newly-inserted element is returned.
205
+ * If the element is to be inserted, then the supplied value will be passed to the clone function supplied to the constructor and the result will be inserted into the array.
206
+ * @param value The value to insert
207
+ * @param onInsert The optional callback method to call if insertion occurs with the inserted value
208
+ * @returns the index in the array of the newly-inserted value, or, if duplicates are not permitted and an equivalent value already exists, the index of the equivalent value.
209
+ */
210
+ _insert(value, onInsert) {
211
+ const bound = this.lowerBound(value);
212
+ if (bound.equal) {
213
+ switch (this._duplicatePolicy) {
214
+ case DuplicatePolicy.Retain:
215
+ return bound.index;
216
+ case DuplicatePolicy.Replace:
217
+ this._array[bound.index] = this._clone(value);
218
+ if (onInsert)
219
+ onInsert(value);
220
+ return bound.index;
221
+ }
222
+ }
223
+ this._array.splice(bound.index, 0, this._clone(value));
224
+ if (undefined !== onInsert)
225
+ onInsert(value);
226
+ return bound.index;
227
+ }
228
+ /**
229
+ * Removes the first occurrence of a value comparing equal to the specified value from the array.
230
+ * @param value The value of the element to delete
231
+ * @returns the index of the deleted value, or -1 if no such element exists.
232
+ */
233
+ _remove(value) {
234
+ const bound = this.lowerBound(value);
235
+ if (bound.equal) {
236
+ this._array.splice(bound.index, 1);
237
+ return bound.index;
238
+ }
239
+ else {
240
+ return -1;
241
+ }
242
+ }
243
+ }
244
+ exports.ReadonlySortedArray = ReadonlySortedArray;
245
+ /**
246
+ * Maintains an array of some type T in sorted order. The ordering is specified by a function supplied
247
+ * by the user.
248
+ * By default, only unique elements are permitted; attempting to insert a new element that compares
249
+ * as equal to an element already in the array will not modify the contents of the array.
250
+ *
251
+ * This allows a SortedArray<T> to behave like a Set<T> where T is an object and equality is determined
252
+ * by some criterion other than object identity.
253
+ *
254
+ * Because the array is always sorted, querying for the presence of an element is performed using binary
255
+ * search, which is more efficient than a linear search for reasonably large arrays.
256
+ *
257
+ * The user can also specify how the SortedArray takes ownership of inserted values, e.g., by cloning them.
258
+ *
259
+ * The comparison function must meet the following criteria, given 'lhs' and 'rhs' of type T:
260
+ * - If lhs is equal to rhs, returns 0
261
+ * - If lhs is less than rhs, returns a negative value
262
+ * - If lhs is greater than rhs, returns a positive value
263
+ * - If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0
264
+ * - If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa.
265
+ *
266
+ * Modifying an element in a way that affects the comparison function will produce unpredictable results, the
267
+ * most likely of which is that the array will cease to be sorted.
268
+ * @public
269
+ */
270
+ class SortedArray extends ReadonlySortedArray {
271
+ /**
272
+ * Construct a new SortedArray<T>.
273
+ * @param compare The function used to compare elements within the array.
274
+ * @param duplicatePolicy Policy for handling attempts to insert a value when an equivalent value already exists. If the input is a boolean, then `true` indicates [[DuplicatePolicy.Allow]], and `false` indicates [[DuplicatePolicy.Retain]].
275
+ * @param clone The function invoked to clone a new element for insertion into the array. The default implementation simply returns its input.
276
+ */
277
+ constructor(compare, duplicatePolicy = false, clone = shallowClone) {
278
+ super(compare, duplicatePolicy, clone);
279
+ }
280
+ /** Clears the contents of the sorted array. */
281
+ clear() { this._clear(); }
282
+ /** Extracts the sorted array as a T[] and empties the contents of this SortedArray.
283
+ * @returns the contents of this SortedArray as a T[].
284
+ */
285
+ extractArray() { return this._extractArray(); }
286
+ /**
287
+ * Attempts to insert a new value into the array at a position determined by the ordering.
288
+ * The behavior differs based on whether or not duplicate elements are permitted.
289
+ * If duplicates are **not** permitted, then:
290
+ * - If an equivalent element already exists in the array, nothing will be inserted and the index of the existing element will be returned.
291
+ * - Otherwise, the element is inserted and its index is returned.
292
+ * If duplicates **are** permitted, then:
293
+ * - The element will be inserted in a correct position based on the sorting criterion;
294
+ * - The position of the element relative to other elements comparing as equal to it is unspecified; and
295
+ * - The actual index of the newly-inserted element is returned.
296
+ * If the element is to be inserted, then the supplied value will be passed to the clone function supplied to the constructor and the result will be inserted into the array.
297
+ * @param value The value to insert
298
+ * @param onInsert The optional callback method to call if insertion occurs with the inserted value
299
+ * @returns the index in the array of the newly-inserted value, or, if duplicates are not permitted and an equivalent value already exists, the index of the equivalent value.
300
+ */
301
+ insert(value, onInsert) { return this._insert(value, onInsert); }
302
+ /**
303
+ * Removes the first occurrence of a value comparing equal to the specified value from the array.
304
+ * @param value The value of the element to delete
305
+ * @returns the index of the deleted value, or -1 if no such element exists.
306
+ */
307
+ remove(value) { return this._remove(value); }
308
+ /** The equivalent of [Array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice). */
309
+ slice(start, end) {
310
+ const slice = new SortedArray(this._compare, this._duplicatePolicy, this._clone);
311
+ slice._array = this._array.slice(start, end);
312
+ return slice;
313
+ }
314
+ }
315
+ exports.SortedArray = SortedArray;
304
316
  //# sourceMappingURL=SortedArray.js.map