0
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>
    >
  >,   // ...
>

As you can see, there is index by Point(x,y) (ByP). Now I'm using the next function:

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

This function returns all points from rectangle: (min.x < x < max.x && min.y < y < max.y). It is working.

But, how you can see, the container returns much more points, that I expected, and I have to filter them one more time. I think, that acting is wrong.


I thought, that I had to define the comparator by myself to get only right points. But that way of comparing is not fit for boost::multi_index_container:

struct Compare
{
  bool  operator() (const Point &p, const Point &q) const
  { return  (p.x < q.x) && (p.y < q.y); }
};

(first comment @RichardHodges)

Kirill Golikov
  • 1,354
  • 1
  • 13
  • 27
  • 2
    that comparator is wrong. it's not reversible. you'll get keys that are neither less than or greater than other keys when the x's are equal and the y's are different. – Richard Hodges Apr 14 '16 at 22:33
  • "t is very important to get the AND-operator between the keys compare_result" - In that case I somehow fail to see how this is a multi-index. – VolkerK Apr 14 '16 at 22:45
  • @VolkerK, Container searches independently by x and y, then takes it intersection - it means that each key comparator returned true. – Kirill Golikov Apr 15 '16 at 08:28
  • @RichardHodges, I'm trying to organize the region searching in that container, I have chosen the incorrect way? – Kirill Golikov Apr 15 '16 at 08:45
  • @KirillGolikov what (in words) defines a discrete region? – Richard Hodges Apr 15 '16 at 09:38
  • @RichardHodges, Return all of points belongs to Rectangle: (x1 <= x <= x2 && y1 <= y <= y2). I'm using `auto itFirstLower = index.lower_bound (min); auto itFirstUpper = index.upper_bound (max); ...` But there is no checking by ordinate. – Kirill Golikov Apr 15 '16 at 11:29

1 Answers1

0

Firstly, the comparator seems wrong to me (it doesn't look like it does define a total weak ordering as required).

Secondly, the key type on a composite key is logically a tuple of the constituent parts. The default comparison implementation is provided by boost (as a a lexicographical memberwise comparison¹).

This is much more likely to be what you want. If you must override it some way, use: 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> and c++11's std::tuple also defines the same operators

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I have read the docs, but do not understand, how to organize the region searching in the container. The default order is unfit for it, but if my comparator is wrong, I do not understand where a correct place to integrate this code. – Kirill Golikov Apr 15 '16 at 08:51
  • 2
    I suspect your question is related to http://stackoverflow.com/questions/35778119/boost-multi-index-composite-key-for-multiple-column-indexes – Joaquín M López Muñoz Apr 15 '16 at 10:18
  • 1
    @KirillGolikov I'm happy to help out if you can update your question with an example what you mean by "region searching" (are you referring to `equal_range` on a partial key? I have several [answers showing samples of that](https://stackoverflow.com/search?q=user%3A85371+composite_key).). Make sure you describe _what_ you want to achieve more than _how_ you think you need to do it. – sehe Apr 16 '16 at 14:33
  • Okay. This clearly indicates that the library author got the right hunch in his comment http://stackoverflow.com/questions/36634751/boostmulti-index-container-how-to-use-composite-key-x-y-to-support-rectangu/36635239#comment60882304_36635239. Look at boost rtree perhaps – sehe Apr 16 '16 at 19:27