@itwin/core-bentley 4.0.0-dev.8 → 4.0.0-dev.80

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,209 +1,207 @@
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.UintArrayBuilder = exports.Uint32ArrayBuilder = exports.Uint16ArrayBuilder = exports.Uint8ArrayBuilder = exports.TypedArrayBuilder = void 0;
11
- const Assert_1 = require("./Assert");
12
- /** Incrementally builds a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of unsigned 8-, 16-, or 32-bit integers.
13
- * Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.
14
- * `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.
15
- *
16
- * `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two "size" properties":
17
- * - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and
18
- * - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].
19
- * When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.
20
- *
21
- * Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].
22
- * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.
23
- * @see [[UintArrayBuilder]] when you don't know the maximum number of bytes required for each element in the array.
24
- * @public
25
- */
26
- class TypedArrayBuilder {
27
- /** Constructs a new builder from the specified options, with a [[length]] of zero. */
28
- constructor(constructor, options) {
29
- var _a, _b;
30
- this._constructor = constructor;
31
- this._data = new constructor((_a = options === null || options === void 0 ? void 0 : options.initialCapacity) !== null && _a !== void 0 ? _a : 0);
32
- this.growthFactor = Math.max(1.0, (_b = options === null || options === void 0 ? void 0 : options.growthFactor) !== null && _b !== void 0 ? _b : 1.5);
33
- this._length = 0;
34
- }
35
- /** The number of elements currently in the array. */
36
- get length() {
37
- return this._length;
38
- }
39
- /** The number of elements that can fit into the memory currently allocated for the array. */
40
- get capacity() {
41
- return this._data.length;
42
- }
43
- /** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),
44
- * returns the element at the specified index, with negative integers counting back from the end of the array.
45
- * @note It is your responsibility to ensure the index falls within the bounds of the array.
46
- */
47
- at(index) {
48
- if (index < 0)
49
- index = this.length - index;
50
- const value = this._data[index];
51
- (0, Assert_1.assert)(value !== undefined, "index out of bounds");
52
- return value;
53
- }
54
- /** Ensure that [[capacity]] is at least equal to `newCapacity`.
55
- * This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.
56
- * It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.
57
- *
58
- * If `newCapacity` is not greater than the current [[capacity]], this function does nothing.
59
- * Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.
60
- * [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.
61
- */
62
- ensureCapacity(newCapacity) {
63
- if (this.capacity >= newCapacity)
64
- return this.capacity;
65
- (0, Assert_1.assert)(this.growthFactor >= 1.0);
66
- newCapacity = Math.ceil(newCapacity * this.growthFactor);
67
- const prevData = this._data;
68
- this._data = new this._constructor(newCapacity);
69
- this._data.set(prevData, 0);
70
- (0, Assert_1.assert)(this.capacity === newCapacity);
71
- return this.capacity;
72
- }
73
- /** Append the specified value, resizing if necessary. */
74
- push(value) {
75
- this.ensureCapacity(this.length + 1);
76
- this._data[this.length] = value;
77
- ++this._length;
78
- }
79
- /** Append an array of values, resizing (at most once) if necessary. */
80
- append(values) {
81
- const newLength = this.length + values.length;
82
- this.ensureCapacity(newLength);
83
- this._data.set(values, this.length);
84
- this._length = newLength;
85
- }
86
- /** Obtain the finished array.
87
- * @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the
88
- * returned array's length will be equal to [[length]].
89
- */
90
- toTypedArray(includeUnusedCapacity = false) {
91
- if (includeUnusedCapacity)
92
- return this._data;
93
- const subarray = this._data.subarray(0, this.length);
94
- (0, Assert_1.assert)(subarray instanceof this._constructor);
95
- (0, Assert_1.assert)(subarray.buffer === this._data.buffer);
96
- return subarray;
97
- }
98
- }
99
- exports.TypedArrayBuilder = TypedArrayBuilder;
100
- /** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
101
- * @public
102
- */
103
- class Uint8ArrayBuilder extends TypedArrayBuilder {
104
- /** See [[TypedArrayBuilder]] constructor. */
105
- constructor(options) {
106
- super(Uint8Array, options);
107
- }
108
- }
109
- exports.Uint8ArrayBuilder = Uint8ArrayBuilder;
110
- /** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).
111
- * @public
112
- */
113
- class Uint16ArrayBuilder extends TypedArrayBuilder {
114
- /** See [[TypedArrayBuilder]] constructor. */
115
- constructor(options) {
116
- super(Uint16Array, options);
117
- }
118
- }
119
- exports.Uint16ArrayBuilder = Uint16ArrayBuilder;
120
- /** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).
121
- * @public
122
- */
123
- class Uint32ArrayBuilder extends TypedArrayBuilder {
124
- /** See [[TypedArrayBuilder]] constructor. */
125
- constructor(options) {
126
- super(Uint32Array, options);
127
- }
128
- /** Obtain a view of the finished array as an array of bytes. */
129
- toUint8Array(includeUnusedCapacity = false) {
130
- if (includeUnusedCapacity)
131
- return new Uint8Array(this._data.buffer);
132
- return new Uint8Array(this._data.buffer, 0, this.length * 4);
133
- }
134
- }
135
- exports.Uint32ArrayBuilder = Uint32ArrayBuilder;
136
- /** A [[TypedArrayBuilder]] that can populate a [[UintArray]] with the minimum
137
- * [bytes per element](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT) required.
138
- *
139
- * By default, the underlying array is a `Uint8Array`, though this can be configured via [[UintArrayBuilderOptions.initialType]].
140
- * As values are added to the array, if the bytes per element supported by the underlying array is too small to hold one of the new values, the array is
141
- * reallocated to a type large enough to hold all of the new values. For example, the following produces a `Uint8Array` because all values are less than 256:
142
- *
143
- * ```ts
144
- * const builder = new UintArrayBuilder();
145
- * builder.append([1, 2, 254, 255]);
146
- * const array = builder.toTypedArray();
147
- * assert(array instanceof Uint8Array);
148
- * ```
149
- *
150
- * However, the following produces a `Uint16Array` because one of the values is larger than 255 but none are larger than 65,535:
151
- *
152
- * ```ts
153
- * const builder = new UintArrayBuilder();
154
- * builder.append([1, 255, 257, 65535]);
155
- * const array = builder.toTypedArray();
156
- * assert(array instanceof Uint16Array);
157
- * ```
158
- *
159
- * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], or [[Uint32ArrayBuilder]] if you know the number of bytes you want to allocate for each element in the array.
160
- * @public
161
- */
162
- class UintArrayBuilder extends TypedArrayBuilder {
163
- constructor(options) {
164
- var _a;
165
- super((_a = options === null || options === void 0 ? void 0 : options.initialType) !== null && _a !== void 0 ? _a : Uint8Array, options);
166
- }
167
- /** The number of bytes (1, 2, or 4) currently allocated per element by the underlying array.
168
- * This may change as larger values are added to the array.
169
- */
170
- get bytesPerElement() {
171
- return this._data.BYTES_PER_ELEMENT;
172
- }
173
- /** Ensures that the underlying array is of a type that can contain the largest value in `newValues`.
174
- * For example, if `_data` is a `Uint16Array` and `newValues` contains any value(s) larger than 65,535, it will be replaced with a `Uint32Array`.
175
- * This method is invoked by [[push]] and [[append]].
176
- */
177
- ensureBytesPerElement(newValues) {
178
- const curBytesPerElem = this.bytesPerElement;
179
- (0, Assert_1.assert)(curBytesPerElem === 1 || curBytesPerElem === 2 || curBytesPerElem === 4);
180
- if (curBytesPerElem >= 4)
181
- return;
182
- let neededBytesPerElem = curBytesPerElem;
183
- for (const value of newValues) {
184
- if (value > 0xffff) {
185
- neededBytesPerElem = 4;
186
- break;
187
- }
188
- else if (value > 0xff) {
189
- neededBytesPerElem = 2;
190
- }
191
- }
192
- if (neededBytesPerElem <= curBytesPerElem)
193
- return;
194
- this._constructor = neededBytesPerElem === 1 ? Uint8Array : (neededBytesPerElem === 2 ? Uint16Array : Uint32Array);
195
- this._data = new this._constructor(this._data);
196
- }
197
- /** See [[TypedArrayBuilder.push]]. */
198
- push(value) {
199
- this.ensureBytesPerElement([value]);
200
- super.push(value);
201
- }
202
- /** See [[TypedArrayBuilder.append]]. */
203
- append(values) {
204
- this.ensureBytesPerElement(values);
205
- super.append(values);
206
- }
207
- }
208
- exports.UintArrayBuilder = UintArrayBuilder;
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.UintArrayBuilder = exports.Uint32ArrayBuilder = exports.Uint16ArrayBuilder = exports.Uint8ArrayBuilder = exports.TypedArrayBuilder = void 0;
11
+ const Assert_1 = require("./Assert");
12
+ /** Incrementally builds a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of unsigned 8-, 16-, or 32-bit integers.
13
+ * Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.
14
+ * `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.
15
+ *
16
+ * `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two "size" properties":
17
+ * - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and
18
+ * - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].
19
+ * When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.
20
+ *
21
+ * Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].
22
+ * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.
23
+ * @see [[UintArrayBuilder]] when you don't know the maximum number of bytes required for each element in the array.
24
+ * @public
25
+ */
26
+ class TypedArrayBuilder {
27
+ /** Constructs a new builder from the specified options, with a [[length]] of zero. */
28
+ constructor(constructor, options) {
29
+ this._constructor = constructor;
30
+ this._data = new constructor(options?.initialCapacity ?? 0);
31
+ this.growthFactor = Math.max(1.0, options?.growthFactor ?? 1.5);
32
+ this._length = 0;
33
+ }
34
+ /** The number of elements currently in the array. */
35
+ get length() {
36
+ return this._length;
37
+ }
38
+ /** The number of elements that can fit into the memory currently allocated for the array. */
39
+ get capacity() {
40
+ return this._data.length;
41
+ }
42
+ /** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),
43
+ * returns the element at the specified index, with negative integers counting back from the end of the array.
44
+ * @note It is your responsibility to ensure the index falls within the bounds of the array.
45
+ */
46
+ at(index) {
47
+ if (index < 0)
48
+ index = this.length - index;
49
+ const value = this._data[index];
50
+ (0, Assert_1.assert)(value !== undefined, "index out of bounds");
51
+ return value;
52
+ }
53
+ /** Ensure that [[capacity]] is at least equal to `newCapacity`.
54
+ * This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.
55
+ * It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.
56
+ *
57
+ * If `newCapacity` is not greater than the current [[capacity]], this function does nothing.
58
+ * Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.
59
+ * [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.
60
+ */
61
+ ensureCapacity(newCapacity) {
62
+ if (this.capacity >= newCapacity)
63
+ return this.capacity;
64
+ (0, Assert_1.assert)(this.growthFactor >= 1.0);
65
+ newCapacity = Math.ceil(newCapacity * this.growthFactor);
66
+ const prevData = this._data;
67
+ this._data = new this._constructor(newCapacity);
68
+ this._data.set(prevData, 0);
69
+ (0, Assert_1.assert)(this.capacity === newCapacity);
70
+ return this.capacity;
71
+ }
72
+ /** Append the specified value, resizing if necessary. */
73
+ push(value) {
74
+ this.ensureCapacity(this.length + 1);
75
+ this._data[this.length] = value;
76
+ ++this._length;
77
+ }
78
+ /** Append an array of values, resizing (at most once) if necessary. */
79
+ append(values) {
80
+ const newLength = this.length + values.length;
81
+ this.ensureCapacity(newLength);
82
+ this._data.set(values, this.length);
83
+ this._length = newLength;
84
+ }
85
+ /** Obtain the finished array.
86
+ * @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the
87
+ * returned array's length will be equal to [[length]].
88
+ */
89
+ toTypedArray(includeUnusedCapacity = false) {
90
+ if (includeUnusedCapacity)
91
+ return this._data;
92
+ const subarray = this._data.subarray(0, this.length);
93
+ (0, Assert_1.assert)(subarray instanceof this._constructor);
94
+ (0, Assert_1.assert)(subarray.buffer === this._data.buffer);
95
+ return subarray;
96
+ }
97
+ }
98
+ exports.TypedArrayBuilder = TypedArrayBuilder;
99
+ /** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
100
+ * @public
101
+ */
102
+ class Uint8ArrayBuilder extends TypedArrayBuilder {
103
+ /** See [[TypedArrayBuilder]] constructor. */
104
+ constructor(options) {
105
+ super(Uint8Array, options);
106
+ }
107
+ }
108
+ exports.Uint8ArrayBuilder = Uint8ArrayBuilder;
109
+ /** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).
110
+ * @public
111
+ */
112
+ class Uint16ArrayBuilder extends TypedArrayBuilder {
113
+ /** See [[TypedArrayBuilder]] constructor. */
114
+ constructor(options) {
115
+ super(Uint16Array, options);
116
+ }
117
+ }
118
+ exports.Uint16ArrayBuilder = Uint16ArrayBuilder;
119
+ /** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).
120
+ * @public
121
+ */
122
+ class Uint32ArrayBuilder extends TypedArrayBuilder {
123
+ /** See [[TypedArrayBuilder]] constructor. */
124
+ constructor(options) {
125
+ super(Uint32Array, options);
126
+ }
127
+ /** Obtain a view of the finished array as an array of bytes. */
128
+ toUint8Array(includeUnusedCapacity = false) {
129
+ if (includeUnusedCapacity)
130
+ return new Uint8Array(this._data.buffer);
131
+ return new Uint8Array(this._data.buffer, 0, this.length * 4);
132
+ }
133
+ }
134
+ exports.Uint32ArrayBuilder = Uint32ArrayBuilder;
135
+ /** A [[TypedArrayBuilder]] that can populate a [[UintArray]] with the minimum
136
+ * [bytes per element](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT) required.
137
+ *
138
+ * By default, the underlying array is a `Uint8Array`, though this can be configured via [[UintArrayBuilderOptions.initialType]].
139
+ * As values are added to the array, if the bytes per element supported by the underlying array is too small to hold one of the new values, the array is
140
+ * reallocated to a type large enough to hold all of the new values. For example, the following produces a `Uint8Array` because all values are less than 256:
141
+ *
142
+ * ```ts
143
+ * const builder = new UintArrayBuilder();
144
+ * builder.append([1, 2, 254, 255]);
145
+ * const array = builder.toTypedArray();
146
+ * assert(array instanceof Uint8Array);
147
+ * ```
148
+ *
149
+ * However, the following produces a `Uint16Array` because one of the values is larger than 255 but none are larger than 65,535:
150
+ *
151
+ * ```ts
152
+ * const builder = new UintArrayBuilder();
153
+ * builder.append([1, 255, 257, 65535]);
154
+ * const array = builder.toTypedArray();
155
+ * assert(array instanceof Uint16Array);
156
+ * ```
157
+ *
158
+ * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], or [[Uint32ArrayBuilder]] if you know the number of bytes you want to allocate for each element in the array.
159
+ * @public
160
+ */
161
+ class UintArrayBuilder extends TypedArrayBuilder {
162
+ constructor(options) {
163
+ super(options?.initialType ?? Uint8Array, options);
164
+ }
165
+ /** The number of bytes (1, 2, or 4) currently allocated per element by the underlying array.
166
+ * This may change as larger values are added to the array.
167
+ */
168
+ get bytesPerElement() {
169
+ return this._data.BYTES_PER_ELEMENT;
170
+ }
171
+ /** Ensures that the underlying array is of a type that can contain the largest value in `newValues`.
172
+ * For example, if `_data` is a `Uint16Array` and `newValues` contains any value(s) larger than 65,535, it will be replaced with a `Uint32Array`.
173
+ * This method is invoked by [[push]] and [[append]].
174
+ */
175
+ ensureBytesPerElement(newValues) {
176
+ const curBytesPerElem = this.bytesPerElement;
177
+ (0, Assert_1.assert)(curBytesPerElem === 1 || curBytesPerElem === 2 || curBytesPerElem === 4);
178
+ if (curBytesPerElem >= 4)
179
+ return;
180
+ let neededBytesPerElem = curBytesPerElem;
181
+ for (const value of newValues) {
182
+ if (value > 0xffff) {
183
+ neededBytesPerElem = 4;
184
+ break;
185
+ }
186
+ else if (value > 0xff) {
187
+ neededBytesPerElem = 2;
188
+ }
189
+ }
190
+ if (neededBytesPerElem <= curBytesPerElem)
191
+ return;
192
+ this._constructor = neededBytesPerElem === 1 ? Uint8Array : (neededBytesPerElem === 2 ? Uint16Array : Uint32Array);
193
+ this._data = new this._constructor(this._data);
194
+ }
195
+ /** See [[TypedArrayBuilder.push]]. */
196
+ push(value) {
197
+ this.ensureBytesPerElement([value]);
198
+ super.push(value);
199
+ }
200
+ /** See [[TypedArrayBuilder.append]]. */
201
+ append(values) {
202
+ this.ensureBytesPerElement(values);
203
+ super.append(values);
204
+ }
205
+ }
206
+ exports.UintArrayBuilder = UintArrayBuilder;
209
207
  //# sourceMappingURL=TypedArrayBuilder.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"TypedArrayBuilder.js","sourceRoot":"","sources":["../../src/TypedArrayBuilder.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;AAGH,qCAAkC;AA2BlC;;;;;;;;;;;;;GAaG;AACH,MAAa,iBAAiB;IAU5B,sFAAsF;IACtF,YAAsB,WAA2B,EAAE,OAAkC;;QACnF,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,IAAI,CAAC,KAAK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EAAE;YAC7B,IAAI,KAAK,GAAG,MAAM,EAAE;gBAClB,kBAAkB,GAAG,CAAC,CAAC;gBACvB,MAAM;aACP;iBAAM,IAAI,KAAK,GAAG,IAAI,EAAE;gBACvB,kBAAkB,GAAG,CAAC,CAAC;aACxB;SACF;QAED,IAAI,kBAAkB,IAAI,eAAe;YACvC,OAAO;QAET,IAAI,CAAC,YAAY,GAAG,kBAAkB,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,kBAAkB,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;QACnH,IAAI,CAAC,KAAK,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACjD,CAAC;IAED,sCAAsC;IACtB,IAAI,CAAC,KAAa;QAChC,IAAI,CAAC,qBAAqB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;QACpC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,wCAAwC;IACxB,MAAM,CAAC,MAAiB;QACtC,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC;QACnC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACvB,CAAC;CACF;AAlDD,4CAkDC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Collections\r\n */\r\n\r\nimport { Constructor } from \"./UtilityTypes\";\r\nimport { assert } from \"./Assert\";\r\n\r\n/** Options used to construct a [[TypedArrayBuilder]].\r\n * @public\r\n */\r\nexport interface TypedArrayBuilderOptions {\r\n /** Controls how much additional memory is allocated when the TypedArray needs to be resized to accomodate more elements.\r\n * [[TypedArrayBuilder.ensureCapacity]] multiplies the required capacity by this factor to determine the new capacity.\r\n * Default: 1.5\r\n * Minimum: 1.0, which causes the TypedArray to allocate exactly the space it needs each time it is resized.\r\n */\r\n growthFactor?: number;\r\n /** The number of elements to allocate memory for in the TypedArray when creating the builder.\r\n * If you know the minimum number of elements you intend to add to the builder, you should specify that as the\r\n * initial capacity to avoid reallocations when populating the array.\r\n * Default: 0.\r\n * Minimum: 0.\r\n */\r\n initialCapacity?: number;\r\n}\r\n\r\n/** A [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) containing unsigned 8-, 16-, or 32-bit integers.\r\n * @see [[UintArrayBuilder]] to construct such an array.\r\n * @public\r\n */\r\nexport type UintArray = Uint8Array | Uint16Array | Uint32Array;\r\n\r\n/** Incrementally builds a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of unsigned 8-, 16-, or 32-bit integers.\r\n * Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.\r\n * `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.\r\n *\r\n * `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two \"size\" properties\":\r\n * - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and\r\n * - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].\r\n * When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.\r\n *\r\n * Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].\r\n * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.\r\n * @see [[UintArrayBuilder]] when you don't know the maximum number of bytes required for each element in the array.\r\n * @public\r\n */\r\nexport class TypedArrayBuilder<T extends UintArray> {\r\n /** The constructor for the specific type of array being populated. */\r\n protected _constructor: Constructor<T>;\r\n /** The underlying typed array, to be reallocated and copied when its capacity is exceeded. */\r\n protected _data: T;\r\n /** The number of elements added to the array so far. */\r\n protected _length: number;\r\n /** Multiplier applied to required capacity by [[ensureCapacity]]. */\r\n public readonly growthFactor: number;\r\n\r\n /** Constructs a new builder from the specified options, with a [[length]] of zero. */\r\n protected constructor(constructor: Constructor<T>, options?: TypedArrayBuilderOptions) {\r\n this._constructor = constructor;\r\n this._data = new constructor(options?.initialCapacity ?? 0);\r\n this.growthFactor = Math.max(1.0, options?.growthFactor ?? 1.5);\r\n this._length = 0;\r\n }\r\n\r\n /** The number of elements currently in the array. */\r\n public get length(): number {\r\n return this._length;\r\n }\r\n\r\n /** The number of elements that can fit into the memory currently allocated for the array. */\r\n public get capacity(): number {\r\n return this._data.length;\r\n }\r\n\r\n /** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),\r\n * returns the element at the specified index, with negative integers counting back from the end of the array.\r\n * @note It is your responsibility to ensure the index falls within the bounds of the array.\r\n */\r\n public at(index: number): number {\r\n if (index < 0)\r\n index = this.length - index;\r\n\r\n const value = this._data[index];\r\n assert(value !== undefined, \"index out of bounds\");\r\n return value;\r\n }\r\n\r\n /** Ensure that [[capacity]] is at least equal to `newCapacity`.\r\n * This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.\r\n * It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.\r\n *\r\n * If `newCapacity` is not greater than the current [[capacity]], this function does nothing.\r\n * Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.\r\n * [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.\r\n */\r\n public ensureCapacity(newCapacity: number): number {\r\n if (this.capacity >= newCapacity)\r\n return this.capacity;\r\n\r\n assert(this.growthFactor >= 1.0);\r\n newCapacity = Math.ceil(newCapacity * this.growthFactor);\r\n const prevData = this._data;\r\n this._data = new this._constructor(newCapacity);\r\n this._data.set(prevData, 0);\r\n\r\n assert(this.capacity === newCapacity);\r\n return this.capacity;\r\n }\r\n\r\n /** Append the specified value, resizing if necessary. */\r\n public push(value: number): void {\r\n this.ensureCapacity(this.length + 1);\r\n this._data[this.length] = value;\r\n ++this._length;\r\n }\r\n\r\n /** Append an array of values, resizing (at most once) if necessary. */\r\n public append(values: T): void {\r\n const newLength = this.length + values.length;\r\n this.ensureCapacity(newLength);\r\n this._data.set(values, this.length);\r\n this._length = newLength;\r\n }\r\n\r\n /** Obtain the finished array.\r\n * @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the\r\n * returned array's length will be equal to [[length]].\r\n */\r\n public toTypedArray(includeUnusedCapacity = false): T {\r\n if (includeUnusedCapacity)\r\n return this._data;\r\n\r\n const subarray = this._data.subarray(0, this.length);\r\n assert(subarray instanceof this._constructor);\r\n assert(subarray.buffer === this._data.buffer);\r\n return subarray;\r\n }\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).\r\n * @public\r\n */\r\nexport class Uint8ArrayBuilder extends TypedArrayBuilder<Uint8Array> {\r\n /** See [[TypedArrayBuilder]] constructor. */\r\n public constructor(options?: TypedArrayBuilderOptions) {\r\n super(Uint8Array, options);\r\n }\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).\r\n * @public\r\n */\r\nexport class Uint16ArrayBuilder extends TypedArrayBuilder<Uint16Array> {\r\n /** See [[TypedArrayBuilder]] constructor. */\r\n public constructor(options?: TypedArrayBuilderOptions) {\r\n super(Uint16Array, options);\r\n }\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).\r\n * @public\r\n */\r\nexport class Uint32ArrayBuilder extends TypedArrayBuilder<Uint32Array> {\r\n /** See [[TypedArrayBuilder]] constructor. */\r\n public constructor(options?: TypedArrayBuilderOptions) {\r\n super(Uint32Array, options);\r\n }\r\n\r\n /** Obtain a view of the finished array as an array of bytes. */\r\n public toUint8Array(includeUnusedCapacity = false): Uint8Array {\r\n if (includeUnusedCapacity)\r\n return new Uint8Array(this._data.buffer);\r\n\r\n return new Uint8Array(this._data.buffer, 0, this.length * 4);\r\n }\r\n}\r\n\r\n/** Options used to construct a [[UintArrayBuilder]].\r\n * @public\r\n */\r\nexport interface UintArrayBuilderOptions extends TypedArrayBuilderOptions {\r\n /** The type of the initial empty `TypedArray` created by the builder. For example, if you know that you will be adding values larger than\r\n * 255 to the array, specify `{ initialType: Uint16Array }` to avoid replacing the otherwise default `Uint8Array` when the first such value is added.\r\n * Default: `Uint8Array`.\r\n */\r\n initialType?: typeof Uint8Array | typeof Uint16Array | typeof Uint32Array;\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] that can populate a [[UintArray]] with the minimum\r\n * [bytes per element](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT) required.\r\n *\r\n * By default, the underlying array is a `Uint8Array`, though this can be configured via [[UintArrayBuilderOptions.initialType]].\r\n * As values are added to the array, if the bytes per element supported by the underlying array is too small to hold one of the new values, the array is\r\n * reallocated to a type large enough to hold all of the new values. For example, the following produces a `Uint8Array` because all values are less than 256:\r\n *\r\n * ```ts\r\n * const builder = new UintArrayBuilder();\r\n * builder.append([1, 2, 254, 255]);\r\n * const array = builder.toTypedArray();\r\n * assert(array instanceof Uint8Array);\r\n * ```\r\n *\r\n * However, the following produces a `Uint16Array` because one of the values is larger than 255 but none are larger than 65,535:\r\n *\r\n * ```ts\r\n * const builder = new UintArrayBuilder();\r\n * builder.append([1, 255, 257, 65535]);\r\n * const array = builder.toTypedArray();\r\n * assert(array instanceof Uint16Array);\r\n * ```\r\n *\r\n * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], or [[Uint32ArrayBuilder]] if you know the number of bytes you want to allocate for each element in the array.\r\n * @public\r\n */\r\nexport class UintArrayBuilder extends TypedArrayBuilder<UintArray> {\r\n public constructor(options?: UintArrayBuilderOptions) {\r\n super(options?.initialType ?? Uint8Array, options);\r\n }\r\n\r\n /** The number of bytes (1, 2, or 4) currently allocated per element by the underlying array.\r\n * This may change as larger values are added to the array.\r\n */\r\n public get bytesPerElement(): number {\r\n return this._data.BYTES_PER_ELEMENT;\r\n }\r\n\r\n /** Ensures that the underlying array is of a type that can contain the largest value in `newValues`.\r\n * For example, if `_data` is a `Uint16Array` and `newValues` contains any value(s) larger than 65,535, it will be replaced with a `Uint32Array`.\r\n * This method is invoked by [[push]] and [[append]].\r\n */\r\n protected ensureBytesPerElement(newValues: Iterable<number>): void {\r\n const curBytesPerElem = this.bytesPerElement;\r\n assert(curBytesPerElem === 1 || curBytesPerElem === 2 || curBytesPerElem === 4);\r\n if (curBytesPerElem >= 4)\r\n return;\r\n\r\n let neededBytesPerElem = curBytesPerElem;\r\n for (const value of newValues) {\r\n if (value > 0xffff) {\r\n neededBytesPerElem = 4;\r\n break;\r\n } else if (value > 0xff) {\r\n neededBytesPerElem = 2;\r\n }\r\n }\r\n\r\n if (neededBytesPerElem <= curBytesPerElem)\r\n return;\r\n\r\n this._constructor = neededBytesPerElem === 1 ? Uint8Array : (neededBytesPerElem === 2 ? Uint16Array : Uint32Array);\r\n this._data = new this._constructor(this._data);\r\n }\r\n\r\n /** See [[TypedArrayBuilder.push]]. */\r\n public override push(value: number): void {\r\n this.ensureBytesPerElement([value]);\r\n super.push(value);\r\n }\r\n\r\n /** See [[TypedArrayBuilder.append]]. */\r\n public override append(values: UintArray): void {\r\n this.ensureBytesPerElement(values);\r\n super.append(values);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"TypedArrayBuilder.js","sourceRoot":"","sources":["../../src/TypedArrayBuilder.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;A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AO;QAET,IAAI,kBAAkB,GAAG,eAAe,CAAC;QACzC,KAAK,MAAM,KAAK,IAAI,SAAS,EAAE;YAC7B,IAAI,KAAK,GAAG,MAAM,EAAE;gBAClB,kBAAkB,GAAG,CAAC,CAAC;gBACvB,MAAM;aACP;iBAAM,IAAI,KAAK,GAAG,IAAI,EAAE;gBACvB,kBAAkB,GAAG,CAAC,CAAC;aACxB;SACF;QAED,IAAI,kBAAkB,IAAI,eAAe;YACvC,OAAO;QAET,IAAI,CAAC,YAAY,GAAG,kBAAkB,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,kBAAkB,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;QACnH,IAAI,CAAC,KAAK,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACjD,CAAC;IAED,sCAAsC;IACtB,IAAI,CAAC,KAAa;QAChC,IAAI,CAAC,qBAAqB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;QACpC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,wCAAwC;IACxB,MAAM,CAAC,MAAiB;QACtC,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC;QACnC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACvB,CAAC;CACF;AAlDD,4CAkDC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Collections\r\n */\r\n\r\nimport { Constructor } from \"./UtilityTypes\";\r\nimport { assert } from \"./Assert\";\r\n\r\n/** Options used to construct a [[TypedArrayBuilder]].\r\n * @public\r\n */\r\nexport interface TypedArrayBuilderOptions {\r\n /** Controls how much additional memory is allocated when the TypedArray needs to be resized to accomodate more elements.\r\n * [[TypedArrayBuilder.ensureCapacity]] multiplies the required capacity by this factor to determine the new capacity.\r\n * Default: 1.5\r\n * Minimum: 1.0, which causes the TypedArray to allocate exactly the space it needs each time it is resized.\r\n */\r\n growthFactor?: number;\r\n /** The number of elements to allocate memory for in the TypedArray when creating the builder.\r\n * If you know the minimum number of elements you intend to add to the builder, you should specify that as the\r\n * initial capacity to avoid reallocations when populating the array.\r\n * Default: 0.\r\n * Minimum: 0.\r\n */\r\n initialCapacity?: number;\r\n}\r\n\r\n/** A [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) containing unsigned 8-, 16-, or 32-bit integers.\r\n * @see [[UintArrayBuilder]] to construct such an array.\r\n * @public\r\n */\r\nexport type UintArray = Uint8Array | Uint16Array | Uint32Array;\r\n\r\n/** Incrementally builds a [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) of unsigned 8-, 16-, or 32-bit integers.\r\n * Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.\r\n * `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.\r\n *\r\n * `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two \"size\" properties\":\r\n * - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and\r\n * - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].\r\n * When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.\r\n *\r\n * Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].\r\n * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.\r\n * @see [[UintArrayBuilder]] when you don't know the maximum number of bytes required for each element in the array.\r\n * @public\r\n */\r\nexport class TypedArrayBuilder<T extends UintArray> {\r\n /** The constructor for the specific type of array being populated. */\r\n protected _constructor: Constructor<T>;\r\n /** The underlying typed array, to be reallocated and copied when its capacity is exceeded. */\r\n protected _data: T;\r\n /** The number of elements added to the array so far. */\r\n protected _length: number;\r\n /** Multiplier applied to required capacity by [[ensureCapacity]]. */\r\n public readonly growthFactor: number;\r\n\r\n /** Constructs a new builder from the specified options, with a [[length]] of zero. */\r\n protected constructor(constructor: Constructor<T>, options?: TypedArrayBuilderOptions) {\r\n this._constructor = constructor;\r\n this._data = new constructor(options?.initialCapacity ?? 0);\r\n this.growthFactor = Math.max(1.0, options?.growthFactor ?? 1.5);\r\n this._length = 0;\r\n }\r\n\r\n /** The number of elements currently in the array. */\r\n public get length(): number {\r\n return this._length;\r\n }\r\n\r\n /** The number of elements that can fit into the memory currently allocated for the array. */\r\n public get capacity(): number {\r\n return this._data.length;\r\n }\r\n\r\n /** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),\r\n * returns the element at the specified index, with negative integers counting back from the end of the array.\r\n * @note It is your responsibility to ensure the index falls within the bounds of the array.\r\n */\r\n public at(index: number): number {\r\n if (index < 0)\r\n index = this.length - index;\r\n\r\n const value = this._data[index];\r\n assert(value !== undefined, \"index out of bounds\");\r\n return value;\r\n }\r\n\r\n /** Ensure that [[capacity]] is at least equal to `newCapacity`.\r\n * This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.\r\n * It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.\r\n *\r\n * If `newCapacity` is not greater than the current [[capacity]], this function does nothing.\r\n * Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.\r\n * [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.\r\n */\r\n public ensureCapacity(newCapacity: number): number {\r\n if (this.capacity >= newCapacity)\r\n return this.capacity;\r\n\r\n assert(this.growthFactor >= 1.0);\r\n newCapacity = Math.ceil(newCapacity * this.growthFactor);\r\n const prevData = this._data;\r\n this._data = new this._constructor(newCapacity);\r\n this._data.set(prevData, 0);\r\n\r\n assert(this.capacity === newCapacity);\r\n return this.capacity;\r\n }\r\n\r\n /** Append the specified value, resizing if necessary. */\r\n public push(value: number): void {\r\n this.ensureCapacity(this.length + 1);\r\n this._data[this.length] = value;\r\n ++this._length;\r\n }\r\n\r\n /** Append an array of values, resizing (at most once) if necessary. */\r\n public append(values: T): void {\r\n const newLength = this.length + values.length;\r\n this.ensureCapacity(newLength);\r\n this._data.set(values, this.length);\r\n this._length = newLength;\r\n }\r\n\r\n /** Obtain the finished array.\r\n * @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the\r\n * returned array's length will be equal to [[length]].\r\n */\r\n public toTypedArray(includeUnusedCapacity = false): T {\r\n if (includeUnusedCapacity)\r\n return this._data;\r\n\r\n const subarray = this._data.subarray(0, this.length);\r\n assert(subarray instanceof this._constructor);\r\n assert(subarray.buffer === this._data.buffer);\r\n return subarray;\r\n }\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).\r\n * @public\r\n */\r\nexport class Uint8ArrayBuilder extends TypedArrayBuilder<Uint8Array> {\r\n /** See [[TypedArrayBuilder]] constructor. */\r\n public constructor(options?: TypedArrayBuilderOptions) {\r\n super(Uint8Array, options);\r\n }\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).\r\n * @public\r\n */\r\nexport class Uint16ArrayBuilder extends TypedArrayBuilder<Uint16Array> {\r\n /** See [[TypedArrayBuilder]] constructor. */\r\n public constructor(options?: TypedArrayBuilderOptions) {\r\n super(Uint16Array, options);\r\n }\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).\r\n * @public\r\n */\r\nexport class Uint32ArrayBuilder extends TypedArrayBuilder<Uint32Array> {\r\n /** See [[TypedArrayBuilder]] constructor. */\r\n public constructor(options?: TypedArrayBuilderOptions) {\r\n super(Uint32Array, options);\r\n }\r\n\r\n /** Obtain a view of the finished array as an array of bytes. */\r\n public toUint8Array(includeUnusedCapacity = false): Uint8Array {\r\n if (includeUnusedCapacity)\r\n return new Uint8Array(this._data.buffer);\r\n\r\n return new Uint8Array(this._data.buffer, 0, this.length * 4);\r\n }\r\n}\r\n\r\n/** Options used to construct a [[UintArrayBuilder]].\r\n * @public\r\n */\r\nexport interface UintArrayBuilderOptions extends TypedArrayBuilderOptions {\r\n /** The type of the initial empty `TypedArray` created by the builder. For example, if you know that you will be adding values larger than\r\n * 255 to the array, specify `{ initialType: Uint16Array }` to avoid replacing the otherwise default `Uint8Array` when the first such value is added.\r\n * Default: `Uint8Array`.\r\n */\r\n initialType?: typeof Uint8Array | typeof Uint16Array | typeof Uint32Array;\r\n}\r\n\r\n/** A [[TypedArrayBuilder]] that can populate a [[UintArray]] with the minimum\r\n * [bytes per element](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT) required.\r\n *\r\n * By default, the underlying array is a `Uint8Array`, though this can be configured via [[UintArrayBuilderOptions.initialType]].\r\n * As values are added to the array, if the bytes per element supported by the underlying array is too small to hold one of the new values, the array is\r\n * reallocated to a type large enough to hold all of the new values. For example, the following produces a `Uint8Array` because all values are less than 256:\r\n *\r\n * ```ts\r\n * const builder = new UintArrayBuilder();\r\n * builder.append([1, 2, 254, 255]);\r\n * const array = builder.toTypedArray();\r\n * assert(array instanceof Uint8Array);\r\n * ```\r\n *\r\n * However, the following produces a `Uint16Array` because one of the values is larger than 255 but none are larger than 65,535:\r\n *\r\n * ```ts\r\n * const builder = new UintArrayBuilder();\r\n * builder.append([1, 255, 257, 65535]);\r\n * const array = builder.toTypedArray();\r\n * assert(array instanceof Uint16Array);\r\n * ```\r\n *\r\n * @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], or [[Uint32ArrayBuilder]] if you know the number of bytes you want to allocate for each element in the array.\r\n * @public\r\n */\r\nexport class UintArrayBuilder extends TypedArrayBuilder<UintArray> {\r\n public constructor(options?: UintArrayBuilderOptions) {\r\n super(options?.initialType ?? Uint8Array, options);\r\n }\r\n\r\n /** The number of bytes (1, 2, or 4) currently allocated per element by the underlying array.\r\n * This may change as larger values are added to the array.\r\n */\r\n public get bytesPerElement(): number {\r\n return this._data.BYTES_PER_ELEMENT;\r\n }\r\n\r\n /** Ensures that the underlying array is of a type that can contain the largest value in `newValues`.\r\n * For example, if `_data` is a `Uint16Array` and `newValues` contains any value(s) larger than 65,535, it will be replaced with a `Uint32Array`.\r\n * This method is invoked by [[push]] and [[append]].\r\n */\r\n protected ensureBytesPerElement(newValues: Iterable<number>): void {\r\n const curBytesPerElem = this.bytesPerElement;\r\n assert(curBytesPerElem === 1 || curBytesPerElem === 2 || curBytesPerElem === 4);\r\n if (curBytesPerElem >= 4)\r\n return;\r\n\r\n let neededBytesPerElem = curBytesPerElem;\r\n for (const value of newValues) {\r\n if (value > 0xffff) {\r\n neededBytesPerElem = 4;\r\n break;\r\n } else if (value > 0xff) {\r\n neededBytesPerElem = 2;\r\n }\r\n }\r\n\r\n if (neededBytesPerElem <= curBytesPerElem)\r\n return;\r\n\r\n this._constructor = neededBytesPerElem === 1 ? Uint8Array : (neededBytesPerElem === 2 ? Uint16Array : Uint32Array);\r\n this._data = new this._constructor(this._data);\r\n }\r\n\r\n /** See [[TypedArrayBuilder.push]]. */\r\n public override push(value: number): void {\r\n this.ensureBytesPerElement([value]);\r\n super.push(value);\r\n }\r\n\r\n /** See [[TypedArrayBuilder.append]]. */\r\n public override append(values: UintArray): void {\r\n this.ensureBytesPerElement(values);\r\n super.append(values);\r\n }\r\n}\r\n"]}
