!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):(t="undefined"!=typeof globalThis?globalThis:t||self).Flatbush=s()}(this,(function(){"use strict";class t{constructor(){this.ids=[],this.values=[],this.length=0}clear(){this.length=0}push(t,s){let i=this.length++;for(;0>1,h=this.values[e];if(h<=s)break;this.ids[i]=this.ids[e],this.values[i]=h,i=e}this.ids[i]=t,this.values[i]=s}pop(){if(0!==this.length){var t=this.ids[0];if(this.length--,0>1;let t=0;for(;t=i)break;this.ids[t]=e,this.values[t]=n,t=s}this.ids[t]=s,this.values[t]=i}return t}}peek(){if(0!==this.length)return this.ids[0]}peekValue(){if(0!==this.length)return this.values[0]}shrink(){this.ids.length=this.values.length=this.length}}const s=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array];function i(t,s,i){return t>1;s[h]>t?e=h:i=1+h}return s[i]}function h(t,s,i,e,h){var r=t[e],n=(t[e]=t[h],t[h]=r,r=4*h,s[t=4*e]),a=s[1+t],o=s[2+t],l=s[3+t];s[t]=s[r],s[1+t]=s[1+r],s[2+t]=s[2+r],s[3+t]=s[3+r],s[r]=n,s[1+r]=a,s[2+r]=o,s[3+r]=l,t=i[e],i[e]=i[h],i[h]=t}return class r{static from(t,i=0){if(i%8!=0)throw new Error("byteOffset must be 8-byte aligned.");if(!t||void 0===t.byteLength||t.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");var e,[h,n]=new Uint8Array(t,i+0,2);if(251!==h)throw new Error("Data does not appear to be in a Flatbush format.");if(3!=(h=n>>4))throw new Error(`Got v${h} data when expected v3.`);if(h=s[15&n])return[n]=new Uint16Array(t,i+2,1),[e]=new Uint32Array(t,i+4,1),new r(e,n,h,void 0,t,i);throw new Error("Unrecognized array type.")}constructor(i,e=16,h=Float64Array,r=ArrayBuffer,n,a=0){if(void 0===i)throw new Error("Missing required argument: numItems.");if(isNaN(i)||i<=0)throw new Error(`Unexpected numItems value: ${i}.`);this.numItems=+i,this.nodeSize=Math.min(Math.max(+e,2),65535),this.byteOffset=a;let o=i,l=o;for(this._levelBounds=[4*o];o=Math.ceil(o/this.nodeSize),l+=o,this._levelBounds.push(4*l),1!==o;);this.ArrayType=h,this.IndexArrayType=l<16384?Uint16Array:Uint32Array;var d=s.indexOf(this.ArrayType),f=4*l*this.ArrayType.BYTES_PER_ELEMENT;if(d<0)throw new Error(`Unexpected typed array class: ${h}.`);n&&void 0!==n.byteLength&&!n.buffer?(this.data=n,this._boxes=new this.ArrayType(this.data,a+8,4*l),this._indices=new this.IndexArrayType(this.data,a+8+f,l),this._pos=4*l,this.minX=this._boxes[this._pos-4],this.minY=this._boxes[this._pos-3],this.maxX=this._boxes[this._pos-2],this.maxY=this._boxes[this._pos-1]):(this.data=new r(8+f+l*this.IndexArrayType.BYTES_PER_ELEMENT),this._boxes=new this.ArrayType(this.data,8,4*l),this._indices=new this.IndexArrayType(this.data,8+f,l),this._pos=0,this.minX=1/0,this.minY=1/0,this.maxX=-1/0,this.maxY=-1/0,new Uint8Array(this.data,0,2).set([251,48+d]),new Uint16Array(this.data,2,1)[0]=e,new Uint32Array(this.data,4,1)[0]=i),this._queue=new t}add(t,s,i=t,e=s){var h=this._pos>>2,r=this._boxes;return this._indices[h]=h,r[this._pos++]=t,r[this._pos++]=s,r[this._pos++]=i,r[this._pos++]=e,tthis.maxX&&(this.maxX=i),e>this.maxY&&(this.maxY=e),h}finish(){if(this._pos>>2!==this.numItems)throw new Error(`Added ${this._pos>>2} items when expected ${this.numItems}.`);var t=this._boxes;if(this.numItems<=this.nodeSize)t[this._pos++]=this.minX,t[this._pos++]=this.minY,t[this._pos++]=this.maxX,t[this._pos++]=this.maxY;else{var s,i,e,r,n,a,o=this.maxX-this.minX||1,l=this.maxY-this.minY||1,d=new Uint32Array(this.numItems);for(let h=0,_=0;h>1^u,n=(s=65535^(f|p))>>1^(m=65535^u)&(i=f&(65535^p))>>1^s,a=u&s>>1^i>>1^i,u=e=u|m>>1,r=e&(m=r)>>2^m&(e^m)>>2,n^=e&(s=n)>>2^m&a>>2,a^=m&s>>2^(e^m)&(i=a)>>2,u=e=e&(u=e)>>2^m&m>>2,n^=e&(s=n)>>4^(m=r)&a>>4,((1431655765&((r=858993459&((r=252645135&((r=16711935&((r=(a=(a^=m&s>>4^(e^m)&(i=a)>>4)^(r=e&m>>4^m&(e^m)>>4)&n>>8^((u=e=e&(u=e)>>4^m&m>>4)^r)&(i=a)>>8)^a>>1|65535^((e=f^p)|(u=(n^=u&(s=n)>>8^(m=r)&i>>8)^n>>1)))|r<<8))|r<<4))|r<<2))|r<<1))<<1|(e=1431655765&((e=858993459&((e=252645135&((e=16711935&(e|e<<8))|e<<4))|e<<2))|e<<1)))>>>0)}!function t(s,i,e,r,n,a){if(!(Math.floor(r/a)>=Math.floor(n/a))){const o=s[r+n>>1];let l=r-1,d=n+1;for(;;){for(;l++,s[l]o;);if(l>=d)break;h(s,i,e,l,d)}t(s,i,e,r,d,a),t(s,i,e,d+1,n,a)}}(d,t,this._indices,0,this.numItems-1,this.nodeSize);for(let s=0,i=0;s>2]=y,t[this._pos++]=s,t[this._pos++]=e,t[this._pos++]=h,t[this._pos++]=r}}}search(t,s,i,h,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let n=this._boxes.length-4;for(var a=[],o=[];void 0!==n;){var l,d=Math.min(n+4*this.nodeSize,e(n,this._levelBounds));for(let e=n;ethis._boxes[e+2]||s>this._boxes[e+3]||(l=0|this._indices[e>>2],n>=4*this.numItems?a.push(l):void 0!==r&&!r(l)||o.push(l));n=a.pop()}return o}neighbors(t,s,h=1/0,r=1/0,n){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let a=this._boxes.length-4;var o=this._queue,l=[],d=r*r;t:for(;void 0!==a;){var f=Math.min(a+4*this.nodeSize,e(a,this._levelBounds));for(let e=a;e>2],p=i(t,this._boxes[e],this._boxes[e+2]),m=i(s,this._boxes[e+1],this._boxes[e+3]);d<(p=p*p+m*m)||(a>=4*this.numItems?o.push(u<<1,p):void 0!==n&&!n(u)||o.push(1+(u<<1),p))}for(;o.length&&1&o.peek();){if(d>1),l.length===h)break t}a=o.length?o.pop()>>1:void 0}return o.clear(),l}}}));