@daneren2005/shared-memory-objects 1.0.0 → 1.2.0

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 (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -55
  3. package/dist/{src/allocated-memory.d.ts → allocated-memory.d.ts} +5 -4
  4. package/dist/allocated-memory.js +45 -0
  5. package/dist/allocated-memory.js.map +1 -0
  6. package/dist/{src/cached-item-list.d.ts → cached-item-list.d.ts} +3 -3
  7. package/dist/cached-item-list.js +71 -0
  8. package/dist/cached-item-list.js.map +1 -0
  9. package/dist/constant-string.d.ts +15 -0
  10. package/dist/constant-string.js +67 -0
  11. package/dist/constant-string.js.map +1 -0
  12. package/dist/interfaces/pow2.js +0 -0
  13. package/dist/interfaces/typed-array-constructor.js +0 -0
  14. package/dist/interfaces/typed-array.js +0 -0
  15. package/dist/{src/local-pool.d.ts → local-pool.d.ts} +2 -2
  16. package/dist/local-pool.js +90 -0
  17. package/dist/local-pool.js.map +1 -0
  18. package/dist/lock/read-write-lock.js +19 -0
  19. package/dist/lock/read-write-lock.js.map +1 -0
  20. package/dist/lock/simple-lock.js +11 -0
  21. package/dist/lock/simple-lock.js.map +1 -0
  22. package/dist/{src/memory-buffer.d.ts → memory-buffer.d.ts} +4 -4
  23. package/dist/memory-buffer.js +267 -0
  24. package/dist/memory-buffer.js.map +1 -0
  25. package/dist/{src/memory-heap.d.ts → memory-heap.d.ts} +2 -2
  26. package/dist/memory-heap.js +96 -0
  27. package/dist/memory-heap.js.map +1 -0
  28. package/dist/{src/serialize-object.d.ts → serialize-object.d.ts} +2 -2
  29. package/dist/serialize-object.js +47 -0
  30. package/dist/serialize-object.js.map +1 -0
  31. package/dist/{src/shared-list.d.ts → shared-list.d.ts} +3 -3
  32. package/dist/shared-list.js +125 -0
  33. package/dist/shared-list.js.map +1 -0
  34. package/dist/{src/shared-map.d.ts → shared-map.d.ts} +2 -2
  35. package/dist/shared-map.js +104 -0
  36. package/dist/shared-map.js.map +1 -0
  37. package/dist/{src/shared-pointer-list.d.ts → shared-pointer-list.d.ts} +3 -3
  38. package/dist/shared-pointer-list.js +47 -0
  39. package/dist/shared-pointer-list.js.map +1 -0
  40. package/dist/{src/shared-pool.d.ts → shared-pool.d.ts} +11 -8
  41. package/dist/shared-pool.js +145 -0
  42. package/dist/shared-pool.js.map +1 -0
  43. package/dist/shared-stack.d.ts +46 -0
  44. package/dist/shared-stack.js +144 -0
  45. package/dist/shared-stack.js.map +1 -0
  46. package/dist/{src/shared-string.d.ts → shared-string.d.ts} +2 -2
  47. package/dist/shared-string.js +73 -0
  48. package/dist/shared-string.js.map +1 -0
  49. package/dist/{src/shared-vector.d.ts → shared-vector.d.ts} +4 -4
  50. package/dist/shared-vector.js +147 -0
  51. package/dist/shared-vector.js.map +1 -0
  52. package/dist/utils/16-from-32-array.js +18 -0
  53. package/dist/utils/16-from-32-array.js.map +1 -0
  54. package/dist/utils/16-from-64-array.js +17 -0
  55. package/dist/utils/16-from-64-array.js.map +1 -0
  56. package/dist/utils/atomic-math.js +34 -0
  57. package/dist/utils/atomic-math.js.map +1 -0
  58. package/dist/utils/float32-atomics.js +24 -0
  59. package/dist/utils/float32-atomics.js.map +1 -0
  60. package/dist/utils/float64-atomics.js +24 -0
  61. package/dist/utils/float64-atomics.js.map +1 -0
  62. package/dist/utils/pointer.js +34 -0
  63. package/dist/utils/pointer.js.map +1 -0
  64. package/dist/{src/utils → utils}/typedarray.d.ts +47 -47
  65. package/dist/utils/typedarray.js +39 -0
  66. package/dist/utils/typedarray.js.map +1 -0
  67. package/package.json +26 -31
  68. package/dist/shared-memory-objects.js +0 -1578
  69. package/dist/shared-memory-objects.js.map +0 -1
  70. package/dist/shared-memory-objects.umd.cjs +0 -2
  71. package/dist/shared-memory-objects.umd.cjs.map +0 -1
  72. package/dist/src/main.d.ts +0 -22
  73. /package/dist/{src/interfaces → interfaces}/pow2.d.ts +0 -0
  74. /package/dist/{src/interfaces → interfaces}/typed-array-constructor.d.ts +0 -0
  75. /package/dist/{src/interfaces → interfaces}/typed-array.d.ts +0 -0
  76. /package/dist/{src/lock → lock}/read-write-lock.d.ts +0 -0
  77. /package/dist/{src/lock → lock}/simple-lock.d.ts +0 -0
  78. /package/dist/{src/utils → utils}/16-from-32-array.d.ts +0 -0
  79. /package/dist/{src/utils → utils}/16-from-64-array.d.ts +0 -0
  80. /package/dist/{src/utils → utils}/atomic-math.d.ts +0 -0
  81. /package/dist/{src/utils → utils}/float32-atomics.d.ts +0 -0
  82. /package/dist/{src/utils → utils}/float64-atomics.d.ts +0 -0
  83. /package/dist/{src/utils → utils}/pointer.d.ts +0 -0
@@ -1,1578 +0,0 @@
1
- const Qt = 20, te = 12, tt = Math.pow(2, 20), et = Math.pow(2, 12);
2
- function c(s, t = 0) {
3
- return y(Atomics.load(s, t));
4
- }
5
- function O(s, t = 0) {
6
- return Atomics.load(s, t);
7
- }
8
- function k(s, t = 0, e, r) {
9
- Atomics.store(s, t, L(e, r));
10
- }
11
- function U(s, t = 0, e) {
12
- Atomics.store(s, t, e);
13
- }
14
- function ee(s, t, e, r, i, o) {
15
- let a = L(i, o);
16
- return Atomics.compareExchange(s, t, a, L(e, r)) === a;
17
- }
18
- function H(s, t, e, r) {
19
- return Atomics.compareExchange(s, t, r, e) === r;
20
- }
21
- function y(s) {
22
- return {
23
- bufferPosition: s & 4095,
24
- bufferByteOffset: s >>> 12
25
- };
26
- }
27
- function L(s, t) {
28
- return s + (t << 12);
29
- }
30
- class f {
31
- memory;
32
- bufferPosition;
33
- get bufferByteOffset() {
34
- return this.data.byteOffset;
35
- }
36
- get pointer() {
37
- return L(this.bufferPosition, this.bufferByteOffset);
38
- }
39
- buffer;
40
- data;
41
- constructor(t, e) {
42
- this.memory = t, "buffer" in e ? (this.data = e.data, this.buffer = e.buffer, this.bufferPosition = this.memory.buffers.indexOf(e.buffer)) : (this.bufferPosition = e.bufferPosition, this.buffer = t.buffers[e.bufferPosition], this.data = new Uint32Array(this.buffer.buf, e.bufferByteOffset));
43
- }
44
- getArray(t, e, r) {
45
- return new t(this.data.buffer, this.data.byteOffset + e * t.BYTES_PER_ELEMENT, r);
46
- }
47
- getArrayMemory(t, e) {
48
- return {
49
- bufferPosition: this.bufferPosition,
50
- bufferByteOffset: this.bufferByteOffset + t * this.data.BYTES_PER_ELEMENT
51
- };
52
- }
53
- free() {
54
- this.buffer.free(this.data.byteOffset);
55
- }
56
- getSharedMemory() {
57
- return {
58
- bufferPosition: this.bufferPosition,
59
- bufferByteOffset: this.bufferByteOffset
60
- };
61
- }
62
- }
63
- const W = 0, D = 1;
64
- function x(s, t = 0) {
65
- for (; Atomics.compareExchange(s, t, W, D) !== W; )
66
- "WorkerGlobalScope" in self && Atomics.wait(s, t, D);
67
- }
68
- function b(s, t = 0) {
69
- Atomics.compareExchange(s, t, D, W) !== D && console.warn("We are unlocking when it was not locked!"), Atomics.notify(s, t);
70
- }
71
- const re = 1, Rt = {
72
- 5120: "i8",
73
- 5121: "u8",
74
- 5122: "i16",
75
- 5123: "u16",
76
- 5124: "i32",
77
- 5125: "u32",
78
- 5126: "f32"
79
- }, $t = {
80
- f32: Float32Array,
81
- f64: Float64Array
82
- }, Ht = {
83
- i8: Int8Array,
84
- i16: Int16Array,
85
- i32: Int32Array
86
- }, Yt = {
87
- u8: Uint8Array,
88
- u8c: Uint8ClampedArray,
89
- u16: Uint16Array,
90
- u32: Uint32Array
91
- }, Vt = {
92
- i64: BigInt64Array,
93
- u64: BigUint64Array
94
- }, Kt = {
95
- ...$t,
96
- ...Ht,
97
- ...Yt
98
- }, Xt = (s) => {
99
- const t = Rt[s];
100
- return t !== void 0 ? t : s;
101
- };
102
- function Gt(s, ...t) {
103
- const e = Vt[s];
104
- return new (e || Kt[Xt(s)])(...t);
105
- }
106
- const rt = 0, it = 1, st = 2, ot = 3, at = 4, g = 5, nt = 6, Y = 1, V = 2, lt = 32, K = 0, X = 1, d = 8;
107
- class G {
108
- buf;
109
- start;
110
- u8;
111
- u32;
112
- state;
113
- lock;
114
- constructor(t = {}) {
115
- if (this.buf = t.buf ? t.buf : new ArrayBuffer(t.size || 4096), this.start = t.start != null ? C(Math.max(t.start, 0), 4) : 0, this.u8 = new Uint8Array(this.buf), this.u32 = new Uint32Array(this.buf), this.state = new Uint32Array(this.buf, this.start, lt / 4), this.lock = new Int32Array(this.buf, this.start + this.state.byteLength - 4, 1), !t.skipInitialization) {
116
- const e = t.align || 8;
117
- if (e < 8)
118
- throw new Error(`invalid alignment: ${e}, must be a pow2 and >= 8`);
119
- const r = this.initialTop(e), i = t.end != null ? Math.min(t.end, this.buf.byteLength) : this.buf.byteLength;
120
- if (r >= i)
121
- throw new Error(
122
- `insufficient address range (0x${this.start.toString(
123
- 16
124
- )} - 0x${i.toString(16)})`
125
- );
126
- this.align = e, this.doCompact = t.compact !== !1, this.doSplit = t.split !== !1, this.minSplit = t.minSplit || 16, this.end = i, this.top = r, this._free = 0, this._used = 0;
127
- }
128
- }
129
- stats() {
130
- const t = (r) => {
131
- let i = 0, o = 0;
132
- for (; r; )
133
- if (i++, o += this.blockSize(r), r = this.blockNext(r), r > this.end) {
134
- console.error(`Trying to get stats for block past end of buffer: ${r} > ${this.end}`);
135
- break;
136
- }
137
- return { count: i, size: o };
138
- }, e = t(this._free);
139
- return {
140
- free: e,
141
- used: t(this._used),
142
- top: this.top,
143
- available: this.end - this.top + e.size,
144
- total: this.buf.byteLength
145
- };
146
- }
147
- callocAs(t, e, r = 0) {
148
- const i = this.mallocAs(t, e);
149
- return i && i.fill(r), i;
150
- }
151
- mallocAs(t, e) {
152
- const r = this.malloc(e * Zt[t]);
153
- return r ? Gt(t, this.buf, r, e) : void 0;
154
- }
155
- calloc(t, e = 0) {
156
- const r = this.malloc(t);
157
- return r && this.u8.fill(e, r, r + t), r;
158
- }
159
- malloc(t) {
160
- if (t <= 0)
161
- return 0;
162
- x(this.lock);
163
- const e = C(t + d, this.align), r = this.end;
164
- let i = this.top, o = this._free, a = 0;
165
- for (; o; ) {
166
- const n = this.blockSize(o), l = o + n >= i;
167
- if (l || n >= e) {
168
- let h = this.mallocTop(
169
- o,
170
- a,
171
- n,
172
- e,
173
- l
174
- );
175
- return b(this.lock), h;
176
- }
177
- a = o, o = this.blockNext(o);
178
- }
179
- if (o = i, i = o + e, i <= r) {
180
- this.initBlock(o, e, this._used), this._used = o, this.top = i;
181
- let n = T(o);
182
- return b(this.lock), n;
183
- }
184
- return b(this.lock), 0;
185
- }
186
- mallocTop(t, e, r, i, o) {
187
- if (o && t + i > this.end) return 0;
188
- if (e ? this.unlinkBlock(e, t) : this._free = this.blockNext(t), this.setBlockNext(t, this._used), this._used = t, o)
189
- this.top = t + this.setBlockSize(t, i);
190
- else if (this.doSplit) {
191
- const a = r - i;
192
- a >= this.minSplit && this.splitBlock(t, i, a);
193
- }
194
- return T(t);
195
- }
196
- realloc(t, e) {
197
- if (e <= 0)
198
- return 0;
199
- const r = _(t);
200
- let i = 0, o = this._used, a = 0;
201
- for (; o; ) {
202
- if (o === r) {
203
- [i, a] = this.reallocBlock(o, e);
204
- break;
205
- }
206
- o = this.blockNext(o);
207
- }
208
- return i && i !== r && this.u8.copyWithin(
209
- T(i),
210
- T(r),
211
- a
212
- ), T(i);
213
- }
214
- reallocBlock(t, e) {
215
- const r = this.blockSize(t), i = t + r, o = i >= this.top, a = C(e + d, this.align);
216
- if (a <= r) {
217
- if (this.doSplit) {
218
- const n = r - a;
219
- n >= this.minSplit ? this.splitBlock(t, a, n) : o && (this.top = t + a);
220
- } else o && (this.top = t + a);
221
- return [t, i];
222
- }
223
- return o && t + a < this.end ? (this.top = t + this.setBlockSize(t, a), [t, i]) : (this.free(t), [_(this.malloc(e)), i]);
224
- }
225
- reallocArray(t, e) {
226
- if (t.buffer !== this.buf)
227
- return;
228
- const r = this.realloc(
229
- t.byteOffset,
230
- e * t.BYTES_PER_ELEMENT
231
- );
232
- return r ? new t.constructor(this.buf, r, e) : void 0;
233
- }
234
- bytesFor(t) {
235
- let e;
236
- if (typeof t != "number") {
237
- if (t.buffer !== this.buf)
238
- return;
239
- e = t.byteOffset;
240
- } else
241
- e = t;
242
- e = _(e);
243
- let r = this._used;
244
- for (; r; ) {
245
- if (r === e)
246
- return this.blockSize(e) - d;
247
- r = this.blockNext(r);
248
- }
249
- }
250
- lengthOf(t) {
251
- let e = this.bytesFor(t);
252
- if (e)
253
- return e / this.u32.BYTES_PER_ELEMENT;
254
- }
255
- free(t) {
256
- let e;
257
- if (typeof t != "number") {
258
- if (t.buffer !== this.buf)
259
- return !1;
260
- e = t.byteOffset;
261
- } else
262
- e = t;
263
- x(this.lock), e = _(e);
264
- let r = this._used, i = 0;
265
- for (; r; ) {
266
- if (r === e)
267
- return i ? this.unlinkBlock(i, r) : this._used = this.blockNext(r), this.insert(r), this.doCompact && this.compact(), b(this.lock), !0;
268
- i = r, r = this.blockNext(r);
269
- }
270
- return b(this.lock), !1;
271
- }
272
- freeAll() {
273
- this._free = 0, this._used = 0, this.top = this.initialTop();
274
- }
275
- release() {
276
- return delete this.u8, delete this.u32, delete this.state, delete this.buf, !0;
277
- }
278
- get align() {
279
- return this.state[at];
280
- }
281
- set align(t) {
282
- this.state[at] = t;
283
- }
284
- get end() {
285
- return this.state[ot];
286
- }
287
- set end(t) {
288
- this.state[ot] = t;
289
- }
290
- get top() {
291
- return Atomics.load(this.state, st);
292
- }
293
- set top(t) {
294
- Atomics.store(this.state, st, t);
295
- }
296
- get _free() {
297
- return Atomics.load(this.state, rt);
298
- }
299
- set _free(t) {
300
- Atomics.store(this.state, rt, t);
301
- }
302
- get _used() {
303
- return Atomics.load(this.state, it);
304
- }
305
- set _used(t) {
306
- Atomics.store(this.state, it, t);
307
- }
308
- get doCompact() {
309
- return !!(this.state[g] & Y);
310
- }
311
- set doCompact(t) {
312
- t ? this.state[g] |= 1 << Y - 1 : this.state[g] &= ~Y;
313
- }
314
- get doSplit() {
315
- return !!(this.state[g] & V);
316
- }
317
- set doSplit(t) {
318
- t ? this.state[g] |= 1 << V - 1 : this.state[g] &= ~V;
319
- }
320
- get minSplit() {
321
- return this.state[nt];
322
- }
323
- set minSplit(t) {
324
- if (t <= d)
325
- throw new Error(`illegal min split threshold: ${t}, require at least ${d + 1}`);
326
- this.state[nt] = t;
327
- }
328
- blockSize(t) {
329
- return Atomics.load(this.u32, (t >> 2) + K);
330
- }
331
- /**
332
- * Sets & returns given block size.
333
- *
334
- * @param block -
335
- * @param size -
336
- */
337
- setBlockSize(t, e) {
338
- return Atomics.store(this.u32, (t >> 2) + K, e), e;
339
- }
340
- blockNext(t) {
341
- return Atomics.load(this.u32, (t >> 2) + X);
342
- }
343
- /**
344
- * Sets block next pointer to `next`. Use zero to indicate list end.
345
- *
346
- * @param block -
347
- */
348
- setBlockNext(t, e) {
349
- Atomics.store(this.u32, (t >> 2) + X, e);
350
- }
351
- /**
352
- * Initializes block header with given `size` and `next` pointer. Returns `block`.
353
- *
354
- * @param block -
355
- * @param size -
356
- * @param next -
357
- */
358
- initBlock(t, e, r) {
359
- const i = t >>> 2;
360
- return Atomics.store(this.u32, i + K, e), Atomics.store(this.u32, i + X, r), t;
361
- }
362
- unlinkBlock(t, e) {
363
- this.setBlockNext(t, this.blockNext(e));
364
- }
365
- splitBlock(t, e, r) {
366
- this.insert(
367
- this.initBlock(
368
- t + this.setBlockSize(t, e),
369
- r,
370
- 0
371
- )
372
- ), this.doCompact && this.compact();
373
- }
374
- initialTop(t = this.align) {
375
- return C(this.start + lt + d, t) - d;
376
- }
377
- /**
378
- * Traverses free list and attempts to recursively merge blocks
379
- * occupying consecutive memory regions. Returns true if any blocks
380
- * have been merged. Only called if `compact` option is enabled.
381
- */
382
- compact() {
383
- let t = this._free, e = 0, r = 0, i, o = !1;
384
- for (; t; ) {
385
- for (i = t, r = this.blockNext(t); r && i + this.blockSize(i) === r; )
386
- i = r, r = this.blockNext(r);
387
- if (i !== t) {
388
- const a = i - t + this.blockSize(i);
389
- this.setBlockSize(t, a);
390
- const n = this.blockNext(i);
391
- let l = this.blockNext(t);
392
- for (; l && l !== n; ) {
393
- const h = this.blockNext(l);
394
- this.setBlockNext(l, 0), l = h;
395
- }
396
- this.setBlockNext(t, n), o = !0;
397
- }
398
- t + this.blockSize(t) >= this.top && (this.top = t, e ? this.unlinkBlock(e, t) : this._free = this.blockNext(t)), e = t, t = this.blockNext(t);
399
- }
400
- return o;
401
- }
402
- /**
403
- * Inserts given block into list of free blocks, sorted by address.
404
- *
405
- * @param block -
406
- */
407
- insert(t) {
408
- let e = this._free, r = 0;
409
- for (; e && !(t <= e); )
410
- r = e, e = this.blockNext(e);
411
- r ? this.setBlockNext(r, t) : this._free = t, this.setBlockNext(t, e);
412
- }
413
- }
414
- const T = (s) => s > 0 ? s + d : 0, _ = (s) => s > 0 ? s - d : 0, C = (s, t) => (t--, s + t & ~t), Zt = {
415
- u8: 1,
416
- u8c: 1,
417
- i8: 1,
418
- u16: 2,
419
- i16: 2,
420
- u32: 4,
421
- i32: 4,
422
- i64: 8,
423
- u64: 8,
424
- f32: 4,
425
- f64: 8
426
- }, Wt = 8192, ft = 0, Z = 1, I = 2;
427
- class ie {
428
- buffers;
429
- onGrowBufferHandlers = [];
430
- isClone;
431
- memory;
432
- get bufferSize() {
433
- return this.memory.data[ft];
434
- }
435
- constructor(t) {
436
- if (t && "buffers" in t)
437
- this.buffers = t.buffers.map((e) => new G({
438
- buf: e,
439
- skipInitialization: !0
440
- })), this.memory = new f(this, {
441
- bufferPosition: 0,
442
- bufferByteOffset: 40
443
- }), this.isClone = !0;
444
- else {
445
- "SharedArrayBuffer" in globalThis || console.warn("SharedArrayBuffer is not working: falling back to ArrayBuffer");
446
- const e = t?.bufferSize ?? Wt;
447
- if (e > tt)
448
- throw new Error(`Buffer size ${e} is greater than max ${tt} that we can reference with pointers`);
449
- let r = this.createBuffer(e);
450
- this.buffers = [
451
- r
452
- ];
453
- const i = r.callocAs("u32", 3);
454
- if (i)
455
- this.memory = new f(this, {
456
- bufferPosition: 0,
457
- bufferByteOffset: i.byteOffset
458
- });
459
- else
460
- throw new Error("Failed to initialize first byte from buffer");
461
- this.memory.data[ft] = e, this.memory.data[Z] = 1, this.memory.data[I] = t?.autoGrowSize ?? 100, this.isClone = !1;
462
- for (let o = 1; o < (t?.initialBuffers ?? 1); o++)
463
- this.buffers.push(this.createBuffer(e));
464
- }
465
- }
466
- addSharedBuffer(t) {
467
- this.buffers[t.bufferPosition] = new G({
468
- buf: t.buffer,
469
- skipInitialization: !0
470
- });
471
- }
472
- growBuffer() {
473
- const t = this.createBuffer();
474
- let e = Atomics.add(this.memory.data, Z, 1);
475
- return this.buffers[e] = t, this.onGrowBufferHandlers.forEach((r) => r({
476
- bufferPosition: e,
477
- buffer: t.buf
478
- })), t;
479
- }
480
- createBuffer(t) {
481
- const e = t ?? this.bufferSize;
482
- let r;
483
- return "SharedArrayBuffer" in globalThis ? r = new SharedArrayBuffer(e) : r = new ArrayBuffer(e), new G({
484
- buf: r,
485
- // We can't use this unless we can 100% guarantee that every thread will stop using memory the instant it is freed
486
- // ex: Allocate 16 bytes. Thread A frees that allocation and then allocates 12 bytes and 4 bytes, but Thread B is mid-execution on the old allocation can changes the internal state of the 4-byte allocation breaking everything
487
- // After the internal state is wrong MemoryBuffer will loose track of which blocks are where and how big they are
488
- compact: !1,
489
- split: !1
490
- });
491
- }
492
- addOnGrowBufferHandlers(t) {
493
- this.onGrowBufferHandlers.push(t);
494
- }
495
- allocUI32(t) {
496
- t = Math.ceil(t);
497
- for (let i = 0; i < this.buffers.length; i++) {
498
- const o = this.buffers[i];
499
- if (!o)
500
- continue;
501
- const a = o.callocAs("u32", t);
502
- if (a)
503
- return i === this.buffers.length - 1 && Atomics.load(this.memory.data, Z) === this.buffers.length && this.memory.data[I] < 100 && this.memory.data[I] > 0 && o.top / o.end > this.memory.data[I] / 100 && this.growBuffer(), new f(this, {
504
- data: a,
505
- buffer: o
506
- });
507
- }
508
- if (this.buffers.length >= et)
509
- throw new Error(`Can't initialize a new buffer since it would have a position greater than the max of ${et}`);
510
- let e = this.growBuffer();
511
- const r = e.callocAs("u32", t);
512
- if (r)
513
- return new f(this, {
514
- data: r,
515
- buffer: e
516
- });
517
- throw new Error(`Unable to allocate ${t} numbers even after adding a new buffer`);
518
- }
519
- getSharedAlloc(t) {
520
- if (this.buffers[t.bufferPosition] !== void 0)
521
- return new f(this, t);
522
- }
523
- get currentUsed() {
524
- return this.totalAllocated - this.buffers.reduce((t, e) => t + e.stats().available, 0);
525
- }
526
- get totalAllocated() {
527
- return this.buffers[0].buf.byteLength * this.buffers.length;
528
- }
529
- getSharedMemory() {
530
- return {
531
- buffers: this.buffers.map((t) => t.buf)
532
- };
533
- }
534
- }
535
- const ht = 4, ct = 1, A = 2;
536
- class u {
537
- static ALLOCATE_COUNT = ht;
538
- memory;
539
- /* First block
540
- 32 index 0
541
- uint16 0 - next buffer position
542
- uint16 1 - next buffer index
543
- 32 index 1
544
- uint16 2 - last buffer position
545
- uint16 3 - last buffer index
546
- 32 index 2
547
- uint32 4 - length
548
- 32 index 3
549
- uint16 6 - type
550
- uint16 7 - data length (defaults to 1 number per data)
551
- */
552
- /* Other blocks
553
- 32 index 0
554
- uint16 0 - next buffer position
555
- uint16 1 - next buffer index
556
- 32 index 1 => data
557
- */
558
- firstBlock;
559
- uint16Array;
560
- onDelete;
561
- get length() {
562
- return Atomics.load(this.firstBlock.data, A);
563
- }
564
- get type() {
565
- return Atomics.load(this.uint16Array, 0);
566
- }
567
- set type(t) {
568
- Atomics.store(this.uint16Array, 0, t);
569
- }
570
- get dataLength() {
571
- return Math.max(1, Atomics.load(this.uint16Array, 1));
572
- }
573
- set dataLength(t) {
574
- Atomics.store(this.uint16Array, 1, t);
575
- }
576
- constructor(t, e) {
577
- if (this.memory = t, e && "firstBlock" in e)
578
- this.firstBlock = new f(t, e.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + (A + 1) * Uint32Array.BYTES_PER_ELEMENT, 2);
579
- else {
580
- e && e.initWithBlock ? this.firstBlock = new f(t, e.initWithBlock) : this.firstBlock = t.allocUI32(ht), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + (A + 1) * Uint32Array.BYTES_PER_ELEMENT, 2);
581
- const r = e?.type ?? Uint32Array;
582
- r === Uint32Array ? this.type = 0 : r === Int32Array ? this.type = 1 : r === Float32Array && (this.type = 2), this.dataLength = e?.dataLength ?? 1;
583
- }
584
- }
585
- insert(t) {
586
- typeof t == "number" && (t = [t]);
587
- let e = this.dataLength;
588
- if (t.length > e)
589
- throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);
590
- let r = this.memory.allocUI32(ct + e), i = this.getDataBlock(r.data), o = r.pointer;
591
- for (let l = 0; l < t.length; l++)
592
- i instanceof Int32Array || i instanceof Uint32Array ? Atomics.store(i, l, t[l]) : i[l] = t[l];
593
- let a, n = !1;
594
- for (; !n; )
595
- a = O(this.firstBlock.data, 1), n = H(this.firstBlock.data, 1, o, a);
596
- if (a) {
597
- let { bufferPosition: l, bufferByteOffset: h } = y(a), $ = new Uint32Array(this.memory.buffers[l].buf, h, 1);
598
- U($, 0, o);
599
- } else
600
- U(this.firstBlock.data, 0, o);
601
- Atomics.add(this.firstBlock.data, A, 1);
602
- }
603
- deleteMatch(t) {
604
- for (let { data: e, index: r, deleteCurrent: i } of this)
605
- if (t(e, r))
606
- return i(), !0;
607
- return !1;
608
- }
609
- deleteIndex(t) {
610
- return t >= this.length || t < 0 ? !1 : this.deleteMatch((e, r) => r === t);
611
- }
612
- deleteValue(t) {
613
- return typeof t == "number" ? this.deleteMatch((e) => e[0] === t) : this.deleteMatch((e) => {
614
- if (e.length !== t.length)
615
- return !1;
616
- for (let r = 0; r < e.length; r++)
617
- if (e[r] !== t[r])
618
- return !1;
619
- return !0;
620
- });
621
- }
622
- clear() {
623
- let t, e, r = !1;
624
- for (; !r; ) {
625
- if (t = O(this.firstBlock.data, 0), e = O(this.firstBlock.data, 1), !e)
626
- return;
627
- r = H(this.firstBlock.data, 1, 0, e);
628
- }
629
- if (!t)
630
- return;
631
- H(this.firstBlock.data, 0, 0, t);
632
- let i = 0, o = t;
633
- for (; o; ) {
634
- let { bufferPosition: a, bufferByteOffset: n } = y(o), l = this.memory.buffers[a];
635
- if (!l)
636
- break;
637
- let h = new Uint32Array(l.buf, n, 2);
638
- o = O(h, 0), i++, this.onDelete && this.onDelete(this.getDataBlock(h)), l.free(h.byteOffset);
639
- }
640
- Atomics.sub(this.firstBlock.data, A, i);
641
- }
642
- *[Symbol.iterator]() {
643
- let t = 0, { bufferPosition: e, bufferByteOffset: r } = c(this.firstBlock.data, 0), i = this.firstBlock.data, o = 0, a = 0;
644
- for (; r; ) {
645
- let n = this.memory.buffers[e];
646
- if (!n)
647
- return;
648
- let l = new Uint32Array(n.buf, r, 2), h = this.getDataBlock(l), $ = e, zt = r;
649
- ({ bufferPosition: e, bufferByteOffset: r } = c(l, 0));
650
- let Q = !0;
651
- yield {
652
- data: h,
653
- index: t,
654
- deleteCurrent: () => {
655
- k(i, 0, e, r), r || k(this.firstBlock.data, 1, o, a), this.onDelete && this.onDelete(this.getDataBlock(l)), n.free(l.byteOffset), Atomics.sub(this.firstBlock.data, A, 1), Q = !1;
656
- }
657
- }, Q && (i = l, o = $, a = zt, t++);
658
- }
659
- }
660
- forEach(t) {
661
- for (let e of this)
662
- t(e.data);
663
- }
664
- getSharedMemory() {
665
- return {
666
- firstBlock: this.firstBlock.getSharedMemory()
667
- };
668
- }
669
- getDataBlock(t) {
670
- const e = t.byteOffset + ct * t.BYTES_PER_ELEMENT;
671
- switch (this.type) {
672
- case 1:
673
- return new Int32Array(t.buffer, e, this.dataLength);
674
- case 0:
675
- return new Uint32Array(t.buffer, e, this.dataLength);
676
- case 2:
677
- return new Float32Array(t.buffer, e, this.dataLength);
678
- default:
679
- throw new Error(`Unknown data block type ${this.type}`);
680
- }
681
- }
682
- free() {
683
- let { bufferPosition: t, bufferByteOffset: e } = c(this.firstBlock.data, 0);
684
- for (; e; ) {
685
- let r = new f(this.memory, {
686
- bufferPosition: t,
687
- bufferByteOffset: e
688
- });
689
- ({ bufferPosition: t, bufferByteOffset: e } = c(r.data, 0)), this.onDelete && this.onDelete(this.getDataBlock(r.data)), r.free();
690
- }
691
- this.firstBlock.free();
692
- }
693
- }
694
- const ut = 10;
695
- class wt {
696
- static ALLOCATE_COUNT = 4;
697
- memory;
698
- // Memory order: Pointer, Lock, Length, MaxHash
699
- pointerMemory;
700
- lock;
701
- get hashMemory() {
702
- return new f(this.memory, c(this.pointerMemory.data, 0));
703
- }
704
- get length() {
705
- return Atomics.load(this.pointerMemory.data, 2);
706
- }
707
- get maxHash() {
708
- return Atomics.load(this.pointerMemory.data, 3);
709
- }
710
- constructor(t, e) {
711
- if (this.memory = t, e)
712
- this.pointerMemory = new f(t, e.firstBlock);
713
- else {
714
- this.pointerMemory = t.allocUI32(wt.ALLOCATE_COUNT);
715
- let r = t.allocUI32(ut);
716
- k(this.pointerMemory.data, 0, r.bufferPosition, r.bufferByteOffset), Atomics.store(this.pointerMemory.data, 3, ut);
717
- }
718
- this.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + Uint32Array.BYTES_PER_ELEMENT, 1);
719
- }
720
- set(t, e) {
721
- this.length >= this.maxHash * 2 && this.growHashTable();
722
- let r = M(t);
723
- this.setHashKey(this.hashMemory, this.maxHash, r, e) && Atomics.add(this.pointerMemory.data, 2, 1);
724
- }
725
- setHashKey(t, e, r, i) {
726
- let o = this.hash(r, e), a, n = c(t.data, o);
727
- if (n.bufferByteOffset === 0) {
728
- a = new u(this.memory, {
729
- dataLength: 2
730
- });
731
- let h = a.getSharedMemory();
732
- k(t.data, o, h.firstBlock.bufferPosition, h.firstBlock.bufferByteOffset);
733
- } else
734
- a = new u(this.memory, {
735
- firstBlock: n
736
- });
737
- let l = !0;
738
- return a.deleteValue(r) && (l = !1), a.insert([r, i]), l;
739
- }
740
- get(t) {
741
- let e = M(t), r = this.hash(e, this.maxHash), i = c(this.hashMemory.data, r);
742
- if (i.bufferByteOffset === 0)
743
- return;
744
- let o = new u(this.memory, {
745
- firstBlock: i
746
- });
747
- for (let { data: a } of o)
748
- if (a[0] === e)
749
- return a[1];
750
- }
751
- has(t) {
752
- let e = M(t), r = this.hash(e, this.maxHash), i = c(this.hashMemory.data, r);
753
- if (i.bufferByteOffset === 0)
754
- return !1;
755
- let o = new u(this.memory, {
756
- firstBlock: i
757
- });
758
- for (let { data: a } of o)
759
- if (a[0] === e)
760
- return !0;
761
- return !1;
762
- }
763
- delete(t) {
764
- let e = M(t), r = this.hash(e, this.maxHash), i = c(this.hashMemory.data, r);
765
- if (i.bufferByteOffset === 0)
766
- return !1;
767
- let o = new u(this.memory, {
768
- firstBlock: i
769
- });
770
- for (let { data: a, deleteCurrent: n } of o)
771
- if (a[0] === e)
772
- return n(), Atomics.sub(this.pointerMemory.data, 2, 1), !0;
773
- return !1;
774
- }
775
- growHashTable() {
776
- let t = this.maxHash, e = t * 2, r = this.memory.allocUI32(e), i = this.hashMemory;
777
- for (let o = 0; o < t; o++) {
778
- let a = c(i.data, o);
779
- if (a.bufferByteOffset === 0)
780
- continue;
781
- let n = new u(this.memory, {
782
- firstBlock: a
783
- });
784
- for (let { data: l } of n)
785
- this.setHashKey(r, e, l[0], l[1]);
786
- n.free();
787
- }
788
- k(this.pointerMemory.data, 0, r.bufferPosition, r.bufferByteOffset), Atomics.store(this.pointerMemory.data, 3, e), i.free();
789
- }
790
- hash(t, e) {
791
- return t % e;
792
- }
793
- free() {
794
- for (let t = 0; t < this.maxHash; t++) {
795
- let e = c(this.hashMemory.data, t);
796
- if (e.bufferByteOffset === 0)
797
- continue;
798
- new u(this.memory, {
799
- firstBlock: e
800
- }).free();
801
- }
802
- this.hashMemory.free(), this.pointerMemory.free();
803
- }
804
- getSharedMemory() {
805
- return {
806
- firstBlock: this.pointerMemory.getSharedMemory()
807
- };
808
- }
809
- }
810
- function M(s) {
811
- return typeof s == "number" ? s : typeof s == "string" ? vt(s) : s;
812
- }
813
- function vt(s) {
814
- let t = s.length, e = 17 ^ t, r = 0, i;
815
- for (; t >= 4; )
816
- i = s.charCodeAt(r) & 255 | (s.charCodeAt(++r) & 255) << 8 | (s.charCodeAt(++r) & 255) << 16 | (s.charCodeAt(++r) & 255) << 14, i = (i & 65535) * 1540483477 + (((i >>> 16) * 1540483477 & 65535) << 16), i ^= i >>> 14, i = (i & 65535) * 1540483477 + (((i >>> 16) * 1540483477 & 65535) << 16), e = (e & 65535) * 1540483477 + (((e >>> 16) * 1540483477 & 65535) << 16) ^ i, t -= 4, ++r;
817
- switch (t) {
818
- case 3:
819
- e ^= (s.charCodeAt(r + 2) & 255) << 16;
820
- case 2:
821
- e ^= (s.charCodeAt(r + 1) & 255) << 8;
822
- case 1:
823
- e ^= s.charCodeAt(r) & 255, e = (e & 65535) * 1540483477 + (((e >>> 16) * 1540483477 & 65535) << 16);
824
- }
825
- return e ^= e >>> 13, e = (e & 65535) * 1540483477 + (((e >>> 16) * 1540483477 & 65535) << 16), e ^= e >>> 15, e = e >>> 0, e;
826
- }
827
- class se {
828
- memory;
829
- list;
830
- constructor(t, e) {
831
- this.memory = t, e ? this.list = new u(t, e) : this.list = new u(t);
832
- }
833
- get length() {
834
- return this.list.length;
835
- }
836
- insert(t) {
837
- this.list.insert(L(t.memory.bufferPosition, t.memory.bufferByteOffset));
838
- }
839
- delete(t) {
840
- return this.list.deleteValue(L(t.memory.bufferPosition, t.memory.bufferByteOffset));
841
- }
842
- *[Symbol.iterator]() {
843
- let t = this.list[Symbol.iterator]();
844
- for (let { data: e } of t) {
845
- let { bufferPosition: r, bufferByteOffset: i } = c(e, 0), o = new f(this.memory, {
846
- bufferPosition: r,
847
- bufferByteOffset: i
848
- });
849
- yield this.createItem(o);
850
- }
851
- }
852
- forEach(t) {
853
- for (let e of this)
854
- t(e);
855
- }
856
- find(t) {
857
- for (let e of this)
858
- if (t(e))
859
- return e;
860
- }
861
- getSharedMemory() {
862
- return this.list.getSharedMemory();
863
- }
864
- free() {
865
- for (let t of this)
866
- "free" in t && typeof t.free == "function" ? t.free() : t.memory.free();
867
- this.list.free();
868
- }
869
- }
870
- const dt = {
871
- 1: Uint8Array,
872
- 2: Uint16Array
873
- }, N = 0, yt = 1, mt = 2, Bt = 3;
874
- class Lt {
875
- static ALLOCATE_COUNT = 4;
876
- memory;
877
- allocatedMemory;
878
- lock;
879
- cachedPointer;
880
- cachedString;
881
- constructor(t, e) {
882
- this.memory = t, typeof e == "string" ? (this.allocatedMemory = this.memory.allocUI32(Lt.ALLOCATE_COUNT), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + Bt * this.allocatedMemory.data.BYTES_PER_ELEMENT), this.updateString(e)) : (this.allocatedMemory = new f(t, e), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + Bt * this.allocatedMemory.data.BYTES_PER_ELEMENT), "value" in e && this.updateString(e.value));
883
- }
884
- updateString(t) {
885
- let e = this.createString(t);
886
- x(this.lock), U(this.allocatedMemory.data, N, e.pointer), Atomics.store(this.allocatedMemory.data, yt, t.length), Atomics.store(this.allocatedMemory.data, mt, e.charType), b(this.lock), this.cachedPointer = e.pointer, this.cachedString = t;
887
- }
888
- createString(t) {
889
- if (t === "")
890
- return {
891
- pointer: 0,
892
- charType: 1
893
- /* ASCII */
894
- };
895
- let e = 0;
896
- for (let n = 0; n < t.length; n++) {
897
- let l = t.charCodeAt(n);
898
- l > e && (e = l);
899
- }
900
- let r = e > 255 ? 2 : 1, i = dt[r], o = this.memory.allocUI32(Math.ceil(t.length / (4 / i.BYTES_PER_ELEMENT))), a = new i(o.data.buffer, o.data.byteOffset, t.length);
901
- for (let n = 0; n < t.length; n++)
902
- a[n] = t.charCodeAt(n);
903
- return {
904
- pointer: o.pointer,
905
- charType: r
906
- };
907
- }
908
- get value() {
909
- let t = O(this.allocatedMemory.data, N);
910
- if (this.cachedPointer === t && this.cachedString !== void 0)
911
- return this.cachedString;
912
- if (t === 0)
913
- return "";
914
- let { bufferPosition: e, bufferByteOffset: r } = y(t);
915
- x(this.lock);
916
- let i = Atomics.load(this.allocatedMemory.data, mt), o = dt[i], a = Atomics.load(this.allocatedMemory.data, yt), n = new o(this.memory.buffers[e].buf, r, a), l = jt(n);
917
- return b(this.lock), this.cachedPointer = t, this.cachedString = l, l;
918
- }
919
- set value(t) {
920
- let { bufferPosition: e, bufferByteOffset: r } = c(this.allocatedMemory.data, N);
921
- this.updateString(t), r && this.memory.buffers[e].free(r);
922
- }
923
- getSharedMemory() {
924
- return this.allocatedMemory.getSharedMemory();
925
- }
926
- get pointer() {
927
- return this.allocatedMemory.pointer;
928
- }
929
- free() {
930
- let { bufferPosition: t, bufferByteOffset: e } = c(this.allocatedMemory.data, N);
931
- e && this.memory.buffers[t].free(e), this.allocatedMemory.free();
932
- }
933
- }
934
- function jt(s) {
935
- if (s.length <= 8192)
936
- return String.fromCharCode.apply(null, s);
937
- let e = "";
938
- for (let r = 0; r < s.length; r += 8192)
939
- e += String.fromCharCode.apply(null, s.subarray(r, r + 8192));
940
- return e;
941
- }
942
- const p = 0, E = 1, bt = 2, kt = 3, gt = 4;
943
- class m {
944
- static ALLOCATE_COUNT = 4;
945
- memory;
946
- // Pointer, List Length, Buffer Length, Type/DataLength
947
- firstBlock;
948
- uint16Array;
949
- get length() {
950
- return Atomics.load(this.firstBlock.data, E);
951
- }
952
- get type() {
953
- return this.uint16Array[0];
954
- }
955
- set type(t) {
956
- Atomics.store(this.uint16Array, 0, t);
957
- }
958
- get dataLength() {
959
- return this.uint16Array[1] || 1;
960
- }
961
- set dataLength(t) {
962
- Atomics.store(this.uint16Array, 1, t);
963
- }
964
- get bufferLength() {
965
- return Atomics.load(this.firstBlock.data, bt);
966
- }
967
- set bufferLength(t) {
968
- Atomics.store(this.firstBlock.data, bt, t);
969
- }
970
- get pointer() {
971
- return this.firstBlock.pointer;
972
- }
973
- cachedFullDataBlock;
974
- cachedPointer;
975
- constructor(t, e) {
976
- if (this.memory = t, e && "firstBlock" in e) {
977
- if (this.firstBlock = new f(t, e.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + kt * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in e || "dataLength" in e) {
978
- const r = e.bufferLength ?? gt;
979
- let i = t.allocUI32(r * (e.dataLength ?? 1));
980
- k(this.firstBlock.data, p, i.bufferPosition, i.bufferByteOffset), this.bufferLength = r, this.dataLength = e.dataLength ?? 1;
981
- }
982
- if ("type" in e) {
983
- const r = e?.type ?? Uint32Array;
984
- r === Uint32Array ? this.type = 0 : r === Int32Array ? this.type = 1 : r === Float32Array && (this.type = 2);
985
- }
986
- } else {
987
- this.firstBlock = t.allocUI32(m.ALLOCATE_COUNT), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + kt * Uint32Array.BYTES_PER_ELEMENT, 2);
988
- let r = e?.dataLength ?? 1, i = e?.bufferLength ?? gt, o = t.allocUI32(i * r);
989
- k(this.firstBlock.data, p, o.bufferPosition, o.bufferByteOffset), this.bufferLength = i;
990
- const a = e?.type ?? Uint32Array;
991
- a === Uint32Array ? this.type = 0 : a === Int32Array ? this.type = 1 : a === Float32Array && (this.type = 2), this.dataLength = r;
992
- }
993
- this.cachedPointer = this.firstBlock.data[0], this.cachedFullDataBlock = this.getFullDataBlock();
994
- }
995
- at(t) {
996
- let e = this.length;
997
- if (t >= e || t < 0)
998
- throw new Error(`${t} is out of bounds ${e}`);
999
- let r = this.getFullDataBlock();
1000
- return this.getDataBlock(r, t);
1001
- }
1002
- get(t, e = 0) {
1003
- if (e >= this.dataLength)
1004
- throw new Error(`${e} is out of dataLength bounds ${this.dataLength}`);
1005
- if (t >= this.length || t < 0)
1006
- throw new Error(`${t} is out of bounds ${this.length}`);
1007
- return this.getFullDataBlock()[t * this.dataLength + e];
1008
- }
1009
- push(t) {
1010
- let e = this.dataLength;
1011
- const r = typeof t == "number";
1012
- if (!r && t.length > e)
1013
- throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);
1014
- let i = this.getFullDataBlock(), o = this.length, a = e * o;
1015
- return r ? i[a] = t : i.set(t, a), Atomics.add(this.firstBlock.data, E, 1) + 1 >= this.bufferLength && this.growBuffer(), o;
1016
- }
1017
- pop() {
1018
- let t = Atomics.sub(this.firstBlock.data, E, 1), e = this.getFullDataBlock();
1019
- return this.getDataBlock(e, t - 1);
1020
- }
1021
- // Returns the first number irregardless of dataLength - faster way to pop if you don't care about the rest of the data block
1022
- popNumber() {
1023
- const t = Atomics.sub(this.firstBlock.data, E, 1);
1024
- return this.getFullDataBlock()[(t - 1) * this.dataLength];
1025
- }
1026
- deleteIndex(t) {
1027
- let e = this.length;
1028
- if (t >= e || t < 0)
1029
- throw new Error(`${t} is out of bounds ${e}`);
1030
- let r = this.dataLength, i = this.getFullDataBlock();
1031
- for (let o = t; o < e; o++)
1032
- for (let a = 0; a < r; a++)
1033
- i[o * r + a] = i[(o + 1) * r + a];
1034
- Atomics.sub(this.firstBlock.data, E, 1);
1035
- }
1036
- clear() {
1037
- this.firstBlock.data[E] = 0;
1038
- }
1039
- *[Symbol.iterator]() {
1040
- let t = this.getFullDataBlock(), e = this.dataLength, r = this.length * e;
1041
- for (let i = 0; i < r; i += e)
1042
- yield t.subarray(i, i + e);
1043
- }
1044
- // Significantly faster but won't be thread safe if anything is removing/adding to it during iteration
1045
- getCurrentArray() {
1046
- return this.getFullDataBlock().subarray(0, this.length * this.dataLength);
1047
- }
1048
- getFullDataBlock() {
1049
- let t = Atomics.load(this.firstBlock.data, p);
1050
- if (this.cachedPointer === t && this.cachedFullDataBlock)
1051
- return this.cachedFullDataBlock;
1052
- let e = y(t), r = new f(this.memory, e), i;
1053
- switch (this.type) {
1054
- case 1:
1055
- i = new Int32Array(r.data.buffer, r.bufferByteOffset, this.dataLength * this.bufferLength);
1056
- break;
1057
- case 0:
1058
- i = new Uint32Array(r.data.buffer, r.bufferByteOffset, this.dataLength * this.bufferLength);
1059
- break;
1060
- case 2:
1061
- i = new Float32Array(r.data.buffer, r.bufferByteOffset, this.dataLength * this.bufferLength);
1062
- break;
1063
- default:
1064
- throw new Error(`Unknown data block type ${this.type}`);
1065
- }
1066
- return this.cachedPointer = t, this.cachedFullDataBlock = i, i;
1067
- }
1068
- getDataBlock(t, e) {
1069
- const r = e * this.dataLength;
1070
- return t.subarray(r, r + this.dataLength);
1071
- }
1072
- growBuffer() {
1073
- let e = this.bufferLength * 2, r = this.dataLength, i = c(this.firstBlock.data, p), o = new f(this.memory, i), a = this.getFullDataBlock(), n = this.memory.allocUI32(e * r), l;
1074
- switch (this.type) {
1075
- case 1:
1076
- l = new Int32Array(n.data.buffer, n.bufferByteOffset, r * e);
1077
- break;
1078
- case 0:
1079
- l = new Uint32Array(n.data.buffer, n.bufferByteOffset, r * e);
1080
- break;
1081
- case 2:
1082
- l = new Float32Array(n.data.buffer, n.bufferByteOffset, r * e);
1083
- break;
1084
- default:
1085
- throw new Error(`Unknown data block type ${this.type}`);
1086
- }
1087
- l.set(a);
1088
- const h = n.pointer;
1089
- U(this.firstBlock.data, p, h), this.bufferLength = e, o.free(), this.cachedPointer = h, this.cachedFullDataBlock = l;
1090
- }
1091
- free() {
1092
- let t = c(this.firstBlock.data, p);
1093
- new f(this.memory, t).free(), this.firstBlock.free();
1094
- }
1095
- getSharedMemory() {
1096
- return {
1097
- firstBlock: this.firstBlock.getSharedMemory()
1098
- };
1099
- }
1100
- }
1101
- class oe {
1102
- static ALLOCATE_COUNT = u.ALLOCATE_COUNT;
1103
- heap;
1104
- list;
1105
- cache = /* @__PURE__ */ new Map();
1106
- constructor(t, e) {
1107
- e ? this.list = new u(t, e) : this.list = new u(t), this.heap = t, this.list.onDelete = (r) => {
1108
- let i = Atomics.load(r, 0);
1109
- if (i) {
1110
- let o = this.cache.get(i);
1111
- o || (o = this.initItem(i)), o && (o.free(), this.cache.delete(i));
1112
- }
1113
- };
1114
- }
1115
- get length() {
1116
- return this.list.length;
1117
- }
1118
- clear() {
1119
- this.list.clear(), this.cache.clear();
1120
- }
1121
- insert(t) {
1122
- this.list.insert(t.pointer), this.cache.set(t.pointer, t);
1123
- }
1124
- delete(t) {
1125
- return this.cache.delete(t.pointer), this.list.deleteValue(t.pointer);
1126
- }
1127
- getByPointer(t) {
1128
- let e = this.cache.get(t);
1129
- return e || (e = this.initItem(t), e && this.cache.set(t, e)), e;
1130
- }
1131
- *[Symbol.iterator]() {
1132
- let t = this.list[Symbol.iterator]();
1133
- for (let { data: e, deleteCurrent: r } of t) {
1134
- let i = Atomics.load(e, 0);
1135
- if (!i)
1136
- continue;
1137
- let o = this.cache.get(i);
1138
- o || (o = this.initItem(i), o && this.cache.set(i, o)), o && (yield {
1139
- item: o,
1140
- deleteCurrent: r
1141
- });
1142
- }
1143
- }
1144
- forEach(t, e) {
1145
- for (let { item: r } of this)
1146
- (!e || e(r)) && t(r);
1147
- }
1148
- find(t) {
1149
- for (let { item: e } of this)
1150
- if (t(e))
1151
- return e;
1152
- }
1153
- filter(t) {
1154
- let e = [];
1155
- for (let { item: r } of this)
1156
- t(r) && e.push(r);
1157
- return e;
1158
- }
1159
- map(t) {
1160
- const e = [];
1161
- for (let { item: r } of this)
1162
- e.push(t(r));
1163
- return e;
1164
- }
1165
- getSharedMemory() {
1166
- return this.list.getSharedMemory();
1167
- }
1168
- free() {
1169
- this.list.free(), this.cache.clear();
1170
- }
1171
- }
1172
- const P = 0, At = 1, pt = 2, v = 3, Et = v + m.ALLOCATE_COUNT;
1173
- class St {
1174
- static ALLOCATE_COUNT = 3 + m.ALLOCATE_COUNT * 2;
1175
- memory;
1176
- // Current Length, Type/DataLength, MaxChunkLength, Pointer vector, Recycle vector
1177
- firstBlock;
1178
- uint16Array;
1179
- pointerVector;
1180
- recycleVector;
1181
- cachedFullDataBlock = {};
1182
- get length() {
1183
- return Atomics.load(this.firstBlock.data, P) - this.recycleVector.length;
1184
- }
1185
- get maxChunkSize() {
1186
- return this.firstBlock.data[pt];
1187
- }
1188
- set maxChunkSize(t) {
1189
- Atomics.store(this.firstBlock.data, pt, t);
1190
- }
1191
- get type() {
1192
- return this.uint16Array[0];
1193
- }
1194
- set type(t) {
1195
- Atomics.store(this.uint16Array, 0, t);
1196
- }
1197
- get dataLength() {
1198
- return Math.max(1, this.uint16Array[1]);
1199
- }
1200
- set dataLength(t) {
1201
- Atomics.store(this.uint16Array, 1, t);
1202
- }
1203
- get bufferLength() {
1204
- return this.maxChunkSize * this.pointerVector.length;
1205
- }
1206
- get recycleBufferLength() {
1207
- return this.recycleVector.bufferLength;
1208
- }
1209
- get byteMultipler() {
1210
- return this.type === 3 ? 2 : 1;
1211
- }
1212
- constructor(t, e) {
1213
- if (this.memory = t, e && "firstBlock" in e)
1214
- this.firstBlock = new f(t, e.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + At * Uint32Array.BYTES_PER_ELEMENT, 2), this.pointerVector = new m(t, {
1215
- firstBlock: {
1216
- bufferPosition: this.firstBlock.bufferPosition,
1217
- bufferByteOffset: this.firstBlock.bufferByteOffset + v * Uint32Array.BYTES_PER_ELEMENT
1218
- }
1219
- }), this.recycleVector = new m(t, {
1220
- firstBlock: {
1221
- bufferPosition: this.firstBlock.bufferPosition,
1222
- bufferByteOffset: this.firstBlock.bufferByteOffset + Et * Uint32Array.BYTES_PER_ELEMENT
1223
- }
1224
- });
1225
- else {
1226
- this.firstBlock = t.allocUI32(St.ALLOCATE_COUNT), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + At * Uint32Array.BYTES_PER_ELEMENT, 2);
1227
- let r = e?.dataLength ?? 1, i = e?.maxChunkSize ?? 100;
1228
- this.pointerVector = new m(t, {
1229
- type: Uint32Array,
1230
- firstBlock: {
1231
- bufferPosition: this.firstBlock.bufferPosition,
1232
- bufferByteOffset: this.firstBlock.bufferByteOffset + v * Uint32Array.BYTES_PER_ELEMENT
1233
- }
1234
- }), this.recycleVector = new m(t, {
1235
- type: Uint32Array,
1236
- firstBlock: {
1237
- bufferPosition: this.firstBlock.bufferPosition,
1238
- bufferByteOffset: this.firstBlock.bufferByteOffset + Et * Uint32Array.BYTES_PER_ELEMENT
1239
- },
1240
- bufferLength: e?.recycleBufferLength
1241
- });
1242
- const o = e?.type ?? Uint32Array;
1243
- o === Uint32Array ? this.type = 0 : o === Int32Array ? this.type = 1 : o === Float32Array ? this.type = 2 : o === Float64Array && (this.type = 3), this.dataLength = r, this.maxChunkSize = i;
1244
- let a = t.allocUI32(i * r * this.byteMultipler);
1245
- this.pointerVector.push(a.pointer);
1246
- }
1247
- }
1248
- at(t) {
1249
- let e = this.getFullDataBlock(t);
1250
- return this.getDataBlock(e, t % this.maxChunkSize);
1251
- }
1252
- get(t, e = 0) {
1253
- const r = this.dataLength;
1254
- if (e >= r)
1255
- throw new Error(`${e} is out of dataLength bounds ${r}`);
1256
- return this.getFullDataBlock(t)[t % this.maxChunkSize * r + e];
1257
- }
1258
- push(t) {
1259
- typeof t == "number" && (t = [t]);
1260
- let e = this.dataLength;
1261
- if (t.length > e)
1262
- throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);
1263
- let r;
1264
- this.recycleVector.length ? r = this.recycleVector.popNumber() : r = Atomics.add(this.firstBlock.data, P, 1);
1265
- let i = this.getFullDataBlock(r), o = r % this.maxChunkSize;
1266
- return i.set(t, e * o), r;
1267
- }
1268
- deleteIndex(t) {
1269
- this.recycleVector.push(t);
1270
- }
1271
- clear() {
1272
- this.firstBlock.data[P] = 0, this.recycleVector.clear();
1273
- }
1274
- *[Symbol.iterator]() {
1275
- const t = {};
1276
- for (let i of this.recycleVector)
1277
- t[i[0]] = !0;
1278
- let e = this.getFullDataBlock(0), r = 0;
1279
- for (let i = 0; i < Atomics.load(this.firstBlock.data, P); i++)
1280
- if (!t[i]) {
1281
- let o = Math.floor(i / this.maxChunkSize);
1282
- o !== r && (e = this.getFullDataBlock(i), r = o), yield this.getDataBlock(e, i % this.maxChunkSize);
1283
- }
1284
- }
1285
- getFullDataBlock(t) {
1286
- let e = Math.floor(t / this.maxChunkSize), r = this.cachedFullDataBlock[e];
1287
- if (r)
1288
- return r;
1289
- if (e >= this.pointerVector.length) {
1290
- let a = this.memory.allocUI32(this.maxChunkSize * this.dataLength * this.byteMultipler);
1291
- this.pointerVector.push(a.pointer);
1292
- }
1293
- let i = new f(this.memory, y(this.pointerVector.get(e))), o;
1294
- switch (this.type) {
1295
- case 1:
1296
- o = new Int32Array(i.data.buffer, i.bufferByteOffset, this.dataLength * this.maxChunkSize);
1297
- break;
1298
- case 0:
1299
- o = new Uint32Array(i.data.buffer, i.bufferByteOffset, this.dataLength * this.maxChunkSize);
1300
- break;
1301
- case 2:
1302
- o = new Float32Array(i.data.buffer, i.bufferByteOffset, this.dataLength * this.maxChunkSize);
1303
- break;
1304
- case 3:
1305
- o = new Float64Array(i.data.buffer, i.bufferByteOffset, this.dataLength * this.maxChunkSize);
1306
- break;
1307
- default:
1308
- throw new Error(`Unknown data block type ${this.type}`);
1309
- }
1310
- return this.cachedFullDataBlock[e] = o, o;
1311
- }
1312
- getDataBlock(t, e) {
1313
- const r = e * this.dataLength;
1314
- return t.subarray(r, r + this.dataLength);
1315
- }
1316
- free() {
1317
- this.recycleVector.free();
1318
- for (let t of this.pointerVector) {
1319
- let e = t[0];
1320
- new f(this.memory, y(e)).free();
1321
- }
1322
- this.pointerVector.free(), this.firstBlock.free();
1323
- }
1324
- getSharedMemory() {
1325
- return {
1326
- firstBlock: this.firstBlock.getSharedMemory()
1327
- };
1328
- }
1329
- }
1330
- class ae {
1331
- memory;
1332
- pointers = [];
1333
- recycledIndexes = [];
1334
- totalLength = 0;
1335
- maxChunkSize = 100;
1336
- dataLength = 1;
1337
- byteMultipler = 1;
1338
- typeConstructor;
1339
- fullDataBlocks = {};
1340
- get length() {
1341
- return this.totalLength - this.recycledIndexes.length;
1342
- }
1343
- get bufferLength() {
1344
- return this.maxChunkSize * this.pointers.length;
1345
- }
1346
- constructor(t, e) {
1347
- this.memory = t;
1348
- const r = e?.dataLength ?? 1, i = e?.maxChunkSize ?? 100;
1349
- this.dataLength = r, this.maxChunkSize = i, this.typeConstructor = e.type, this.typeConstructor === Float64Array && (this.byteMultipler = 2);
1350
- let o = t.allocUI32(i * r * this.byteMultipler);
1351
- this.pointers.push(o.pointer);
1352
- let a = new this.typeConstructor(o.data.buffer, o.bufferByteOffset, this.dataLength * this.maxChunkSize);
1353
- this.fullDataBlocks[0] = a;
1354
- }
1355
- at(t) {
1356
- let e = this.fullDataBlocks[Math.floor(t / this.maxChunkSize)];
1357
- return this.getDataBlock(e, t % this.maxChunkSize);
1358
- }
1359
- get(t, e = 0) {
1360
- const r = this.dataLength;
1361
- if (e >= r)
1362
- throw new Error(`${e} is out of dataLength bounds ${r}`);
1363
- return this.fullDataBlocks[Math.floor(t / this.maxChunkSize)][t % this.maxChunkSize * r + e];
1364
- }
1365
- push(t) {
1366
- const e = typeof t == "number";
1367
- if (!e && t.length > this.dataLength)
1368
- throw new Error(`Can't insert ${t.length} array into shared list of ${this.dataLength} dataLength`);
1369
- let r = this.recycledIndexes.pop();
1370
- r === void 0 && (r = this.totalLength++);
1371
- let i = Math.floor(r / this.maxChunkSize);
1372
- if (i >= this.pointers.length) {
1373
- let l = this.memory.allocUI32(this.maxChunkSize * this.dataLength * this.byteMultipler);
1374
- this.pointers.push(l.pointer);
1375
- let h = new this.typeConstructor(l.data.buffer, l.bufferByteOffset, this.dataLength * this.maxChunkSize);
1376
- this.fullDataBlocks[i] = h;
1377
- }
1378
- let o = this.fullDataBlocks[i], a = r % this.maxChunkSize, n = this.dataLength * a;
1379
- return e ? o[n] = t : o.set(t, n), r;
1380
- }
1381
- deleteIndex(t) {
1382
- this.recycledIndexes.push(t);
1383
- }
1384
- clear() {
1385
- this.totalLength = 0, this.recycledIndexes = [];
1386
- }
1387
- *[Symbol.iterator]() {
1388
- const t = {};
1389
- for (let i of this.recycledIndexes)
1390
- t[i] = !0;
1391
- let e = this.getFullDataBlock(0), r = 0;
1392
- for (let i = 0; i < this.totalLength; i++)
1393
- if (!t[i]) {
1394
- let o = Math.floor(i / this.maxChunkSize);
1395
- o !== r && (e = this.getFullDataBlock(i), r = o), yield this.getDataBlock(e, i % this.maxChunkSize);
1396
- }
1397
- }
1398
- getFullDataBlock(t) {
1399
- let e = Math.floor(t / this.maxChunkSize), r = this.fullDataBlocks[e];
1400
- if (r)
1401
- return r;
1402
- if (e >= this.pointers.length) {
1403
- let a = this.memory.allocUI32(this.maxChunkSize * this.dataLength * this.byteMultipler);
1404
- this.pointers.push(a.pointer);
1405
- }
1406
- let i = new f(this.memory, y(this.pointers[e])), o = new this.typeConstructor(i.data.buffer, i.bufferByteOffset, this.dataLength * this.maxChunkSize);
1407
- return this.fullDataBlocks[e] = o, o;
1408
- }
1409
- getDataBlock(t, e) {
1410
- const r = e * this.dataLength;
1411
- return t.subarray(r, r + this.dataLength);
1412
- }
1413
- free() {
1414
- for (let t of this.pointers)
1415
- new f(this.memory, y(t)).free();
1416
- this.pointers = [], this.recycledIndexes = [], this.totalLength = 0, this.fullDataBlocks = {};
1417
- }
1418
- }
1419
- const Tt = new ArrayBuffer(4), Ot = new Uint32Array(Tt), F = new Uint16Array(Tt);
1420
- function ne(s, t) {
1421
- return qt(Atomics.load(s, t));
1422
- }
1423
- function le(s, t, e, r) {
1424
- Atomics.store(s, t, Jt(e, r));
1425
- }
1426
- function qt(s) {
1427
- return Ot[0] = s, [F[0], F[1]];
1428
- }
1429
- function Jt(s, t) {
1430
- return F[0] = s, F[1] = t, Ot[0];
1431
- }
1432
- const _t = new ArrayBuffer(8), Ct = new BigUint64Array(_t), B = new Uint16Array(_t);
1433
- function fe(s, t) {
1434
- return Ct[0] = Atomics.load(s, t), [B[0], B[1], B[2], B[3]];
1435
- }
1436
- function he(s, t, e, r, i, o = 0) {
1437
- B[0] = e, B[1] = r, B[2] = i, B[3] = o, Atomics.store(s, t, Ct[0]);
1438
- }
1439
- const It = new ArrayBuffer(4), Mt = new Float32Array(It), Nt = new Int32Array(It);
1440
- function Pt(s, t) {
1441
- return s instanceof Float32Array && (s = new Int32Array(s.buffer, s.byteOffset, s.length)), q(Atomics.load(s, t));
1442
- }
1443
- function ce(s, t, e) {
1444
- s instanceof Float32Array && (s = new Int32Array(s.buffer, s.byteOffset, s.length)), Atomics.store(s, t, z(e));
1445
- }
1446
- function q(s) {
1447
- return Nt[0] = s, Mt[0];
1448
- }
1449
- function z(s) {
1450
- return Mt[0] = s, Nt[0];
1451
- }
1452
- function ue(s, t, e) {
1453
- return s instanceof Float32Array && (s = new Int32Array(s.buffer, s.byteOffset, s.length)), q(Atomics.exchange(s, t, z(e)));
1454
- }
1455
- function Ut(s, t, e, r) {
1456
- return s instanceof Float32Array && (s = new Int32Array(s.buffer, s.byteOffset, s.length)), q(Atomics.compareExchange(s, t, z(e), z(r)));
1457
- }
1458
- function de(s, t, e, r) {
1459
- let i, o;
1460
- do
1461
- i = Atomics.load(s, t), o = Math.min(i + e, r);
1462
- while (Atomics.compareExchange(s, t, i, o) !== i);
1463
- return o;
1464
- }
1465
- function ye(s, t, e, r) {
1466
- let i, o;
1467
- do
1468
- i = Atomics.load(s, t), o = Math.max(i - e, r);
1469
- while (Atomics.compareExchange(s, t, i, o) !== i);
1470
- return o;
1471
- }
1472
- function me(s, t, e, r) {
1473
- let i, o;
1474
- do
1475
- i = Pt(s, t), o = Math.min(i + e, r);
1476
- while (Ut(s, t, i, o) !== i);
1477
- return o;
1478
- }
1479
- function Be(s, t, e, r) {
1480
- let i, o;
1481
- do
1482
- i = Pt(s, t), o = Math.max(i - e, r);
1483
- while (Ut(s, t, i, o) !== i);
1484
- return o;
1485
- }
1486
- const Dt = new ArrayBuffer(8), xt = new Float64Array(Dt), Ft = new BigInt64Array(Dt);
1487
- function be(s, t) {
1488
- return s instanceof Float64Array && (s = new BigInt64Array(s.buffer, s.byteOffset, s.length)), J(Atomics.load(s, t));
1489
- }
1490
- function ke(s, t, e) {
1491
- s instanceof Float64Array && (s = new BigInt64Array(s.buffer, s.byteOffset, s.length)), Atomics.store(s, t, R(e));
1492
- }
1493
- function J(s) {
1494
- return Ft[0] = s, xt[0];
1495
- }
1496
- function R(s) {
1497
- return xt[0] = s, Ft[0];
1498
- }
1499
- function ge(s, t, e) {
1500
- return s instanceof Float64Array && (s = new BigInt64Array(s.buffer, s.byteOffset, s.length)), J(Atomics.exchange(s, t, R(e)));
1501
- }
1502
- function Ae(s, t, e, r) {
1503
- return s instanceof Float64Array && (s = new BigInt64Array(s.buffer, s.byteOffset, s.length)), J(Atomics.compareExchange(s, t, R(e), R(r)));
1504
- }
1505
- const w = 0, j = 1, S = 2;
1506
- function pe(s, t = 0) {
1507
- for (; Atomics.compareExchange(s, t, w, j) === S; )
1508
- Atomics.wait(s, t, S);
1509
- Atomics.add(s, t + 1, 1);
1510
- }
1511
- function Ee(s, t = 0) {
1512
- let e = Atomics.compareExchange(s, t, w, S);
1513
- for (; e !== w; )
1514
- Atomics.wait(s, t, e), e = Atomics.compareExchange(s, t, w, S);
1515
- }
1516
- function we(s, t = 0) {
1517
- Atomics.sub(s, t + 1, 1) - 1 <= 0 && (Atomics.compareExchange(s, t, j, w) !== j && console.warn("We are unlocking when it was not read locked!"), Atomics.notify(s, t));
1518
- }
1519
- function Le(s, t = 0) {
1520
- Atomics.compareExchange(s, t, S, w) !== S && console.warn("We are unlocking when it was not write locked!"), Atomics.notify(s, t);
1521
- }
1522
- const Se = 2;
1523
- export {
1524
- f as AllocatedMemory,
1525
- Qt as BYTE_OFFSET_BIT_COUNT,
1526
- oe as CachedItemList,
1527
- ae as LocalPool,
1528
- tt as MAX_BYTE_OFFSET_LENGTH,
1529
- et as MAX_POSITION_LENGTH,
1530
- G as MemoryBuffer,
1531
- ie as MemoryHeap,
1532
- te as POSITION_BIT_COUNT,
1533
- Se as READ_WRITE_LOCK_ALLOCATE_COUNT,
1534
- re as SIMPLE_LOCK_ALLOCATE_COUNT,
1535
- u as SharedList,
1536
- wt as SharedMap,
1537
- se as SharedPointerList,
1538
- St as SharedPool,
1539
- Lt as SharedString,
1540
- m as SharedVector,
1541
- me as addAtomicFloat32,
1542
- de as addAtomicInt,
1543
- Ut as compareExchangeFloat32,
1544
- Ae as compareExchangeFloat64,
1545
- Jt as convert16To32,
1546
- qt as convert32To16,
1547
- z as convertFloat32ToInt32,
1548
- R as convertFloat64ToInt64,
1549
- q as convertInt32ToFloat32,
1550
- J as convertInt64ToFloat64,
1551
- L as createPointer,
1552
- ue as exchangeFloat32,
1553
- ge as exchangeFloat64,
1554
- y as getPointer,
1555
- ne as load16From32,
1556
- fe as load16From64,
1557
- Pt as loadFloat32,
1558
- be as loadFloat64,
1559
- c as loadPointer,
1560
- O as loadRawPointer,
1561
- x as lock,
1562
- pe as readLock,
1563
- we as readUnlock,
1564
- ee as replacePointer,
1565
- H as replaceRawPointer,
1566
- le as store16In32,
1567
- he as store16In64,
1568
- ce as storeFloat32,
1569
- ke as storeFloat64,
1570
- k as storePointer,
1571
- U as storeRawPointer,
1572
- Be as subtractAtomicFloat,
1573
- ye as subtractAtomicInt,
1574
- b as unlock,
1575
- Ee as writeLock,
1576
- Le as writeUnlock
1577
- };
1578
- //# sourceMappingURL=shared-memory-objects.js.map