Boost logo

Boost :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2002-11-21 03:34:08


Note that MPL already have algorithm that works on "sorted" sequences:
mpl::unique.

Here is my modification of it that should work for "non-sorted" one:

#include "boost/mpl/find_if.hpp"
#include "boost/mpl/bind.hpp"

#include "boost/mpl/fold_backward.hpp"
#include "boost/mpl/push_front.hpp"
#include "boost/mpl/clear.hpp"
#include "boost/mpl/front.hpp"
#include "boost/mpl/identity.hpp"
#include "boost/mpl/lambda.hpp"
#include "boost/mpl/apply_if.hpp"
#include "boost/mpl/apply.hpp"
#include "boost/mpl/void.hpp"
#include "boost/mpl/aux_/void_spec.hpp"
#include "boost/mpl/aux_/lambda_spec.hpp"
#include "boost/mpl/aux_/config/eti.hpp"
#include "boost/type_traits/is_same.hpp"

namespace boost {
namespace mpl {

namespace aux {

template< typename Predicate >
struct unique_op
{
    template< typename Seq, typename T > struct apply
    {
        typedef typename find_if< Seq, bind2<Predicate,_,T> >::type iter;
        typedef typename apply_if<
            typename is_same< iter, typename end<Seq>::type >::type
            , push_front<Seq,T>
            , identity<Seq>
>::type type;
    };
};

} // namespace aux

BOOST_MPL_AUX_PASS_THROUGH_LAMBDA_SPEC(1,aux::unique_op)

template<
      typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Sequence)
    , typename Predicate = is_same<_,_>
>
struct unique
{
 private:
    typedef typename lambda<Predicate>::type pred_;
    typedef typename clear<Sequence>::type result_;

 public:
    typedef typename fold_backward<
          Sequence
        , result_
        , aux::unique_op<pred_>
>::type type;
};

BOOST_MPL_AUX_VOID_SPEC(1, unique)

} // namespace mpl
} // namespace boost

It does have O(n^2) complexity. And I also do not know how to overcome it.

Gennadiy.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk