@maplibre/mlt 1.1.2 → 1.1.5

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 (96) hide show
  1. package/dist/decoding/decodingTestUtils.d.ts +75 -0
  2. package/dist/decoding/decodingTestUtils.js +285 -0
  3. package/dist/decoding/decodingTestUtils.js.map +1 -0
  4. package/dist/decoding/decodingUtils.d.ts +5 -8
  5. package/dist/decoding/decodingUtils.js +22 -49
  6. package/dist/decoding/decodingUtils.js.map +1 -1
  7. package/dist/decoding/decodingUtils.spec.js +85 -69
  8. package/dist/decoding/decodingUtils.spec.js.map +1 -1
  9. package/dist/decoding/fsstDecoder.spec.js +52 -35
  10. package/dist/decoding/fsstDecoder.spec.js.map +1 -1
  11. package/dist/decoding/geometryDecoder.js +41 -30
  12. package/dist/decoding/geometryDecoder.js.map +1 -1
  13. package/dist/decoding/integerDecodingUtils.d.ts +18 -31
  14. package/dist/decoding/integerDecodingUtils.js +134 -299
  15. package/dist/decoding/integerDecodingUtils.js.map +1 -1
  16. package/dist/decoding/integerDecodingUtils.spec.js +254 -148
  17. package/dist/decoding/integerDecodingUtils.spec.js.map +1 -1
  18. package/dist/decoding/integerStreamDecoder.d.ts +4 -6
  19. package/dist/decoding/integerStreamDecoder.js +104 -122
  20. package/dist/decoding/integerStreamDecoder.js.map +1 -1
  21. package/dist/decoding/integerStreamDecoder.spec.js +370 -131
  22. package/dist/decoding/integerStreamDecoder.spec.js.map +1 -1
  23. package/dist/decoding/propertyDecoder.js +14 -24
  24. package/dist/decoding/propertyDecoder.js.map +1 -1
  25. package/dist/decoding/propertyDecoder.spec.js +409 -606
  26. package/dist/decoding/propertyDecoder.spec.js.map +1 -1
  27. package/dist/decoding/stringDecoder.js +10 -10
  28. package/dist/decoding/stringDecoder.js.map +1 -1
  29. package/dist/decoding/stringDecoder.spec.js +352 -320
  30. package/dist/decoding/stringDecoder.spec.js.map +1 -1
  31. package/dist/decoding/unpackNullableUtils.d.ts +25 -0
  32. package/dist/decoding/unpackNullableUtils.js +51 -0
  33. package/dist/decoding/unpackNullableUtils.js.map +1 -0
  34. package/dist/decoding/unpackNullableUtils.spec.js +71 -0
  35. package/dist/decoding/unpackNullableUtils.spec.js.map +1 -0
  36. package/dist/encoding/embeddedTilesetMetadataEncoder.d.ts +16 -0
  37. package/dist/encoding/embeddedTilesetMetadataEncoder.js +40 -0
  38. package/dist/encoding/embeddedTilesetMetadataEncoder.js.map +1 -0
  39. package/dist/encoding/encodingUtils.d.ts +7 -0
  40. package/dist/encoding/encodingUtils.js +107 -0
  41. package/dist/encoding/encodingUtils.js.map +1 -0
  42. package/dist/encoding/fsstEncoder.d.ts +21 -0
  43. package/dist/encoding/fsstEncoder.js +78 -0
  44. package/dist/encoding/fsstEncoder.js.map +1 -0
  45. package/dist/encoding/integerEncodingUtils.d.ts +68 -0
  46. package/dist/encoding/integerEncodingUtils.js +655 -0
  47. package/dist/encoding/integerEncodingUtils.js.map +1 -0
  48. package/dist/encoding/integerStreamEncoder.d.ts +27 -0
  49. package/dist/encoding/integerStreamEncoder.js +139 -0
  50. package/dist/encoding/integerStreamEncoder.js.map +1 -0
  51. package/dist/encoding/packNullableUtils.d.ts +4 -0
  52. package/dist/encoding/packNullableUtils.js +55 -0
  53. package/dist/encoding/packNullableUtils.js.map +1 -0
  54. package/dist/encoding/propertyEncoder.d.ts +78 -0
  55. package/dist/encoding/propertyEncoder.js +335 -0
  56. package/dist/encoding/propertyEncoder.js.map +1 -0
  57. package/dist/encoding/stringEncoder.d.ts +12 -0
  58. package/dist/encoding/stringEncoder.js +182 -0
  59. package/dist/encoding/stringEncoder.js.map +1 -0
  60. package/dist/encoding/zOrderCurveEncoder.d.ts +1 -0
  61. package/dist/encoding/zOrderCurveEncoder.js +10 -0
  62. package/dist/encoding/zOrderCurveEncoder.js.map +1 -0
  63. package/dist/metadata/tileset/embeddedTilesetMetadataDecoder.d.ts +5 -1
  64. package/dist/metadata/tileset/embeddedTilesetMetadataDecoder.js +29 -41
  65. package/dist/metadata/tileset/embeddedTilesetMetadataDecoder.js.map +1 -1
  66. package/dist/metadata/tileset/embeddedTilesetMetadataDecoder.spec.d.ts +1 -0
  67. package/dist/metadata/tileset/embeddedTilesetMetadataDecoder.spec.js +142 -0
  68. package/dist/metadata/tileset/embeddedTilesetMetadataDecoder.spec.js.map +1 -0
  69. package/dist/mltDecoder.js +2 -3
  70. package/dist/mltDecoder.js.map +1 -1
  71. package/dist/vector/dictionary/stringDictionaryVector.d.ts +1 -1
  72. package/dist/vector/dictionary/stringDictionaryVector.js.map +1 -1
  73. package/dist/vector/flat/stringFlatVector.d.ts +1 -1
  74. package/dist/vector/flat/stringFlatVector.js.map +1 -1
  75. package/dist/vector/fsst-dictionary/stringFsstDictionaryVector.d.ts +1 -1
  76. package/dist/vector/fsst-dictionary/stringFsstDictionaryVector.js.map +1 -1
  77. package/dist/vector/fsst-dictionary/stringFsstDictionaryVector.spec.js +2 -2
  78. package/dist/vector/fsst-dictionary/stringFsstDictionaryVector.spec.js.map +1 -1
  79. package/dist/vector/geometry/constGpuVector.d.ts +2 -2
  80. package/dist/vector/geometry/constGpuVector.js.map +1 -1
  81. package/dist/vector/geometry/flatGpuVector.d.ts +2 -2
  82. package/dist/vector/geometry/flatGpuVector.js.map +1 -1
  83. package/dist/vector/geometry/gpuVector.d.ts +2 -2
  84. package/dist/vector/geometry/gpuVector.js.map +1 -1
  85. package/dist/vector/geometry/topologyVector.d.ts +4 -4
  86. package/dist/vector/geometry/topologyVector.js +0 -1
  87. package/dist/vector/geometry/topologyVector.js.map +1 -1
  88. package/dist/vector/geometry/zOrderCurve.spec.js +17 -11
  89. package/dist/vector/geometry/zOrderCurve.spec.js.map +1 -1
  90. package/dist/vector/variableSizeVector.d.ts +2 -2
  91. package/dist/vector/variableSizeVector.js +0 -1
  92. package/dist/vector/variableSizeVector.js.map +1 -1
  93. package/package.json +6 -8
  94. package/dist/decoding/geometryDecoder.spec.js +0 -5
  95. package/dist/decoding/geometryDecoder.spec.js.map +0 -1
  96. /package/dist/decoding/{geometryDecoder.spec.d.ts → unpackNullableUtils.spec.d.ts} +0 -0
