Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search accepts a predicate argument #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,25 @@ Returns an array of data items (points or rectangles) that the given bounding bo
Note that the `search` method accepts a bounding box in `{minX, minY, maxX, maxY}` format
regardless of the format specified in the constructor (which only affects inserted objects).

Also `search` accepts a predicate, which is a function that takes a single result as an argument and returns `true` or `false` if the item should be included to the final result or not respectively:
```js
var allItems = tree.all();
var result = tree.search({
minX: 40,
minY: 20,
maxX: 80,
maxY: 70
}, function (dot) {
if (dot.someCheckHere) {
return false;
}
return true;
});
```

Returns all items of the tree.
Return all items of the tree:
```js
var allItems = tree.all();
```

### Collisions

Expand Down
29 changes: 23 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rbush.prototype = {
return this._all(this.data, []);
},

search: function (bbox) {
search: function (bbox, predicate) {

var node = this.data,
result = [],
Expand All @@ -42,8 +42,15 @@ rbush.prototype = {
childBBox = node.leaf ? toBBox(child) : child;

if (intersects(bbox, childBBox)) {
if (node.leaf) result.push(child);
else if (contains(bbox, childBBox)) this._all(child, result);
if (node.leaf) {
if (predicate) {
if (predicate(child)) {
result.push(child);
}
} else {
result.push(child);
}
} else if (contains(bbox, childBBox)) this._all(child, result, predicate);
else nodesToSearch.push(child);
}
}
Expand Down Expand Up @@ -187,11 +194,21 @@ rbush.prototype = {
return this;
},

_all: function (node, result) {
_all: function (node, result, predicate) {
var nodesToSearch = [];
while (node) {
if (node.leaf) result.push.apply(result, node.children);
else nodesToSearch.push.apply(nodesToSearch, node.children);
if (node.leaf) {
if (predicate) {
for (var i = 0; i < node.children.length; i++) {
var child = node.children[i];
if (predicate(child)) {
result.push(child);
}
}
} else {
result.push.apply(result, node.children);
}
} else nodesToSearch.push.apply(nodesToSearch, node.children);

node = nodesToSearch.pop();
}
Expand Down
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,36 @@ t('#clear should clear all the data in the tree', function (t) {
t.end();
});

t('#search accepts predicate: exclude results', function (t) {
var tree = rbush(4).load(data);
var predicate = function (point) {
if (point.isReturned) {
return false;
}
point.isReturned = true;
return true;
};
var result = tree.search({minX: 40, minY: 20, maxX: 80, maxY: 70}, predicate);

var setIsReturned = function (point) {
point.isReturned = true;
return point;
};

sortedEqual(t, result, [
[70,20,70,20],[75,25,75,25],[45,45,45,45],[50,50,50,50],[60,60,60,60],[70,70,70,70],
[45,20,45,20],[45,70,45,70],[75,50,75,50],[50,25,50,25],[60,35,60,35],[70,45,70,45]
].map(arrToBBox).map(setIsReturned));

result = tree.search({minX: 35, minY: 20, maxX: 80, maxY: 70}, predicate);

t.equal(result.length, 2);
sortedEqual(t, result, [
[35, 35, 35, 35], [35, 60, 35, 60]
].map(arrToBBox).map(setIsReturned));
t.end();
});

t('should have chainable API', function (t) {
t.doesNotThrow(function () {
rbush()
Expand Down