Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ you may prefer `SharedArrayBuffer` if you want to share the index between thread

Adds a given point to the index. Returns a zero-based, incremental number that represents the newly added point.

#### index.range(minX, minY, maxX, maxY)
#### index.range(minX, minY, maxX, maxY, maxResults=Infinity)

Finds all items within the given bounding box and returns an array of indices that refer to the order the items were added (the values returned by `index.add(x, y)`).
Finds all items within the given bounding box and returns an array of indices that refer to the order the items were added (the values returned by `index.add(x, y)`). If set, returns at most `maxResults` results.

#### index.within(x, y, radius)

Expand Down
7 changes: 7 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ for (let i = 0; i < 10000; i++) {
}
console.timeEnd('10000 small bbox queries');

console.time('10000 small bbox queries (with maxResults)');
for (let i = 0; i < 10000; i++) {
const p = randomPoint(1000);
index.range(p.x - 1, p.y - 1, p.x + 1, p.y + 1, 8);
}
console.timeEnd('10000 small bbox queries (with maxResults)');

console.time('10000 small radius queries');
for (let i = 0; i < 10000; i++) {
const p = randomPoint(1000);
Expand Down
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ export default class KDBush {
* @param {number} minY
* @param {number} maxX
* @param {number} maxY
* @param {number} [maxResults] Limit the number of results to return (no limit by default).
* @returns {number[]} An array of indices correponding to the found items.
*/
range(minX, minY, maxX, maxY) {
range(minX, minY, maxX, maxY, maxResults=Infinity) {
if (!this._finished) throw new Error('Data not yet indexed - call index.finish().');

const {ids, coords, nodeSize} = this;
Expand All @@ -144,7 +145,10 @@ export default class KDBush {
for (let i = left; i <= right; i++) {
const x = coords[2 * i];
const y = coords[2 * i + 1];
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]);
if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
result.push(ids[i]);
if (result.length >= maxResults) return result;
};
}
continue;
}
Expand All @@ -155,7 +159,10 @@ export default class KDBush {
// include the middle item if it's in range
const x = coords[2 * m];
const y = coords[2 * m + 1];
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]);
if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
result.push(ids[m]);
if (result.length >= maxResults) return result;
};

// queue search in halves that intersect the query
if (axis === 0 ? minX <= x : minY <= y) {
Expand Down