Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Apr 8, 2016
1 parent 9077b60 commit db37000
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 50 deletions.
3 changes: 2 additions & 1 deletion bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ for (var i = 0; i < 1000000; i++) {
console.time('index ' + points.length + ' points');
var index = kdbush(points,
(p) => p.x,
(p) => p.y, 64, Int32Array);
(p) => p.y,
64, Int32Array);
console.timeEnd('index ' + points.length + ' points');

console.time('10000 small bbox queries');
Expand Down
3 changes: 2 additions & 1 deletion src/kdbush.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var within = require('./within');
module.exports = kdbush;

function kdbush(points, getX, getY, nodeSize, arrayType) {
/* eslint consistent-return: 0, new-cap: 0 */
if (!(this instanceof kdbush)) return new kdbush(points, getX, getY, nodeSize, arrayType);

getX = getX || defaultGetX;
Expand All @@ -23,7 +24,7 @@ function kdbush(points, getX, getY, nodeSize, arrayType) {
coords[2 * i + 1] = getY(points[i]);
}

sort(ids, coords, this.nodeSize);
sort(ids, coords, this.nodeSize, 0, ids.length - 1, 0);
}

kdbush.prototype = {
Expand Down
35 changes: 16 additions & 19 deletions src/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ function range(ids, coords, minX, minY, maxX, maxY, nodeSize) {
var result = [];
var x, y;

nodeSize = nodeSize || 64;

while (stack.length) {
var axis = stack.pop();
var right = stack.pop();
Expand All @@ -20,27 +18,26 @@ function range(ids, coords, minX, minY, maxX, maxY, nodeSize) {
y = coords[2 * i + 1];
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]);
}
continue;
}

} else {
var m = Math.floor((left + right) / 2);
var m = Math.floor((left + right) / 2);

x = coords[2 * m];
y = coords[2 * m + 1];
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]);
x = coords[2 * m];
y = coords[2 * m + 1];
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]);

var nextAxis = (axis + 1) % 2;
var val = axis === 0 ? x : y;
var nextAxis = (axis + 1) % 2;

if ((axis === 0 ? minX : minY) <= val) {
stack.push(left);
stack.push(m);
stack.push(nextAxis);
}
if ((axis === 0 ? maxX : maxY) >= val) {
stack.push(m);
stack.push(right);
stack.push(nextAxis);
}
if (axis === 0 ? minX <= x : minY <= y) {
stack.push(left);
stack.push(m - 1);
stack.push(nextAxis);
}
if (axis === 0 ? maxX >= x : maxY >= y) {
stack.push(m + 1);
stack.push(right);
stack.push(nextAxis);
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
module.exports = sortKD;

function sortKD(ids, coords, nodeSize, left, right, depth) {
nodeSize = nodeSize || 64;
left = left || 0;
right = right || (ids.length - 1);
depth = depth || 0;

if (right - left <= nodeSize) return;

var m = Math.floor((left + right) / 2);
Expand Down
40 changes: 16 additions & 24 deletions src/within.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@ module.exports = within;
function within(ids, coords, qx, qy, r, nodeSize) {
var stack = [0, ids.length - 1, 0];
var result = [];

var minX = qx - r;
var minY = qy - r;
var maxX = qx + r;
var maxY = qy + r;
var r2 = r * r;

nodeSize = nodeSize || 64;

while (stack.length) {
var axis = stack.pop();
var right = stack.pop();
Expand All @@ -23,27 +16,26 @@ function within(ids, coords, qx, qy, r, nodeSize) {
for (var i = left; i <= right; i++) {
if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]);
}
continue;
}

} else {
var m = Math.floor((left + right) / 2);
var m = Math.floor((left + right) / 2);

var x = coords[2 * m];
var y = coords[2 * m + 1];
if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]);
var x = coords[2 * m];
var y = coords[2 * m + 1];
if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]);

var nextAxis = (axis + 1) % 2;
var val = axis === 0 ? x : y;
var nextAxis = (axis + 1) % 2;

if ((axis === 0 ? minX : minY) <= val) {
stack.push(left);
stack.push(m);
stack.push(nextAxis);
}
if ((axis === 0 ? maxX : maxY) >= val) {
stack.push(m);
stack.push(right);
stack.push(nextAxis);
}
if (axis === 0 ? qx - r <= x : qy - r <= y) {
stack.push(left);
stack.push(m - 1);
stack.push(nextAxis);
}
if (axis === 0 ? qx + r >= x : qy + r >= y) {
stack.push(m + 1);
stack.push(right);
stack.push(nextAxis);
}
}

Expand Down

0 comments on commit db37000

Please sign in to comment.