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

how to get an intersection of a boost::multi_index container

本文关键字:multiindex 何获得 boost      更新时间:2023-10-16

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

这是我的密码。

#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));
    }
}

好的,现在我知道你想在区域[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或一些类似的空间数据结构可能更适合这份工作。