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