Boost logo

Boost :

From: vladimir josef sykora (vladimir.sykora_at_[hidden])
Date: 2002-11-26 08:13:10


"David Abrahams" <dave_at_[hidden]> wrote in message
news:uznrx4txh.fsf_at_boost-consulting.com...
> Consider applying the fold algorithm to the first sequence, using an
> adapted binary operation, something like:
>
> // ***** Untested! ********
>
> // A dummy iterator that prevents us running off the end of the RHS
sequence
> template <class T>
> struct prefix_iter
> {
> typedef T next;
> };
>
> template <class binop, class accum = mpl::push_back<mpl::_,mpl::_> >
> struct zip_op
> {
> template <class L, class ResultIterPair>
> struct apply
> {
> // pre-increment the rhs iterator
> typedef typename ResultIterPair::second_type::next rhs_iter;
>
> // Dereference the rhs iterator to get the rhs value
> typedef typename rhs_iter::type rhs;
>
> // x = binop(L,rhs)
> typedef typename mpl::apply2<binop,L,rhs>::type x;
>
> // Get the result of the previous step
> typedef typename ResultIterPair::first_type prev_result;
>
> // accumulate x with that result.
> typedef typename mpl::apply2<accum,prev_result,x>::type result;
>
> // Finally, the return type
> typedef std::pair<rhs_iter,result> type;
> };
> };
>
> typedef mpl::fold<LhsSequence, zip_op<BinaryOperation>
> , std::pair<mpl::begin<RhsSequence>::type,mpl::vector<>
> >::type::second_type answer;
>

I tinkered with it a bit:

template <class binop>
struct zip_op
{
template <class ResultIterPair, class LHS_type>
struct apply
    {
        typedef typename ResultIterPair::first_type
prev_rhs_iter;
        typedef typename ResultIterPair::second_type
prev_result;

        typedef typename prev_rhs_iter::type
prev_rhs;
        typedef typename mpl::apply2<binop,LHS_type,prev_rhs>::type
interim_result;

typedef typename mpl::insert<prev_result,typename mpl::end<prev_result>::typ
e,interim_result>::type result;
        typedef typename prev_rhs_iter::next
next_rhs_iter;
        typedef std::pair<next_rhs_iter,result>
type;
    };
};

// user:
//typedef mpl::vector<...> lhs_seq;
//typedef mpl::vector<...> rhs_seq;
typedef mpl::fold<
  lhs_seq
, std::pair<mpl::begin<rhs_seq>::type,mpl::vector<> >
, zip_op<BinaryOp>
>type::second_type answer;

Works cool, thanks :-)


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