I'm using boost multi_index_container, which is queried by equal_range and the result returned from the function using range::join and boost::any_range
The any_range Reference argument is defined as const reference to the type - must be const because of the multi_index_container nature, not quite sure about the reference. Example:
typedef boost::any_range<TestData, boost::random_access_traversal_tag,
const TestData &, std::ptrdiff_t> TestRange;
Now what I need is to use mutating range algorithms like boost::sort, unique, etc., which obviously cannot run on range because of the constness of elements in range.
Is it any workaround except copying elements into new container?
EDIT 1:
struct and MIC example:
struct TestData {
TestData()
: m_strMem01("test"), m_intMem02(rand()),
m_boolMem03(rand() < RAND_MAX / 2) {}
std::string m_strMem01;
int m_intMem02;
bool m_boolMem03;
};
typedef boost::multi_index_container<
TestData,
bmi::indexed_by<
bmi::random_access<bmi::tag<struct RndKey1>>,
bmi::ordered_non_unique<
bmi::tag<struct Key1>,
bmi::composite_key<
TestData,
bmi::member<TestData, std::string, &TestData::m_strMem01>,
bmi::member<TestData, bool, &TestData::m_boolMem03>>>,
bmi::ordered_non_unique<
bmi::tag<struct Key4>,
bmi::composite_key<
TestData,
bmi::member<TestData, std::string, &TestData::m_strMem01>,
bmi::member<TestData, bool, &TestData::m_intMem02>>>,
bmi::ordered_non_unique<
bmi::tag<struct Key2>,
bmi::member<TestData, int, &TestData::m_intMem02>>,
bmi::ordered_non_unique<
bmi::tag<struct Key3>,
bmi::member<TestData, bool, &TestData::m_boolMem03>>>>
TestDataContainer;
OK, once you have your range you really can't sort it or somehow rearrange it because the order of elements is fixed by the index --this is an absolutely fundamental invariant of indices enforced by the constness of elements, much as you'd find with say a
std::set
. Rather than doing a full copy to an external container you can create a more lightweight view out of pointers or references to the original elements that can later be manipulated as you wish. This is an example with views constructed asstd::vector
s ofstd::reference_wrapper
s to the elements:Live On Coliru
Output: