Boost :: multi_index_container: take equal_range from arbitrary index, implement loop only once
I have multi_index_container
one that basically looks like this:
struct MyStruct {
int a, b, c;
};
struct Tag1;
struct Tag2;
typedef multi_index_container<
MyStruct,
indexed_by<
hashed_non_unique<
tag<Tag1>,
composite_key<
MyStruct,
member<MyStruct, int, &MyStruct::a>,
member<MyStruct, int, &MyStruct::b>
>
>,
hashed_non_unique<
tag<Tag2>,
composite_key<
MyStruct,
member<MyStruct, int, &MyStruct::a>,
member<MyStruct, int, &MyStruct::b>,
member<MyStruct, int, &MyStruct::c>
>
>
>
> MyContainer;
I create an instance of such a container and use its indices like this:
MyContainer c;
MyContainer::index<Tag1>::type& index1 = c.get<Tag1>;
MyContainer::index<Tag2>::type& index2 = c.get<Tag2>;
Now, at runtime, I want to do equal_range
one of the two indices. Which index is actually used depends on the current configuration. What I'm trying to accomplish looks something like this:
// Search in container
SomeType range;
if (useIndex1)
range = index1.equal_range(...);
else
range = index2.equal_range(...);
// Loop through range
for (auto i = range.first; i != range.second; ++i)
...
I do not know how to do that. As it turns out, the return type index1.equal_range
is a pair of different iterators than the ones returned index2.equal_range
. Is there a common base type for the two? Looking at my example, how would it look SomeType
? I don't want to repeat the loop for
in my code for every index that can be used.
source to share
Instead of trying to erase a type with a range, put your loop logic in a lambda and apply it using std :: for_each:
#include <boost\multi_index_container.hpp>
#include <boost\multi_index\composite_key.hpp>
#include <boost\multi_index\member.hpp>
#include <boost\multi_index\hashed_index.hpp>
using namespace boost::multi_index;
struct MyStruct {
int a, b, c;
};
struct Tag1;
struct Tag2;
typedef multi_index_container
<
MyStruct
, indexed_by
<
hashed_non_unique
<
tag<Tag1>
, composite_key
<
MyStruct
, member<MyStruct, int, &MyStruct::a>
, member<MyStruct, int, &MyStruct::b>
>
>
, hashed_non_unique
<
tag<Tag2>
, composite_key
<
MyStruct
, member<MyStruct, int, &MyStruct::a>
, member<MyStruct, int, &MyStruct::b>
, member<MyStruct, int, &MyStruct::c>
>
>
>
> MyContainer;
int main()
{
MyContainer c;
MyContainer::index<Tag1>::type& index1 = c.get<Tag1>();
MyContainer::index<Tag2>::type& index2 = c.get<Tag2>();
//! Add some values
for (int i = 0; i < 10; ++i)
{
MyStruct s = { i, i * 2, i * 3 };
c.insert(s);
}
auto loop = [](const MyStruct& s){ std::cout << "{ " << s.a << ", " << s.b << ", " << s.c << " }" << std::endl; };
// Search in container
bool useIndex1 = true;
if (useIndex1)
{
auto range = std::make_pair(index1.begin(), index1.end());
std::for_each(range.first, range.second, loop);
}
else
{
auto range = std::make_pair(index1.begin(), index1.end());
std::for_each(range.first, range.second, loop);
}
// Loop through range
//for (auto i = range.first; i != range.second; ++i)
return 0;
}
source to share