c++boostboost-multi-index

boost::multi_index_container MRU


I have a bmi multi_index_container for MRU defined as following

bmi::multi_index_container<Item, bmi::indexed_by<bmi::sequenced<>, bmi::hashed_unique<bmi::tag<hashed>, KeyExtractor>>>

It is actually based on the bmi example. Once inserting and relocating the item to head everything is ok. The problem starts when I'm looking up by a key and retrieving it as following.

item_type & get(const key_type & key) const
{
    auto item = items.template get<hashed>().find(key);
    if (item == items.template get<hashed>().end())
        throw std::logic_error("Item not found");

    //relocate the item to the head
    return const_cast<item_type &>(*item);
}

Since I'm looking it by the key I use the hashed index. Then I have to call relocate on the sequenced but the iterator from another index and it of course will not work. Sure, iterating over sequenced and finding the same item is an option but quite ugly and inefficient one. Is there some other way to do it?


Solution

  • To convert between different indices' iterators, use projection.