@@ -0,0 +1,655 @@
1
+ import IntWrapper from "../decoding/intWrapper";
2
+ export function encodeVarintInt32Value(value, dst, offset) {
3
+ let v = value;
4
+ while (v > 0x7f) {
5
+ dst[offset.get()] = (v & 0x7f) | 0x80;
6
+ offset.increment();
7
+ v >>>= 7;
8
+ }
9
+ dst[offset.get()] = v & 0x7f;
10
+ offset.increment();
11
+ }
12
+ export function encodeVarintInt32(values) {
13
+ const buffer = new Uint8Array(values.length * 5);
14
+ const offset = new IntWrapper(0);
15
+ for (const value of values) {
16
+ encodeVarintInt32Value(value, buffer, offset);
17
+ }
18
+ return buffer.slice(0, offset.get());
19
+ }
20
+ export function encodeVarintInt64(values) {
21
+ const buffer = new Uint8Array(values.length * 10);
22
+ const offset = new IntWrapper(0);
23
+ for (const value of values) {
24
+ encodeVarintInt64Value(value, buffer, offset);
25
+ }
26
+ return buffer.slice(0, offset.get());
27
+ }
28
+ function encodeVarintInt64Value(value, dst, offset) {
29
+ let v = value;
30
+ while (v > 0x7fn) {
31
+ dst[offset.get()] = Number(v & 0x7fn) | 0x80;
32
+ offset.increment();
33
+ v >>= 7n;
34
+ }
35
+ dst[offset.get()] = Number(v & 0x7fn);
36
+ offset.increment();
37
+ }
38
+ export function encodeVarintFloat64(values) {
39
+ // 1. Calculate the exact size required for the buffer
40
+ let size = 0;
41
+ for (let i = 0; i < values.length; i++) {
42
+ let val = values[i];
43
+ // Ensure we handle the value as a positive integer
44
+ val = val < 0 ? 0 : Math.floor(val);
45
+ // 0 always takes 1 byte
46
+ if (val === 0) {
47
+ size++;
48
+ continue;
49
+ }
50
+ // Calculate bytes needed: ceil(log128(val + 1))
51
+ while (val > 0) {
52
+ size++;
53
+ val = Math.floor(val / 128);
54
+ }
55
+ }
56
+ const dst = new Uint8Array(size);
57
+ const offset = new IntWrapper(0);
58
+ for (let i = 0; i < values.length; i++) {
59
+ encodeVarintFloat64Value(values[i], dst, offset);
60
+ }
61
+ return dst;
62
+ }
63
+ /**
64
+ * Encodes a single number into the buffer at the given offset using Varint encoding.
65
+ * Handles numbers up to 2^53 (MAX_SAFE_INTEGER) correctly.
66
+ */
67
+ function encodeVarintFloat64Value(val, buf, offset) {
68
+ // Ensure integer
69
+ val = Math.floor(val);
70
+ // Handle 0 explicitly or ensure loop runs once
71
+ if (val === 0) {
72
+ buf[offset.get()] = 0;
73
+ offset.increment();
74
+ return;
75
+ }
76
+ while (val >= 128) {
77
+ // Write 7 bits of data | 0x80 (continuation bit)
78
+ buf[offset.get()] = (val % 128) | 0x80;
79
+ offset.increment();
80
+ // Shift right by 7 bits
81
+ val = Math.floor(val / 128);
82
+ }
83
+ // Write the last byte (no continuation bit)
84
+ buf[offset.get()] = val;
85
+ offset.increment();
86
+ }
87
+ export function encodeFastPfor(data) {
88
+ throw new Error("FastPFor is not implemented yet.");
89
+ }
90
+ export function encodeZigZagInt32Value(value) {
91
+ return (value << 1) ^ (value >> 31);
92
+ }
93
+ export function encodeZigZagInt64Value(value) {
94
+ return (value << 1n) ^ (value >> 63n);
95
+ }
96
+ export function encodeZigZagFloat64Value(n) {
97
+ return n >= 0 ? n * 2 : n * -2 - 1;
98
+ }
99
+ export function encodeZigZagInt32(data) {
100
+ for (let i = 0; i < data.length; i++) {
101
+ data[i] = encodeZigZagInt32Value(data[i]);
102
+ }
103
+ }
104
+ export function encodeZigZagInt64(data) {
105
+ for (let i = 0; i < data.length; i++) {
106
+ data[i] = encodeZigZagInt64Value(data[i]);
107
+ }
108
+ }
109
+ export function encodeZigZagFloat64(data) {
110
+ for (let i = 0; i < data.length; i++) {
111
+ data[i] = encodeZigZagFloat64Value(data[i]);
112
+ }
113
+ }
114
+ export function encodeUnsignedRleInt32(input) {
115
+ if (input.length === 0) {
116
+ return { data: new Int32Array(0), runs: 0 };
117
+ }
118
+ const runLengths = [];
119
+ const runValues = [];
120
+ let currentRunLength = 0;
121
+ let currentValue = input[0];
122
+ for (let i = 0; i < input.length; i++) {
123
+ const nextValue = input[i];
124
+ if (nextValue === currentValue) {
125
+ currentRunLength++;
126
+ }
127
+ else {
128
+ // End of the current run, record it
129
+ runLengths.push(currentRunLength);
130
+ runValues.push(currentValue);
131
+ // Start a new run
132
+ currentValue = nextValue;
133
+ currentRunLength = 1;
134
+ }
135
+ }
136
+ // Record the final run after the loop finishes
137
+ runLengths.push(currentRunLength);
138
+ runValues.push(currentValue);
139
+ // Combine lengths and values into the final structured output array
140
+ const numRuns = runLengths.length;
141
+ const encodedData = new Int32Array(numRuns * 2);
142
+ // Populate the first half with lengths
143
+ encodedData.set(runLengths, 0);
144
+ // Populate the second half with values, offset by the total number of runs
145
+ encodedData.set(runValues, numRuns);
146
+ return { data: encodedData, runs: numRuns };
147
+ }
148
+ export function encodeUnsignedRleInt64(input) {
149
+ if (input.length === 0) {
150
+ return { data: new BigInt64Array(0), runs: 0 };
151
+ }
152
+ const runLengths = [];
153
+ const runValues = [];
154
+ let currentRunLength = 0;
155
+ let currentValue = input[0];
156
+ for (let i = 0; i < input.length; i++) {
157
+ const nextValue = input[i];
158
+ if (nextValue === currentValue) {
159
+ currentRunLength++;
160
+ }
161
+ else {
162
+ // End of the current run, record it
163
+ runLengths.push(currentRunLength);
164
+ runValues.push(currentValue);
165
+ // Start a new run
166
+ currentValue = nextValue;
167
+ currentRunLength = 1;
168
+ }
169
+ }
170
+ // Record the final run after the loop finishes
171
+ runLengths.push(currentRunLength);
172
+ runValues.push(currentValue);
173
+ // Combine lengths and values into the final structured output array (BigInt64Array)
174
+ const numRuns = runLengths.length;
175
+ const encodedData = new BigInt64Array(numRuns * 2);
176
+ // Populate the first half with lengths. We must convert the numbers back to BigInts here.
177
+ for (let i = 0; i < numRuns; i++) {
178
+ encodedData[i] = BigInt(runLengths[i]);
179
+ }
180
+ // Populate the second half with values, offset by the total number of runs
181
+ encodedData.set(runValues, numRuns);
182
+ return { data: encodedData, runs: numRuns };
183
+ }
184
+ export function encodeUnsignedRleFloat64(input) {
185
+ if (input.length === 0) {
186
+ return { data: new Float64Array(0), runs: 0 };
187
+ }
188
+ const runLengths = [];
189
+ const runValues = [];
190
+ let currentRunLength = 0;
191
+ let currentValue = input[0];
192
+ for (let i = 0; i < input.length; i++) {
193
+ const nextValue = input[i];
194
+ if (nextValue === currentValue) {
195
+ currentRunLength++;
196
+ }
197
+ else {
198
+ // End of the current run, record it
199
+ runLengths.push(currentRunLength);
200
+ runValues.push(currentValue);
201
+ // Start a new run
202
+ currentValue = nextValue;
203
+ currentRunLength = 1;
204
+ }
205
+ }
206
+ // Record the final run after the loop finishes
207
+ runLengths.push(currentRunLength);
208
+ runValues.push(currentValue);
209
+ // Combine lengths and values into the final structured output array (Float64Array)
210
+ const numRuns = runLengths.length;
211
+ // The final array is twice the size of the number of runs
212
+ const encodedData = new Float64Array(numRuns * 2);
213
+ // Populate the first half with lengths
214
+ encodedData.set(runLengths, 0);
215
+ // Populate the second half with values, offset by the total number of runs
216
+ encodedData.set(runValues, numRuns);
217
+ return { data: encodedData, runs: numRuns };
218
+ }
219
+ export function encodeZigZagDeltaInt32(data) {
220
+ if (data.length === 0) {
221
+ return;
222
+ }
223
+ let previousValue = data[0];
224
+ data[0] = encodeZigZagInt32Value(previousValue);
225
+ for (let i = 1; i < data.length; i++) {
226
+ const currentValue = data[i];
227
+ const delta = currentValue - previousValue;
228
+ const encodedDelta = encodeZigZagInt32Value(delta);
229
+ // Store the encoded delta back into the array
230
+ data[i] = encodedDelta;
231
+ // Update the previous value tracker for the next iteration's delta calculation
232
+ previousValue = currentValue;
233
+ }
234
+ }
235
+ export function encodeZigZagDeltaInt64(data) {
236
+ if (data.length === 0) {
237
+ return;
238
+ }
239
+ let previousValue = data[0];
240
+ data[0] = encodeZigZagInt64Value(previousValue);
241
+ for (let i = 1; i < data.length; i++) {
242
+ const currentValue = data[i];
243
+ const delta = currentValue - previousValue;
244
+ const encodedDelta = encodeZigZagInt64Value(delta);
245
+ // Store the encoded delta back into the array
246
+ data[i] = encodedDelta;
247
+ // Update the previous value tracker for the next iteration's delta calculation
248
+ previousValue = currentValue;
249
+ }
250
+ }
251
+ export function encodeZigZagDeltaFloat64(data) {
252
+ if (data.length === 0) {
253
+ return;
254
+ }
255
+ let previousValue = data[0];
256
+ data[0] = encodeZigZagFloat64Value(previousValue);
257
+ for (let i = 1; i < data.length; i++) {
258
+ const currentValue = data[i];
259
+ const delta = currentValue - previousValue;
260
+ const encodedDelta = encodeZigZagFloat64Value(delta);
261
+ // Store the encoded delta back into the array
262
+ data[i] = encodedDelta;
263
+ // Update the previous value tracker for the next iteration's delta calculation
264
+ previousValue = currentValue;
265
+ }
266
+ }
267
+ export function encodeZigZagRleInt32(input) {
268
+ if (input.length === 0) {
269
+ return { data: new Int32Array(0), runs: 0, numTotalValues: 0 };
270
+ }
271
+ const zigzagEncodedStream = [];
272
+ // Step 1: Apply Zigzag Encoding to all values
273
+ for (let i = 0; i < input.length; i++) {
274
+ zigzagEncodedStream.push(encodeZigZagInt32Value(input[i]));
275
+ }
276
+ // zigzagEncodedStream now holds the intermediate stream of zigzag values
277
+ // Step 2: Apply RLE to the stream of zigzag-encoded values
278
+ const runLengths = [];
279
+ const runZigZagValues = [];
280
+ let currentRunLength = 0;
281
+ let currentValue = zigzagEncodedStream[0];
282
+ for (let i = 0; i < zigzagEncodedStream.length; i++) {
283
+ const nextValue = zigzagEncodedStream[i];
284
+ if (nextValue === currentValue) {
285
+ currentRunLength++;
286
+ }
287
+ else {
288
+ runLengths.push(currentRunLength);
289
+ runZigZagValues.push(currentValue);
290
+ currentValue = nextValue;
291
+ currentRunLength = 1;
292
+ }
293
+ }
294
+ // Record the final run
295
+ runLengths.push(currentRunLength);
296
+ runZigZagValues.push(currentValue);
297
+ // Step 3: Combine lengths and values into the final structured output array
298
+ const numRuns = runLengths.length;
299
+ // The final array uses Int32Array for lengths AND values
300
+ const encodedData = new Int32Array(numRuns * 2);
301
+ // Populate the first half with lengths
302
+ encodedData.set(runLengths, 0);
303
+ // Populate the second half with zigzagged values
304
+ encodedData.set(runZigZagValues, numRuns);
305
+ return {
306
+ data: encodedData,
307
+ runs: numRuns,
308
+ numTotalValues: input.length, // Total original values count
309
+ };
310
+ }
311
+ export function encodeZigZagRleInt64(input) {
312
+ if (input.length === 0) {
313
+ return { data: new BigInt64Array(0), runs: 0, numTotalValues: 0 };
314
+ }
315
+ const zigzagEncodedStream = [];
316
+ // Step 1: Apply Zigzag Encoding to all values
317
+ for (let i = 0; i < input.length; i++) {
318
+ zigzagEncodedStream.push(encodeZigZagInt64Value(input[i]));
319
+ }
320
+ // zigzagEncodedStream now holds the intermediate stream of zigzag values
321
+ // Step 2: Apply RLE to the stream of zigzag-encoded values
322
+ const runLengths = [];
323
+ const runZigZagValues = [];
324
+ let currentRunLength = 0;
325
+ let currentValue = zigzagEncodedStream[0];
326
+ for (let i = 0; i < zigzagEncodedStream.length; i++) {
327
+ const nextValue = zigzagEncodedStream[i];
328
+ if (nextValue === currentValue) {
329
+ currentRunLength++;
330
+ }
331
+ else {
332
+ runLengths.push(currentRunLength);
333
+ runZigZagValues.push(currentValue);
334
+ currentValue = nextValue;
335
+ currentRunLength = 1;
336
+ }
337
+ }
338
+ // Record the final run
339
+ runLengths.push(currentRunLength);
340
+ runZigZagValues.push(currentValue);
341
+ // Step 3: Combine lengths and values into the final structured output array
342
+ const numRuns = runLengths.length;
343
+ // The final array uses BigInt64Array for lengths AND values
344
+ const encodedData = new BigInt64Array(numRuns * 2);
345
+ // Populate the first half with lengths (converting numbers back to BigInts)
346
+ for (let i = 0; i < numRuns; i++) {
347
+ encodedData[i] = BigInt(runLengths[i]);
348
+ }
349
+ // Populate the second half with zigzagged values
350
+ encodedData.set(runZigZagValues, numRuns);
351
+ return {
352
+ data: encodedData,
353
+ runs: numRuns,
354
+ numTotalValues: input.length, // Total original values count
355
+ };
356
+ }
357
+ export function encodeZigZagRleFloat64(input) {
358
+ if (input.length === 0) {
359
+ return { data: new Float64Array(0), runs: 0, numTotalValues: 0 };
360
+ }
361
+ const zigzagEncodedStream = [];
362
+ // Step 1: Apply Float-based Zigzag Encoding to all values
363
+ for (let i = 0; i < input.length; i++) {
364
+ zigzagEncodedStream.push(encodeZigZagFloat64Value(input[i]));
365
+ }
366
+ // zigzagEncodedStream now holds the intermediate stream of zigzag values (as floats acting as integers)
367
+ // Step 2: Apply RLE to the stream of zigzag-encoded values
368
+ const runLengths = [];
369
+ const runZigZagValues = [];
370
+ let currentRunLength = 0;
371
+ let currentValue = zigzagEncodedStream[0];
372
+ for (let i = 0; i < zigzagEncodedStream.length; i++) {
373
+ const nextValue = zigzagEncodedStream[i];
374
+ if (nextValue === currentValue) {
375
+ currentRunLength++;
376
+ }
377
+ else {
378
+ runLengths.push(currentRunLength);
379
+ runZigZagValues.push(currentValue);
380
+ currentValue = nextValue;
381
+ currentRunLength = 1;
382
+ }
383
+ }
384
+ // Record the final run
385
+ runLengths.push(currentRunLength);
386
+ runZigZagValues.push(currentValue);
387
+ // Step 3: Combine lengths and values into the final structured output array
388
+ const numRuns = runLengths.length;
389
+ // The final array uses Float64Array for lengths AND values
390
+ const encodedData = new Float64Array(numRuns * 2);
391
+ // Populate the first half with lengths
392
+ encodedData.set(runLengths, 0);
393
+ // Populate the second half with zigzagged values
394
+ encodedData.set(runZigZagValues, numRuns);
395
+ return {
396
+ data: encodedData,
397
+ runs: numRuns,
398
+ numTotalValues: input.length, // Total original values count
399
+ };
400
+ }
401
+ /**
402
+ * This is not really a encode, but more of a decode method...
403
+ */
404
+ export function encodeDeltaInt32(data) {
405
+ if (data.length === 0) {
406
+ return;
407
+ }
408
+ for (let i = data.length - 1; i >= 1; i--) {
409
+ data[i] = data[i] - data[i - 1];
410
+ }
411
+ }
412
+ export function encodeComponentwiseDeltaVec2(data) {
413
+ if (data.length < 2)
414
+ return;
415
+ // Reverse iterate to avoid overwriting data needed for delta computation
416
+ for (let i = data.length - 2; i >= 2; i -= 2) {
417
+ const deltaX = data[i] - data[i - 2];
418
+ const deltaY = data[i + 1] - data[i - 1];
419
+ data[i] = encodeZigZagInt32Value(deltaX);
420
+ data[i + 1] = encodeZigZagInt32Value(deltaY);
421
+ }
422
+ // Encode first vertex last (after computing all deltas)
423
+ data[0] = encodeZigZagInt32Value(data[0]);
424
+ data[1] = encodeZigZagInt32Value(data[1]);
425
+ }
426
+ export function encodeComponentwiseDeltaVec2Scaled(data, scale) {
427
+ if (data.length < 2)
428
+ return;
429
+ // First, inverse scale all values (tile space -> original space)
430
+ for (let i = 0; i < data.length; i++) {
431
+ data[i] = Math.round(data[i] / scale);
432
+ }
433
+ // Then apply componentwise delta encoding (same as non-scaled version)
434
+ // Reverse iterate to avoid overwriting data needed for delta computation
435
+ for (let i = data.length - 2; i >= 2; i -= 2) {
436
+ const deltaX = data[i] - data[i - 2];
437
+ const deltaY = data[i + 1] - data[i - 1];
438
+ data[i] = encodeZigZagInt32Value(deltaX);
439
+ data[i + 1] = encodeZigZagInt32Value(deltaY);
440
+ }
441
+ // Encode first vertex last (after computing all deltas)
442
+ data[0] = encodeZigZagInt32Value(data[0]);
443
+ data[1] = encodeZigZagInt32Value(data[1]);
444
+ }
445
+ // HM TODO:
446
+ // zigZagDeltaOfDeltaDecoding
447
+ export function encodeZigZagRleDeltaInt32(values) {
448
+ if (values.length === 0) {
449
+ return { data: new Int32Array(0), runs: 0, numTotalValues: 0 };
450
+ }
451
+ const runLengths = [];
452
+ const encodedDeltas = [];
453
+ // The decoder explicitly sets decodedValues[0] = 0 and uses previousValue = 0.
454
+ // Therefore, we initialize our 'previous' tracker to 0 to calculate the first delta correctly.
455
+ let previousValue = 0;
456
+ // Variables to track the current run
457
+ let currentDelta = null;
458
+ let currentRunLength = 0;
459
+ for (let i = 0; i < values.length; i++) {
460
+ const value = values[i];
461
+ const delta = value - previousValue;
462
+ previousValue = value;
463
+ if (currentDelta === null) {
464
+ // First element initialization
465
+ currentDelta = delta;
466
+ currentRunLength = 1;
467
+ }
468
+ else if (delta === currentDelta) {
469
+ // Continuation of the current run
470
+ currentRunLength++;
471
+ }
472
+ else {
473
+ // The run has broken (delta changed)
474
+ // 1. Push the length of the previous run
475
+ runLengths.push(currentRunLength);
476
+ // 2. ZigZag encode the previous delta and push it
477
+ encodedDeltas.push(encodeZigZagInt32Value(currentDelta));
478
+ // Start the new run
479
+ currentDelta = delta;
480
+ currentRunLength = 1;
481
+ }
482
+ }
483
+ // Flush the final run remaining after the loop finishes
484
+ if (currentDelta !== null) {
485
+ runLengths.push(currentRunLength);
486
+ encodedDeltas.push(encodeZigZagInt32Value(currentDelta));
487
+ }
488
+ const numRuns = runLengths.length;
489
+ // The decoder expects 'data' to be: [RunLength 1, RunLength 2... | Value 1, Value 2...]
490
+ // Size is numRuns * 2 (First half lengths, second half values)
491
+ const data = new Int32Array(numRuns * 2);
492
+ for (let i = 0; i < numRuns; i++) {
493
+ data[i] = runLengths[i]; // First half: Run Lengths
494
+ data[i + numRuns] = encodedDeltas[i]; // Second half: ZigZag Encoded Deltas
495
+ }
496
+ return {
497
+ data: data,
498
+ runs: numRuns,
499
+ numTotalValues: values.length,
500
+ };
501
+ }
502
+ export function encodeRleDeltaInt32(values) {
503
+ if (values.length === 0) {
504
+ return { data: new Int32Array(0), runs: 0, numTotalValues: 0 };
505
+ }
506
+ const runLengths = [];
507
+ const deltas = [];
508
+ // The decoder logic relies on: decodedValues[0] = 0; previousValue = 0;
509
+ // So the encoder must assume the sequence starts relative to 0.
510
+ let previousValue = 0;
511
+ // Track the current run of deltas
512
+ let currentDelta = null;
513
+ let currentRunLength = 0;
514
+ for (let i = 0; i < values.length; i++) {
515
+ const value = values[i];
516
+ const delta = value - previousValue;
517
+ previousValue = value;
518
+ if (currentDelta === null) {
519
+ // Initialize first run
520
+ currentDelta = delta;
521
+ currentRunLength = 1;
522
+ }
523
+ else if (delta === currentDelta) {
524
+ // Continue current run
525
+ currentRunLength++;
526
+ }
527
+ else {
528
+ // Delta changed: flush the previous run
529
+ runLengths.push(currentRunLength);
530
+ deltas.push(currentDelta);
531
+ // Start new run
532
+ currentDelta = delta;
533
+ currentRunLength = 1;
534
+ }
535
+ }
536
+ // Flush the final run
537
+ if (currentDelta !== null) {
538
+ runLengths.push(currentRunLength);
539
+ deltas.push(currentDelta);
540
+ }
541
+ const numRuns = runLengths.length;
542
+ // Pack into Int32Array: [ RunLength 1...N | Delta 1...N ]
543
+ const data = new Int32Array(numRuns * 2);
544
+ for (let i = 0; i < numRuns; i++) {
545
+ data[i] = runLengths[i];
546
+ data[i + numRuns] = deltas[i];
547
+ }
548
+ return {
549
+ data: data,
550
+ runs: numRuns,
551
+ numTotalValues: values.length,
552
+ };
553
+ }
554
+ export function encodeDeltaRleInt32(input) {
555
+ if (input.length === 0) {
556
+ return { data: new Int32Array(0), runs: 0, numValues: 0 };
557
+ }
558
+ const deltasAndEncoded = [];
559
+ let previousValue = 0;
560
+ // Step 1 & 2: Calculate Deltas and Zigzag Encode them
561
+ for (let i = 0; i < input.length; i++) {
562
+ const currentValue = input[i];
563
+ const delta = currentValue - previousValue;
564
+ const encodedDelta = encodeZigZagInt32Value(delta);
565
+ deltasAndEncoded.push(encodedDelta);
566
+ previousValue = currentValue;
567
+ }
568
+ // deltasAndEncoded now holds the intermediate stream of zigzagged deltas
569
+ // Step 3: Apply RLE to the stream of zigzag-encoded deltas
570
+ const runLengths = [];
571
+ const runZigZagDeltas = [];
572
+ let currentRunLength = 0;
573
+ let currentRunValue = deltasAndEncoded[0];
574
+ for (let i = 0; i < deltasAndEncoded.length; i++) {
575
+ const nextValue = deltasAndEncoded[i];
576
+ if (nextValue === currentRunValue) {
577
+ currentRunLength++;
578
+ }
579
+ else {
580
+ runLengths.push(currentRunLength);
581
+ runZigZagDeltas.push(currentRunValue);
582
+ currentRunValue = nextValue;
583
+ currentRunLength = 1;
584
+ }
585
+ }
586
+ // Record the final run
587
+ runLengths.push(currentRunLength);
588
+ runZigZagDeltas.push(currentRunValue);
589
+ // Step 4: Combine lengths and values into the final structured output array
590
+ const numRuns = runLengths.length;
591
+ const encodedData = new Int32Array(numRuns * 2);
592
+ // Populate the first half with lengths
593
+ for (let i = 0; i < numRuns; i++) {
594
+ encodedData[i] = runLengths[i];
595
+ }
596
+ // Populate the second half with zigzagged deltas
597
+ // Int32Array.set() works with standard number arrays
598
+ encodedData.set(runZigZagDeltas, numRuns);
599
+ return {
600
+ data: encodedData,
601
+ runs: numRuns,
602
+ numValues: input.length, // Total original values count
603
+ };
604
+ }
605
+ export function encodeDeltaRleInt64(input) {
606
+ if (input.length === 0) {
607
+ return { data: new BigInt64Array(0), runs: 0, numValues: 0 };
608
+ }
609
+ const deltasAndEncoded = [];
610
+ let previousValue = 0n;
611
+ // Step 1 & 2: Calculate Deltas and Zigzag Encode them
612
+ for (let i = 0; i < input.length; i++) {
613
+ const currentValue = input[i];
614
+ const delta = currentValue - previousValue;
615
+ const encodedDelta = encodeZigZagInt64Value(delta);
616
+ deltasAndEncoded.push(encodedDelta);
617
+ previousValue = currentValue;
618
+ }
619
+ // deltasAndEncoded now holds the intermediate stream of zigzagged deltas
620
+ // Step 3: Apply RLE to the stream of zigzag-encoded deltas
621
+ const runLengths = [];
622
+ const runZigZagDeltas = [];
623
+ let currentRunLength = 0;
624
+ let currentValue = deltasAndEncoded[0];
625
+ for (let i = 0; i < deltasAndEncoded.length; i++) {
626
+ const nextValue = deltasAndEncoded[i];
627
+ if (nextValue === currentValue) {
628
+ currentRunLength++;
629
+ }
630
+ else {
631
+ runLengths.push(currentRunLength);
632
+ runZigZagDeltas.push(currentValue);
633
+ currentValue = nextValue;
634
+ currentRunLength = 1;
635
+ }
636
+ }
637
+ // Record the final run
638
+ runLengths.push(currentRunLength);
639
+ runZigZagDeltas.push(currentValue);
640
+ // Step 4: Combine lengths and values into the final structured output array
641
+ const numRuns = runLengths.length;
642
+ const encodedData = new BigInt64Array(numRuns * 2);
643
+ // Populate the first half with lengths (converting numbers back to BigInts for storage)
644
+ for (let i = 0; i < numRuns; i++) {
645
+ encodedData[i] = BigInt(runLengths[i]);
646
+ }
647
+ // Populate the second half with zigzagged deltas
648
+ encodedData.set(runZigZagDeltas, numRuns);
649
+ return {
650
+ data: encodedData,
651
+ runs: numRuns,
652
+ numValues: input.length, // Total original values count
653
+ };
654
+ }
655
+ //# sourceMappingURL=integerEncodingUtils.js.map