Boost logo

Boost Users :

Subject: Re: [Boost-users] [Multi-Index] Get container view of a non-unique-index equal_range?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-06-11 16:12:43


AMDG

Dominique Devienne wrote:
> Given for example
>
>
> <snip>
>
> I'd like to be able to have a "view" of VehicleIndex, for example
> compatible with unordered_set in this case since using a hashed index,
> for a given vehicle type ("Audi" for example), and going further have
> this view allow inserts which would go into the "master" vehicle index
> under the cover, validated by its unique indexes.
>
> In pseudo-code, something a bit like this:
>
> struct VehicleMake {
> VehicleByType::EqualRangeView& models_;
>
> VehicleMake(const string& name)
> : models_(get<ByType>(get_vehicle_index()).equal_range_view(name))
> {}
> };
>
> With models_ behaving has a normal container one can iterate on
> (begin, end), query (empty, find, count), modify (insert, erase,
> clear, etc...), those operations only affecting the VehicleIndex
> elements matching the VehicleByType given range, and possibly
> asserting all inserts are valid for the range (the vehicles added via
> the "range view" have the type matching the range's type).
>
> Can this be done? Has it been considered as a possible extension, and
> is it even feasible given the current B.MI design?
>
> Any input on how to achieve or emulate the above would be appreciated.
> Thanks, --DD
>

Because of the way multi_index implements hashed_nonunique,
there is no sequence of equal elements. If you try to store a pair
of iterators, they may be invalidated or cease to delimit the
equal_range.

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_;
};

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