added two-sequence forms

[SVN r21238]
This commit is contained in:
Dave Abrahams
2003-12-12 16:16:21 +00:00
parent b70e276a01
commit 648b830d58
2 changed files with 111 additions and 10 deletions

View File

@@ -3,8 +3,8 @@
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2000-02
// Aleksey Gurtovoy
// Copyright (c) 2000-03
// Dave Abrahams, Aleksey Gurtovoy
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
@@ -22,9 +22,13 @@
#include "boost/mpl/clear.hpp"
#include "boost/mpl/lambda.hpp"
#include "boost/mpl/apply.hpp"
#include "boost/mpl/pair.hpp"
#include "boost/mpl/protect.hpp"
#include "boost/mpl/iterator_tag.hpp"
#include "boost/mpl/apply_if.hpp"
#include "boost/mpl/aux_/common_name_wknd.hpp"
#include "boost/mpl/aux_/void_spec.hpp"
#include "boost/type_traits/is_same.hpp"
namespace boost {
namespace mpl {
@@ -45,6 +49,48 @@ struct transform_op
};
};
template< typename Op >
struct transform2_op
{
template< typename Sequence, typename T > struct apply
{
typedef typename push_front<
Sequence
, typename apply2<
Op
, typename T::first
, typename T::second
>::type
>::type type;
};
};
template< typename I1, typename I2 >
struct pair_iterator
{
typedef input_iter_tag_ category;
typedef pair<typename I1::type, typename I2::type> type;
typedef pair_iterator<typename I1::next, typename I2::next> next;
};
template<
typename Seq1, typename Seq2
>
struct pair_view
{
public:
typedef nested_begin_end_tag tag;
typedef pair_iterator<
typename mpl::begin<Seq1>::type
, typename mpl::begin<Seq2>::type
> begin;
typedef pair_iterator<
typename mpl::end<Seq1>::type
, typename mpl::end<Seq2>::type
> end;
};
} // namespace aux
BOOST_MPL_AUX_AGLORITHM_NAMESPACE_BEGIN
@@ -53,7 +99,7 @@ template<
typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Sequence)
, typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Operation)
>
struct transform
struct transform1
{
private:
typedef typename lambda<Operation>::type op_;
@@ -67,9 +113,44 @@ struct transform
>::type type;
};
template<
typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Seq1)
, typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Seq2)
, typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Operation)
>
struct transform2
{
private:
typedef typename lambda<Operation>::type op_;
typedef typename clear<Seq1>::type result_;
public:
typedef typename fold_backward<
aux::pair_view<Seq1,Seq2>
, result_
, protect< aux::transform2_op<op_> >
>::type type;
};
template<
typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Seq1)
, typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Seq2OrOperation)
, typename BOOST_MPL_AUX_VOID_SPEC_PARAM(Operation)
>
struct transform
: apply_if<
is_same<Operation,void_>
, transform1<Seq1,Seq2OrOperation>
, transform2<Seq1,Seq2OrOperation,Operation>
>
{
};
BOOST_MPL_AUX_AGLORITHM_NAMESPACE_END
BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(2, transform)
BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(2, transform1)
BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(3, transform2)
BOOST_MPL_AUX_ALGORITHM_VOID_SPEC(3, transform)
} // namespace mpl
} // namespace boost

View File

@@ -3,8 +3,8 @@
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2000-02
// Aleksey Gurtovoy
// Copyright (c) 2000-03
// Aleksey Gurtovoy, Dave Abrahams
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
@@ -16,7 +16,9 @@
#include "boost/mpl/transform.hpp"
#include "boost/mpl/list.hpp"
#include "boost/mpl/list_c.hpp"
#include "boost/mpl/equal.hpp"
#include "boost/mpl/plus.hpp"
#include "boost/type_traits/add_pointer.hpp"
#include "boost/static_assert.hpp"
@@ -25,11 +27,29 @@ namespace mpl = boost::mpl;
int main()
{
using namespace mpl::placeholders;
typedef mpl::list<char,short,int,long,float,double> types;
typedef mpl::list<char*,short*,int*,long*,float*,double*> pointers;
{
typedef mpl::list<char,short,int,long,float,double> types;
typedef mpl::list<char*,short*,int*,long*,float*,double*> pointers;
typedef mpl::transform< types,boost::add_pointer<_1> >::type result;
BOOST_STATIC_ASSERT((mpl::equal<result,pointers>::type::value));
typedef mpl::transform1< types,boost::add_pointer<_1> >::type result1;
BOOST_STATIC_ASSERT((mpl::equal<result1,pointers>::type::value));
typedef mpl::transform< types,boost::add_pointer<_1> >::type result;
BOOST_STATIC_ASSERT((mpl::equal<result,pointers>::type::value));
}
{
typedef mpl::list_c<long,0,2,4,6,8,10> evens;
typedef mpl::list_c<long,2,3,5,7,11,13> primes;
typedef mpl::list_c<long,2,5,9,13,19,23> sums;
typedef mpl::transform2< evens, primes, mpl::plus<> >::type result1;
BOOST_STATIC_ASSERT((mpl::equal<result1::type,sums::type>::type::value));
typedef mpl::transform< evens, primes, mpl::plus<> >::type result;
BOOST_STATIC_ASSERT((mpl::equal<result::type,sums::type>::type::value));
}
return 0;
}