|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2005-06-17 06:20:52
"Brian Braatz" <brianb_at_[hidden]> writes:
> i.e.
>
> the example for mpl::unique shows:
>
> typedef vector<int,float,float,char,int,int,int,double> types;
> typedef vector<int,float,char,int,double> expected;
> typedef unique< types, is_same<_1,_2> >::type result;
>
> BOOST_MPL_ASSERT(( equal< result,expected > ));
>
> note that type "expected" :
> typedef vector<int,float,char,int,double> expected;
>
> still has TWO ints in it.
>
> I am looking for a way to basically do this:
>
> typedef vector<int,float,float,char,int,int,int,double> types;
> typedef vector<int,float,char, double> expected;
> typedef unique_all< types, is_same<_1,_2> >::type result;
>
> BOOST_MPL_ASSERT(( equal< result,expected > ));
>
>
> (note the examples I am using use integral types, in my situation I am
> not using integral types)
>
> I dug through the archives and found a few mentions of people asking
> this question and talking about it- but no substantial solution.
If you don't need to preserve order, just stick them in an
mpl::set<>. In fact, you might be able to use a set<> to begin with
and just skip the vector<>.
Otherwise, I would write a little algorithm based on the
implementation of remove_if (which you can find nicely described in
the online reference manual). Instead of passing along a predicate at
each stage of the fold<>, pass along a set<> that accumulates the
elements you've already seen. When the next element is already in the
set<>, skip it.
Try working that out for yourself; it'll be good for you ;-)
If you get stuck, ask again!
HTH,
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk