| 1 |
- !function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t="undefined"!=typeof globalThis?globalThis:t||self).KDBush=r()}(this,function(){"use strict";const t=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],r=new Uint32Array(96);class n{static from(r){if(!r||void 0===r.byteLength||r.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");const[e,i]=new Uint8Array(r,0,2);if(219!==e)throw new Error("Data does not appear to be in a KDBush format.");const s=i>>4;if(1!==s)throw new Error(`Got v${s} data when expected v1.`);const o=t[15&i];if(!o)throw new Error("Unrecognized array type.");const[a]=new Uint16Array(r,2,1),[h]=new Uint32Array(r,4,1);return new n(h,a,o,void 0,r)}constructor(r,n=64,e=Float64Array,i=ArrayBuffer,s){if(isNaN(r)||r<0)throw new Error(`Unexpected numItems value: ${r}.`);this.numItems=+r,this.nodeSize=Math.min(Math.max(+n,2),65535),this.ArrayType=e,this.IndexArrayType=r<65536?Uint16Array:Uint32Array;const o=t.indexOf(this.ArrayType),a=2*r*this.ArrayType.BYTES_PER_ELEMENT,h=r*this.IndexArrayType.BYTES_PER_ELEMENT,d=(8-h%8)%8;if(o<0)throw new Error(`Unexpected typed array class: ${e}.`);if(s)this.data=s,this.ids=new this.IndexArrayType(s,8,r),this.coords=new e(s,8+h+d,2*r),this._pos=2*r,this._finished=!0;else{const t=this.data=new i(8+a+h+d);this.ids=new this.IndexArrayType(t,8,r),this.coords=new e(t,8+h+d,2*r),this._pos=0,this._finished=!1,new Uint8Array(t,0,2).set([219,16+o]),new Uint16Array(t,2,1)[0]=n,new Uint32Array(t,4,1)[0]=r}}add(t,r){const n=this._pos>>1;return this.ids[n]=n,this.coords[this._pos++]=t,this.coords[this._pos++]=r,n}finish(){const t=this._pos>>1;if(t!==this.numItems)throw new Error(`Added ${t} items when expected ${this.numItems}.`);return e(this.ids,this.coords,this.nodeSize,0,this.numItems-1,0),this._finished=!0,this}range(t,n,e,i){if(!this._finished)throw new Error("Data not yet indexed - call index.finish().");const{ids:s,coords:o,nodeSize:a}=this;r[0]=0,r[1]=s.length-1,r[2]=0;let h=3;const d=[];for(;h>0;){const f=r[--h],c=r[--h],y=r[--h];if(c-y<=a){for(let r=y;r<=c;r++){const a=o[2*r],h=o[2*r+1];a>=t&&a<=e&&h>=n&&h<=i&&d.push(s[r])}continue}const u=y+c>>1,w=o[2*u],p=o[2*u+1];w>=t&&w<=e&&p>=n&&p<=i&&d.push(s[u]),(0===f?t<=w:n<=p)&&(r[h++]=y,r[h++]=u-1,r[h++]=1-f),(0===f?e>=w:i>=p)&&(r[h++]=u+1,r[h++]=c,r[h++]=1-f)}return d}within(t,r,n){const e=[];return this.withinInto(t,r,n,e),e}withinInto(t,n,e,i){if(!this._finished)throw new Error("Data not yet indexed - call index.finish().");const{ids:s,coords:o,nodeSize:h}=this;r[0]=0,r[1]=s.length-1,r[2]=0;let d=3,f=0;const c=e*e;for(;d>0;){const y=r[--d],u=r[--d],w=r[--d];if(u-w<=h){for(let r=w;r<=u;r++)a(o[2*r],o[2*r+1],t,n)<=c&&(i[f++]=s[r]);continue}const p=w+u>>1,A=o[2*p],l=o[2*p+1];a(A,l,t,n)<=c&&(i[f++]=s[p]),(0===y?t-e<=A:n-e<=l)&&(r[d++]=w,r[d++]=p-1,r[d++]=1-y),(0===y?t+e>=A:n+e>=l)&&(r[d++]=p+1,r[d++]=u,r[d++]=1-y)}return f}}function e(t,r,n,s,o,a){if(o-s<=n)return;const h=s+o>>1;i(t,r,h,s,o,a),e(t,r,n,s,h-1,1-a),e(t,r,n,h+1,o,1-a)}function i(t,r,n,e,o,a){for(;o>e;){if(o-e>600){const s=o-e+1,h=n-e+1,d=Math.log(s),f=.5*Math.exp(2*d/3),c=.5*Math.sqrt(d*f*(s-f)/s)*(h-s/2<0?-1:1);i(t,r,n,Math.max(e,Math.floor(n-h*f/s+c)),Math.min(o,Math.floor(n+(s-h)*f/s+c)),a)}const h=r[2*n+a];let d=e,f=o;for(s(t,r,e,n),r[2*o+a]>h&&s(t,r,e,o);d<f;){for(s(t,r,d,f),d++,f--;r[2*d+a]<h;)d++;for(;r[2*f+a]>h;)f--}r[2*e+a]===h?s(t,r,e,f):(f++,s(t,r,f,o)),f<=n&&(e=f+1),n<=f&&(o=f-1)}}function s(t,r,n,e){o(t,n,e),o(r,2*n,2*e),o(r,2*n+1,2*e+1)}function o(t,r,n){const e=t[r];t[r]=t[n],t[n]=e}function a(t,r,n,e){const i=t-n,s=r-e;return i*i+s*s}return n});
|