@fluid-experimental/property-common 2.0.0-dev.2.3.0.115467 → 2.0.0-dev.4.1.0.148229

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 (127) hide show
  1. package/.eslintrc.js +9 -11
  2. package/.mocharc.json +6 -6
  3. package/README.md +1 -0
  4. package/api-extractor.json +2 -2
  5. package/dist/chronometer.d.ts.map +1 -1
  6. package/dist/chronometer.js.map +1 -1
  7. package/dist/consoleUtils.d.ts.map +1 -1
  8. package/dist/consoleUtils.js +4 -1
  9. package/dist/consoleUtils.js.map +1 -1
  10. package/dist/constants.d.ts +6 -6
  11. package/dist/constants.d.ts.map +1 -1
  12. package/dist/constants.js +16 -26
  13. package/dist/constants.js.map +1 -1
  14. package/dist/datastructures/collection.d.ts.map +1 -1
  15. package/dist/datastructures/collection.js +5 -4
  16. package/dist/datastructures/collection.js.map +1 -1
  17. package/dist/datastructures/dataArray.d.ts.map +1 -1
  18. package/dist/datastructures/dataArray.js +18 -9
  19. package/dist/datastructures/dataArray.js.map +1 -1
  20. package/dist/datastructures/integer64.d.ts.map +1 -1
  21. package/dist/datastructures/integer64.js +1 -1
  22. package/dist/datastructures/integer64.js.map +1 -1
  23. package/dist/datastructures/sortedCollection.d.ts.map +1 -1
  24. package/dist/datastructures/sortedCollection.js +3 -1
  25. package/dist/datastructures/sortedCollection.js.map +1 -1
  26. package/dist/deferredPromise.d.ts +1 -1
  27. package/dist/deferredPromise.d.ts.map +1 -1
  28. package/dist/deferredPromise.js +9 -3
  29. package/dist/deferredPromise.js.map +1 -1
  30. package/dist/deterministicRandomGenerator.d.ts.map +1 -1
  31. package/dist/deterministicRandomGenerator.js +6 -6
  32. package/dist/deterministicRandomGenerator.js.map +1 -1
  33. package/dist/error_objects/flaggedError.d.ts.map +1 -1
  34. package/dist/error_objects/flaggedError.js.map +1 -1
  35. package/dist/error_objects/httpError.d.ts.map +1 -1
  36. package/dist/error_objects/httpError.js +9 -9
  37. package/dist/error_objects/httpError.js.map +1 -1
  38. package/dist/error_objects/httpErrorNoStack.d.ts.map +1 -1
  39. package/dist/error_objects/httpErrorNoStack.js.map +1 -1
  40. package/dist/error_objects/operationError.d.ts +7 -7
  41. package/dist/error_objects/operationError.d.ts.map +1 -1
  42. package/dist/error_objects/operationError.js +7 -7
  43. package/dist/error_objects/operationError.js.map +1 -1
  44. package/dist/guidUtils.d.ts.map +1 -1
  45. package/dist/guidUtils.js +10 -9
  46. package/dist/guidUtils.js.map +1 -1
  47. package/dist/hashCalculator.d.ts.map +1 -1
  48. package/dist/hashCalculator.js.map +1 -1
  49. package/dist/joinPaths.d.ts.map +1 -1
  50. package/dist/joinPaths.js.map +1 -1
  51. package/dist/packageVersion.d.ts +1 -1
  52. package/dist/packageVersion.js +1 -1
  53. package/dist/packageVersion.js.map +1 -1
  54. package/lib/chronometer.d.ts.map +1 -1
  55. package/lib/chronometer.js.map +1 -1
  56. package/lib/consoleUtils.d.ts.map +1 -1
  57. package/lib/consoleUtils.js +4 -1
  58. package/lib/consoleUtils.js.map +1 -1
  59. package/lib/constants.d.ts +6 -6
  60. package/lib/constants.d.ts.map +1 -1
  61. package/lib/constants.js +16 -26
  62. package/lib/constants.js.map +1 -1
  63. package/lib/datastructures/collection.d.ts.map +1 -1
  64. package/lib/datastructures/collection.js +5 -4
  65. package/lib/datastructures/collection.js.map +1 -1
  66. package/lib/datastructures/dataArray.d.ts.map +1 -1
  67. package/lib/datastructures/dataArray.js +18 -9
  68. package/lib/datastructures/dataArray.js.map +1 -1
  69. package/lib/datastructures/integer64.d.ts.map +1 -1
  70. package/lib/datastructures/integer64.js +1 -1
  71. package/lib/datastructures/integer64.js.map +1 -1
  72. package/lib/datastructures/sortedCollection.d.ts.map +1 -1
  73. package/lib/datastructures/sortedCollection.js +3 -1
  74. package/lib/datastructures/sortedCollection.js.map +1 -1
  75. package/lib/deferredPromise.d.ts +1 -1
  76. package/lib/deferredPromise.d.ts.map +1 -1
  77. package/lib/deferredPromise.js +9 -3
  78. package/lib/deferredPromise.js.map +1 -1
  79. package/lib/deterministicRandomGenerator.d.ts.map +1 -1
  80. package/lib/deterministicRandomGenerator.js +6 -6
  81. package/lib/deterministicRandomGenerator.js.map +1 -1
  82. package/lib/error_objects/flaggedError.d.ts.map +1 -1
  83. package/lib/error_objects/flaggedError.js.map +1 -1
  84. package/lib/error_objects/httpError.d.ts.map +1 -1
  85. package/lib/error_objects/httpError.js +9 -9
  86. package/lib/error_objects/httpError.js.map +1 -1
  87. package/lib/error_objects/httpErrorNoStack.d.ts.map +1 -1
  88. package/lib/error_objects/httpErrorNoStack.js.map +1 -1
  89. package/lib/error_objects/operationError.d.ts +7 -7
  90. package/lib/error_objects/operationError.d.ts.map +1 -1
  91. package/lib/error_objects/operationError.js +7 -7
  92. package/lib/error_objects/operationError.js.map +1 -1
  93. package/lib/guidUtils.d.ts.map +1 -1
  94. package/lib/guidUtils.js +10 -9
  95. package/lib/guidUtils.js.map +1 -1
  96. package/lib/hashCalculator.d.ts.map +1 -1
  97. package/lib/hashCalculator.js.map +1 -1
  98. package/lib/joinPaths.d.ts.map +1 -1
  99. package/lib/joinPaths.js.map +1 -1
  100. package/lib/packageVersion.d.ts +1 -1
  101. package/lib/packageVersion.js +1 -1
  102. package/lib/packageVersion.js.map +1 -1
  103. package/package.json +42 -40
  104. package/platform-dependent/README.md +1 -0
  105. package/platform-dependent/browser.js +5 -5
  106. package/platform-dependent/package.json +14 -14
  107. package/platform-dependent/server.js +5 -5
  108. package/prettier.config.cjs +1 -1
  109. package/src/chronometer.ts +174 -169
  110. package/src/consoleUtils.ts +16 -11
  111. package/src/constants.ts +1683 -1640
  112. package/src/datastructures/collection.ts +417 -416
  113. package/src/datastructures/dataArray.ts +516 -492
  114. package/src/datastructures/integer64.ts +102 -99
  115. package/src/datastructures/sortedCollection.ts +155 -145
  116. package/src/deferredPromise.ts +43 -42
  117. package/src/deterministicRandomGenerator.ts +59 -59
  118. package/src/error_objects/flaggedError.ts +29 -29
  119. package/src/error_objects/httpError.ts +45 -42
  120. package/src/error_objects/httpErrorNoStack.ts +17 -17
  121. package/src/error_objects/operationError.ts +62 -62
  122. package/src/guidUtils.ts +182 -171
  123. package/src/hashCalculator.ts +5 -2
  124. package/src/joinPaths.ts +15 -9
  125. package/src/packageVersion.ts +1 -1
  126. package/tsconfig.esnext.json +6 -6
  127. package/tsconfig.json +9 -13
