Boost logo

Boost Users :

Subject: [Boost-users] [multi_index] are indices in a random access index stable on addition, can we have an "index_of" method?
From: Nick Stokes (randomaccessiterator_at_[hidden])
Date: 2015-06-26 10:24:44


I have a stream of pairs of elements of type T, and want to insert
individual T's uniquely into a container and convert the pairs to a pair of
indices into this container.

Boost.MultiIndex seems to be cut out exactly for this job, but I not sure
about how to obtain the indices or stability of them on addition:

Take,

namespace bmi = boost::multi_index;
typedef bmi::multi_index_container< T, bmi::indexed_by
   < bmi::hashed_unique< bmi::identity<T> >
   , bmi::random_access<>
>> MIC;

MIC elements;
std::vector<std::pair<int,int>> pairs;
for( const std::pair<T,T>& pt : stream ) {
    auto t1 = elements.insert(pt.first).first; // not all insertions are
successful of course
    auto t2 = elements.insert(pt.second).first;
    pairs.insert( index_of(t1), index_of(t2) );
}

// where index_of() is
auto index_of = [&]( MIC::const_iterator i ) {
   return elements.project<1>(i) - elements.get<1>().begin();
}

So my questions are

(1) Is the implementation of index_of() correct? (or better, is there
something like this already for random access indices)

(2) Is the way they are used in above loop stable, where elements are
inserted after an index is obtained?

Thanks
Nick



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net