Boost logo

Boost Users :

Subject: Re: [Boost-users] [Multi-Index] Categorised Container
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2011-04-30 08:57:01


Hossein Haeri <powerprogman <at> yahoo.com> writes:

>
>
> 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<T,category_t,&T::category>,
        member<T,second_type,&T::second>
>
>,
    ordered_non_unique<...>
> 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#composite_keys

Hope this helps,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net