модификация значений в multi_index_container
От: Adriano  
Дата: 20.03.08 11:21
Оценка:
Имеем тип emploee, множество emploee в виде multi_index_container и пытаемся изменить имя одного из сотрудников
#include <boost/progress.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/member.hpp>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
namespace bl = boost::lambda;

struct emploee {
    explicit emploee(
        std::size_t id_,
        const std::string& name_,
        const std::string& surname_
    ):
        id(id_),
        name(name_),
        surname(surname_)
    {}

    std::size_t id;
    std::string name;
    std::string surname;

    bool operator<(const emploee& e) const{
        return id < e.id;
    }
};

struct by_id{};
struct by_name{};
struct by_surname{};
struct by_sequence{};

typedef boost::multi_index_container<
    emploee,
    bmi::indexed_by<
        //индекс di
        bmi::ordered_unique<
            bmi::tag<by_id>,
            bmi::identity<emploee>
        >,
        //индекс порядок добавления
        bmi::sequenced<bmi::tag<by_sequence> >,
        //индекс name
        bmi::ordered_non_unique<
            bmi::tag<by_name>,
            bmi::member<emploee,std::string,&emploee::name>
        >,
        //индекс surname
        bmi::ordered_non_unique<
            bmi::tag<by_surname>,
            bmi::member<emploee,std::string,&emploee::surname>
        >
    >
> emploee_set_type;

int    _tmain(int argc, _TCHAR* argv[])
{
    emploee_set_type es;
    es.insert(emploee(35,"n3","s3"));
    es.insert(emploee(100,"n1","s1"));
    es.insert(emploee(1,"n2","s2"));

    emploee_set_by_name& name_index = es.get<by_name>();
    emploee_set_by_name::iterator i_name = name_index.find("n3");

    name_index.modify_key(i_name,bl::_1 = "new name");//меняем имя "n3" на "new name"
    //при модификации имени перестраиваются все индексы, в том числе и фамилии.

    return 0;
}

Формат описания multi_index_container никак не задает зависимости между индексами,
и вдруг имя как-то зависит от фамилии, или
и вдруг operator< выглядит так
    bool operator<(const emploee& e) const{
        if ( id == e.id ) {
            return name < e.name;
        }
        return id < e.id;
    }

тогда при модификации name надо перестраивать индекс by_id, что и делается

Но мне надо, что бы при модификации имени не перестраивались другие индексы
Можно как-то указать, что индекс by_name не зависит ни от чего, и при его модификации не надо перестраивать все индексы?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.