【问题标题】:how to get an intersection of a boost::multi_index container如何获得 boost::multi_index 容器的交集
【发布时间】:2015-11-27 13:46:54
【问题描述】:

我想以最快的方式获得 ordered_non_unique 类型的 4 个索引的交集。这样的multi_index-intersection 是否比嵌套的 4 倍 std::map 快?是否有可能使用 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));
    }
}

【问题讨论】:

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


    【解决方案1】:

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

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

    boost::geometry::index::rtree 或一些类似的空间数据结构,正如您所指出的,可能更适合这项工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 2013-07-14
      • 1970-01-01
      • 2017-01-23
      • 2017-01-27
      • 2016-03-23
      • 2011-03-29
      相关资源
      最近更新 更多