boost::multi_index_container:如何使用composite_key (x,y) 支持矩形搜索

boost::multi_index_container: How to use composite_key (x,y) to support rectangular search?

本文关键字:支持 搜索 key container index multi 何使用 boost composite      更新时间:2023-10-16
typedef boost::multi_index_container
< Record,
  indexed_by 
  < ordered_non_unique 
    < tag<ByP>,
      composite_key < Record,
                      const_mem_fun<Record, double, &Record::hit_x>,
                      const_mem_fun<Record, double, &Record::hit_y>
    >
  >,   // ...
>

如您所见,有按点(x,y((ByP(的索引。现在我正在使用下一个函数:

size_t  adjacencyRectPoints (std::list<Record> &range, const Point  &min, const Point &max)
{
  MultiIndexMoves::index<ByP>::type  &index = store_.get<ByP> ();     
  /* Range searching, i.e.the lookup of all elements in a given interval */
  auto itFirstLower = index.lower_bound (boost::tuple<double, double> (min));
  auto itFirstUpper = index.upper_bound (boost::tuple<double, double> (max));
  size_t count = 0U;
  for ( auto it = itFirstLower; it != itFirstUpper; ++it )
  {
    if ( (min.x <= it->hit.x && min.y <= it->hit.y)
      && (max.x >= it->hit.x && max.y >= it->hit.y) )
    {
      range.push_back (*it);
      ++count;
    }
  }
  return count;
}

此函数返回矩形中的所有点:(min.x struct Compare { bool operator() (const Point &p, const Point &q) const { return (p.x < q.x) && (p.y < q.y); } };

(第一条评论@RichardHodges(

首先,比较器对我来说似乎是错误的(看起来它并没有根据需要定义一个完全弱的排序(。

其次,组合键上的键类型在逻辑上是组成部分的元组。默认比较实现由 boost 提供(作为字典编纂成员比较¹(。

这更有可能是你想要的。如果必须以某种方式覆盖它,请使用:http://www.boost.org/doc/libs/1_60_0/libs/multi_index/doc/reference/key_extraction.html#composite_key_compare


¹ <boost/tuple/tuple_comparison.hpp> 和 C++11 的 std::tuple 也定义了相同的运算符