boost::multi_index help
I'm trying to use multi_index to store objects pointers. These objects have mainly 3 fields : - string name - int id - int block_id name & id are unique among all instances, but block_id can be the same for many instances. I want to distribute the object instances among a few blocks, so many instances can have block_id 0, many others block_id 1, and so on. (there are 6 blocks for the moment) here is my struct : /* tags */ struct name_{} ; struct id_{} ; struct block_id_{} ; typedef hashed_unique< tag<name_>, boost::multi_index::mem_fun<Foo, string, &Foo::getName> > NameIndex ; typedef ordered_unique< tag<id_>, boost::multi_index::mem_fun<Foo, int, &Foo::getId> > IdIndex ; typedef ordered_non_unique< tag<block_id_>, boost::multi_index::mem_fun<Foo, int, &Foo::getBlockId> > BlockIndex ; typedef multi_index_container<Foo*, indexed_by<NameIndex, IdIndex, BlockIndex> > FooList ; The name & id indexes work well but I dont manage to get the Foo* pointers for a given block_id. I m using : foo_list_.get<block_id_>().find(1) to grab the wanted list. My problem is that in the result I have not only Foo instances from the block 1. This is what I get when I try to query for a given block_id : For block 5 : only objects with block_id == 5 For block 4 : objects with block_id == 4 and 5 For block 3 : objects with block_id == 3, 4 and 5 For block 2 : objects with block_id == 2, 3, 4 and 5 and so on. I d like to get only the objects with block_id = 4 when I query the multi_index object on its block_id index. I now wonder if what I want to do can be done with multi_index. Thanks in advance for your help
Axel escribió:
I'm trying to use multi_index to store objects pointers. These objects have mainly 3 fields : - string name - int id - int block_id
[...]
The name & id indexes work well but I dont manage to get the Foo* pointers for a given block_id. I m using : foo_list_.get<block_id_>().find(1) to grab the wanted list. My problem is that in the result I have not only Foo instances from the block 1. This is what I get when I try to query for a given block_id :
For block 5 : only objects with block_id == 5 For block 4 : objects with block_id == 4 and 5 For block 3 : objects with block_id == 3, 4 and 5 For block 2 : objects with block_id == 2, 3, 4 and 5 and so on.
I think you need to use equal_range here: // return a range comprising objects with block_id==3 foo_list_.get<block_id_>().equal_range(3); Does this help? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
joaquin@tid.es wrote:
I think you need to use equal_range here:
// return a range comprising objects with block_id==3 foo_list_.get<block_id_>().equal_range(3);
Does this help?
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
You're right, It works well, it was a part of the documentation I didn't go deeply. Thanks for your help. Axel
participants (2)
-
Axel
-
joaquin@tid.es