|
Boost : |
From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-11-26 05:10:10
David Abrahams wrote:
> Well I hope that was all instructive, but it was probably way more
> complicated than neccessary. I think it would actually be far superior
> to build a solution around an mpl iterator adaptor like this one:
>
> template <class IteratorSeq> struct zip_iterator
> {
> typedef typename mpl::transform<
> IteratorSeq,mpl::apply0<mpl::_1>
> >::type type;
>
> typedef zip_iterator<
> typename mpl::transform<
> IteratorSeq,mpl::next<mpl::_1>
> >::type
> > next;
> };
>
> and an operator adaptor like this one (don't know how to generalize
> this one to N arguments yet):
>
> template <class Op, class Seq>
> struct binop_adaptor
> : mpl::apply2<
> Op
> , typename mpl::begin<Seq>::type
> , typename mpl::next<mpl::begin<Seq>::type>::type
> >
> {
> };
>
> How about that?
To finish the sketch:
template<
typename Sequences
>
struct zip_view
{
typedef typename transform< Sequences, begin<_1> >::type
first_ones_;
typedef typename transform< Sequences, end<_1> >::type last_ones_;
typedef nested_begin_end_tag tag;
typedef zip_iterator<first_ones_> begin;
typedef zip_iterator<last_ones_> end;
};
// corrected 'binop_adaptor'
template< typename BinaryOp >
struct apply_two_args_seq
{
template< typename Args > struct apply
: apply2<
BinaryOp
, typename at_c<Args,0>::type
, typename at_c<Args,1>::type
>
{
};
};
// user code
typedef range_c<int,0,10> s1;
typedef range_c<int,10,20> s2;
typedef transform_view<
zip_view< list<s1,s2> >
, apply_two_args_seq< plus<> >
> result;
// here!
BOOST_STATIC_ASSERT((equal<
result
, filter_view< range_c<int,10,30>, math::is_even<_> >
, equal_to<_,_>
>::type::value));
The 'zip_view' is in the CVS, as well as the rest of the sample -
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/m
pl/test/zip_view.cpp. 'apply_two_args_seq' needs generalization, so it
didn't go into a header.
Note that the sample doesn't work on all the supported compilers yet.
Aleksey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk