如何获得boost :: multi_index容器的交集

时间:2015-11-27 13:46:55

标签: c++ boost stdmap boost-multi-index

我想以最快的方式获得ordered_non_unique类型的4个索引的交集。这样的multi_index - 交叉点是否比嵌套std::map的4倍快?是否有可能使用类似std :: map()。emplace()的东西。

这是我的代码。

#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>

using boost::multi_index_container;
using namespace boost::multi_index;

struct Kpt {
    Kpt(float _x0, float _x1, float _y0, float _y1)
      : x0_(_x0),x1_(_x1),y0_(_y0),y1_(_y1) {
    }
    friend std::ostream& operator<<(std::ostream & _os, Kpt const & _kpt) {
        _os
            << "\nx0 " << _kpt.x0_ << ","
            << " y0 " << _kpt.y0_ << ","
            << " x1 " << _kpt.x1_ << ","
            << " y1 " << _kpt.y1_ << std::endl
        ;
        return _os;
    }
    float x0_;
    float x1_;
    float y0_;
    float y1_;
};

struct x0_{};
struct x1_{};
struct y0_{};
struct y1_{};

typedef multi_index_container <
    Kpt
  , indexed_by <
        ordered_non_unique <
            tag<x0_>,BOOST_MULTI_INDEX_MEMBER(Kpt,float,x0_)
        >
      , ordered_non_unique <
            tag<x1_>,BOOST_MULTI_INDEX_MEMBER(Kpt,float,x1_)
        >
      , ordered_non_unique <
              tag<y0_>,BOOST_MULTI_INDEX_MEMBER(Kpt,float,y0_)
          >
      , ordered_non_unique <
              tag<y1_>,BOOST_MULTI_INDEX_MEMBER(Kpt,float,y1_)
          >
    >
> Kpts;

int main() {
    Kpts kpts;
    for (int i=0; i<1000000; ++i) {
        if (i%10000==0) std::cout << "." << std::flush;
        kpts.insert(Kpt(0.1,0.1,0.1,0.1));
    }
}

1 个答案:

答案 0 :(得分:3)

好的,现在我知道您想在区域[x0,x0+d]×[x1,x1+d]×[y0,y0+d]×[y1,y1+d]中搜索四维点,对吧?

好吧,我不敢说Boost.MultiIndex不是正确的工具,因为获得索引#0,#1,#2,#3中的范围的交集只能通过扫描其中一个来完成。范围(比如#0)并手动验证遍历点的剩余坐标(x1, y0, y1)是否位于感兴趣的区域内(std::set_intersection在此处不适用,因为它要求比较范围按相同标准排序,我们的指数并非如此)。

正如您所指出的,

boost::geometry::index::rtree或某些类似的空间数据结构可能更适合这项工作。