@daneren2005/shared-memory-objects 0.0.10 → 0.0.11

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