UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 1.79 kB
import{isNone as t}from"../../../../../core/maybe.js";class a{constructor(t){if(this.next=null,!Array.isArray(t))return void(this.data=t);this.data=t[0];let e=this;for(let n=1;n<t.length;n++)e.next=new a([t[n]]),e=e.next}*values(){let t=this;for(;t;)yield t.data,t=t.next}forEach(t){let a=this;for(;a;)t(a.data),a=a.next}find(t){return t(this.data)?this:this.next?.find(t)}max(t,a=this){const e=t(this.data)>t(a.data)?this:a;return this.next?this.next.max(t,e):e}remove(t,a=null){return this===t?a?(a.next=this.next,a):this.next:this.next?.remove(t,this)??null}get last(){return this.next?this.next.last:this}}class e{constructor(e){this._head=null,t(e)||(this._head=new a(e))}get head(){return this._head}maxAvailableSpace(){if(t(this._head))return 0;const a=this._head.max((t=>t.end-t.start));return a.data.end-a.data.start}firstFit(a){if(t(this._head))return null;let e=null,n=this._head;for(;n;){const t=n.data.end-n.data.start;if(t===a)return e?e.next=n.next:this._head=n.next,n.data.start;if(t>a){const t=n.data.start;return n.data.start+=a,t}e=n,n=n.next}return null}free(e,n){const r=e+n;if(t(this._head)){const t=new a({start:e,end:r});return void(this._head=t)}if(r<=this._head.data.start){if(r===this._head.data.start)return void(this._head.data.start-=n);const t=new a({start:e,end:r});return t.next=this._head,void(this._head=t)}let s=this._head,d=s.next;for(;d;){if(d.data.start>=r){if(s.data.end===e){if(s.data.end+=n,s.data.end===d.data.start){const t=d.data.end-d.data.start;return s.data.end+=t,void(s.next=d.next)}return}if(d.data.start===r)return void(d.data.start-=n);const t=new a({start:e,end:r});return t.next=s.next,void(s.next=t)}s=d,d=d.next}if(e===s.data.end)return void(s.data.end+=n);const i=new a({start:e,end:r});s.next=i}}export{e as FreeList,a as List};