/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import h from"../webgl/Rect.js";class e{constructor(e,t){this._width=0,this._height=0,this._free=[],this._width=e,this._height=t,this._free.push(new h(0,0,e,t))}get width(){return this._width}get height(){return this._height}allocate(e,t){if(e>this._width||t>this._height)return new h;let i=null,s=-1;for(let h=0;h<this._free.length;++h){const w=this._free[h];e<=w.width&&t<=w.height&&(null===i||w.y<=i.y&&w.x<=i.x)&&(i=w,s=h)}return null===i?new h:(this._free.splice(s,1),i.width<i.height?(i.width>e&&this._free.push(new h(i.x+e,i.y,i.width-e,t)),i.height>t&&this._free.push(new h(i.x,i.y+t,i.width,i.height-t))):(i.width>e&&this._free.push(new h(i.x+e,i.y,i.width-e,i.height)),i.height>t&&this._free.push(new h(i.x,i.y+t,e,i.height-t))),new h(i.x,i.y,e,t))}release(h){for(let e=0;e<this._free.length;++e){const t=this._free[e];if(t.y===h.y&&t.height===h.height&&t.x+t.width===h.x)t.width+=h.width;else if(t.x===h.x&&t.width===h.width&&t.y+t.height===h.y)t.height+=h.height;else if(h.y===t.y&&h.height===t.height&&h.x+h.width===t.x)t.x=h.x,t.width+=h.width;else{if(h.x!==t.x||h.width!==t.width||h.y+h.height!==t.y)continue;t.y=h.y,t.height+=h.height}this._free.splice(e,1),this.release(h)}this._free.push(h)}}export{e as default};
