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