/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{nextHighestPowerOfTwo as t}from"../../../../core/mathUtils.js";class s{constructor(t,s=0){this._capacity=0,this._count=0,this._emptySlots=[],this._emptySlotCount=0,this._id2slot=new Map,this._slot2id=[],this.layout=t,this._resize(s)}prepareAllocate(t){const s=this._count+t;s>this._capacity&&this._resize(s)}allocate(t){this._count>=this._capacity&&this._resize(this._count+1);const s=this._emptySlotCount>0?this._emptySlots[--this._emptySlotCount]:this._count;return this._id2slot.set(t,s),this._slot2id[s]=t,this._count++,s}prepareFree(t){this._emptySlots.length+=t}free(t){const s=this._id2slot.get(t);null!=s&&(this._emptySlots.length<=this._emptySlotCount&&(this._emptySlots.length=this._emptySlotCount+1),this._emptySlots[this._emptySlotCount++]=s,this._id2slot.delete(t),this._slot2id[s]=null,this._count--)}getCount(){return this._count}getSlot(t){return this._id2slot.get(t)}hasSlot(t){return this._id2slot.has(t)}getBuffer(){return this._buffer}compact(){if(this._emptySlotCount>0){const t=this._emptySlots;let s=this._emptySlotCount,i=this._count+s;for(t.length=s,t.sort((t,s)=>t-s);s>0;){if(t[s-1]!==i-1){const e=i-1,o=t[s-1];this._buffer.copyFrom(this._buffer,e,o,1);const h=this._slot2id[e];this._slot2id[e]=null,this._slot2id[o]=h,this._id2slot.set(h,o)}s--,i--}}this._emptySlotCount=0,this._emptySlots.length=0,this._resize(this._count)}_resize(s){const i=this._capacity;if((s=Math.max(0,s))>0&&(s=t(s)),s>i){const t=this.layout.createBuffer(s);this._buffer&&t.copyFrom(this._buffer,0,0,i),this._buffer=t}else if(s<i){const t=this.layout.createBuffer(s);t.copyFrom(this._buffer,0,0,s),this._buffer=t}this._buffer||(this._buffer=this.layout.createBuffer(0)),this._capacity=s,this._slot2id.length=s}}export{s as default};
