Boost logo

Boost Users :

Subject: Re: [Boost-users] [multi-index] retrieve unique keys of a non-unique index
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2013-08-02 03:21:06


On Thu, Aug 1, 2013 at 10:47 PM, Rory McCarthy <
rorymccarthy77_at_[hidden]> wrote:

> In the following example is there a more efficient way to retrieve the
> unique keys of a non-unique index?
>
> typedef multi_index_container<
> TaggedNodeEntry,
> indexed_by<
> hashed_non_unique<
> tag<name>,
> BOOST_MULTI_INDEX_MEMBER(**TaggedNodeEntry, int, name)
> >,
> ... >
> > TaggedNodes;
>
> int main() { ...
> boost::multi_index::index<**TaggedNodes, name>::type::iterator ic0 =
> get<name>(taggedNodes).begin()**;
> boost::multi_index::index<**TaggedNodes, name>::type::iterator ic1 =
> get<name>(taggedNodes).end();
> while (ic0 != ic1) {
> std::cout << ic0->name << std::endl;
> ic0 = get<name>(taggedNodes).equal_**range(ic0->name).second;
> }
>

Why not simply switch from hashed_non_unique to ordered_non_unique?

That way you can full-scan that ordered index, and check yourself when the
key changes, since you are guaranteed the entries will be clustered by the
key, w/o resorting to equal_range() like for the hashed container. You lose
a bit of speed for arbitrary lookups compared to the hashed index (not much
I suspect), but should gain on getting the unique keys IMHO. --DD



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