[Multi-Index] Categorised Container

Dear all, I have an application where something we call a "categorised container" with colleagues comes very hand. We do currently have a working version and I am looking to see whether it's possible to do some refactoring on that using Multi-Index. Here is an exemplified explanation of what constitutes a decision-making point for me. I would be pleased if anyone here could tell me whether this facility is available under Multi-Index: multi_index_container< T hashed_non_unique<...>, ordered_non_unique<...>> my_container; Now, is there anyway I can get all elements of my_container which have a certain hash and sorted according to the second index? In our own local terminology, we call that retrieving all the elements of a given CATEGORY in a sorted manner. (Sorry I'm not still fluent in the MultiIndex syntax to make that more formal with actual code.) TIA,--Hossein

Warning: I'm no expert on Multi-Index, I just find it interesting. On 04/29/2011 11:46 AM, Hossein Haeri wrote:
I have an application where something we call a "categorised container" with colleagues comes very hand. We do currently have a working version and I am looking to see whether it's possible to do some refactoring on that using Multi-Index. Here is an exemplified explanation of what constitutes a decision-making point for me. I would be pleased if anyone here could tell me whether this facility is available under Multi-Index:
multi_index_container < T hashed_non_unique<...>, ordered_non_unique<...>
my_container;
Now, is there anyway I can get all elements of my_container which have a certain hash and sorted according to the second index?
So are you expecting hash collisions and doing something useful with them? This is interesting, can you explain more about the problem and why this is useful?
In our own local terminology, we call that retrieving all the elements of a given CATEGORY in a sorted manner. (Sorry I'm not still fluent in the MultiIndex syntax to make that more formal with actual code.)
Usually the hash values used for data structure hashes are not given any additional meaning. I think you're on the right track by calling them 'categories' and suggest you not try to overload it with the hash value used to index the collection (even if they happen to be the same). Could you just define a member of T to hold the category and index it with any non-unique index type? (maybe even a index with an identity hash function ;-) If you didn't want to store redundant data in T, you could define a key extractor which runs your preferred hash function to calculate the category on the fly. - Marsh

Hossein Haeri
Dear all,
I have an application where something we call a "categorised container" with colleagues comes very hand. We do currently have a working version and I am looking to see whether it's possible to do some refactoring on that using Multi-Index. Here is an exemplified explanation of what constitutes a decision-making point for me. I would be pleased if anyone here could tell me whether this facility is available under Multi-Index:
multi_index_container < T hashed_non_unique<...>, ordered_non_unique<...>
my_container;
Now, is there anyway I can get all elements of my_container which have a certain hash and sorted according to the second index?
Short answer: no, not in an efficient manner. If you use
equal_range on some particular key for the first index, elements
retrieved won't be ordered accoording to the second index. You
need, for instance, to sort the retrieved range externally.
Longer answer: if you replace the first index with an ordered
index with a composite key you can get the behavior you're after:
multi_index_conainer<
T,
ordered_non_unique<
composite_key<
T,
member
my_container;
Now, if you do an equal_range on a category elements will indeed be sorted by the second member. Whether this solution is OK for your problem (having replaced hashing by sorting) is for you to decide, of course. More in on composite keys on http://www.boost.org/libs/multi_index/doc/tutorial/key_extraction.html#compo... Hope this helps, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (3)
-
Hossein Haeri
-
Joaquin M Lopez Munoz
-
Marsh Ray