@daneren2005/shared-memory-objects 0.0.15 → 0.0.16

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