/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../chunks/_rollupPluginBabelHelpers"],(function(e,t){"use strict";let n=function(){function e(e){this._largestRange=null,this._parent=e,this._updateLargestRange()}var n=e.prototype;return n.rangeCreated=function(e){(!this._largestRange||e.count>this._largestRange.count)&&(this._largestRange=e)},n.rangeResized=function(e,t){e===this._largestRange?e.count<t&&this._updateLargestRange():(!this._largestRange||e.count>this._largestRange.count)&&(this._largestRange=e)},n.findBestRange=function(e){let t=this._parent._freeHead,n=null;for(;null!==t;)t.count>=e&&(!n||t.count-e<n.count-e)&&(n=t),t=t.next;return n},n.findAdjacentRanges=function(e,t){let n=!0,r=!1,o=null,s=this._parent._freeHead;for(;n&&!r;){const a=null!==o?o.from+o.count:0,u=null!==s?s.from:this._parent._size;e>=a&&e+t<=u?(n=!1,r=!0):null!==s?(o=s,s=s.next):n=!1}return[o,s]},n._updateLargestRange=function(){let e=null,t=this._parent._freeHead;for(;null!==t;)(!e||t.count>e.count)&&(e=t),t=t.next;this._largestRange=e},t._createClass(e,[{key:"largestRange",get:function(){return this._largestRange}}]),e}(),r=function(){function e(e,t){this._allocated=0,this._size=e,this._freeHead=e>0?{from:0,count:e,prev:null,next:null}:null,this._bookKeeper=t||new n(this),this._freeHead&&this._bookKeeper.rangeCreated(this._freeHead)}var r=e.prototype;return r.allocate=function(t){const n=this._bookKeeper.findBestRange(t);if(null===n)return-1;const r=n.from,o=n.count;if(n.from+=t,n.count-=t,this._bookKeeper.rangeResized(n,r,o),this._allocated+=t,0===n.count){const t=null!==n.prev?this._freeHead:n.next;e._removeRange(n),this._freeHead=t}return r},r.free=function(t,n){const[r,o]=this._bookKeeper.findAdjacentRanges(t,n),s={from:t,count:n,prev:r,next:o};if(null!==r&&(r.next=s),null!==o&&(o.prev=s),this._bookKeeper.rangeCreated(s),this._allocated-=n,null!==o&&s.from+s.count===o.from){const t=s.from,n=s.count;e._fuse(s,o),e._removeRange(o),this._bookKeeper.rangeResized(s,t,n),this._bookKeeper.rangeResized(o,void 0,0)}if(null!==r&&r.from+r.count===s.from){const t=r.from,n=r.count;e._fuse(r,s),e._removeRange(s),this._bookKeeper.rangeResized(r,t,n),this._bookKeeper.rangeResized(s,void 0,0)}this._freeHead=null!==s.prev?this._freeHead:s},e._removeRange=function(e){null!==e.prev?null!==e.next?(e.prev.next=e.next,e.next.prev=e.prev):e.prev.next=null:null!==e.next&&(e.next.prev=null)},e._fuse=function(e,t){e.count+=t.count,e.next=t.next,t.from+=t.count,t.count=0,null!==t.next&&(t.next.prev=e)},t._createClass(e,[{key:"fragmentation",get:function(){const e=this._size-this._allocated;if(0===e)return 0;return 1-this._bookKeeper.largestRange.count/e}}]),e}();e.FreeList=r,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