@@ -15,307 +15,316 @@
15
15
  * as a backing store for collaborative property sets.
16
16
  */
17
17
  class BaseDataArray {
18
- protected _buffer: any;
19
- private readonly bufferConstructor;
20
- protected size: number;
21
-
22
- constructor(size: number);
23
-
24
- /**
25
- * @param bufferConstructor - This is the constructor to be used to setup the internal buffer of the DataArray.
26
- * @param size - The initial size with which to allocate the array.
27
- */
28
- constructor(bufferConstructor: any, size: number);
29
- constructor(a, b?) {
30
- if (typeof a === "number") {
31
- this.size = a;
32
- this.bufferConstructor = this.getBufferCtor();
33
- } else {
34
- this.bufferConstructor = a;
35
- this.size = b;
36
- }
37
- // The 'eslint-...' is used to disable the rule that requires
38
- // constructors to start with a capitalized letter.
39
- this._buffer = new this.bufferConstructor(this.size); // eslint-disable-line new-cap
40
- }
41
-
42
- /**
43
- * Get the value at an index. If no index is passed, return zeroth item.
44
- * @param in_idx - The specific item in the data array.
45
- * @returns The value at that index.
46
- */
47
- getValue(in_idx = 0): any {
48
- if (in_idx >= this.size || in_idx < 0) {
49
- throw new Error("Trying to access out of bounds!");
50
- }
51
-
52
- return this._buffer[in_idx];
53
- }
54
-
55
- /**
56
- * Return a range of values in the array.
57
- * @param in_idxStart - the starting index
58
- * @param in_idxEnd - the end index
59
- * @returns the array of values in the range
60
- */
61
- getValueRange(in_idxStart: number, in_idxEnd: number) {
62
- if (in_idxStart >= this.size || in_idxEnd > this.size || in_idxStart < 0 || in_idxEnd < 0) {
63
- throw new Error("Trying to access out of bounds!");
64
- }
65
-
66
- return this._buffer.subarray(in_idxStart, in_idxEnd);
67
- }
68
-
69
- /**
70
- * Return the serialized form of Data Arrays.
71
- * @returns An object containing an array of the values.
72
- */
73
- serialize(): number[] {
74
- return Array.from(this._buffer.subarray(0, this.size));
75
- }
76
-
77
- /**
78
- * Deserialize data from a serialized representation
79
- * @param in_serialized - the serialized representation
80
- */
81
- deserialize(in_serialized) {
82
- const values = in_serialized;
83
- const length = in_serialized.length;
84
- if (length !== this.size) {
85
- this.resize(length);
86
- this.size = length;
87
- }
88
- let i;
89
- for (i = 0; i < length; i++) {
90
- this._buffer[i] = values[i];
91
- }
92
- }
93
-
94
- /**
95
- * Set value at an index.
96
- * @param in_idx - the index
97
- * @param in_value - the value we want to set at index
98
- */
99
- setValue(in_idx: number, in_value) {
100
- if (in_idx < this._buffer.length) {
101
- this._buffer[in_idx] = in_value;
102
- } else {
103
- console.error("DataArray setValue in_idx is out of bounds.");
104
- }
105
- }
106
-
107
- /**
108
- * creates a copy of a typed array with removed elements
109
- * @param in_arr - the input array (won't be modified)
110
- * @param in_offset - starting index of range that will be removed
111
- * @param in_deleteCount - number of removed elements
112
- * @returns a copy of the input array without the selected range
113
- */
114
- private _removeElementsFromArray(in_arr, in_offset: number, in_deleteCount: number) {
115
- // TODO: this function can be optimized
116
- const newSize = this.size - in_deleteCount;
117
- const splicedArray = new in_arr.constructor(newSize);
118
- splicedArray.set(in_arr.subarray(0, in_offset));
119
- splicedArray.set(in_arr.subarray(in_offset + in_deleteCount, this.size), in_offset);
120
- return splicedArray;
121
- }
122
-
123
- /**
124
- * remove a range of elements from the array
125
- * @param in_offset - start of the range
126
- * @param in_deleteCount - number of elements to be removed
127
- */
128
- removeRange(in_offset: number, in_deleteCount: number) {
129
- if (in_offset + in_deleteCount < (this._buffer.length as number) + 1) {
130
- this._buffer = this._removeElementsFromArray(this._buffer, in_offset, in_deleteCount);
131
- this.size = this.size - in_deleteCount;
132
- } else {
133
- console.error("DataArray removeRange in_offset + in_deleteCount is out of bounds.");
134
- }
135
- }
136
-
137
- /**
138
- * copy an array with elements inserted into the copy
139
- * @param in_arr - the input array (won't be modified)
140
- * @param in_offset - the index where the new elements will be inserted
141
- * @param in_addedArray - the array with the elements that will be added
142
- * @returns the combined array
143
- */
144
- private _insert(in_arr, in_offset: number, in_addedArray) {
145
- // TODO: this function can be optimized
146
- const newSize = this.size + (in_addedArray.length as number);
147
- const insertedArray = new in_arr.constructor(newSize);
148
- insertedArray.set(in_arr.subarray(0, in_offset));
149
- insertedArray.set(in_addedArray, in_offset);
150
- insertedArray.set(in_arr.subarray(in_offset, this.size), in_offset + (in_addedArray.length as number));
151
- return insertedArray;
152
- }
153
-
154
- /**
155
- * insert the content of an array into the DataArray
156
- * @param in_offset - the target index
157
- * @param in_array - the array to be inserted
158
- */
159
- insertRange(in_offset: number, in_array) {
160
- this._buffer = this._insert(this._buffer, in_offset, in_array);
161
- this.size = this.size + (in_array.length as number);
162
- }
163
-
164
- /**
165
- * Set this array values to be equal to in_array values.
166
- * @param in_offset - An optional offset in this array to begin start setting this arrays values to in_array values.
167
- * @param in_array - The input array.
168
- */
169
- set(in_offset: number, in_array) {
170
- if (in_array instanceof ArrayBuffer || in_array instanceof Array || in_array instanceof this.getBufferCtor()) {
171
- this._buffer.set(in_array, in_offset);
172
- } else if (in_array instanceof BaseDataArray) {
173
- this._buffer.set(in_array.getBuffer(), in_offset);
174
- } else {
175
- console.error("DataArray set() must be called with Array, TypedArray or DataArray");
176
- }
177
- }
178
-
179
- /**
180
- * Insert a value at the end of the array, creates a new element at the end and sets the value.
181
- * @param in_value - The new value.
182
- */
183
- push(in_value) {
184
- // Adjust the buffer if necessary
185
- const bufferLength = this._buffer.length;
186
- if (this.size > bufferLength - 1) {
187
- this._alloc(this.size * 2 || 1); // grow by a factor of 2x
188
- }
189
- // set the value and update size
190
- this.setValue(this.size, in_value);
191
- this.size++;
192
- }
193
-
194
- /**
195
- * Get direct access to the data (for performance reasons) this should be uses read only.
196
- * @returns The (read only) raw data.
197
- */
198
- getBuffer() {
199
- return this._buffer;
200
- }
201
-
202
- /**
203
- * Get the constructor of the underlying `TypedArray`.
204
- * @returns The constructor for the data buffer.
205
- */
206
- getBufferCtor() {
207
- return this.bufferConstructor;
208
- }
209
-
210
- /**
211
- * Apply a given function to all elements of the array.
212
- * @param in_fn - The function that will be applied to every element.
213
- */
214
- iterate(in_fn) {
215
- const l = this.size;
216
- for (let i = 0; i < l; i++) {
217
- in_fn(this._buffer[i]);
218
- }
219
- }
220
-
221
- /**
222
- * Tet a resized buffer copy.
223
- * @param in_bufferCtor - The constructor for the returned buffer.
224
- * @param in_buffer - The input buffer (won't be modified).
225
- * @param in_newSize - The target size.
226
- * @returns The buffer with the new size.
227
- */
228
- private resizeBuffer(in_bufferCtor, in_buffer, in_newSize: number) {
229
- // target buffer with the desired new size
230
- // The 'eslint-...' is used to disable the rule that requires
231
- // constructors to start with a capitalized letter.
232
- const newBuffer = new in_bufferCtor(in_newSize); // eslint-disable-line new-cap
233
- const oldSize = in_buffer.length;
234
- const oldBuffer = in_buffer;
235
- const isShrinking = oldSize > in_newSize;
236
- newBuffer.set(isShrinking ? oldBuffer.subarray(0, in_newSize) : oldBuffer);
237
- return newBuffer;
238
- }
239
-
240
- /**
241
- * Allocate memory for the array (for performance reasons, you can allocate more space than the current length,
242
- * which makes pushes to the array less expensive later).
243
- * @param size - The target allocated space.
244
- * @returns The DataArray itself.
245
- */
246
- protected _alloc(size: number): any {
247
- this._buffer = this.resizeBuffer(this.bufferConstructor, this._buffer, size);
248
- return this;
249
- }
250
-
251
- /**
252
- * Change the size of the array
253
- * @param size - The target size
254
- * @returns The DataArray itself
255
- */
256
- resize(size: number) { // this can be costly!!!
257
- this._alloc(size);
258
- this.size = size;
259
- return this;
260
- }
261
-
262
- copy() { // and this!
263
- const newBuffer = new this.bufferConstructor(this.size); // buffer with the desired new size
264
- newBuffer.set(this._buffer);
265
- return newBuffer;
266
- }
267
-
268
- get length() {
269
- return this.size;
270
- }
18
+ protected _buffer: any;
19
+ private readonly bufferConstructor;
20
+ protected size: number;
21
+
22
+ constructor(size: number);
23
+
24
+ /**
25
+ * @param bufferConstructor - This is the constructor to be used to setup the internal buffer of the DataArray.
26
+ * @param size - The initial size with which to allocate the array.
27
+ */
28
+ constructor(bufferConstructor: any, size: number);
29
+ constructor(a, b?) {
30
+ if (typeof a === "number") {
31
+ this.size = a;
32
+ this.bufferConstructor = this.getBufferCtor();
33
+ } else {
34
+ this.bufferConstructor = a;
35
+ this.size = b;
36
+ }
37
+ // The 'eslint-...' is used to disable the rule that requires
38
+ // constructors to start with a capitalized letter.
39
+ this._buffer = new this.bufferConstructor(this.size);
40
+ }
41
+
42
+ /**
43
+ * Get the value at an index. If no index is passed, return zeroth item.
44
+ * @param in_idx - The specific item in the data array.
45
+ * @returns The value at that index.
46
+ */
47
+ getValue(in_idx = 0): any {
48
+ if (in_idx >= this.size || in_idx < 0) {
49
+ throw new Error("Trying to access out of bounds!");
50
+ }
51
+
52
+ return this._buffer[in_idx];
53
+ }
54
+
55
+ /**
56
+ * Return a range of values in the array.
57
+ * @param in_idxStart - the starting index
58
+ * @param in_idxEnd - the end index
59
+ * @returns the array of values in the range
60
+ */
61
+ getValueRange(in_idxStart: number, in_idxEnd: number) {
62
+ if (in_idxStart >= this.size || in_idxEnd > this.size || in_idxStart < 0 || in_idxEnd < 0) {
63
+ throw new Error("Trying to access out of bounds!");
64
+ }
65
+
66
+ return this._buffer.subarray(in_idxStart, in_idxEnd);
67
+ }
68
+
69
+ /**
70
+ * Return the serialized form of Data Arrays.
71
+ * @returns An object containing an array of the values.
72
+ */
73
+ serialize(): number[] {
74
+ return Array.from(this._buffer.subarray(0, this.size));
75
+ }
76
+
77
+ /**
78
+ * Deserialize data from a serialized representation
79
+ * @param in_serialized - the serialized representation
80
+ */
81
+ deserialize(in_serialized) {
82
+ const values = in_serialized;
83
+ const length = in_serialized.length;
84
+ if (length !== this.size) {
85
+ this.resize(length);
86
+ this.size = length;
87
+ }
88
+ let i;
89
+ for (i = 0; i < length; i++) {
90
+ this._buffer[i] = values[i];
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Set value at an index.
96
+ * @param in_idx - the index
97
+ * @param in_value - the value we want to set at index
98
+ */
99
+ setValue(in_idx: number, in_value) {
100
+ if (in_idx < this._buffer.length) {
101
+ this._buffer[in_idx] = in_value;
102
+ } else {
103
+ console.error("DataArray setValue in_idx is out of bounds.");
104
+ }
105
+ }
106
+
107
+ /**
108
+ * creates a copy of a typed array with removed elements
109
+ * @param in_arr - the input array (won't be modified)
110
+ * @param in_offset - starting index of range that will be removed
111
+ * @param in_deleteCount - number of removed elements
112
+ * @returns a copy of the input array without the selected range
113
+ */
114
+ private _removeElementsFromArray(in_arr, in_offset: number, in_deleteCount: number) {
115
+ // TODO: this function can be optimized
116
+ const newSize = this.size - in_deleteCount;
117
+ const splicedArray = new in_arr.constructor(newSize);
118
+ splicedArray.set(in_arr.subarray(0, in_offset));
119
+ splicedArray.set(in_arr.subarray(in_offset + in_deleteCount, this.size), in_offset);
120
+ return splicedArray;
121
+ }
122
+
123
+ /**
124
+ * remove a range of elements from the array
125
+ * @param in_offset - start of the range
126
+ * @param in_deleteCount - number of elements to be removed
127
+ */
128
+ removeRange(in_offset: number, in_deleteCount: number) {
129
+ if (in_offset + in_deleteCount < (this._buffer.length as number) + 1) {
130
+ this._buffer = this._removeElementsFromArray(this._buffer, in_offset, in_deleteCount);
131
+ this.size = this.size - in_deleteCount;
132
+ } else {
133
+ console.error("DataArray removeRange in_offset + in_deleteCount is out of bounds.");
134
+ }
135
+ }
136
+
137
+ /**
138
+ * copy an array with elements inserted into the copy
139
+ * @param in_arr - the input array (won't be modified)
140
+ * @param in_offset - the index where the new elements will be inserted
141
+ * @param in_addedArray - the array with the elements that will be added
142
+ * @returns the combined array
143
+ */
144
+ private _insert(in_arr, in_offset: number, in_addedArray) {
145
+ // TODO: this function can be optimized
146
+ const newSize = this.size + (in_addedArray.length as number);
147
+ const insertedArray = new in_arr.constructor(newSize);
148
+ insertedArray.set(in_arr.subarray(0, in_offset));
149
+ insertedArray.set(in_addedArray, in_offset);
150
+ insertedArray.set(
151
+ in_arr.subarray(in_offset, this.size),
152
+ in_offset + (in_addedArray.length as number),
153
+ );
154
+ return insertedArray;
155
+ }
156
+
157
+ /**
158
+ * insert the content of an array into the DataArray
159
+ * @param in_offset - the target index
160
+ * @param in_array - the array to be inserted
161
+ */
162
+ insertRange(in_offset: number, in_array) {
163
+ this._buffer = this._insert(this._buffer, in_offset, in_array);
164
+ this.size = this.size + (in_array.length as number);
165
+ }
166
+
167
+ /**
168
+ * Set this array values to be equal to in_array values.
169
+ * @param in_offset - An optional offset in this array to begin start setting this arrays values to in_array values.
170
+ * @param in_array - The input array.
171
+ */
172
+ set(in_offset: number, in_array) {
173
+ if (
174
+ in_array instanceof ArrayBuffer ||
175
+ in_array instanceof Array ||
176
+ in_array instanceof this.getBufferCtor()
177
+ ) {
178
+ this._buffer.set(in_array, in_offset);
179
+ } else if (in_array instanceof BaseDataArray) {
180
+ this._buffer.set(in_array.getBuffer(), in_offset);
181
+ } else {
182
+ console.error("DataArray set() must be called with Array, TypedArray or DataArray");
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Insert a value at the end of the array, creates a new element at the end and sets the value.
188
+ * @param in_value - The new value.
189
+ */
190
+ push(in_value) {
191
+ // Adjust the buffer if necessary
192
+ const bufferLength = this._buffer.length;
193
+ if (this.size > bufferLength - 1) {
194
+ this._alloc(this.size * 2 || 1); // grow by a factor of 2x
195
+ }
196
+ // set the value and update size
197
+ this.setValue(this.size, in_value);
198
+ this.size++;
199
+ }
200
+
201
+ /**
202
+ * Get direct access to the data (for performance reasons) this should be uses read only.
203
+ * @returns The (read only) raw data.
204
+ */
205
+ getBuffer() {
206
+ return this._buffer;
207
+ }
208
+
209
+ /**
210
+ * Get the constructor of the underlying `TypedArray`.
211
+ * @returns The constructor for the data buffer.
212
+ */
213
+ getBufferCtor() {
214
+ return this.bufferConstructor;
215
+ }
216
+
217
+ /**
218
+ * Apply a given function to all elements of the array.
219
+ * @param in_fn - The function that will be applied to every element.
220
+ */
221
+ iterate(in_fn) {
222
+ const l = this.size;
223
+ for (let i = 0; i < l; i++) {
224
+ in_fn(this._buffer[i]);
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Tet a resized buffer copy.
230
+ * @param in_bufferCtor - The constructor for the returned buffer.
231
+ * @param in_buffer - The input buffer (won't be modified).
232
+ * @param in_newSize - The target size.
233
+ * @returns The buffer with the new size.
234
+ */
235
+ private resizeBuffer(in_bufferCtor, in_buffer, in_newSize: number) {
236
+ // target buffer with the desired new size
237
+ // The 'eslint-...' is used to disable the rule that requires
238
+ // constructors to start with a capitalized letter.
239
+ const newBuffer = new in_bufferCtor(in_newSize);
240
+ const oldSize = in_buffer.length;
241
+ const oldBuffer = in_buffer;
242
+ const isShrinking = oldSize > in_newSize;
243
+ newBuffer.set(isShrinking ? oldBuffer.subarray(0, in_newSize) : oldBuffer);
244
+ return newBuffer;
245
+ }
246
+
247
+ /**
248
+ * Allocate memory for the array (for performance reasons, you can allocate more space than the current length,
249
+ * which makes pushes to the array less expensive later).
250
+ * @param size - The target allocated space.
251
+ * @returns The DataArray itself.
252
+ */
253
+ protected _alloc(size: number): any {
254
+ this._buffer = this.resizeBuffer(this.bufferConstructor, this._buffer, size);
255
+ return this;
256
+ }
257
+
258
+ /**
259
+ * Change the size of the array
260
+ * @param size - The target size
261
+ * @returns The DataArray itself
262
+ */
263
+ resize(size: number) {
264
+ // this can be costly!!!
265
+ this._alloc(size);
266
+ this.size = size;
267
+ return this;
268
+ }
269
+
270
+ copy() {
271
+ // and this!
272
+ const newBuffer = new this.bufferConstructor(this.size); // buffer with the desired new size
273
+ newBuffer.set(this._buffer);
274
+ return newBuffer;
275
+ }
276
+
277
+ get length() {
278
+ return this.size;
279
+ }
271
280
  }
272
281
 
273
282
  class Int8DataArray extends BaseDataArray {
274
- constructor(size: number) {
275
- super(Int8Array, size);
276
- }
283
+ constructor(size: number) {
284
+ super(Int8Array, size);
285
+ }
277
286
  }
278
287
 
279
288
  class Int16DataArray extends BaseDataArray {
280
- constructor(size: number) {
281
- super(Int16Array, size);
282
- }
289
+ constructor(size: number) {
290
+ super(Int16Array, size);
291
+ }
283
292
  }
284
293
 
285
294
  class Int32DataArray extends BaseDataArray {
286
- constructor(size: number) {
287
- super(Int32Array, size);
288
- }
295
+ constructor(size: number) {
296
+ super(Int32Array, size);
297
+ }
289
298
  }
290
299
 
291
300
  class Uint8DataArray extends BaseDataArray {
292
- constructor(size: number) {
293
- super(Uint8Array, size);
294
- }
301
+ constructor(size: number) {
302
+ super(Uint8Array, size);
303
+ }
295
304
  }
296
305
 
297
306
  class Uint16DataArray extends BaseDataArray {
298
- constructor(size: number) {
299
- super(Uint16Array, size);
300
- }
307
+ constructor(size: number) {
308
+ super(Uint16Array, size);
309
+ }
301
310
  }
302
311
 
303
312
  class Uint32DataArray extends BaseDataArray {
304
- constructor(size: number) {
305
- super(Uint32Array, size);
306
- }
313
+ constructor(size: number) {
314
+ super(Uint32Array, size);
315
+ }
307
316
  }
308
317
 
309
318
  class Float32DataArray extends BaseDataArray {
310
- constructor(size: number) {
311
- super(Float32Array, size);
312
- }
319
+ constructor(size: number) {
320
+ super(Float32Array, size);
321
+ }
313
322
  }
314
323
 
315
324
  class Float64DataArray extends BaseDataArray {
316
- constructor(size: number) {
317
- super(Float64Array, size);
318
- }
325
+ constructor(size: number) {
326
+ super(Float64Array, size);
327
+ }
319
328
  }
320
329
 
321
330
  /**
@@ -324,234 +333,249 @@ class Float64DataArray extends BaseDataArray {
324
333
  * @param size - The initial size with which to allocate the array.
325
334
  */
326
335
  class UniversalDataArray extends BaseDataArray {
327
- constructor(bufferConstructor: any, size: number);
328
- constructor(size: number);
329
- constructor(a?, b?) {
330
- if (b === undefined) {
331
- super(Array, a);
332
- } else {
333
- super(a, b);
334
- }
335
- }
336
-
337
- /**
338
- * Helper function to write array values into another array at a given offset.
339
- *
340
- * @param array - The target array.
341
- * @param values - The values we need to write.
342
- * @param offset - The starting index in target array.
343
- */
344
- private arraySet(array, values, offset = 0) {
345
- let index = 0;
346
- values.forEach(function(value) {
347
- array[index + offset] = value;
348
- index++;
349
- });
350
- }
351
-
352
- /**
353
- * Insert the content of an array into the DataArray.
354
- * @param in_offset - The target index.
355
- * @param in_array - The array to be inserted.
356
- */
357
- insertRange(in_offset: number, in_array: any[]) {
358
- this._buffer.splice.call(this._buffer, ...[in_offset, 0].concat(in_array));
359
- this.size = this.size + in_array.length;
360
- }
361
-
362
- /**
363
- * Eemove a range of elements from the array.
364
- * @param in_offset - The start of the range.
365
- * @param in_deleteCount - The number of elements to be removed.
366
- */
367
- removeRange(in_offset: number, in_deleteCount: number) {
368
- if (in_offset + in_deleteCount < (this._buffer.length as number) + 1) {
369
- this._buffer.splice(in_offset, in_deleteCount);
370
- this.size -= in_deleteCount;
371
- } else {
372
- throw Error("DataArray removeRange in_offset + in_deleteCount is out of bounds.");
373
- }
374
- }
375
-
376
- /**
377
- * Set this array values to be equal to in_array values.
378
- * @param in_offset - An optional offset in this array to begin start
379
- * setting this arrays values to in_array values.
380
- * @param in_array - The input array.
381
- */
382
- set(in_offset: number, in_array) {
383
- if (in_array instanceof ArrayBuffer || in_array instanceof Array || in_array instanceof this.getBufferCtor()) {
384
- this.arraySet(this._buffer, in_array, in_offset);
385
- } else if (in_array instanceof BaseDataArray) {
386
- this.arraySet(this._buffer, in_array.getBuffer(), in_offset);
387
- } else {
388
- console.error("DataArray set() must be called with Array, TypedArray or DataArray");
389
- }
390
- }
391
-
392
- /**
393
- * Return a range of values in the array.
394
- * @param in_idxStart - the starting index
395
- * @param in_idxEnd - the end index - this offset is exclusive
396
- * @returns the array of values in the range
397
- */
398
- getValueRange(in_idxStart: number, in_idxEnd: number) {
399
- if (in_idxStart >= this.size || in_idxEnd > this.size || in_idxStart < 0 || in_idxEnd < 0) {
400
- throw new Error("Trying to access out of bounds!");
401
- }
402
- return this._buffer.slice(in_idxStart, in_idxEnd);
403
- }
404
-
405
- /**
406
- * change the size of a javascript array and keep the content, if possible. Keeps the input buffer.
407
- * @param in_buffer - input buffer - not changed
408
- * @param in_newSize - target size
409
- * @returns an Array of the new size
410
- */
411
- private resizeBufferArray(in_buffer, in_newSize) {
412
- // target buffer with the desired new size
413
- const newBuffer = new Array(in_newSize);
414
- const oldSize = in_buffer.length;
415
- const oldBuffer = in_buffer;
416
- const isShrinking = oldSize > in_newSize;
417
- this.arraySet(newBuffer, isShrinking ? oldBuffer.slice(0, in_newSize) : oldBuffer);
418
- return newBuffer;
419
- }
420
-
421
- /**
422
- * allocate memory for the array (for performance reasons, you can allocate more space than the current length,
423
- * which makes pushes to the array less expensive later)
424
- * @param size - the target allocated space
425
- * @returns the DataArray itself
426
- */
427
- protected _alloc(size: number): any {
428
- this._buffer = this.resizeBufferArray(this._buffer, size);
429
- return this;
430
- }
336
+ constructor(bufferConstructor: any, size: number);
337
+ constructor(size: number);
338
+ constructor(a?, b?) {
339
+ if (b === undefined) {
340
+ super(Array, a);
341
+ } else {
342
+ super(a, b);
343
+ }
344
+ }
345
+
346
+ /**
347
+ * Helper function to write array values into another array at a given offset.
348
+ *
349
+ * @param array - The target array.
350
+ * @param values - The values we need to write.
351
+ * @param offset - The starting index in target array.
352
+ */
353
+ private arraySet(array, values, offset = 0) {
354
+ let index = 0;
355
+ values.forEach(function (value) {
356
+ array[index + offset] = value;
357
+ index++;
358
+ });
359
+ }
360
+
361
+ /**
362
+ * Insert the content of an array into the DataArray.
363
+ * @param in_offset - The target index.
364
+ * @param in_array - The array to be inserted.
365
+ */
366
+ insertRange(in_offset: number, in_array: any[]) {
367
+ this._buffer.splice.call(this._buffer, ...[in_offset, 0].concat(in_array));
368
+ this.size = this.size + in_array.length;
369
+ }
370
+
371
+ /**
372
+ * Eemove a range of elements from the array.
373
+ * @param in_offset - The start of the range.
374
+ * @param in_deleteCount - The number of elements to be removed.
375
+ */
376
+ removeRange(in_offset: number, in_deleteCount: number) {
377
+ if (in_offset + in_deleteCount < (this._buffer.length as number) + 1) {
378
+ this._buffer.splice(in_offset, in_deleteCount);
379
+ this.size -= in_deleteCount;
380
+ } else {
381
+ throw Error("DataArray removeRange in_offset + in_deleteCount is out of bounds.");
382
+ }
383
+ }
384
+
385
+ /**
386
+ * Set this array values to be equal to in_array values.
387
+ * @param in_offset - An optional offset in this array to begin start
388
+ * setting this arrays values to in_array values.
389
+ * @param in_array - The input array.
390
+ */
391
+ set(in_offset: number, in_array) {
392
+ if (
393
+ in_array instanceof ArrayBuffer ||
394
+ in_array instanceof Array ||
395
+ in_array instanceof this.getBufferCtor()
396
+ ) {
397
+ this.arraySet(this._buffer, in_array, in_offset);
398
+ } else if (in_array instanceof BaseDataArray) {
399
+ this.arraySet(this._buffer, in_array.getBuffer(), in_offset);
400
+ } else {
401
+ console.error("DataArray set() must be called with Array, TypedArray or DataArray");
402
+ }
403
+ }
404
+
405
+ /**
406
+ * Return a range of values in the array.
407
+ * @param in_idxStart - the starting index
408
+ * @param in_idxEnd - the end index - this offset is exclusive
409
+ * @returns the array of values in the range
410
+ */
411
+ getValueRange(in_idxStart: number, in_idxEnd: number) {
412
+ if (in_idxStart >= this.size || in_idxEnd > this.size || in_idxStart < 0 || in_idxEnd < 0) {
413
+ throw new Error("Trying to access out of bounds!");
414
+ }
415
+ return this._buffer.slice(in_idxStart, in_idxEnd);
416
+ }
417
+
418
+ /**
419
+ * change the size of a javascript array and keep the content, if possible. Keeps the input buffer.
420
+ * @param in_buffer - input buffer - not changed
421
+ * @param in_newSize - target size
422
+ * @returns an Array of the new size
423
+ */
424
+ private resizeBufferArray(in_buffer, in_newSize) {
425
+ // target buffer with the desired new size
426
+ const newBuffer = new Array(in_newSize);
427
+ const oldSize = in_buffer.length;
428
+ const oldBuffer = in_buffer;
429
+ const isShrinking = oldSize > in_newSize;
430
+ this.arraySet(newBuffer, isShrinking ? oldBuffer.slice(0, in_newSize) : oldBuffer);
431
+ return newBuffer;
432
+ }
433
+
434
+ /**
435
+ * allocate memory for the array (for performance reasons, you can allocate more space than the current length,
436
+ * which makes pushes to the array less expensive later)
437
+ * @param size - the target allocated space
438
+ * @returns the DataArray itself
439
+ */
440
+ protected _alloc(size: number): any {
441
+ this._buffer = this.resizeBufferArray(this._buffer, size);
442
+ return this;
443
+ }
431
444
  }
432
445
 
433
446
  /**
434
447
  * A data container that contains a string
435
448
  */
436
449
  class StringDataArray extends BaseDataArray {
437
- constructor() {
438
- super(String, 0);
439
- this.size = 0;
440
- this._buffer = "";
441
- }
442
- /**
443
- * insert the content of a string into the StringDataArray
444
- * @param in_offset - the target index
445
- * @param in_string - the string to be inserted
446
- */
447
- insertRange(in_offset: number, in_string: string) {
448
- this._buffer = `${this._buffer.substr(0, in_offset)}${in_string}${this._buffer.substr(in_offset)}`;
449
- this.size = this.size + in_string.length;
450
- }
451
-
452
- /**
453
- * remove a range of elements from the string
454
- * @param in_offset - start of the range
455
- * @param in_deleteCount - number of elements to be removed
456
- */
457
- removeRange(in_offset: number, in_deleteCount: number) {
458
- if (in_offset + in_deleteCount < (this._buffer.length as number) + 1) {
459
- this._buffer = `${this._buffer.substr(0, in_offset)}${this._buffer.substr(in_offset + in_deleteCount)}`;
460
- this.size -= in_deleteCount;
461
- } else {
462
- throw Error("DataArray removeRange in_offset + in_deleteCount is out of bounds.");
463
- }
464
- }
465
-
466
- /**
467
- * Set this array values to be equal to in_string values.
468
- * @param in_offset - The offset in this array to begin start setting this arrays values to in_string values.
469
- * @param in_string - The input string.
470
- */
471
- set(in_offset: number, in_string: string) {
472
- this._buffer =
473
- `${this._buffer.substr(0, in_offset)}${in_string}${this._buffer.substr(in_offset + in_string.length)}`;
474
- }
475
-
476
- /**
477
- * Return a range of characters in the string.
478
- * @param in_idxStart - the starting index
479
- * @param in_idxEnd - the end index - this offset is exclusive
480
- * @returns the characters in the range
481
- */
482
- getValueRange(in_idxStart: number, in_idxEnd: number): string {
483
- if (in_idxStart >= this.size || in_idxEnd > this.size || in_idxStart < 0 || in_idxEnd < 0) {
484
- throw new Error("Trying to access out of bounds!");
485
- }
486
- return this._buffer.slice(in_idxStart, in_idxEnd);
487
- }
488
-
489
- get length() { return this._buffer.length; }
450
+ constructor() {
451
+ super(String, 0);
452
+ this.size = 0;
453
+ this._buffer = "";
454
+ }
455
+ /**
456
+ * insert the content of a string into the StringDataArray
457
+ * @param in_offset - the target index
458
+ * @param in_string - the string to be inserted
459
+ */
460
+ insertRange(in_offset: number, in_string: string) {
461
+ this._buffer = `${this._buffer.substr(0, in_offset)}${in_string}${this._buffer.substr(
462
+ in_offset,
463
+ )}`;
464
+ this.size = this.size + in_string.length;
465
+ }
466
+
467
+ /**
468
+ * remove a range of elements from the string
469
+ * @param in_offset - start of the range
470
+ * @param in_deleteCount - number of elements to be removed
471
+ */
472
+ removeRange(in_offset: number, in_deleteCount: number) {
473
+ if (in_offset + in_deleteCount < (this._buffer.length as number) + 1) {
474
+ this._buffer = `${this._buffer.substr(0, in_offset)}${this._buffer.substr(
475
+ in_offset + in_deleteCount,
476
+ )}`;
477
+ this.size -= in_deleteCount;
478
+ } else {
479
+ throw Error("DataArray removeRange in_offset + in_deleteCount is out of bounds.");
480
+ }
481
+ }
482
+
483
+ /**
484
+ * Set this array values to be equal to in_string values.
485
+ * @param in_offset - The offset in this array to begin start setting this arrays values to in_string values.
486
+ * @param in_string - The input string.
487
+ */
488
+ set(in_offset: number, in_string: string) {
489
+ this._buffer = `${this._buffer.substr(0, in_offset)}${in_string}${this._buffer.substr(
490
+ in_offset + in_string.length,
491
+ )}`;
492
+ }
493
+
494
+ /**
495
+ * Return a range of characters in the string.
496
+ * @param in_idxStart - the starting index
497
+ * @param in_idxEnd - the end index - this offset is exclusive
498
+ * @returns the characters in the range
499
+ */
500
+ getValueRange(in_idxStart: number, in_idxEnd: number): string {
501
+ if (in_idxStart >= this.size || in_idxEnd > this.size || in_idxStart < 0 || in_idxEnd < 0) {
502
+ throw new Error("Trying to access out of bounds!");
503
+ }
504
+ return this._buffer.slice(in_idxStart, in_idxEnd);
505
+ }
506
+
507
+ get length() {
508
+ return this._buffer.length;
509
+ }
490
510
  }
491
511
 
492
512
  /**
493
513
  * A data container that can contain boolean type
494
514
  */
495
515
  class BoolDataArray extends UniversalDataArray {
496
- /**
497
- * @param size - The initial size with which to allocate the array.
498
- */
499
- constructor(size: number) {
500
- super(Array, size);
501
- }
502
-
503
- /**
504
- * helper function to write and cast to boolean array values into another array at a given offset
505
- * @param array - target array
506
- * @param values - the values we need to write
507
- * @param offset - starting index in target array
508
- */
509
- private arraySetBool(array, values, offset = 0) {
510
- let index = 0;
511
- values.forEach(function(value) {
512
- array[index + offset] = !!(value as boolean);
513
- index++;
514
- });
515
- }
516
-
517
- /**
518
- * insert the content of an array into the DataArray
519
- * @param in_offset - the target index
520
- * @param in_array - the array to be inserted
521
- */
522
- insertRange(in_offset: number, in_array: any[]) {
523
- const toBeAdded: any[] = in_array.map((val) => !!(val as boolean));
524
- this._buffer.splice.call(this._buffer, ...([in_offset, 0].concat(toBeAdded)));
525
- this.size = this.size + in_array.length;
526
- }
527
-
528
- /**
529
- * Set this array values to be equal to in_array values.
530
- * @param in_offset - An optional offset in this array to begin start setting this arrays values to in_array values.
531
- * @param in_array - The input array.
532
- */
533
- set(in_offset: number, in_array) {
534
- if (in_array instanceof ArrayBuffer || in_array instanceof Array || in_array instanceof this.getBufferCtor()) {
535
- this.arraySetBool(this._buffer, in_array, in_offset);
536
- } else if (in_array instanceof BaseDataArray) {
537
- this.arraySetBool(this._buffer, in_array.getBuffer(), in_offset);
538
- } else {
539
- console.error("DataArray set() must be called with Array, TypedArray or DataArray");
540
- }
541
- }
516
+ /**
517
+ * @param size - The initial size with which to allocate the array.
518
+ */
519
+ constructor(size: number) {
520
+ super(Array, size);
521
+ }
522
+
523
+ /**
524
+ * helper function to write and cast to boolean array values into another array at a given offset
525
+ * @param array - target array
526
+ * @param values - the values we need to write
527
+ * @param offset - starting index in target array
528
+ */
529
+ private arraySetBool(array, values, offset = 0) {
530
+ let index = 0;
531
+ values.forEach(function (value) {
532
+ array[index + offset] = !!(value as boolean);
533
+ index++;
534
+ });
535
+ }
536
+
537
+ /**
538
+ * insert the content of an array into the DataArray
539
+ * @param in_offset - the target index
540
+ * @param in_array - the array to be inserted
541
+ */
542
+ insertRange(in_offset: number, in_array: any[]) {
543
+ const toBeAdded: any[] = in_array.map((val) => !!(val as boolean));
544
+ this._buffer.splice.call(this._buffer, ...[in_offset, 0].concat(toBeAdded));
545
+ this.size = this.size + in_array.length;
546
+ }
547
+
548
+ /**
549
+ * Set this array values to be equal to in_array values.
550
+ * @param in_offset - An optional offset in this array to begin start setting this arrays values to in_array values.
551
+ * @param in_array - The input array.
552
+ */
553
+ set(in_offset: number, in_array) {
554
+ if (
555
+ in_array instanceof ArrayBuffer ||
556
+ in_array instanceof Array ||
557
+ in_array instanceof this.getBufferCtor()
558
+ ) {
559
+ this.arraySetBool(this._buffer, in_array, in_offset);
560
+ } else if (in_array instanceof BaseDataArray) {
561
+ this.arraySetBool(this._buffer, in_array.getBuffer(), in_offset);
562
+ } else {
563
+ console.error("DataArray set() must be called with Array, TypedArray or DataArray");
564
+ }
565
+ }
542
566
  }
543
567
 
544
568
  export {
545
- BaseDataArray,
546
- Float32DataArray,
547
- Float64DataArray,
548
- Int8DataArray,
549
- Int16DataArray,
550
- Int32DataArray,
551
- Uint8DataArray,
552
- Uint16DataArray,
553
- Uint32DataArray,
554
- UniversalDataArray,
555
- StringDataArray,
556
- BoolDataArray,
569
+ BaseDataArray,
570
+ Float32DataArray,
571
+ Float64DataArray,
572
+ Int8DataArray,
573
+ Int16DataArray,
574
+ Int32DataArray,
575
+ Uint8DataArray,
576
+ Uint16DataArray,
577
+ Uint32DataArray,
578
+ UniversalDataArray,
579
+ StringDataArray,
580
+ BoolDataArray,
557
581
  };