Re: [boost] MPL: clever way to remove *all* duplicates in a (nonintegral) sequence?

"Brian Braatz" <brianb@rmtg.com> writes:
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)
[David Abrahams Writes:]
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!
[Brian Braatz Writes:] :) Thank you for the advice. There is a common mental pattern that occurs: "Hmm.. there SHOULD be a SIMPLE way to do X." (flip through MPL book, MPL ref manual, google boost archives, write sample code) "Hmm.. Ok I can see there maybe a way to do this COMPLETELY from scratch, but I am not totally certain I need to do that. I need to keep looking. There probably is a clever one liner to solve the problem".... Your advice was EXACLY what I needed. (i..e how to THINK about the solution) THANK YOU :) Brian

"Brian Braatz" <brianb@rmtg.com> writes:
There is a common mental pattern that occurs:
"Hmm.. there SHOULD be a SIMPLE way to do X." (flip through MPL book, MPL ref manual, google boost archives, write sample code) "Hmm.. Ok I can see there maybe a way to do this COMPLETELY from scratch, but I am not totally certain I need to do that. I need to keep looking. There probably is a clever one liner to solve the problem"....
Your advice was EXACLY what I needed. (i..e how to THINK about the solution)
THANK YOU
You're welcome. If you wouldn't mind writing up the answer for the wiki, that would be awesome. Not sure where it should go; neither http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?CPP_Template_... nor http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Effective_MPL seem to be quite right. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
Brian Braatz
-
David Abrahams