@@ -1,44 +1,44 @@
1
- /** @packageDocumentation
2
- * @module Errors
3
- */
4
- /// <reference types="node" />
5
- /** A function to be notified when an unexpected error happens
6
- * @public
7
- */
8
- export declare type OnUnexpectedError = (error: any) => void;
9
- /**
10
- * Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
11
- * unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
12
- * errors should not cause program termination, and should instead be logged and swallowed. However, for
13
- * development/debugging, it can be helpful to re-throw exceptions so they are not missed.
14
- * @public
15
- */
16
- export declare class UnexpectedErrors {
17
- /** handler for re-throwing exceptions directly */
18
- static readonly reThrowImmediate: (e: any) => never;
19
- /** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
20
- static readonly reThrowDeferred: (e: any) => NodeJS.Timeout;
21
- /** handler for logging exception to console */
22
- static readonly consoleLog: (e: any) => void;
23
- /** handler for logging exception with [[Logger]] */
24
- static readonly errorLog: (e: any) => void;
25
- private static _telemetry;
26
- private static _handler;
27
- private constructor();
28
- /** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
29
- * @returns a method to remove the tracker
30
- */
31
- static addTelemetry(tracker: OnUnexpectedError): () => void;
32
- /** call this method when an unexpected error happens so the global handler can process it.
33
- * @param error the unexpected error
34
- * @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
35
- */
36
- static handle(error: any, notifyTelemetry?: boolean): void;
37
- /** establish a new global *unexpected error* handler.
38
- * @param handler the new global handler. You may provide your own function or use one of the static members of this class.
39
- * The default is [[errorLog]].
40
- * @returns the previous handler. Useful to temporarily change the handler.
41
- */
42
- static setHandler(handler: OnUnexpectedError): OnUnexpectedError;
43
- }
1
+ /** @packageDocumentation
2
+ * @module Errors
3
+ */
4
+ /// <reference types="node" />
5
+ /** A function to be notified when an unexpected error happens
6
+ * @public
7
+ */
8
+ export type OnUnexpectedError = (error: any) => void;
9
+ /**
10
+ * Utility for handling/reporting unexpected runtime errors. This class establishes a global handler for
11
+ * unexpected errors, and programmers should use its `handle` method when they occur. Generally, unexpected
12
+ * errors should not cause program termination, and should instead be logged and swallowed. However, for
13
+ * development/debugging, it can be helpful to re-throw exceptions so they are not missed.
14
+ * @public
15
+ */
16
+ export declare class UnexpectedErrors {
17
+ /** handler for re-throwing exceptions directly */
18
+ static readonly reThrowImmediate: (e: any) => never;
19
+ /** handler for re-throwing exceptions from an asynchronous interval (so the current call stack is not aborted) */
20
+ static readonly reThrowDeferred: (e: any) => NodeJS.Timeout;
21
+ /** handler for logging exception to console */
22
+ static readonly consoleLog: (e: any) => void;
23
+ /** handler for logging exception with [[Logger]] */
24
+ static readonly errorLog: (e: any) => void;
25
+ private static _telemetry;
26
+ private static _handler;
27
+ private constructor();
28
+ /** Add a "telemetry tracker" for unexpected errors. Useful for tracking/reporting errors without changing handler.
29
+ * @returns a method to remove the tracker
30
+ */
31
+ static addTelemetry(tracker: OnUnexpectedError): () => void;
32
+ /** call this method when an unexpected error happens so the global handler can process it.
33
+ * @param error the unexpected error
34
+ * @param notifyTelemetry if false, don't notify telemetry trackers. Use this for exceptions from third-party code, for example.
35
+ */
36
+ static handle(error: any, notifyTelemetry?: boolean): void;
37
+ /** establish a new global *unexpected error* handler.
38
+ * @param handler the new global handler. You may provide your own function or use one of the static members of this class.
39
+ * The default is [[errorLog]].
40
+ * @returns the previous handler. Useful to temporarily change the handler.
41
+ */
42
+ static setHandler(handler: OnUnexpectedError): OnUnexpectedError;
43
+ }
44
44
  //# sourceMappingURL=UnexpectedErrors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"UnexpectedErrors.d.ts","sourceRoot":"","sources":["../../src/UnexpectedErrors.ts"],"names":[],"mappings":"AAIA;;GAEG;;AAIH;;GAEG;AACH,oBAAY,iBAAiB,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC;AAErD;;;;;;GAMG;AACH,qBAAa,gBAAgB;IAC3B,kDAAkD;IAClD,gBAAuB,gBAAgB,MAAO,GAAG,WAAkB;IACnE,kHAAkH;IAClH,gBAA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