@daneren2005/shared-memory-objects 0.0.10 → 0.0.12

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