Re: [Boost-users] [multi_index] non-intrusive keys
----- Mensaje original -----
De: Adi Shavit
Hi,
In my application I used a multi_map of multi_maps as a data structure to provide indexing with 2 keys, say long strings. Access is logarithmic on both keys and the keys are not part of the stored object, thus are potentially only stored once.
I'd like to use multi_index as a more elegant replacement. However, I'm not clear about where the keys are stored.
In my app, the keys are stored separately from the data, as the keys to maps, as part of the map data structure. It seems that in multi_index the keys have to be inside the stored data.In this case, 10 objects with the same key(s) will all have to hold copies of the keys (as members).
Is there a way to avoid this?
I don't think you can avoid duplication of keys --if the keys are really expensive to duplicate, you might consider holding shared_ptrs to them, but this is AFAICS the furthest you can get. Take into acount that a multi_index_container with two keys is not a map of maps. These are different beasts, serving different purposes, * a map of maps applies two concatenated lookups to get to a "terminal" object, * a multi-index container with two indices applies either one of two poosible lookups to get to the object, so you might want to ask yourself whether it makes sense to try to replace your current code with Boost.MultiIndex. What are the advantages / use cases you envision? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Hi Joaquin, Thanks for the quick answer.
I don't think you can avoid duplication of keys --if the keys are really expensive to duplicate, you might consider holding shared_ptrs to them, but this is AFAICS the furthest you can get.
I see. My question is actually more general, than copy-ctor issues. Often you might like to insert existing objects into a container, ones that do not have the indexing keys in them. But I guess, this is what makes multi_index more like std::set than std::map.
Take into acount that a multi_index_container with two keys is not a map of maps. These are different beasts, serving different purposes,
* a map of maps applies two concatenated lookups to get to a "terminal" object, * a multi-index container with two indices applies either one of two poosible lookups to get to the object,
so you might want to ask yourself whether it makes sense to try to replace your current code with Boost.MultiIndex. What are the advantages / use cases you envision?
Well, I guess you're right. Initially I thought I could use it to "cut" the elements according to the various indices, like a lightweight DB. BTW, is there some C++ other data structure library (in boost or out), that might fit the bill? I really don't need a full fledged DB, just "map-of-maps..." with readable semantics. Thanks, Adi
participants (2)
-
Adi Shavit
-
JOAQUIN LOPEZ MU?Z