Boost R树:计算满足查询的元素

Boost R-tree : counting elements satisfying a query

本文关键字:查询 元素 满足 计算 Boost      更新时间:2023-10-16

到目前为止,当我想计算R树中有多少元素满足特定的空间查询时,可以归结为运行查询,收集匹配项,然后计数,大致如下:

std::vector<my_type> results;
rtree_ptr->query(bgi::intersects(query_box), std::back_inserter(results));
int nbElements = results.size();

有没有更好的方法,即在不检索实际元素的情况下直接计数?我还没找到什么办法,但谁知道呢。(我正在用打包算法构建我的树,以防它有任何相关性。)

我的动机是注意到查询的速度取决于匹配的数量。如果有0个匹配项,则查询或多或少是即时的;如果有10000场比赛,需要几秒钟。由于可以很快确定是否有匹配,因此遍历树似乎非常快(至少在我创建的索引中);它收集了所有的结果,这使得在有很多匹配的情况下查询速度变慢。由于我对收集不感兴趣,只是简单地计数(至少对于一些查询),如果我可以跳过收集,那就太棒了。

我的脑电波很晚。甚至比使用function_output_iterator更好的是使用boost::geometry::index query_iterator。

原则上,它将导致与稍微简单的代码完全相同的行为:

box query_box;
auto r = boost::make_iterator_range(bgi::qbegin(tree, bgi::intersects(query_box)), {});
// in c++03, spell out the end iterator: bgi::qend(tree)
size_t nbElements = boost::distance(r);

注意size()不可用,因为query_const_iterator不属于随机访问类别。

但组合起来可能会稍微舒服一些。比如说,如果你想对每件物品进行额外的检查,你可以使用标准的库算法,比如:

size_t matching = std::count_if(r.begin(), r.end(), some_predicate);

我认为基于范围的解决方案更灵活(相同的代码可以用于实现其他算法,如partial_sort_copystd::transform,这很难适应我之前回答中的输出迭代器习惯用法)。

您可以使用函数输出迭代器:

size_t cardinality = 0; // number of matches in set
auto count_only = boost::make_function_output_iterator([&cardinality] (Tree::value_type const&) { ++cardinality; });

这样使用:

使用lambda的C++11

在Coliru上直播

#include <boost/function_output_iterator.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace bgi = boost::geometry::index;
using point = boost::geometry::model::d2::point_xy<int, boost::geometry::cs::cartesian>;
using box = boost::geometry::model::box<point>;
int main()
{
    using Tree = bgi::rtree<box, bgi::rstar<32> >;
    Tree tree;
    size_t cardinality = 0; // number of matches in set
    auto count_only = boost::make_function_output_iterator([&cardinality] (Tree::value_type const&) { ++cardinality; });
    box query_box;
    tree.query(bgi::intersects(query_box), count_only);
    int nbElements = cardinality;
    return nbElements;
}

C++03使用函数对象

对于C++,您可以将lambda替换为(多态!)函数对象:

struct count_only_f {
    count_only_f(size_t& card) : _cardinality(&card) { }
    template <typename X>
    void operator()(X) const {
        ++(*_cardinality);
    }
  private:
    size_t *_cardinality;
};
// .... later:
boost::function_output_iterator<count_only_f> count_only(cardinality);

C++03使用Boost Phoenix

我认为这是一个使用Boost Phoenix:的好地方

#include <boost/phoenix.hpp>
// ...
size_t cardinality = 0; // number of matches in set
tree.query(bgi::intersects(query_box), boost::make_function_output_iterator(++boost::phoenix::ref(cardinality)));

或者,更典型的名称空间别名:

#include <boost/phoenix.hpp>
// ...
size_t cardinality = 0; // number of matches in set
tree.query(bgi::intersects(query_box), make_function_output_iterator(++phx::ref(cardinality)));