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

fix for_each filter #28

Open
wants to merge 8 commits into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ compile_commands.json
perf.data*
build

/cmake-build-debug/
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
Nothing yet.
### Changed
- Fixed issue #2: for_each(callback, filter) was traversing too many nodes.
- Build improvements for bazel/cmake

## [1.1.1] - 2022-01-30
### Changed
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**Note: for updates please also check the [fork](https://github.com/tzaeschke/phtree-cpp) by the original PH-Tree developer.**
**This is a fork of [Improbable's PH-tree](https://github.com/improbable-eng/phtree-cpp)**.

# PH-Tree C++

Expand Down Expand Up @@ -128,9 +128,9 @@ tree.estimate_count(query);

#### Queries

* For-each over all elements: `tree.fore_each(callback);`
* For-each over all elements: `tree.for_each(callback);`
* Iterator over all elements: `auto iterator = tree.begin();`
* For-each with box shaped window queries: `tree.fore_each(PhBoxD(min, max), callback);`
* For-each with box shaped window queries: `tree.for_each(PhBoxD(min, max), callback);`
* Iterator for box shaped window queries: `auto q = tree.begin_query(PhBoxD(min, max));`
* Iterator for _k_ nearest neighbor queries: `auto q = tree.begin_knn_query(k, center_point, distance_function);`
* Custom query shapes, such as spheres: `tree.for_each(callback, FilterSphere(center, radius, tree.converter()));`
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ http_archive(
load("@bazel_skylib//lib:versions.bzl", "versions")

versions.check(
minimum_bazel_version = "4.2.2",
minimum_bazel_version = "3.0.0",
maximum_bazel_version = "4.2.2",
)

Expand Down
26 changes: 26 additions & 0 deletions phtree/phtree_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,32 @@ TEST(PhTreeTest, TestWindowQuery1) {
ASSERT_EQ(N, n);
}

TEST(PhTreeTest, TestWindowQuery1_WithFilter) {
size_t N = 1000;
const dimension_t dim = 3;
TestTree<dim, Id> tree;
std::vector<TestPoint<dim>> points;
populate(tree, points, N);

struct Counter {
void operator()(TestPoint<dim>, Id& t) {
++n_;
id_ = t;
}
Id id_{};
size_t n_ = 0;
};

for (size_t i = 0; i < N; i++) {
TestPoint<dim>& p = points.at(i);
Counter callback{};
FilterAABB filter(p, p, tree.converter());
tree.for_each(callback, filter);
ASSERT_EQ(i, callback.id_._i);
ASSERT_EQ(1, callback.n_);
}
}

TEST(PhTreeTest, TestWindowQueryMany) {
const dimension_t dim = 3;
TestPoint<dim> min{-100, -100, -100};
Expand Down
10 changes: 5 additions & 5 deletions phtree/v16/for_each.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ class ForEach {

void run(const EntryT& root) {
assert(root.IsNode());
TraverseNode(root.GetKey(), root.GetNode());
TraverseNode(root.GetNode());
}

private:
void TraverseNode(const KeyInternal& key, const NodeT& node) {
void TraverseNode(const NodeT& node) {
auto iter = node.Entries().begin();
auto end = node.Entries().end();
for (; iter != end; ++iter) {
const auto& child = iter->second;
const auto& child_key = child.GetKey();
if (child.IsNode()) {
const auto& child_node = child.GetNode();
if (filter_.IsNodeValid(key, node.GetPostfixLen() + 1)) {
TraverseNode(child_key, child_node);
if (filter_.IsNodeValid(child_key, child_node.GetPostfixLen() + 1)) {
TraverseNode(child_node);
}
} else {
T& value = child.GetValue();
if (filter_.IsEntryValid(key, value)) {
if (filter_.IsEntryValid(child_key, value)) {
callback_(converter_.post(child_key), value);
}
}
Expand Down