我如何更新一个boost :: multi_index_container的重叠索引? [英] How do I update overlapping indexes in a boost::multi_index_container?

查看:214
本文介绍了我如何更新一个boost :: multi_index_container的重叠索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压1.48.0,和我没有升级的选项。

我已经建立了与有重叠的标准索引的multi_index_container的。指标是如何实现共享相同的索引标准,如下面的例子吗?这个例子code的最后一行暗示什么,我要求。

 结构street_address_key:composite_key<
    t_postal_address
    ,const_mem_fun< const_mem_fun< t_postal_address,字符串和放大器; t_postal_address :: STREET_NAME>>
    ,const_mem_fun< const_mem_fun< t_postal_address,长,和放大器; t_postal_address :: house_number>>
    ,const_mem_fun< const_mem_fun< t_postal_address,字符串和放大器; t_postal_address ::城市>>
    ,const_mem_fun< const_mem_fun< t_postal_address,字符串和放大器; t_postal_address ::状态>>
    ,const_mem_fun< const_mem_fun< t_postal_address,长,和放大器; t_postal_address :: zip_ code>>
> {};multi_index<
    t_postal_address
    的indexed_by<
        ordered_unique< street_address_key>
        ,ordered_non_unique< const_mem_fun< t_postal_address,字符串和放大器; t_postal_address ::名称>>
        ,ordered_non_unique< const_mem_fun< const_mem_fun< t_postal_address,字符串和放大器; t_postal_address :: STREET_NAME>>
        ,ordered_non_unique< const_mem_fun< const_mem_fun< t_postal_address,长,和放大器; t_postal_address :: house_number>>
        ,ordered_non_unique< const_mem_fun< const_mem_fun< t_postal_address,字符串和放大器; t_postal_address ::城市>>
        ,ordered_non_unique< const_mem_fun< const_mem_fun< t_postal_address,字符串和放大器; t_postal_address ::状态>>
        ,ordered_non_unique< const_mem_fun< const_mem_fun< t_postal_address,长,和放大器; t_postal_address :: zip_ code>>
    >
> t_address_book;...t_address_book :: nth_element℃的> ::类型:迭代address_iter = book.find(some_address_tuple);book.modify(address_iter,modify_city_method);自动city_range = book.get&所述4是氢;()equal_range(\\ * ??? * \\)。

市指标股的相同的标准street_address_key。通过street_address_key修改,按照我的理解,将确保该指数被适当地更新。但什么是城市指标的状态呢?被更新,以反映城市的名字改变?或者是处于中断状态?我仍然可以找到与老城区同一个地址索引?我一定要单独更新这个指数?我可以打一个空操作修改更新索引?

  // presume我从索引中城市元素。
//难道这是它上面的修改后,更新城市指数?book.get&所述4是氢;()修改。(city_iter,[](t_postal_address&放大器;){});


解决方案

在我看来像有应该没有问题:

说明文档中提到:


  

效果:调用mod(e)如e为元素指向位置,并重新排列 *位置进入multi_index_container的所有索引。重排是成功的,如果


  
  

      
  • 索引是非唯一的或者没有其它元素具有同等键存在,

  •   
  • 及重排被multi_index_container的其它所有索引都允许。
      如果重排失败,则元件被擦除。

  •   

这意味着所有的索引进行更新。请注意,所有索引可以在原则上拒绝修改。您可能需要使用回滚,以防止数据丢失。

I'm using boost 1.48.0, and I don't have the option to upgrade.

I've built a multi_index_container with indexes that have overlapping criteria. How are indexes sharing the same indexing criteria effected, as in the following example? The last line of the example code alludes to what I am asking for.

struct street_address_key : composite_key<
    t_postal_address
    , const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::street_name>>
    , const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::house_number>>
    , const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::city>>
    , const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::state>>
    , const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::zip_code>>
> {};

multi_index<
    t_postal_address
    indexed_by<
        ordered_unique<street_address_key>
        , ordered_non_unique<const_mem_fun<t_postal_address, string, &t_postal_address::name>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::street_name>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::house_number>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::city>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::state>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::zip_code>>
    >
> t_address_book;

...

t_address_book::nth_element<0>::type::iterator address_iter = book.find(some_address_tuple);

book.modify(address_iter, modify_city_method);

auto city_range = book.get<4>().equal_range( \*???*\ );

The city index shares the same criteria as the street_address_key. Modifying via the street_address_key, as I understand it, would ensure the index is appropriately updated. But what is the state of the "city" index? Was it updated to reflect the change in city name? Or is it in a broken state? Could I still locate the same address index with the old city? Do I have to update this index separately? Could I call a no-op Modifier to update the index?

// Presume I have the city element from the index.
// Is this supposed to update the city index after it was modified above?

book.get<4>().modify(city_iter, [](t_postal_address &) {});

解决方案

It looks to me like there should be no problem:

The documentation mentions:

Effects: Calls mod(e) where e is the element pointed to by position and rearranges *position into all the indices of the multi_index_container. Rearrangement is successful if

  • the index is non-unique OR no other element exists with equivalent key,
  • AND rearrangement is allowed by all other indices of the multi_index_container. If the rearrangement fails, the element is erased.

This implies all indexes are updated. Note that all indexes can in principle reject the edit. You may want to use a rollback to protect against loss of data.

这篇关于我如何更新一个boost :: multi_index_container的重叠索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