Boost logo

Boost Users :

Subject: Re: [Boost-users] [Multi-Index] Get container view of a non-unique-index equal_range?
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2009-06-11 17:27:21


On Thu, Jun 11, 2009 at 3:12 PM, Steven Watanabe<watanabesj_at_[hidden]> wrote:
> AMDG
>
> Dominique Devienne wrote:
>>  <snip>
>> I'd like to be able to have a "view" of VehicleIndex, for example
>
> Because of the way multi_index implements hashed_non_unique,
> there is no sequence of equal elements.

What if I switch to using an ordered rather than hashed non-unique index?
Would that change your answer?

> If you try to store a pair of iterators,
> they may be invalidated or cease to delimit the equal_range.

Aren't B.MI iterators pretty resilient in the face of changes to the
container? I remember reading that in the doc, and was actually
thinking of replacing some yummy code (below) that takes copies of a
container with a B.MI, since my reading of the doc was that i, as an
iterator from the B.MI, would not be invalidated by side-effects of
calling detach() which end up erasing other elements (or itself) from
observers_. Did I read that wrong?

        ObserverList copy = *observers_ ;
        for (ObserverList::iterator i(copy.begin()); i != copy.end(); ++i) {
           Observer* obs = *i;
           // still a valid observer?
           if (observers_->contains(obs)) {
               if (obs) {
                   obs->detach(this) ;
               } else {
                   cerr << "null observer ignored" << endl;
               }
           }
        }

> You could try looking up the iterators on demand,
> if you don't mind the extra overhead.
>
> template<class MultiIndexContainer, class Tag, class Key>
> struct equal_range_view {
> public:
>   equal_range_view(MultiIndexContainer& c, const Key& k) : container_(c),
> key_(k) {}
>   typedef ... iterator;
>   iterator begin() const { return(container_.equal_range(key_).first); }
>   iterator end() const { return(container_.equal_range(key_).second); }
> private:
>   MultiIndexContainer& container_;
>   Key key_;
> };

That's two equal_range lookups everytime to iterate the range. I was
hoping that B.MI had some kind of "logical" begin-range, end-range
iterators that adjusted themselves automatically upon inserts and
updates in fact. Am I in luck and using an ordered_non_unique index
give me that? Maybe in 1.40 ;-) --DD

> In Christ,
> Steven Watanabe


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