diff --git a/include/boost/mpl/transform.hpp b/include/boost/mpl/transform.hpp index 131d230..f8b3149 100644 --- a/include/boost/mpl/transform.hpp +++ b/include/boost/mpl/transform.hpp @@ -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 type; + typedef pair_iterator next; +}; + +template< + typename Seq1, typename Seq2 + > +struct pair_view +{ + public: + typedef nested_begin_end_tag tag; + typedef pair_iterator< + typename mpl::begin::type + , typename mpl::begin::type + > begin; + + typedef pair_iterator< + typename mpl::end::type + , typename mpl::end::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::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::type op_; + typedef typename clear::type result_; + + public: + typedef typename fold_backward< + aux::pair_view + , result_ + , protect< aux::transform2_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 + , transform1 + , transform2 + > +{ +}; + 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 diff --git a/test/transform.cpp b/test/transform.cpp index 2e53026..c53acf4 100644 --- a/test/transform.cpp +++ b/test/transform.cpp @@ -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 types; - typedef mpl::list pointers; + + { + typedef mpl::list types; + typedef mpl::list pointers; - typedef mpl::transform< types,boost::add_pointer<_1> >::type result; - BOOST_STATIC_ASSERT((mpl::equal::type::value)); + typedef mpl::transform1< types,boost::add_pointer<_1> >::type result1; + BOOST_STATIC_ASSERT((mpl::equal::type::value)); + + typedef mpl::transform< types,boost::add_pointer<_1> >::type result; + BOOST_STATIC_ASSERT((mpl::equal::type::value)); + } + + { + typedef mpl::list_c evens; + typedef mpl::list_c primes; + typedef mpl::list_c sums; + + typedef mpl::transform2< evens, primes, mpl::plus<> >::type result1; + BOOST_STATIC_ASSERT((mpl::equal::type::value)); + + typedef mpl::transform< evens, primes, mpl::plus<> >::type result; + BOOST_STATIC_ASSERT((mpl::equal::type::value)); + } return 0; }