diff --git a/dist/kd-tree.mjs b/dist/kd-tree.mjs index 34bba70..5bdeafd 100644 --- a/dist/kd-tree.mjs +++ b/dist/kd-tree.mjs @@ -1,4 +1,4 @@ -"use strict";let a=class{left=null;right=null;obj;parent;dimension;constructor(t,e,i){let l=this;l.obj=t,l.parent=i,l.dimension=e}};function o(u){this.content=[],this.scoreFunction=u}o.prototype={push:function(u){this.content.push(u),this.bubbleUp(this.content.length-1)},pop:function(){let u=this.content[0],t=this.content.pop();return this.content.length>0&&(this.content[0]=t,this.sinkDown(0)),u},peek:function(){return this.content[0]},remove:function(u){for(let t=this.content.length,e=0;e0;){let e=Math.floor((u+1)/2)-1,i=this.content[e];if(this.scoreFunction(t)h[l.#t[r]]-g[l.#t[r]]);let n=t.length>>1,f=new a(t[n],r,i);return f.left=l.#l(t.slice(0,n),e+1,f),f.right=l.#l(t.slice(n+1),e+1,f),f}#r(t){let e=this;t.left&&(t.left.parent=t,e.#r(t.left)),t.right&&(t.right.parent=t,e.#r(t.right))}#a(t){this.root=t,this.#r(this.root)}#n(t,e,i){if(t===null)return e;let l=dimensions[t.dimension];return i[l]l&&i.pop()}#h(t,e,i,l){let r=this,n=r.#t[t.dimension],f=r.#i(e,t.obj),h=[];for(let s=0;s-n[1]);if(i)for(let n=0;n0&&(this.content[0]=t,this.sinkDown(0)),h},peek:function(){return this.content[0]},remove:function(h){for(let t=this.content.length,e=0;e0;){let e=Math.floor((h+1)/2)-1,i=this.content[e];if(this.scoreFunction(t)s[l.#t[n]]-c[l.#t[n]]);let r=t.length>>1,u=new g(t[r],n,i);return u.left=l.#l(t.slice(0,r),e+1,u),u.right=l.#l(t.slice(r+1),e+1,u),u}#n(t){let e=this;t.left&&(t.left.parent=t,e.#n(t.left)),t.right&&(t.right.parent=t,e.#n(t.right))}#g(t){this.root=t,this.#n(this.root)}#r(t,e,i){if(t===null)return e;let l=dimensions[t.dimension];return i[l]l&&i.pop()}#s(t,e,i,l){let n=this,r=n.#t[t.dimension],u=n.#i(e,t.obj),s=[];for(let f=0;f-r[1]);if(i)for(let r=0;r maxNodes) { - bestNodes.pop(); - } - } - - for (i = 0; i < dimensions.length; i += 1) { - if (i === node.dimension) { - linearPoint[dimensions[i]] = point[dimensions[i]]; - } else { - linearPoint[dimensions[i]] = node.obj[dimensions[i]]; - } - } - - linearDistance = metric(linearPoint, node.obj); - - if (node.right === null && node.left === null) { - if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) { - saveNode(node, ownDistance); - } - return; - } - - if (node.right === null) { - bestChild = node.left; - } else if (node.left === null) { - bestChild = node.right; - } else { - if (point[dimension] < node.obj[dimension]) { - bestChild = node.left; - } else { - bestChild = node.right; - } - } - - nearestSearch(bestChild); - - if (bestNodes.size() < maxNodes || ownDistance < bestNodes.peek()[1]) { - saveNode(node, ownDistance); - } - - if (bestNodes.size() < maxNodes || Math.abs(linearDistance) < bestNodes.peek()[1]) { - if (bestChild === node.left) { - otherChild = node.right; - } else { - otherChild = node.left; - } - if (otherChild !== null) { - nearestSearch(otherChild); - } - } - } - - if (maxDistance) { - for (i = 0; i < maxNodes; i += 1) { - bestNodes.push([null, maxDistance]); - } - } - - if(self.root) - nearestSearch(self.root); - - result = []; - - for (i = 0; i < Math.min(maxNodes, bestNodes.content.length); i += 1) { - if (bestNodes.content[i][0]) { - result.push([bestNodes.content[i][0].obj, bestNodes.content[i][1]]); - } - } - return result; - }; - - this.balanceFactor = function () { - function height(node) { - if (node === null) { - return 0; - } - return Math.max(height(node.left), height(node.right)) + 1; - } - - function count(node) { - if (node === null) { - return 0; - } - return count(node.left) + count(node.right) + 1; - } - - return height(self.root) / (Math.log(count(self.root)) / Math.log(2)); - }; -} - export { KDTree, BinaryHeap as KDTreeBinaryHeap