|
export default function(d) { |
|
const x = +this._x.call(null, d), |
|
y = +this._y.call(null, d); |
|
return add(this.cover(x, y), x, y, d); |
|
} |
|
|
|
function add(tree, x, y, d) { |
|
if (isNaN(x) || isNaN(y)) return tree; |
|
|
|
var parent, |
|
node = tree._root, |
|
leaf = {data: d}, |
|
x0 = tree._x0, |
|
y0 = tree._y0, |
|
x1 = tree._x1, |
|
y1 = tree._y1, |
|
xm, |
|
ym, |
|
xp, |
|
yp, |
|
right, |
|
bottom, |
|
i, |
|
j; |
|
|
|
|
|
if (!node) return tree._root = leaf, tree; |
|
|
|
|
|
while (node.length) { |
|
if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm; |
|
if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym; |
|
if (parent = node, !(node = node[i = bottom << 1 | right])) return parent[i] = leaf, tree; |
|
} |
|
|
|
|
|
xp = +tree._x.call(null, node.data); |
|
yp = +tree._y.call(null, node.data); |
|
if (x === xp && y === yp) return leaf.next = node, parent ? parent[i] = leaf : tree._root = leaf, tree; |
|
|
|
|
|
do { |
|
parent = parent ? parent[i] = new Array(4) : tree._root = new Array(4); |
|
if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm; |
|
if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym; |
|
} while ((i = bottom << 1 | right) === (j = (yp >= ym) << 1 | (xp >= xm))); |
|
return parent[j] = node, parent[i] = leaf, tree; |
|
} |
|
|
|
export function addAll(data) { |
|
var d, i, n = data.length, |
|
x, |
|
y, |
|
xz = new Array(n), |
|
yz = new Array(n), |
|
x0 = Infinity, |
|
y0 = Infinity, |
|
x1 = -Infinity, |
|
y1 = -Infinity; |
|
|
|
|
|
for (i = 0; i < n; ++i) { |
|
if (isNaN(x = +this._x.call(null, d = data[i])) || isNaN(y = +this._y.call(null, d))) continue; |
|
xz[i] = x; |
|
yz[i] = y; |
|
if (x < x0) x0 = x; |
|
if (x > x1) x1 = x; |
|
if (y < y0) y0 = y; |
|
if (y > y1) y1 = y; |
|
} |
|
|
|
|
|
if (x0 > x1 || y0 > y1) return this; |
|
|
|
|
|
this.cover(x0, y0).cover(x1, y1); |
|
|
|
|
|
for (i = 0; i < n; ++i) { |
|
add(this, xz[i], yz[i], data[i]); |
|
} |
|
|
|
return this; |
|
} |
|
|