@daneren2005/shared-memory-objects 0.0.12 → 0.0.14

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.
@@ -1,2 +1,2 @@
1
- (function(f,P){typeof exports=="object"&&typeof module<"u"?P(exports):typeof define=="function"&&define.amd?define(["exports"],P):(f=typeof globalThis<"u"?globalThis:f||self,P(f.SharedMemoryObjects={}))})(this,(function(f){"use strict";const z=Math.pow(2,20),G=Math.pow(2,12);function c(r,t=0){return E(Atomics.load(r,t))}function k(r,t=0){return Atomics.load(r,t)}function y(r,t=0,e,i){Atomics.store(r,t,A(e,i))}function N(r,t=0,e){Atomics.store(r,t,e)}function Ct(r,t,e,i,s,o){let a=A(s,o);return Atomics.compareExchange(r,t,a,A(e,i))===a}function I(r,t,e,i){return Atomics.compareExchange(r,t,i,e)===i}function E(r){return{bufferPosition:r&4095,bufferByteOffset:r>>>12}}function A(r,t){return r+(t<<12)}class u{memory;bufferPosition;get bufferByteOffset(){return this.data.byteOffset}get pointer(){return A(this.bufferPosition,this.bufferByteOffset)}buffer;data;constructor(t,e){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))}getArray(t,e,i){return new t(this.data.buffer,this.data.byteOffset+e*t.BYTES_PER_ELEMENT,i)}getArrayMemory(t,e){return{bufferPosition:this.bufferPosition,bufferByteOffset:this.bufferByteOffset+t*this.data.BYTES_PER_ELEMENT}}free(){this.buffer.free(this.data.byteOffset)}getSharedMemory(){return{bufferPosition:this.bufferPosition,bufferByteOffset:this.bufferByteOffset}}}const x=0,U=1;function p(r,t=0){for(;Atomics.compareExchange(r,t,x,U)!==x;)"WorkerGlobalScope"in self&&Atomics.wait(r,t,U)}function B(r,t=0){Atomics.compareExchange(r,t,U,x)!==U&&console.warn("We are unlocking when it was not locked!"),Atomics.notify(r,t)}const Ft=1,Dt={5120:"i8",5121:"u8",5122:"i16",5123:"u16",5124:"i32",5125:"u32",5126:"f32"},Rt={f32:Float32Array,f64:Float64Array},$t={i8:Int8Array,i16:Int16Array,i32:Int32Array},Yt={u8:Uint8Array,u8c:Uint8ClampedArray,u16:Uint16Array,u32:Uint32Array},Ht={i64:BigInt64Array,u64:BigUint64Array},Kt={...Rt,...$t,...Yt},zt=r=>{const t=Dt[r];return t!==void 0?t:r};function Gt(r,...t){const e=Ht[r];return new(e||Kt[zt(r)])(...t)}const st=0,ot=1,at=2,nt=3,ft=4,T=5,lt=6,X=1,Z=2,ht=32,v=0,W=1,m=8;class C{buf;start;u8;u32;state;lock;constructor(t={}){if(this.buf=t.buf?t.buf:new ArrayBuffer(t.size||4096),this.start=t.start!=null?D(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,ht/4),this.lock=new Int32Array(this.buf,this.start+this.state.byteLength-4,1),!t.skipInitialization){const e=t.align||8;if(e<8)throw new Error(`invalid alignment: ${e}, must be a pow2 and >= 8`);const i=this.initialTop(e),s=t.end!=null?Math.min(t.end,this.buf.byteLength):this.buf.byteLength;if(i>=s)throw new Error(`insufficient address range (0x${this.start.toString(16)} - 0x${s.toString(16)})`);this.align=e,this.doCompact=t.compact!==!1,this.doSplit=t.split!==!1,this.minSplit=t.minSplit||16,this.end=s,this.top=i,this._free=0,this._used=0}}stats(){const t=i=>{let s=0,o=0;for(;i;)if(s++,o+=this.blockSize(i),i=this.blockNext(i),i>this.end){console.error(`Trying to get stats for block past end of buffer: ${i} > ${this.end}`);break}return{count:s,size:o}},e=t(this._free);return{free:e,used:t(this._used),top:this.top,available:this.end-this.top+e.size,total:this.buf.byteLength}}callocAs(t,e,i=0){const s=this.mallocAs(t,e);return s&&s.fill(i),s}mallocAs(t,e){const i=this.malloc(e*xt[t]);return i?Gt(t,this.buf,i,e):void 0}calloc(t,e=0){const i=this.malloc(t);return i&&this.u8.fill(e,i,i+t),i}malloc(t){if(t<=0)return 0;p(this.lock);const e=D(t+m,this.align),i=this.end;let s=this.top,o=this._free,a=0;for(;o;){const l=this.blockSize(o),n=o+l>=s;if(n||l>=e){let h=this.mallocTop(o,a,l,e,n);return B(this.lock),h}a=o,o=this.blockNext(o)}if(o=s,s=o+e,s<=i){this.initBlock(o,e,this._used),this._used=o,this.top=s;let l=L(o);return B(this.lock),l}return B(this.lock),0}mallocTop(t,e,i,s,o){if(o&&t+s>this.end)return 0;if(e?this.unlinkBlock(e,t):this._free=this.blockNext(t),this.setBlockNext(t,this._used),this._used=t,o)this.top=t+this.setBlockSize(t,s);else if(this.doSplit){const a=i-s;a>=this.minSplit&&this.splitBlock(t,s,a)}return L(t)}realloc(t,e){if(e<=0)return 0;const i=F(t);let s=0,o=this._used,a=0;for(;o;){if(o===i){[s,a]=this.reallocBlock(o,e);break}o=this.blockNext(o)}return s&&s!==i&&this.u8.copyWithin(L(s),L(i),a),L(s)}reallocBlock(t,e){const i=this.blockSize(t),s=t+i,o=s>=this.top,a=D(e+m,this.align);if(a<=i){if(this.doSplit){const l=i-a;l>=this.minSplit?this.splitBlock(t,a,l):o&&(this.top=t+a)}else o&&(this.top=t+a);return[t,s]}return o&&t+a<this.end?(this.top=t+this.setBlockSize(t,a),[t,s]):(this.free(t),[F(this.malloc(e)),s])}reallocArray(t,e){if(t.buffer!==this.buf)return;const i=this.realloc(t.byteOffset,e*t.BYTES_PER_ELEMENT);return i?new t.constructor(this.buf,i,e):void 0}bytesFor(t){let e;if(typeof t!="number"){if(t.buffer!==this.buf)return;e=t.byteOffset}else e=t;e=F(e);let i=this._used;for(;i;){if(i===e)return this.blockSize(e)-m;i=this.blockNext(i)}}lengthOf(t){let e=this.bytesFor(t);if(e)return e/this.u32.BYTES_PER_ELEMENT}free(t){let e;if(typeof t!="number"){if(t.buffer!==this.buf)return!1;e=t.byteOffset}else e=t;p(this.lock),e=F(e);let i=this._used,s=0;for(;i;){if(i===e)return s?this.unlinkBlock(s,i):this._used=this.blockNext(i),this.insert(i),this.doCompact&&this.compact(),B(this.lock),!0;s=i,i=this.blockNext(i)}return B(this.lock),!1}freeAll(){this._free=0,this._used=0,this.top=this.initialTop()}release(){return delete this.u8,delete this.u32,delete this.state,delete this.buf,!0}get align(){return this.state[ft]}set align(t){this.state[ft]=t}get end(){return this.state[nt]}set end(t){this.state[nt]=t}get top(){return Atomics.load(this.state,at)}set top(t){Atomics.store(this.state,at,t)}get _free(){return Atomics.load(this.state,st)}set _free(t){Atomics.store(this.state,st,t)}get _used(){return Atomics.load(this.state,ot)}set _used(t){Atomics.store(this.state,ot,t)}get doCompact(){return!!(this.state[T]&X)}set doCompact(t){t?this.state[T]|=1<<X-1:this.state[T]&=~X}get doSplit(){return!!(this.state[T]&Z)}set doSplit(t){t?this.state[T]|=1<<Z-1:this.state[T]&=~Z}get minSplit(){return this.state[lt]}set minSplit(t){if(t<=m)throw new Error(`illegal min split threshold: ${t}, require at least ${m+1}`);this.state[lt]=t}blockSize(t){return Atomics.load(this.u32,(t>>2)+v)}setBlockSize(t,e){return Atomics.store(this.u32,(t>>2)+v,e),e}blockNext(t){return Atomics.load(this.u32,(t>>2)+W)}setBlockNext(t,e){Atomics.store(this.u32,(t>>2)+W,e)}initBlock(t,e,i){const s=t>>>2;return Atomics.store(this.u32,s+v,e),Atomics.store(this.u32,s+W,i),t}unlinkBlock(t,e){this.setBlockNext(t,this.blockNext(e))}splitBlock(t,e,i){this.insert(this.initBlock(t+this.setBlockSize(t,e),i,0)),this.doCompact&&this.compact()}initialTop(t=this.align){return D(this.start+ht+m,t)-m}compact(){let t=this._free,e=0,i=0,s,o=!1;for(;t;){for(s=t,i=this.blockNext(t);i&&s+this.blockSize(s)===i;)s=i,i=this.blockNext(i);if(s!==t){const a=s-t+this.blockSize(s);this.setBlockSize(t,a);const l=this.blockNext(s);let n=this.blockNext(t);for(;n&&n!==l;){const h=this.blockNext(n);this.setBlockNext(n,0),n=h}this.setBlockNext(t,l),o=!0}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)}return o}insert(t){let e=this._free,i=0;for(;e&&!(t<=e);)i=e,e=this.blockNext(e);i?this.setBlockNext(i,t):this._free=t,this.setBlockNext(t,e)}}const L=r=>r>0?r+m:0,F=r=>r>0?r-m:0,D=(r,t)=>(t--,r+t&~t),xt={u8:1,u8c:1,i8:1,u16:2,i16:2,u32:4,i32:4,i64:8,u64:8,f32:4,f64:8},Xt=["B","kB","MB","GB","TB","PB","EB","ZB","YB"],Zt=["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],vt=["b","kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],Wt=["b","kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],ct=(r,t,e)=>{let i=r;return typeof t=="string"||Array.isArray(t)?i=r.toLocaleString(t,e):(t===!0||e!==void 0)&&(i=r.toLocaleString(void 0,e)),i},ut=r=>{if(typeof r=="number")return Math.log10(r);const t=r.toString(10);return t.length+Math.log10(`0.${t.slice(0,15)}`)},jt=r=>typeof r=="number"?Math.log(r):ut(r)*Math.log(10),Vt=(r,t)=>{if(typeof r=="number")return r/t;const e=r/BigInt(t),i=r%BigInt(t);return Number(e)+Number(i)/t},dt=(r,t)=>{if(t===void 0)return r;if(typeof t!="number"||!Number.isSafeInteger(t)||t<0)throw new TypeError(`Expected fixedWidth to be a non-negative integer, got ${typeof t}: ${t}`);return t===0?r:r.length<t?r.padStart(t," "):r},qt=r=>{const{minimumFractionDigits:t,maximumFractionDigits:e}=r;if(!(t===void 0&&e===void 0))return{...t!==void 0&&{minimumFractionDigits:t},...e!==void 0&&{maximumFractionDigits:e},roundingMode:"trunc"}};function Jt(r,t){if(typeof r!="bigint"&&!Number.isFinite(r))throw new TypeError(`Expected a finite number, got ${typeof r}: ${r}`);t={bits:!1,binary:!1,space:!0,nonBreakingSpace:!1,...t};const e=t.bits?t.binary?Wt:vt:t.binary?Zt:Xt,i=t.space?t.nonBreakingSpace?" ":" ":"",s=typeof r=="number"?r===0:r===0n;if(t.signed&&s){const h=` 0${i}${e[0]}`;return dt(h,t.fixedWidth)}const o=r<0,a=o?"-":t.signed?"+":"";o&&(r=-r);const l=qt(t);let n;if(r<1){const h=ct(r,t.locale,l);n=a+h+i+e[0]}else{const h=Math.min(Math.floor(t.binary?jt(r)/Math.log(1024):ut(r)/3),e.length-1);if(r=Vt(r,(t.binary?1024:1e3)**h),!l){const K=Math.max(3,Math.floor(r).toString().length);r=r.toPrecision(K)}const M=ct(Number(r),t.locale,l),rt=e[h];n=a+M+i+rt}return dt(n,t.fixedWidth)}const Qt=8192,yt=0,j=1,R=2;class te{buffers;onGrowBufferHandlers=[];isClone;memory;get bufferSize(){return this.memory.data[yt]}constructor(t){if(t&&"buffers"in t)this.buffers=t.buffers.map(e=>new C({buf:e,skipInitialization:!0})),this.memory=new u(this,{bufferPosition:0,bufferByteOffset:40}),this.isClone=!0;else{"SharedArrayBuffer"in globalThis||console.warn("SharedArrayBuffer is not working: falling back to ArrayBuffer");const e=t?.bufferSize??Qt;if(e>z)throw new Error(`Buffer size ${e} is greater than max ${z} that we can reference with pointers`);let i=this.createBuffer(e);this.buffers=[i];const s=i.callocAs("u32",3);if(s)this.memory=new u(this,{bufferPosition:0,bufferByteOffset:s.byteOffset});else throw new Error("Failed to initialize first byte from buffer");this.memory.data[yt]=e,this.memory.data[j]=1,this.memory.data[R]=t?.autoGrowSize??100,this.isClone=!1;for(let o=1;o<(t?.initialBuffers??1);o++)this.buffers.push(this.createBuffer(e))}}addSharedBuffer(t){this.buffers[t.bufferPosition]=new C({buf:t.buffer,skipInitialization:!0})}growBuffer(){const t=this.createBuffer();let e=Atomics.add(this.memory.data,j,1);return this.buffers[e]=t,this.onGrowBufferHandlers.forEach(i=>i({bufferPosition:e,buffer:t.buf})),t}createBuffer(t){const e=t??this.bufferSize;let i;return"SharedArrayBuffer"in globalThis?i=new SharedArrayBuffer(e):i=new ArrayBuffer(e),new C({buf:i,compact:!1,split:!1})}addOnGrowBufferHandlers(t){this.onGrowBufferHandlers.push(t)}allocUI32(t){t=Math.ceil(t);for(let s=0;s<this.buffers.length;s++){const o=this.buffers[s];if(!o)continue;const a=o.callocAs("u32",t);if(a)return s===this.buffers.length-1&&Atomics.load(this.memory.data,j)===this.buffers.length&&this.memory.data[R]<100&&this.memory.data[R]>0&&o.top/o.end>this.memory.data[R]/100&&this.growBuffer(),new u(this,{data:a,buffer:o})}if(this.buffers.length>=G)throw new Error(`Can't initialize a new buffer since it would have a position greater than the max of ${G}`);let e=this.growBuffer();const i=e.callocAs("u32",t);if(i)return new u(this,{data:i,buffer:e});throw new Error(`Unable to allocate ${t} numbers even after adding a new buffer`)}getSharedAlloc(t){if(this.buffers[t.bufferPosition]!==void 0)return new u(this,t)}get currentUsed(){return this.totalAllocated-this.buffers.reduce((t,e)=>t+e.stats().available,0)}get totalAllocated(){return this.buffers[0].buf.byteLength*this.buffers.length}prettyMemory(){return`${mt(this.currentUsed)} / ${mt(this.totalAllocated)}`}getSharedMemory(){return{buffers:this.buffers.map(t=>t.buf)}}}function mt(r){return Jt(r,{binary:!0,minimumFractionDigits:1,maximumFractionDigits:1})}const Bt=4,bt=1,w=2;class d{static ALLOCATE_COUNT=Bt;memory;firstBlock;uint16Array;onDelete;get length(){return Atomics.load(this.firstBlock.data,w)}get type(){return Atomics.load(this.uint16Array,0)}set type(t){Atomics.store(this.uint16Array,0,t)}get dataLength(){return Math.max(1,Atomics.load(this.uint16Array,1))}set dataLength(t){Atomics.store(this.uint16Array,1,t)}constructor(t,e){if(this.memory=t,e&&"firstBlock"in e)this.firstBlock=new u(t,e.firstBlock),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+(w+1)*Uint32Array.BYTES_PER_ELEMENT,2);else{e&&e.initWithBlock?this.firstBlock=new u(t,e.initWithBlock):this.firstBlock=t.allocUI32(Bt),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+(w+1)*Uint32Array.BYTES_PER_ELEMENT,2);const i=e?.type??Uint32Array;i===Uint32Array?this.type=0:i===Int32Array?this.type=1:i===Float32Array&&(this.type=2),this.dataLength=e?.dataLength??1}}insert(t){typeof t=="number"&&(t=[t]);let e=this.dataLength;if(t.length>e)throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);let i=this.memory.allocUI32(bt+e),s=this.getDataBlock(i.data),o=i.pointer;for(let n=0;n<t.length;n++)s instanceof Int32Array||s instanceof Uint32Array?Atomics.store(s,n,t[n]):s[n]=t[n];let a,l=!1;for(;!l;)a=k(this.firstBlock.data,1),l=I(this.firstBlock.data,1,o,a);if(a){let{bufferPosition:n,bufferByteOffset:h}=E(a),M=new Uint32Array(this.memory.buffers[n].buf,h,1);N(M,0,o)}else N(this.firstBlock.data,0,o);Atomics.add(this.firstBlock.data,w,1)}deleteMatch(t){for(let{data:e,index:i,deleteCurrent:s}of this)if(t(e,i))return s(),!0;return!1}deleteIndex(t){return t>=this.length||t<0?!1:this.deleteMatch((e,i)=>i===t)}deleteValue(t){return typeof t=="number"?this.deleteMatch(e=>e[0]===t):this.deleteMatch(e=>{if(e.length!==t.length)return!1;for(let i=0;i<e.length;i++)if(e[i]!==t[i])return!1;return!0})}clear(){let t,e,i=!1;for(;!i;){if(t=k(this.firstBlock.data,0),e=k(this.firstBlock.data,1),!e)return;i=I(this.firstBlock.data,1,0,e)}if(!t)return;I(this.firstBlock.data,0,0,t);let s=0,o=t;for(;o;){let{bufferPosition:a,bufferByteOffset:l}=E(o),n=this.memory.buffers[a];if(!n)break;let h=new Uint32Array(n.buf,l,2);o=k(h,0),s++,this.onDelete&&this.onDelete(this.getDataBlock(h)),n.free(h.byteOffset)}Atomics.sub(this.firstBlock.data,w,s)}*[Symbol.iterator](){let t=0,{bufferPosition:e,bufferByteOffset:i}=c(this.firstBlock.data,0),s=this.firstBlock.data,o=0,a=0;for(;i;){let l=this.memory.buffers[e];if(!l)return;let n=new Uint32Array(l.buf,i,2),h=this.getDataBlock(n),M=e,rt=i;({bufferPosition:e,bufferByteOffset:i}=c(n,0));let K=!0;yield{data:h,index:t,deleteCurrent:()=>{y(s,0,e,i),i||y(this.firstBlock.data,1,o,a),this.onDelete&&this.onDelete(this.getDataBlock(n)),l.free(n.byteOffset),Atomics.sub(this.firstBlock.data,w,1),K=!1}},K&&(s=n,o=M,a=rt,t++)}}forEach(t){for(let e of this)t(e.data)}getSharedMemory(){return{firstBlock:this.firstBlock.getSharedMemory()}}getDataBlock(t){const e=t.byteOffset+bt*t.BYTES_PER_ELEMENT;switch(this.type){case 1:return new Int32Array(t.buffer,e,this.dataLength);case 0:return new Uint32Array(t.buffer,e,this.dataLength);case 2:return new Float32Array(t.buffer,e,this.dataLength);default:throw new Error(`Unknown data block type ${this.type}`)}}free(){let{bufferPosition:t,bufferByteOffset:e}=c(this.firstBlock.data,0);for(;e;){let i=new u(this.memory,{bufferPosition:t,bufferByteOffset:e});({bufferPosition:t,bufferByteOffset:e}=c(i.data,0)),this.onDelete&&this.onDelete(this.getDataBlock(i.data)),i.free()}this.firstBlock.free()}}const At=10;class V{static ALLOCATE_COUNT=4;memory;pointerMemory;lock;get hashMemory(){return new u(this.memory,c(this.pointerMemory.data,0))}get length(){return Atomics.load(this.pointerMemory.data,2)}get maxHash(){return Atomics.load(this.pointerMemory.data,3)}constructor(t,e){if(this.memory=t,e)this.pointerMemory=new u(t,e.firstBlock);else{this.pointerMemory=t.allocUI32(V.ALLOCATE_COUNT);let i=t.allocUI32(At);y(this.pointerMemory.data,0,i.bufferPosition,i.bufferByteOffset),Atomics.store(this.pointerMemory.data,3,At)}this.lock=new Int32Array(this.pointerMemory.data.buffer,this.pointerMemory.bufferByteOffset+Uint32Array.BYTES_PER_ELEMENT,1)}set(t,e){this.length>=this.maxHash*2&&this.growHashTable();let i=$(t);this.setHashKey(this.hashMemory,this.maxHash,i,e)&&Atomics.add(this.pointerMemory.data,2,1)}setHashKey(t,e,i,s){let o=this.hash(i,e),a,l=c(t.data,o);if(l.bufferByteOffset===0){a=new d(this.memory,{dataLength:2});let h=a.getSharedMemory();y(t.data,o,h.firstBlock.bufferPosition,h.firstBlock.bufferByteOffset)}else a=new d(this.memory,{firstBlock:l});let n=!0;return a.deleteValue(i)&&(n=!1),a.insert([i,s]),n}get(t){let e=$(t),i=this.hash(e,this.maxHash),s=c(this.hashMemory.data,i);if(s.bufferByteOffset===0)return;let o=new d(this.memory,{firstBlock:s});for(let{data:a}of o)if(a[0]===e)return a[1]}has(t){let e=$(t),i=this.hash(e,this.maxHash),s=c(this.hashMemory.data,i);if(s.bufferByteOffset===0)return!1;let o=new d(this.memory,{firstBlock:s});for(let{data:a}of o)if(a[0]===e)return!0;return!1}delete(t){let e=$(t),i=this.hash(e,this.maxHash),s=c(this.hashMemory.data,i);if(s.bufferByteOffset===0)return!1;let o=new d(this.memory,{firstBlock:s});for(let{data:a,deleteCurrent:l}of o)if(a[0]===e)return l(),Atomics.sub(this.pointerMemory.data,2,1),!0;return!1}growHashTable(){let t=this.maxHash,e=t*2,i=this.memory.allocUI32(e),s=this.hashMemory;for(let o=0;o<t;o++){let a=c(s.data,o);if(a.bufferByteOffset===0)continue;let l=new d(this.memory,{firstBlock:a});for(let{data:n}of l)this.setHashKey(i,e,n[0],n[1])}y(this.pointerMemory.data,0,i.bufferPosition,i.bufferByteOffset),Atomics.store(this.pointerMemory.data,3,e)}hash(t,e){return t%e}free(){for(let t=0;t<this.maxHash;t++){let e=c(this.hashMemory.data,t);if(e.bufferByteOffset===0)continue;new d(this.memory,{firstBlock:e}).free()}this.hashMemory.free(),this.pointerMemory.free()}getSharedMemory(){return{firstBlock:this.pointerMemory.getSharedMemory()}}}function $(r){return typeof r=="number"?r:typeof r=="string"?ee(r):r}function ee(r){let t=r.length,e=17^t,i=0,s;for(;t>=4;)s=r.charCodeAt(i)&255|(r.charCodeAt(++i)&255)<<8|(r.charCodeAt(++i)&255)<<16|(r.charCodeAt(++i)&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,++i;switch(t){case 3:e^=(r.charCodeAt(i+2)&255)<<16;case 2:e^=(r.charCodeAt(i+1)&255)<<8;case 1:e^=r.charCodeAt(i)&255,e=(e&65535)*1540483477+(((e>>>16)*1540483477&65535)<<16)}return e^=e>>>13,e=(e&65535)*1540483477+(((e>>>16)*1540483477&65535)<<16),e^=e>>>15,e=e>>>0,e}class ie{memory;list;constructor(t,e){this.memory=t,e?this.list=new d(t,e):this.list=new d(t)}get length(){return this.list.length}insert(t){this.list.insert(A(t.memory.bufferPosition,t.memory.bufferByteOffset))}delete(t){return this.list.deleteValue(A(t.memory.bufferPosition,t.memory.bufferByteOffset))}*[Symbol.iterator](){let t=this.list[Symbol.iterator]();for(let{data:e}of t){let{bufferPosition:i,bufferByteOffset:s}=c(e,0),o=new u(this.memory,{bufferPosition:i,bufferByteOffset:s});yield this.createItem(o)}}forEach(t){for(let e of this)t(e)}find(t){for(let e of this)if(t(e))return e}getSharedMemory(){return this.list.getSharedMemory()}free(){for(let t of this)"free"in t&&typeof t.free=="function"?t.free():t.memory.free();this.list.free()}}const gt={1:Uint8Array,2:Uint16Array},Y=0,kt=1,Et=2,Tt=3;class q{static ALLOCATE_COUNT=4;memory;allocatedMemory;lock;cachedPointer;cachedString;constructor(t,e){this.memory=t,typeof e=="string"?(this.allocatedMemory=this.memory.allocUI32(q.ALLOCATE_COUNT),this.lock=new Int32Array(this.allocatedMemory.data.buffer,this.allocatedMemory.bufferByteOffset+Tt*this.allocatedMemory.data.BYTES_PER_ELEMENT),this.updateString(e)):(this.allocatedMemory=new u(t,e),this.lock=new Int32Array(this.allocatedMemory.data.buffer,this.allocatedMemory.bufferByteOffset+Tt*this.allocatedMemory.data.BYTES_PER_ELEMENT),"value"in e&&this.updateString(e.value))}updateString(t){let e=this.createString(t);p(this.lock),N(this.allocatedMemory.data,Y,e.pointer),Atomics.store(this.allocatedMemory.data,kt,t.length),Atomics.store(this.allocatedMemory.data,Et,e.charType),B(this.lock),this.cachedPointer=e.pointer,this.cachedString=t}createString(t){if(t==="")return{pointer:0,charType:1};let e=[];for(let n=0;n<t.length;n++)e.push(t.charCodeAt(n));let s=Math.max(...e)>255?2:1,o=gt[s],a=this.memory.allocUI32(Math.ceil(t.length/(4/o.BYTES_PER_ELEMENT))),l=new o(a.data.buffer,a.data.byteOffset,t.length);for(let n=0;n<t.length;n++)l[n]=t.charCodeAt(n);return{pointer:a.pointer,charType:s}}get value(){let t=k(this.allocatedMemory.data,Y);if(this.cachedPointer===t&&this.cachedString!==void 0)return this.cachedString;if(t===0)return"";let{bufferPosition:e,bufferByteOffset:i}=E(t);p(this.lock);let s=Atomics.load(this.allocatedMemory.data,Et),o=gt[s],a=Atomics.load(this.allocatedMemory.data,kt),l=new o(this.memory.buffers[e].buf,i,a),n=String.fromCharCode.apply(null,l);return B(this.lock),this.cachedPointer=t,this.cachedString=n,n}set value(t){let{bufferPosition:e,bufferByteOffset:i}=c(this.allocatedMemory.data,Y);this.updateString(t),i&&this.memory.buffers[e].free(i)}getSharedMemory(){return this.allocatedMemory.getSharedMemory()}get pointer(){return this.allocatedMemory.pointer}free(){let{bufferPosition:t,bufferByteOffset:e}=c(this.allocatedMemory.data,Y);e&&this.memory.buffers[t].free(e),this.allocatedMemory.free()}}const _=0,g=1,wt=2,_t=3,J=4;class Q{static ALLOCATE_COUNT=4;memory;firstBlock;uint16Array;get length(){return Atomics.load(this.firstBlock.data,g)}get type(){return this.uint16Array[0]}set type(t){Atomics.store(this.uint16Array,0,t)}get dataLength(){return Math.max(1,this.uint16Array[1])}set dataLength(t){Atomics.store(this.uint16Array,1,t)}get bufferLength(){return Atomics.load(this.firstBlock.data,wt)}set bufferLength(t){Atomics.store(this.firstBlock.data,wt,t)}get pointer(){return this.firstBlock.pointer}cachedFullDataBlock;cachedPointer;constructor(t,e){if(this.memory=t,e&&"firstBlock"in e){if(this.firstBlock=new u(t,e.firstBlock),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+_t*Uint32Array.BYTES_PER_ELEMENT,2),"type"in e||"dataLength"in e){let i=t.allocUI32(J*(e.dataLength??1));y(this.firstBlock.data,_,i.bufferPosition,i.bufferByteOffset),this.bufferLength=J,this.dataLength=e.dataLength??1}if("type"in e){const i=e?.type??Uint32Array;i===Uint32Array?this.type=0:i===Int32Array?this.type=1:i===Float32Array&&(this.type=2)}}else{this.firstBlock=t.allocUI32(Q.ALLOCATE_COUNT),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+_t*Uint32Array.BYTES_PER_ELEMENT,2);let i=e?.dataLength??1,s=e?.bufferLength??J,o=t.allocUI32(s*i);y(this.firstBlock.data,_,o.bufferPosition,o.bufferByteOffset),this.bufferLength=s;const a=e?.type??Uint32Array;a===Uint32Array?this.type=0:a===Int32Array?this.type=1:a===Float32Array&&(this.type=2),this.dataLength=i}this.cachedPointer=this.firstBlock.data[0],this.cachedFullDataBlock=this.getFullDataBlock()}at(t){let e=this.length;if(t>=e||t<0)throw new Error(`${t} is out of bounds ${e}`);let i=this.getFullDataBlock();return this.getDataBlock(i,t)}get(t,e=0){if(e>=this.dataLength)throw new Error(`${e} is out of dataLength bounds ${this.dataLength}`);if(t>=this.length||t<0)throw new Error(`${t} is out of bounds ${this.length}`);return this.getFullDataBlock()[t*this.dataLength+e]}push(t){typeof t=="number"&&(t=[t]);let e=this.dataLength;if(t.length>e)throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);let i=this.getFullDataBlock(),s=this.length;return i.set(t,e*s),Atomics.add(this.firstBlock.data,g,1)+1>=this.bufferLength&&this.growBuffer(),s}pop(){let t=Atomics.sub(this.firstBlock.data,g,g),e=this.getFullDataBlock();return this.getDataBlock(e,t-1)}deleteIndex(t){let e=this.length;if(t>=e||t<0)throw new Error(`${t} is out of bounds ${e}`);let i=this.dataLength,s=this.getFullDataBlock();for(let o=t;o<e;o++)for(let a=0;a<i;a++)s[o*i+a]=s[(o+1)*i+a];Atomics.sub(this.firstBlock.data,g,g)}clear(){this.firstBlock.data[g]=0}*[Symbol.iterator](){let t=this.getFullDataBlock();for(let e=0;e<this.length;e++)yield this.getDataBlock(t,e)}getFullDataBlock(){let t=Atomics.load(this.firstBlock.data,_);if(this.cachedPointer===t&&this.cachedFullDataBlock)return this.cachedFullDataBlock;let e=E(t),i=new u(this.memory,e),s;switch(this.type){case 1:s=new Int32Array(i.data.buffer,i.bufferByteOffset,this.dataLength*this.bufferLength);break;case 0:s=new Uint32Array(i.data.buffer,i.bufferByteOffset,this.dataLength*this.bufferLength);break;case 2:s=new Float32Array(i.data.buffer,i.bufferByteOffset,this.dataLength*this.bufferLength);break;default:throw new Error(`Unknown data block type ${this.type}`)}return this.cachedPointer=t,this.cachedFullDataBlock=s,s}getDataBlock(t,e){const i=e*this.dataLength;return t.subarray(i,i+this.dataLength)}growBuffer(){let e=this.bufferLength*2,i=this.dataLength,s=c(this.firstBlock.data,_),o=new u(this.memory,s),a=this.getFullDataBlock(),l=this.memory.allocUI32(e*i),n;switch(this.type){case 1:n=new Int32Array(l.data.buffer,l.bufferByteOffset,i*this.bufferLength);break;case 0:n=new Uint32Array(l.data.buffer,l.bufferByteOffset,i*this.bufferLength);break;case 2:n=new Float32Array(l.data.buffer,l.bufferByteOffset,i*this.bufferLength);break;default:throw new Error(`Unknown data block type ${this.type}`)}n.set(a),y(this.firstBlock.data,_,l.bufferPosition,l.bufferByteOffset),this.bufferLength=e,o.free()}free(){let t=c(this.firstBlock.data,_);new u(this.memory,t).free(),this.firstBlock.free()}getSharedMemory(){return{firstBlock:this.firstBlock.getSharedMemory()}}}class re{static ALLOCATE_COUNT=d.ALLOCATE_COUNT;heap;list;cache=new Map;constructor(t,e){e?this.list=new d(t,e):this.list=new d(t),this.heap=t,this.list.onDelete=i=>{let s=Atomics.load(i,0);if(s){let o=this.cache.get(s);o||(o=this.initItem(s)),o&&(o.free(),this.cache.delete(s))}}}get length(){return this.list.length}clear(){this.list.clear(),this.cache.clear()}insert(t){this.list.insert(t.pointer),this.cache.set(t.pointer,t)}delete(t){return this.cache.delete(t.pointer),this.list.deleteValue(t.pointer)}getByPointer(t){let e=this.cache.get(t);return e||(e=this.initItem(t),e&&this.cache.set(t,e)),e}*[Symbol.iterator](){let t=this.list[Symbol.iterator]();for(let{data:e,deleteCurrent:i}of t){let s=Atomics.load(e,0);if(!s)continue;let o=this.cache.get(s);o||(o=this.initItem(s),o&&this.cache.set(s,o)),o&&(yield{item:o,deleteCurrent:i})}}forEach(t,e){for(let{item:i}of this)(!e||e(i))&&t(i)}find(t){for(let{item:e}of this)if(t(e))return e}filter(t){let e=[];for(let{item:i}of this)t(i)&&e.push(i);return e}map(t){const e=[];for(let{item:i}of this)e.push(t(i));return e}getSharedMemory(){return this.list.getSharedMemory()}free(){this.list.free(),this.cache.clear()}}const St=new ArrayBuffer(4),Ot=new Uint32Array(St),H=new Uint16Array(St);function se(r,t){return pt(Atomics.load(r,t))}function oe(r,t,e,i){Atomics.store(r,t,Lt(e,i))}function pt(r){return Ot[0]=r,[H[0],H[1]]}function Lt(r,t){return H[0]=r,H[1]=t,Ot[0]}const Mt=new ArrayBuffer(8),Pt=new BigUint64Array(Mt),b=new Uint16Array(Mt);function ae(r,t){return Pt[0]=Atomics.load(r,t),[b[0],b[1],b[2],b[3]]}function ne(r,t,e,i,s,o=0){b[0]=e,b[1]=i,b[2]=s,b[3]=o,Atomics.store(r,t,Pt[0])}const Nt=new ArrayBuffer(4),It=new Float32Array(Nt),Ut=new Int32Array(Nt);function fe(r,t){return tt(Atomics.load(r,t))}function le(r,t,e){Atomics.store(r,t,et(e))}function tt(r){return Ut[0]=r,It[0]}function et(r){return It[0]=r,Ut[0]}function he(r,t,e){return tt(Atomics.exchange(r,t,et(e)))}const S=0,it=1,O=2;function ce(r,t=0){for(;Atomics.compareExchange(r,t,S,it)===O;)Atomics.wait(r,t,O);Atomics.add(r,t+1,1)}function ue(r,t=0){let e=Atomics.compareExchange(r,t,S,O);for(;e!==S;)Atomics.wait(r,t,e),e=Atomics.compareExchange(r,t,S,O)}function de(r,t=0){Atomics.sub(r,t+1,1)-1<=0&&(Atomics.compareExchange(r,t,it,S)!==it&&console.warn("We are unlocking when it was not read locked!"),Atomics.notify(r,t))}function ye(r,t=0){Atomics.compareExchange(r,t,O,S)!==O&&console.warn("We are unlocking when it was not write locked!"),Atomics.notify(r,t)}const me=2;f.AllocatedMemory=u,f.BYTE_OFFSET_BIT_COUNT=20,f.CachedItemList=re,f.MAX_BYTE_OFFSET_LENGTH=z,f.MAX_POSITION_LENGTH=G,f.MemoryBuffer=C,f.MemoryHeap=te,f.POSITION_BIT_COUNT=12,f.READ_WRITE_LOCK_ALLOCATE_COUNT=me,f.SIMPLE_LOCK_ALLOCATE_COUNT=Ft,f.SharedList=d,f.SharedMap=V,f.SharedPointerList=ie,f.SharedString=q,f.SharedVector=Q,f.convert16To32=Lt,f.convert32To16=pt,f.convertFloat32ToInt32=et,f.convertInt32ToFloat32=tt,f.createPointer=A,f.exchangeFloat32=he,f.getPointer=E,f.load16From32=se,f.load16From64=ae,f.loadFloat32=fe,f.loadPointer=c,f.loadRawPointer=k,f.lock=p,f.readLock=ce,f.readUnlock=de,f.replacePointer=Ct,f.replaceRawPointer=I,f.store16In32=oe,f.store16In64=ne,f.storeFloat32=le,f.storePointer=y,f.storeRawPointer=N,f.unlock=B,f.writeLock=ue,f.writeUnlock=ye,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(f,N){typeof exports=="object"&&typeof module<"u"?N(exports):typeof define=="function"&&define.amd?define(["exports"],N):(f=typeof globalThis<"u"?globalThis:f||self,N(f.SharedMemoryObjects={}))})(this,(function(f){"use strict";const X=Math.pow(2,20),Z=Math.pow(2,12);function u(r,t=0){return b(Atomics.load(r,t))}function w(r,t=0){return Atomics.load(r,t)}function y(r,t=0,e,i){Atomics.store(r,t,g(e,i))}function C(r,t=0,e){Atomics.store(r,t,e)}function xt(r,t,e,i,s,o){let a=g(s,o);return Atomics.compareExchange(r,t,a,g(e,i))===a}function U(r,t,e,i){return Atomics.compareExchange(r,t,i,e)===i}function b(r){return{bufferPosition:r&4095,bufferByteOffset:r>>>12}}function g(r,t){return r+(t<<12)}class c{memory;bufferPosition;get bufferByteOffset(){return this.data.byteOffset}get pointer(){return g(this.bufferPosition,this.bufferByteOffset)}buffer;data;constructor(t,e){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))}getArray(t,e,i){return new t(this.data.buffer,this.data.byteOffset+e*t.BYTES_PER_ELEMENT,i)}getArrayMemory(t,e){return{bufferPosition:this.bufferPosition,bufferByteOffset:this.bufferByteOffset+t*this.data.BYTES_PER_ELEMENT}}free(){this.buffer.free(this.data.byteOffset)}getSharedMemory(){return{bufferPosition:this.bufferPosition,bufferByteOffset:this.bufferByteOffset}}}const W=0,F=1;function O(r,t=0){for(;Atomics.compareExchange(r,t,W,F)!==W;)"WorkerGlobalScope"in self&&Atomics.wait(r,t,F)}function A(r,t=0){Atomics.compareExchange(r,t,F,W)!==F&&console.warn("We are unlocking when it was not locked!"),Atomics.notify(r,t)}const Vt=1,Kt={5120:"i8",5121:"u8",5122:"i16",5123:"u16",5124:"i32",5125:"u32",5126:"f32"},Gt={f32:Float32Array,f64:Float64Array},Xt={i8:Int8Array,i16:Int16Array,i32:Int32Array},Zt={u8:Uint8Array,u8c:Uint8ClampedArray,u16:Uint16Array,u32:Uint32Array},Wt={i64:BigInt64Array,u64:BigUint64Array},vt={...Gt,...Xt,...Zt},jt=r=>{const t=Kt[r];return t!==void 0?t:r};function qt(r,...t){const e=Wt[r];return new(e||vt[jt(r)])(...t)}const lt=0,ht=1,ct=2,ut=3,dt=4,T=5,yt=6,v=1,j=2,Bt=32,q=0,J=1,B=8;class D{buf;start;u8;u32;state;lock;constructor(t={}){if(this.buf=t.buf?t.buf:new ArrayBuffer(t.size||4096),this.start=t.start!=null?$(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,Bt/4),this.lock=new Int32Array(this.buf,this.start+this.state.byteLength-4,1),!t.skipInitialization){const e=t.align||8;if(e<8)throw new Error(`invalid alignment: ${e}, must be a pow2 and >= 8`);const i=this.initialTop(e),s=t.end!=null?Math.min(t.end,this.buf.byteLength):this.buf.byteLength;if(i>=s)throw new Error(`insufficient address range (0x${this.start.toString(16)} - 0x${s.toString(16)})`);this.align=e,this.doCompact=t.compact!==!1,this.doSplit=t.split!==!1,this.minSplit=t.minSplit||16,this.end=s,this.top=i,this._free=0,this._used=0}}stats(){const t=i=>{let s=0,o=0;for(;i;)if(s++,o+=this.blockSize(i),i=this.blockNext(i),i>this.end){console.error(`Trying to get stats for block past end of buffer: ${i} > ${this.end}`);break}return{count:s,size:o}},e=t(this._free);return{free:e,used:t(this._used),top:this.top,available:this.end-this.top+e.size,total:this.buf.byteLength}}callocAs(t,e,i=0){const s=this.mallocAs(t,e);return s&&s.fill(i),s}mallocAs(t,e){const i=this.malloc(e*Jt[t]);return i?qt(t,this.buf,i,e):void 0}calloc(t,e=0){const i=this.malloc(t);return i&&this.u8.fill(e,i,i+t),i}malloc(t){if(t<=0)return 0;O(this.lock);const e=$(t+B,this.align),i=this.end;let s=this.top,o=this._free,a=0;for(;o;){const l=this.blockSize(o),n=o+l>=s;if(n||l>=e){let h=this.mallocTop(o,a,l,e,n);return A(this.lock),h}a=o,o=this.blockNext(o)}if(o=s,s=o+e,s<=i){this.initBlock(o,e,this._used),this._used=o,this.top=s;let l=M(o);return A(this.lock),l}return A(this.lock),0}mallocTop(t,e,i,s,o){if(o&&t+s>this.end)return 0;if(e?this.unlinkBlock(e,t):this._free=this.blockNext(t),this.setBlockNext(t,this._used),this._used=t,o)this.top=t+this.setBlockSize(t,s);else if(this.doSplit){const a=i-s;a>=this.minSplit&&this.splitBlock(t,s,a)}return M(t)}realloc(t,e){if(e<=0)return 0;const i=R(t);let s=0,o=this._used,a=0;for(;o;){if(o===i){[s,a]=this.reallocBlock(o,e);break}o=this.blockNext(o)}return s&&s!==i&&this.u8.copyWithin(M(s),M(i),a),M(s)}reallocBlock(t,e){const i=this.blockSize(t),s=t+i,o=s>=this.top,a=$(e+B,this.align);if(a<=i){if(this.doSplit){const l=i-a;l>=this.minSplit?this.splitBlock(t,a,l):o&&(this.top=t+a)}else o&&(this.top=t+a);return[t,s]}return o&&t+a<this.end?(this.top=t+this.setBlockSize(t,a),[t,s]):(this.free(t),[R(this.malloc(e)),s])}reallocArray(t,e){if(t.buffer!==this.buf)return;const i=this.realloc(t.byteOffset,e*t.BYTES_PER_ELEMENT);return i?new t.constructor(this.buf,i,e):void 0}bytesFor(t){let e;if(typeof t!="number"){if(t.buffer!==this.buf)return;e=t.byteOffset}else e=t;e=R(e);let i=this._used;for(;i;){if(i===e)return this.blockSize(e)-B;i=this.blockNext(i)}}lengthOf(t){let e=this.bytesFor(t);if(e)return e/this.u32.BYTES_PER_ELEMENT}free(t){let e;if(typeof t!="number"){if(t.buffer!==this.buf)return!1;e=t.byteOffset}else e=t;O(this.lock),e=R(e);let i=this._used,s=0;for(;i;){if(i===e)return s?this.unlinkBlock(s,i):this._used=this.blockNext(i),this.insert(i),this.doCompact&&this.compact(),A(this.lock),!0;s=i,i=this.blockNext(i)}return A(this.lock),!1}freeAll(){this._free=0,this._used=0,this.top=this.initialTop()}release(){return delete this.u8,delete this.u32,delete this.state,delete this.buf,!0}get align(){return this.state[dt]}set align(t){this.state[dt]=t}get end(){return this.state[ut]}set end(t){this.state[ut]=t}get top(){return Atomics.load(this.state,ct)}set top(t){Atomics.store(this.state,ct,t)}get _free(){return Atomics.load(this.state,lt)}set _free(t){Atomics.store(this.state,lt,t)}get _used(){return Atomics.load(this.state,ht)}set _used(t){Atomics.store(this.state,ht,t)}get doCompact(){return!!(this.state[T]&v)}set doCompact(t){t?this.state[T]|=1<<v-1:this.state[T]&=~v}get doSplit(){return!!(this.state[T]&j)}set doSplit(t){t?this.state[T]|=1<<j-1:this.state[T]&=~j}get minSplit(){return this.state[yt]}set minSplit(t){if(t<=B)throw new Error(`illegal min split threshold: ${t}, require at least ${B+1}`);this.state[yt]=t}blockSize(t){return Atomics.load(this.u32,(t>>2)+q)}setBlockSize(t,e){return Atomics.store(this.u32,(t>>2)+q,e),e}blockNext(t){return Atomics.load(this.u32,(t>>2)+J)}setBlockNext(t,e){Atomics.store(this.u32,(t>>2)+J,e)}initBlock(t,e,i){const s=t>>>2;return Atomics.store(this.u32,s+q,e),Atomics.store(this.u32,s+J,i),t}unlinkBlock(t,e){this.setBlockNext(t,this.blockNext(e))}splitBlock(t,e,i){this.insert(this.initBlock(t+this.setBlockSize(t,e),i,0)),this.doCompact&&this.compact()}initialTop(t=this.align){return $(this.start+Bt+B,t)-B}compact(){let t=this._free,e=0,i=0,s,o=!1;for(;t;){for(s=t,i=this.blockNext(t);i&&s+this.blockSize(s)===i;)s=i,i=this.blockNext(i);if(s!==t){const a=s-t+this.blockSize(s);this.setBlockSize(t,a);const l=this.blockNext(s);let n=this.blockNext(t);for(;n&&n!==l;){const h=this.blockNext(n);this.setBlockNext(n,0),n=h}this.setBlockNext(t,l),o=!0}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)}return o}insert(t){let e=this._free,i=0;for(;e&&!(t<=e);)i=e,e=this.blockNext(e);i?this.setBlockNext(i,t):this._free=t,this.setBlockNext(t,e)}}const M=r=>r>0?r+B:0,R=r=>r>0?r-B:0,$=(r,t)=>(t--,r+t&~t),Jt={u8:1,u8c:1,i8:1,u16:2,i16:2,u32:4,i32:4,i64:8,u64:8,f32:4,f64:8},Qt=["B","kB","MB","GB","TB","PB","EB","ZB","YB"],te=["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],ee=["b","kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],ie=["b","kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],mt=(r,t,e)=>{let i=r;return typeof t=="string"||Array.isArray(t)?i=r.toLocaleString(t,e):(t===!0||e!==void 0)&&(i=r.toLocaleString(void 0,e)),i},bt=r=>{if(typeof r=="number")return Math.log10(r);const t=r.toString(10);return t.length+Math.log10(`0.${t.slice(0,15)}`)},re=r=>typeof r=="number"?Math.log(r):bt(r)*Math.log(10),se=(r,t)=>{if(typeof r=="number")return r/t;const e=r/BigInt(t),i=r%BigInt(t);return Number(e)+Number(i)/t},At=(r,t)=>{if(t===void 0)return r;if(typeof t!="number"||!Number.isSafeInteger(t)||t<0)throw new TypeError(`Expected fixedWidth to be a non-negative integer, got ${typeof t}: ${t}`);return t===0?r:r.length<t?r.padStart(t," "):r},oe=r=>{const{minimumFractionDigits:t,maximumFractionDigits:e}=r;if(!(t===void 0&&e===void 0))return{...t!==void 0&&{minimumFractionDigits:t},...e!==void 0&&{maximumFractionDigits:e},roundingMode:"trunc"}};function ae(r,t){if(typeof r!="bigint"&&!Number.isFinite(r))throw new TypeError(`Expected a finite number, got ${typeof r}: ${r}`);t={bits:!1,binary:!1,space:!0,nonBreakingSpace:!1,...t};const e=t.bits?t.binary?ie:ee:t.binary?te:Qt,i=t.space?t.nonBreakingSpace?" ":" ":"",s=typeof r=="number"?r===0:r===0n;if(t.signed&&s){const h=` 0${i}${e[0]}`;return At(h,t.fixedWidth)}const o=r<0,a=o?"-":t.signed?"+":"";o&&(r=-r);const l=oe(t);let n;if(r<1){const h=mt(r,t.locale,l);n=a+h+i+e[0]}else{const h=Math.min(Math.floor(t.binary?re(r)/Math.log(1024):bt(r)/3),e.length-1);if(r=se(r,(t.binary?1024:1e3)**h),!l){const G=Math.max(3,Math.floor(r).toString().length);r=r.toPrecision(G)}const I=mt(Number(r),t.locale,l),ft=e[h];n=a+I+i+ft}return At(n,t.fixedWidth)}const ne=8192,kt=0,Q=1,Y=2;class fe{buffers;onGrowBufferHandlers=[];isClone;memory;get bufferSize(){return this.memory.data[kt]}constructor(t){if(t&&"buffers"in t)this.buffers=t.buffers.map(e=>new D({buf:e,skipInitialization:!0})),this.memory=new c(this,{bufferPosition:0,bufferByteOffset:40}),this.isClone=!0;else{"SharedArrayBuffer"in globalThis||console.warn("SharedArrayBuffer is not working: falling back to ArrayBuffer");const e=t?.bufferSize??ne;if(e>X)throw new Error(`Buffer size ${e} is greater than max ${X} that we can reference with pointers`);let i=this.createBuffer(e);this.buffers=[i];const s=i.callocAs("u32",3);if(s)this.memory=new c(this,{bufferPosition:0,bufferByteOffset:s.byteOffset});else throw new Error("Failed to initialize first byte from buffer");this.memory.data[kt]=e,this.memory.data[Q]=1,this.memory.data[Y]=t?.autoGrowSize??100,this.isClone=!1;for(let o=1;o<(t?.initialBuffers??1);o++)this.buffers.push(this.createBuffer(e))}}addSharedBuffer(t){this.buffers[t.bufferPosition]=new D({buf:t.buffer,skipInitialization:!0})}growBuffer(){const t=this.createBuffer();let e=Atomics.add(this.memory.data,Q,1);return this.buffers[e]=t,this.onGrowBufferHandlers.forEach(i=>i({bufferPosition:e,buffer:t.buf})),t}createBuffer(t){const e=t??this.bufferSize;let i;return"SharedArrayBuffer"in globalThis?i=new SharedArrayBuffer(e):i=new ArrayBuffer(e),new D({buf:i,compact:!1,split:!1})}addOnGrowBufferHandlers(t){this.onGrowBufferHandlers.push(t)}allocUI32(t){t=Math.ceil(t);for(let s=0;s<this.buffers.length;s++){const o=this.buffers[s];if(!o)continue;const a=o.callocAs("u32",t);if(a)return s===this.buffers.length-1&&Atomics.load(this.memory.data,Q)===this.buffers.length&&this.memory.data[Y]<100&&this.memory.data[Y]>0&&o.top/o.end>this.memory.data[Y]/100&&this.growBuffer(),new c(this,{data:a,buffer:o})}if(this.buffers.length>=Z)throw new Error(`Can't initialize a new buffer since it would have a position greater than the max of ${Z}`);let e=this.growBuffer();const i=e.callocAs("u32",t);if(i)return new c(this,{data:i,buffer:e});throw new Error(`Unable to allocate ${t} numbers even after adding a new buffer`)}getSharedAlloc(t){if(this.buffers[t.bufferPosition]!==void 0)return new c(this,t)}get currentUsed(){return this.totalAllocated-this.buffers.reduce((t,e)=>t+e.stats().available,0)}get totalAllocated(){return this.buffers[0].buf.byteLength*this.buffers.length}prettyMemory(){return`${gt(this.currentUsed)} / ${gt(this.totalAllocated)}`}getSharedMemory(){return{buffers:this.buffers.map(t=>t.buf)}}}function gt(r){return ae(r,{binary:!0,minimumFractionDigits:1,maximumFractionDigits:1})}const Et=4,wt=1,_=2;class d{static ALLOCATE_COUNT=Et;memory;firstBlock;uint16Array;onDelete;get length(){return Atomics.load(this.firstBlock.data,_)}get type(){return Atomics.load(this.uint16Array,0)}set type(t){Atomics.store(this.uint16Array,0,t)}get dataLength(){return Math.max(1,Atomics.load(this.uint16Array,1))}set dataLength(t){Atomics.store(this.uint16Array,1,t)}constructor(t,e){if(this.memory=t,e&&"firstBlock"in e)this.firstBlock=new c(t,e.firstBlock),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+(_+1)*Uint32Array.BYTES_PER_ELEMENT,2);else{e&&e.initWithBlock?this.firstBlock=new c(t,e.initWithBlock):this.firstBlock=t.allocUI32(Et),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+(_+1)*Uint32Array.BYTES_PER_ELEMENT,2);const i=e?.type??Uint32Array;i===Uint32Array?this.type=0:i===Int32Array?this.type=1:i===Float32Array&&(this.type=2),this.dataLength=e?.dataLength??1}}insert(t){typeof t=="number"&&(t=[t]);let e=this.dataLength;if(t.length>e)throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);let i=this.memory.allocUI32(wt+e),s=this.getDataBlock(i.data),o=i.pointer;for(let n=0;n<t.length;n++)s instanceof Int32Array||s instanceof Uint32Array?Atomics.store(s,n,t[n]):s[n]=t[n];let a,l=!1;for(;!l;)a=w(this.firstBlock.data,1),l=U(this.firstBlock.data,1,o,a);if(a){let{bufferPosition:n,bufferByteOffset:h}=b(a),I=new Uint32Array(this.memory.buffers[n].buf,h,1);C(I,0,o)}else C(this.firstBlock.data,0,o);Atomics.add(this.firstBlock.data,_,1)}deleteMatch(t){for(let{data:e,index:i,deleteCurrent:s}of this)if(t(e,i))return s(),!0;return!1}deleteIndex(t){return t>=this.length||t<0?!1:this.deleteMatch((e,i)=>i===t)}deleteValue(t){return typeof t=="number"?this.deleteMatch(e=>e[0]===t):this.deleteMatch(e=>{if(e.length!==t.length)return!1;for(let i=0;i<e.length;i++)if(e[i]!==t[i])return!1;return!0})}clear(){let t,e,i=!1;for(;!i;){if(t=w(this.firstBlock.data,0),e=w(this.firstBlock.data,1),!e)return;i=U(this.firstBlock.data,1,0,e)}if(!t)return;U(this.firstBlock.data,0,0,t);let s=0,o=t;for(;o;){let{bufferPosition:a,bufferByteOffset:l}=b(o),n=this.memory.buffers[a];if(!n)break;let h=new Uint32Array(n.buf,l,2);o=w(h,0),s++,this.onDelete&&this.onDelete(this.getDataBlock(h)),n.free(h.byteOffset)}Atomics.sub(this.firstBlock.data,_,s)}*[Symbol.iterator](){let t=0,{bufferPosition:e,bufferByteOffset:i}=u(this.firstBlock.data,0),s=this.firstBlock.data,o=0,a=0;for(;i;){let l=this.memory.buffers[e];if(!l)return;let n=new Uint32Array(l.buf,i,2),h=this.getDataBlock(n),I=e,ft=i;({bufferPosition:e,bufferByteOffset:i}=u(n,0));let G=!0;yield{data:h,index:t,deleteCurrent:()=>{y(s,0,e,i),i||y(this.firstBlock.data,1,o,a),this.onDelete&&this.onDelete(this.getDataBlock(n)),l.free(n.byteOffset),Atomics.sub(this.firstBlock.data,_,1),G=!1}},G&&(s=n,o=I,a=ft,t++)}}forEach(t){for(let e of this)t(e.data)}getSharedMemory(){return{firstBlock:this.firstBlock.getSharedMemory()}}getDataBlock(t){const e=t.byteOffset+wt*t.BYTES_PER_ELEMENT;switch(this.type){case 1:return new Int32Array(t.buffer,e,this.dataLength);case 0:return new Uint32Array(t.buffer,e,this.dataLength);case 2:return new Float32Array(t.buffer,e,this.dataLength);default:throw new Error(`Unknown data block type ${this.type}`)}}free(){let{bufferPosition:t,bufferByteOffset:e}=u(this.firstBlock.data,0);for(;e;){let i=new c(this.memory,{bufferPosition:t,bufferByteOffset:e});({bufferPosition:t,bufferByteOffset:e}=u(i.data,0)),this.onDelete&&this.onDelete(this.getDataBlock(i.data)),i.free()}this.firstBlock.free()}}const Tt=10;class tt{static ALLOCATE_COUNT=4;memory;pointerMemory;lock;get hashMemory(){return new c(this.memory,u(this.pointerMemory.data,0))}get length(){return Atomics.load(this.pointerMemory.data,2)}get maxHash(){return Atomics.load(this.pointerMemory.data,3)}constructor(t,e){if(this.memory=t,e)this.pointerMemory=new c(t,e.firstBlock);else{this.pointerMemory=t.allocUI32(tt.ALLOCATE_COUNT);let i=t.allocUI32(Tt);y(this.pointerMemory.data,0,i.bufferPosition,i.bufferByteOffset),Atomics.store(this.pointerMemory.data,3,Tt)}this.lock=new Int32Array(this.pointerMemory.data.buffer,this.pointerMemory.bufferByteOffset+Uint32Array.BYTES_PER_ELEMENT,1)}set(t,e){this.length>=this.maxHash*2&&this.growHashTable();let i=z(t);this.setHashKey(this.hashMemory,this.maxHash,i,e)&&Atomics.add(this.pointerMemory.data,2,1)}setHashKey(t,e,i,s){let o=this.hash(i,e),a,l=u(t.data,o);if(l.bufferByteOffset===0){a=new d(this.memory,{dataLength:2});let h=a.getSharedMemory();y(t.data,o,h.firstBlock.bufferPosition,h.firstBlock.bufferByteOffset)}else a=new d(this.memory,{firstBlock:l});let n=!0;return a.deleteValue(i)&&(n=!1),a.insert([i,s]),n}get(t){let e=z(t),i=this.hash(e,this.maxHash),s=u(this.hashMemory.data,i);if(s.bufferByteOffset===0)return;let o=new d(this.memory,{firstBlock:s});for(let{data:a}of o)if(a[0]===e)return a[1]}has(t){let e=z(t),i=this.hash(e,this.maxHash),s=u(this.hashMemory.data,i);if(s.bufferByteOffset===0)return!1;let o=new d(this.memory,{firstBlock:s});for(let{data:a}of o)if(a[0]===e)return!0;return!1}delete(t){let e=z(t),i=this.hash(e,this.maxHash),s=u(this.hashMemory.data,i);if(s.bufferByteOffset===0)return!1;let o=new d(this.memory,{firstBlock:s});for(let{data:a,deleteCurrent:l}of o)if(a[0]===e)return l(),Atomics.sub(this.pointerMemory.data,2,1),!0;return!1}growHashTable(){let t=this.maxHash,e=t*2,i=this.memory.allocUI32(e),s=this.hashMemory;for(let o=0;o<t;o++){let a=u(s.data,o);if(a.bufferByteOffset===0)continue;let l=new d(this.memory,{firstBlock:a});for(let{data:n}of l)this.setHashKey(i,e,n[0],n[1])}y(this.pointerMemory.data,0,i.bufferPosition,i.bufferByteOffset),Atomics.store(this.pointerMemory.data,3,e)}hash(t,e){return t%e}free(){for(let t=0;t<this.maxHash;t++){let e=u(this.hashMemory.data,t);if(e.bufferByteOffset===0)continue;new d(this.memory,{firstBlock:e}).free()}this.hashMemory.free(),this.pointerMemory.free()}getSharedMemory(){return{firstBlock:this.pointerMemory.getSharedMemory()}}}function z(r){return typeof r=="number"?r:typeof r=="string"?le(r):r}function le(r){let t=r.length,e=17^t,i=0,s;for(;t>=4;)s=r.charCodeAt(i)&255|(r.charCodeAt(++i)&255)<<8|(r.charCodeAt(++i)&255)<<16|(r.charCodeAt(++i)&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,++i;switch(t){case 3:e^=(r.charCodeAt(i+2)&255)<<16;case 2:e^=(r.charCodeAt(i+1)&255)<<8;case 1:e^=r.charCodeAt(i)&255,e=(e&65535)*1540483477+(((e>>>16)*1540483477&65535)<<16)}return e^=e>>>13,e=(e&65535)*1540483477+(((e>>>16)*1540483477&65535)<<16),e^=e>>>15,e=e>>>0,e}class he{memory;list;constructor(t,e){this.memory=t,e?this.list=new d(t,e):this.list=new d(t)}get length(){return this.list.length}insert(t){this.list.insert(g(t.memory.bufferPosition,t.memory.bufferByteOffset))}delete(t){return this.list.deleteValue(g(t.memory.bufferPosition,t.memory.bufferByteOffset))}*[Symbol.iterator](){let t=this.list[Symbol.iterator]();for(let{data:e}of t){let{bufferPosition:i,bufferByteOffset:s}=u(e,0),o=new c(this.memory,{bufferPosition:i,bufferByteOffset:s});yield this.createItem(o)}}forEach(t){for(let e of this)t(e)}find(t){for(let e of this)if(t(e))return e}getSharedMemory(){return this.list.getSharedMemory()}free(){for(let t of this)"free"in t&&typeof t.free=="function"?t.free():t.memory.free();this.list.free()}}const _t={1:Uint8Array,2:Uint16Array},H=0,St=1,Lt=2,pt=3;class et{static ALLOCATE_COUNT=4;memory;allocatedMemory;lock;cachedPointer;cachedString;constructor(t,e){this.memory=t,typeof e=="string"?(this.allocatedMemory=this.memory.allocUI32(et.ALLOCATE_COUNT),this.lock=new Int32Array(this.allocatedMemory.data.buffer,this.allocatedMemory.bufferByteOffset+pt*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+pt*this.allocatedMemory.data.BYTES_PER_ELEMENT),"value"in e&&this.updateString(e.value))}updateString(t){let e=this.createString(t);O(this.lock),C(this.allocatedMemory.data,H,e.pointer),Atomics.store(this.allocatedMemory.data,St,t.length),Atomics.store(this.allocatedMemory.data,Lt,e.charType),A(this.lock),this.cachedPointer=e.pointer,this.cachedString=t}createString(t){if(t==="")return{pointer:0,charType:1};let e=[];for(let n=0;n<t.length;n++)e.push(t.charCodeAt(n));let s=Math.max(...e)>255?2:1,o=_t[s],a=this.memory.allocUI32(Math.ceil(t.length/(4/o.BYTES_PER_ELEMENT))),l=new o(a.data.buffer,a.data.byteOffset,t.length);for(let n=0;n<t.length;n++)l[n]=t.charCodeAt(n);return{pointer:a.pointer,charType:s}}get value(){let t=w(this.allocatedMemory.data,H);if(this.cachedPointer===t&&this.cachedString!==void 0)return this.cachedString;if(t===0)return"";let{bufferPosition:e,bufferByteOffset:i}=b(t);O(this.lock);let s=Atomics.load(this.allocatedMemory.data,Lt),o=_t[s],a=Atomics.load(this.allocatedMemory.data,St),l=new o(this.memory.buffers[e].buf,i,a),n=String.fromCharCode.apply(null,l);return A(this.lock),this.cachedPointer=t,this.cachedString=n,n}set value(t){let{bufferPosition:e,bufferByteOffset:i}=u(this.allocatedMemory.data,H);this.updateString(t),i&&this.memory.buffers[e].free(i)}getSharedMemory(){return this.allocatedMemory.getSharedMemory()}get pointer(){return this.allocatedMemory.pointer}free(){let{bufferPosition:t,bufferByteOffset:e}=u(this.allocatedMemory.data,H);e&&this.memory.buffers[t].free(e),this.allocatedMemory.free()}}const S=0,E=1,Ot=2,Mt=3,it=4;class m{static ALLOCATE_COUNT=4;memory;firstBlock;uint16Array;get length(){return Atomics.load(this.firstBlock.data,E)}get type(){return this.uint16Array[0]}set type(t){Atomics.store(this.uint16Array,0,t)}get dataLength(){return Math.max(1,this.uint16Array[1])}set dataLength(t){Atomics.store(this.uint16Array,1,t)}get bufferLength(){return Atomics.load(this.firstBlock.data,Ot)}set bufferLength(t){Atomics.store(this.firstBlock.data,Ot,t)}get pointer(){return this.firstBlock.pointer}cachedFullDataBlock;cachedPointer;constructor(t,e){if(this.memory=t,e&&"firstBlock"in e){if(this.firstBlock=new c(t,e.firstBlock),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+Mt*Uint32Array.BYTES_PER_ELEMENT,2),"type"in e||"dataLength"in e){let i=t.allocUI32(it*(e.dataLength??1));y(this.firstBlock.data,S,i.bufferPosition,i.bufferByteOffset),this.bufferLength=it,this.dataLength=e.dataLength??1}if("type"in e){const i=e?.type??Uint32Array;i===Uint32Array?this.type=0:i===Int32Array?this.type=1:i===Float32Array&&(this.type=2)}}else{this.firstBlock=t.allocUI32(m.ALLOCATE_COUNT),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+Mt*Uint32Array.BYTES_PER_ELEMENT,2);let i=e?.dataLength??1,s=e?.bufferLength??it,o=t.allocUI32(s*i);y(this.firstBlock.data,S,o.bufferPosition,o.bufferByteOffset),this.bufferLength=s;const a=e?.type??Uint32Array;a===Uint32Array?this.type=0:a===Int32Array?this.type=1:a===Float32Array&&(this.type=2),this.dataLength=i}this.cachedPointer=this.firstBlock.data[0],this.cachedFullDataBlock=this.getFullDataBlock()}at(t){let e=this.length;if(t>=e||t<0)throw new Error(`${t} is out of bounds ${e}`);let i=this.getFullDataBlock();return this.getDataBlock(i,t)}get(t,e=0){if(e>=this.dataLength)throw new Error(`${e} is out of dataLength bounds ${this.dataLength}`);if(t>=this.length||t<0)throw new Error(`${t} is out of bounds ${this.length}`);return this.getFullDataBlock()[t*this.dataLength+e]}push(t){typeof t=="number"&&(t=[t]);let e=this.dataLength;if(t.length>e)throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);let i=this.getFullDataBlock(),s=this.length;return i.set(t,e*s),Atomics.add(this.firstBlock.data,E,1)+1>=this.bufferLength&&this.growBuffer(),s}pop(){let t=Atomics.sub(this.firstBlock.data,E,E),e=this.getFullDataBlock();return this.getDataBlock(e,t-1)}deleteIndex(t){let e=this.length;if(t>=e||t<0)throw new Error(`${t} is out of bounds ${e}`);let i=this.dataLength,s=this.getFullDataBlock();for(let o=t;o<e;o++)for(let a=0;a<i;a++)s[o*i+a]=s[(o+1)*i+a];Atomics.sub(this.firstBlock.data,E,E)}clear(){this.firstBlock.data[E]=0}*[Symbol.iterator](){let t=this.getFullDataBlock();for(let e=0;e<this.length;e++)yield this.getDataBlock(t,e)}getFullDataBlock(){let t=Atomics.load(this.firstBlock.data,S);if(this.cachedPointer===t&&this.cachedFullDataBlock)return this.cachedFullDataBlock;let e=b(t),i=new c(this.memory,e),s;switch(this.type){case 1:s=new Int32Array(i.data.buffer,i.bufferByteOffset,this.dataLength*this.bufferLength);break;case 0:s=new Uint32Array(i.data.buffer,i.bufferByteOffset,this.dataLength*this.bufferLength);break;case 2:s=new Float32Array(i.data.buffer,i.bufferByteOffset,this.dataLength*this.bufferLength);break;default:throw new Error(`Unknown data block type ${this.type}`)}return this.cachedPointer=t,this.cachedFullDataBlock=s,s}getDataBlock(t,e){const i=e*this.dataLength;return t.subarray(i,i+this.dataLength)}growBuffer(){let e=this.bufferLength*2,i=this.dataLength,s=u(this.firstBlock.data,S),o=new c(this.memory,s),a=this.getFullDataBlock(),l=this.memory.allocUI32(e*i),n;switch(this.type){case 1:n=new Int32Array(l.data.buffer,l.bufferByteOffset,i*this.bufferLength);break;case 0:n=new Uint32Array(l.data.buffer,l.bufferByteOffset,i*this.bufferLength);break;case 2:n=new Float32Array(l.data.buffer,l.bufferByteOffset,i*this.bufferLength);break;default:throw new Error(`Unknown data block type ${this.type}`)}n.set(a),y(this.firstBlock.data,S,l.bufferPosition,l.bufferByteOffset),this.bufferLength=e,o.free()}free(){let t=u(this.firstBlock.data,S);new c(this.memory,t).free(),this.firstBlock.free()}getSharedMemory(){return{firstBlock:this.firstBlock.getSharedMemory()}}}class ce{static ALLOCATE_COUNT=d.ALLOCATE_COUNT;heap;list;cache=new Map;constructor(t,e){e?this.list=new d(t,e):this.list=new d(t),this.heap=t,this.list.onDelete=i=>{let s=Atomics.load(i,0);if(s){let o=this.cache.get(s);o||(o=this.initItem(s)),o&&(o.free(),this.cache.delete(s))}}}get length(){return this.list.length}clear(){this.list.clear(),this.cache.clear()}insert(t){this.list.insert(t.pointer),this.cache.set(t.pointer,t)}delete(t){return this.cache.delete(t.pointer),this.list.deleteValue(t.pointer)}getByPointer(t){let e=this.cache.get(t);return e||(e=this.initItem(t),e&&this.cache.set(t,e)),e}*[Symbol.iterator](){let t=this.list[Symbol.iterator]();for(let{data:e,deleteCurrent:i}of t){let s=Atomics.load(e,0);if(!s)continue;let o=this.cache.get(s);o||(o=this.initItem(s),o&&this.cache.set(s,o)),o&&(yield{item:o,deleteCurrent:i})}}forEach(t,e){for(let{item:i}of this)(!e||e(i))&&t(i)}find(t){for(let{item:e}of this)if(t(e))return e}filter(t){let e=[];for(let{item:i}of this)t(i)&&e.push(i);return e}map(t){const e=[];for(let{item:i}of this)e.push(t(i));return e}getSharedMemory(){return this.list.getSharedMemory()}free(){this.list.free(),this.cache.clear()}}const x=0,Pt=1,It=2,rt=3,Nt=rt+m.ALLOCATE_COUNT;class st{static ALLOCATE_COUNT=3+m.ALLOCATE_COUNT*2;memory;firstBlock;uint16Array;pointerVector;recycleVector;cachedFullDataBlock={};get length(){return Atomics.load(this.firstBlock.data,x)-this.recycleVector.length}get maxChunkSize(){return this.firstBlock.data[It]}set maxChunkSize(t){Atomics.store(this.firstBlock.data,It,t)}get type(){return this.uint16Array[0]}set type(t){Atomics.store(this.uint16Array,0,t)}get dataLength(){return Math.max(1,this.uint16Array[1])}set dataLength(t){Atomics.store(this.uint16Array,1,t)}get bufferLength(){return this.maxChunkSize*this.pointerVector.length}constructor(t,e){if(this.memory=t,e&&"firstBlock"in e)this.firstBlock=new c(t,e.firstBlock),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+Pt*Uint32Array.BYTES_PER_ELEMENT,2),this.pointerVector=new m(t,{firstBlock:{bufferPosition:this.firstBlock.bufferPosition,bufferByteOffset:this.firstBlock.bufferByteOffset+rt*Uint32Array.BYTES_PER_ELEMENT}}),this.recycleVector=new m(t,{firstBlock:{bufferPosition:this.firstBlock.bufferPosition,bufferByteOffset:this.firstBlock.bufferByteOffset+Nt*Uint32Array.BYTES_PER_ELEMENT}});else{this.firstBlock=t.allocUI32(st.ALLOCATE_COUNT),this.uint16Array=new Uint16Array(this.firstBlock.data.buffer,this.firstBlock.bufferByteOffset+Pt*Uint32Array.BYTES_PER_ELEMENT,2);let i=e?.dataLength??1,s=e?.maxChunkSize??100;this.pointerVector=new m(t,{type:Uint32Array,firstBlock:{bufferPosition:this.firstBlock.bufferPosition,bufferByteOffset:this.firstBlock.bufferByteOffset+rt*Uint32Array.BYTES_PER_ELEMENT}}),this.recycleVector=new m(t,{type:Uint32Array,firstBlock:{bufferPosition:this.firstBlock.bufferPosition,bufferByteOffset:this.firstBlock.bufferByteOffset+Nt*Uint32Array.BYTES_PER_ELEMENT}});let o=t.allocUI32(s*i);this.pointerVector.push(o.pointer);const a=e?.type??Uint32Array;a===Uint32Array?this.type=0:a===Int32Array?this.type=1:a===Float32Array&&(this.type=2),this.dataLength=i,this.maxChunkSize=s}}at(t){let e=this.getFullDataBlock(t);return this.getDataBlock(e,t%this.maxChunkSize)}get(t,e=0){const i=this.dataLength;if(e>=i)throw new Error(`${e} is out of dataLength bounds ${i}`);return this.getFullDataBlock(t)[t%this.maxChunkSize*i+e]}push(t){typeof t=="number"&&(t=[t]);let e=this.dataLength;if(t.length>e)throw new Error(`Can't insert ${t.length} array into shared list of ${e} dataLength`);let i;this.recycleVector.length?i=this.recycleVector.pop()[0]:i=Atomics.add(this.firstBlock.data,x,1);let s=this.getFullDataBlock(i),o=i%this.maxChunkSize;return s.set(t,e*o),i}deleteIndex(t){this.recycleVector.push(t)}clear(){this.firstBlock.data[x]=0}*[Symbol.iterator](){let t=[...this.recycleVector].reduce((s,o)=>(s.push(...o),s),[]),e=this.getFullDataBlock(0),i=0;for(let s=0;s<Atomics.load(this.firstBlock.data,x);s++)if(!t.includes(s)){let o=Math.floor(s/this.maxChunkSize);o!==i&&(e=this.getFullDataBlock(s),i=o),yield this.getDataBlock(e,s%this.maxChunkSize)}}getFullDataBlock(t){let e=Math.floor(t/this.maxChunkSize),i=this.cachedFullDataBlock[e];if(i)return i;if(e>=this.pointerVector.length){let a=this.memory.allocUI32(this.maxChunkSize*this.dataLength);this.pointerVector.push(a.pointer)}let s=new c(this.memory,b(this.pointerVector.get(e))),o;switch(this.type){case 1:o=new Int32Array(s.data.buffer,s.bufferByteOffset,this.dataLength*this.maxChunkSize);break;case 0:o=new Uint32Array(s.data.buffer,s.bufferByteOffset,this.dataLength*this.maxChunkSize);break;case 2:o=new Float32Array(s.data.buffer,s.bufferByteOffset,this.dataLength*this.maxChunkSize);break;default:throw new Error(`Unknown data block type ${this.type}`)}return this.cachedFullDataBlock[e]=o,o}getDataBlock(t,e){const i=e*this.dataLength;return t.subarray(i,i+this.dataLength)}free(){this.recycleVector.free();for(let t of this.pointerVector){let e=t[0];new c(this.memory,b(e)).free()}this.pointerVector.free(),this.firstBlock.free()}getSharedMemory(){return{firstBlock:this.firstBlock.getSharedMemory()}}}const Ct=new ArrayBuffer(4),Ut=new Uint32Array(Ct),V=new Uint16Array(Ct);function ue(r,t){return Ft(Atomics.load(r,t))}function de(r,t,e,i){Atomics.store(r,t,Dt(e,i))}function Ft(r){return Ut[0]=r,[V[0],V[1]]}function Dt(r,t){return V[0]=r,V[1]=t,Ut[0]}const Rt=new ArrayBuffer(8),$t=new BigUint64Array(Rt),k=new Uint16Array(Rt);function ye(r,t){return $t[0]=Atomics.load(r,t),[k[0],k[1],k[2],k[3]]}function Be(r,t,e,i,s,o=0){k[0]=e,k[1]=i,k[2]=s,k[3]=o,Atomics.store(r,t,$t[0])}const Yt=new ArrayBuffer(4),zt=new Float32Array(Yt),Ht=new Int32Array(Yt);function ot(r,t){return r instanceof Float32Array&&(r=new Int32Array(r.buffer,r.byteOffset,r.length)),K(Atomics.load(r,t))}function me(r,t,e){r instanceof Float32Array&&(r=new Int32Array(r.buffer,r.byteOffset,r.length)),Atomics.store(r,t,P(e))}function K(r){return Ht[0]=r,zt[0]}function P(r){return zt[0]=r,Ht[0]}function be(r,t,e){return r instanceof Float32Array&&(r=new Int32Array(r.buffer,r.byteOffset,r.length)),K(Atomics.exchange(r,t,P(e)))}function at(r,t,e,i){return r instanceof Float32Array&&(r=new Int32Array(r.buffer,r.byteOffset,r.length)),K(Atomics.compareExchange(r,t,P(e),P(i)))}function Ae(r,t,e,i){let s,o;do s=Atomics.load(r,t),o=Math.min(s+e,i);while(Atomics.compareExchange(r,t,s,o)!==s)}function ke(r,t,e,i){let s,o;do s=Atomics.load(r,t),o=Math.max(s-e,i);while(Atomics.compareExchange(r,t,s,o)!==s)}function ge(r,t,e,i){let s,o;do s=ot(r,t),o=Math.min(s+e,i);while(at(r,t,s,o)!==s)}function Ee(r,t,e,i){let s,o;do s=ot(r,t),o=Math.max(s-e,i);while(at(r,t,s,o)!==s)}const L=0,nt=1,p=2;function we(r,t=0){for(;Atomics.compareExchange(r,t,L,nt)===p;)Atomics.wait(r,t,p);Atomics.add(r,t+1,1)}function Te(r,t=0){let e=Atomics.compareExchange(r,t,L,p);for(;e!==L;)Atomics.wait(r,t,e),e=Atomics.compareExchange(r,t,L,p)}function _e(r,t=0){Atomics.sub(r,t+1,1)-1<=0&&(Atomics.compareExchange(r,t,nt,L)!==nt&&console.warn("We are unlocking when it was not read locked!"),Atomics.notify(r,t))}function Se(r,t=0){Atomics.compareExchange(r,t,p,L)!==p&&console.warn("We are unlocking when it was not write locked!"),Atomics.notify(r,t)}const Le=2;f.AllocatedMemory=c,f.BYTE_OFFSET_BIT_COUNT=20,f.CachedItemList=ce,f.MAX_BYTE_OFFSET_LENGTH=X,f.MAX_POSITION_LENGTH=Z,f.MemoryBuffer=D,f.MemoryHeap=fe,f.POSITION_BIT_COUNT=12,f.READ_WRITE_LOCK_ALLOCATE_COUNT=Le,f.SIMPLE_LOCK_ALLOCATE_COUNT=Vt,f.SharedList=d,f.SharedMap=tt,f.SharedPointerList=he,f.SharedPool=st,f.SharedString=et,f.SharedVector=m,f.addAtomicFloat32=ge,f.addAtomicInt=Ae,f.compareExchangeFloat32=at,f.convert16To32=Dt,f.convert32To16=Ft,f.convertFloat32ToInt32=P,f.convertInt32ToFloat32=K,f.createPointer=g,f.exchangeFloat32=be,f.getPointer=b,f.load16From32=ue,f.load16From64=ye,f.loadFloat32=ot,f.loadPointer=u,f.loadRawPointer=w,f.lock=O,f.readLock=we,f.readUnlock=_e,f.replacePointer=xt,f.replaceRawPointer=U,f.store16In32=de,f.store16In64=Be,f.storeFloat32=me,f.storePointer=y,f.storeRawPointer=C,f.subtractAtomicFloat=Ee,f.subtractAtomicInt=ke,f.unlock=A,f.writeLock=Te,f.writeUnlock=Se,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}));
2
2
  //# sourceMappingURL=shared-memory-objects.umd.cjs.map