Как переместить элемент без удаления и повторной вставки его в boost :: multi_index_container ?

Я использую boost :: multi_index_container для обеспечения произвольного доступа и доступа на основе хэша к коллекции элементов. Я хотел изменить индекс произвольного доступа элемента без изменения индекса на основе хэша.

Вот фрагмент кода:

# include 
# include 
# include 
# include 
# include 

using namespace std ;
using namespace boost ;
using namespace boost::multi_index ;

// class representing my elements
class Element
{
    public :
      Element(const string & new_key) : key(new_key) {}
      string key ;      // the hash-based index in the multi_index_container
      // ... many stuff skipped
    private :
      // ... many stuff skipped
} ;

typedef multi_index_container<
            Element,
            indexed_by<
                random_access< >,
                hashed_unique<
                    member
                >
            >    
        > ElementContainer ;

typedef ElementContainer::nth_index<0>::type::iterator ElementRandomIter ;
typedef ElementContainer::nth_index<1>::type::iterator ElementHashedIter ;

int main(int, char*[])
{
    ElementContainer ec ;

    // insert some elements
    ec.push_back(Element("Alice")) ;       // random-access index = 0
    ec.push_back(Element("Bob")) ;         // random-access index = 1
    ec.push_back(Element("Carl")) ;        // random-access index = 2
    ec.push_back(Element("Denis")) ;       // random-access index = 3

    // Here I want to move "Denis" to position 1
    // The (bad looking) solution I found involves removing and inserting the element
    ElementRandomIter it = ec.get<0>().begin() + 3 ;
    Element e = *(it) ;                    // store a copy
    ec.get<0>().erase(it) ;                // remove the element
    it = ec.get<0>().begin() + 1 ;
    ec.get<0>().insert(it, e) ;            // insert the copy

    // Elements are now in the following order
    // random-access index 0 : Alice
    // random-access index 1 : Denis
    // random-access index 2 : Bob
    // random-access index 3 : Carl

    return 0 ;
}

Я знаю, что даже если в этом примере я использовал только итераторы произвольного доступа для управления элементов, хеширование происходит за кулисами по крайней мере дважды в multi_index_container , в дополнение к копии объекта, что может быть дорогостоящим.

Есть ли способ изменить индекс произвольного доступа элемента внутри boost :: multi_index без необходимости дорогостоящего уродства удаления и вставки при сохранении копии?

Я искал в документации multi_index_container , возможно, я пропустил что нибудь. Спасибо за любой совет!

Примечание: извините за возможные ошибки на английском :)

5
задан overcoder 6 July 2011 в 13:35
поделиться