#ifndef BOOST_PP_IS_ITERATING /////////////////////////////////////////////////////////////////////////////// /// \file fold.hpp /// Contains definition of the fold<> and reverse_fold<> transforms. // // Copyright 2008 Eric Niebler. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_PROTO_TRANSFORM_FOLD_HPP_EAN_11_04_2007 #define BOOST_PROTO_TRANSFORM_FOLD_HPP_EAN_11_04_2007 #include #include #include #include #include #include #include #if BOOST_VERSION >= 103500 #include #else #include #endif #include #include #include #include #include #include namespace boost { namespace proto { namespace detail { template struct as_callable { as_callable(Data v) : v_(v) {} template struct result; template struct result { typedef typename when<_, Transform>::template impl::result_type type; }; #if BOOST_VERSION < 103500 template struct apply : result {}; #endif template typename when<_, Transform>::template impl::result_type operator ()(Expr &e, State const &s) const { return typename when<_, Transform>::template impl()(e, s, this->v_); } private: Data v_; }; #if BOOST_VERSION < 103500 template struct as_fusion_sequence_type { typedef Sequence const type; }; template Sequence const &as_fusion_sequence(Sequence const &sequence, ...) { return sequence; } template struct as_fusion_sequence_type { typedef typename Sequence::proto_base_expr const type; }; template typename Sequence::proto_base_expr const &as_fusion_sequence(Sequence const &sequence, int) { return sequence.proto_base(); } #define BOOST_PROTO_AS_FUSION_SEQUENCE_TYPE(X) typename detail::as_fusion_sequence_type::type #define BOOST_PROTO_AS_FUSION_SEQUENCE(X) detail::as_fusion_sequence(X, 0) #else #define BOOST_PROTO_AS_FUSION_SEQUENCE_TYPE(X) X #define BOOST_PROTO_AS_FUSION_SEQUENCE(X) X #endif template< typename State0 , typename Fun , typename Expr , typename State , typename Data , long Arity = arity_of::value > struct fold_impl {}; template< typename State0 , typename Fun , typename Expr , typename State , typename Data , long Arity = arity_of::value > struct reverse_fold_impl {}; #define BOOST_PROTO_CHILD_N_TYPE(N)\ BOOST_PP_CAT(proto_child, N)\ /**/ #define BOOST_PROTO_FOLD_STATE_TYPE(Z, N, DATA) \ typedef \ typename when<_, Fun>::template impl< \ typename result_of::child_c::type \ , BOOST_PP_CAT(state, N) \ , Data \ >::result_type \ BOOST_PP_CAT(state, BOOST_PP_INC(N)); \ /**/ #define BOOST_PROTO_FOLD_STATE(Z, N, DATA) \ BOOST_PP_CAT(state, BOOST_PP_INC(N)) \ BOOST_PP_CAT(s, BOOST_PP_INC(N)) \ = typename when<_, Fun>::template impl< \ typename result_of::child_c::type \ , BOOST_PP_CAT(state, N) \ , Data \ >()( \ proto::child_c(e) \ , BOOST_PP_CAT(s, N) \ , d \ ); \ /**/ #define BOOST_PROTO_REVERSE_FOLD_STATE_TYPE(Z, N, DATA) \ typedef \ typename when<_, Fun>::template impl< \ typename result_of::child_c< \ Expr \ , BOOST_PP_SUB(DATA, BOOST_PP_INC(N)) \ >::type \ , BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, N)) \ , Data \ >::result_type \ BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, BOOST_PP_INC(N))); \ /**/ #define BOOST_PROTO_REVERSE_FOLD_STATE(Z, N, DATA) \ BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, BOOST_PP_INC(N))) \ BOOST_PP_CAT(s, BOOST_PP_SUB(DATA, BOOST_PP_INC(N))) \ = typename when<_, Fun>::template impl< \ typename result_of::child_c< \ Expr \ , BOOST_PP_SUB(DATA, BOOST_PP_INC(N)) \ >::type \ , BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, N)) \ , Data \ >()( \ proto::child_c(e) \ , BOOST_PP_CAT(s, BOOST_PP_SUB(DATA, N)) \ , d \ ); \ /**/ #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PROTO_MAX_ARITY, )) #include BOOST_PP_ITERATE() #undef BOOST_PROTO_REVERSE_FOLD_STATE #undef BOOST_PROTO_REVERSE_FOLD_STATE_TYPE #undef BOOST_PROTO_FOLD_STATE #undef BOOST_PROTO_FOLD_STATE_TYPE #undef BOOST_PROTO_CHILD_N_TYPE } // namespace detail /// \brief A PrimitiveTransform that invokes the fusion::fold\<\> /// algorithm to accumulate template struct fold : transform > { template struct impl : transform_impl { /// \brief A Fusion sequence. typedef typename remove_reference< typename when<_, Sequence>::template impl::result_type >::type sequence; /// \brief An initial state for the fold. typedef typename remove_reference< typename when<_, State0>::template impl::result_type >::type state0; /// \brief fun(v)(e,s) == when\<_,Fun\>()(e,s,v) typedef detail::as_callable fun; typedef typename fusion::BOOST_PROTO_FUSION_RESULT_OF::fold< BOOST_PROTO_AS_FUSION_SEQUENCE_TYPE(sequence) , state0 , fun >::type result_type; /// Let \c seq be when\<_, Sequence\>()(e, s, d), let /// \c state0 be when\<_, State0\>()(e, s, d), and /// let \c fun(d) be an object such that fun(d)(e, s) /// is equivalent to when\<_, Fun\>()(e, s, d). Then, this /// function returns fusion::fold(seq, state0, fun(d)). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data result_type operator ()( typename impl::expr_param e , typename impl::state_param s , typename impl::data_param d ) const { typename when<_, Sequence>::template impl seq; detail::as_callable f(d); return fusion::fold( BOOST_PROTO_AS_FUSION_SEQUENCE(seq(e, s, d)) , typename when<_, State0>::template impl()(e, s, d) , f ); } }; }; /// \brief A PrimitiveTransform that is the same as the /// fold\<\> transform, except that it folds /// back-to-front instead of front-to-back. It uses /// the \c _reverse callable PolymorphicFunctionObject /// to create a fusion::reverse_view\<\> of the /// sequence before invoking fusion::fold\<\>. template struct reverse_fold : fold, State0, Fun> {}; // This specialization is only for improved compile-time performance // in the commom case when the Sequence transform is \c proto::_. // /// INTERNAL ONLY /// template struct fold<_, State0, Fun> : transform > { template struct impl : detail::fold_impl {}; }; // This specialization is only for improved compile-time performance // in the commom case when the Sequence transform is \c proto::_. // /// INTERNAL ONLY /// template struct reverse_fold<_, State0, Fun> : transform > { template struct impl : detail::reverse_fold_impl {}; }; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; }} #endif #else #define N BOOST_PP_ITERATION() template struct fold_impl : transform_impl { typedef typename when<_, State0>::template impl::result_type state0; BOOST_PP_REPEAT(N, BOOST_PROTO_FOLD_STATE_TYPE, N) typedef BOOST_PP_CAT(state, N) result_type; result_type operator ()( typename fold_impl::expr_param e , typename fold_impl::state_param s , typename fold_impl::data_param d ) const { state0 s0 = typename when<_, State0>::template impl()(e, s, d); BOOST_PP_REPEAT(N, BOOST_PROTO_FOLD_STATE, N) return BOOST_PP_CAT(s, N); } }; template struct reverse_fold_impl : transform_impl { typedef typename when<_, State0>::template impl::result_type BOOST_PP_CAT(state, N); BOOST_PP_REPEAT(N, BOOST_PROTO_REVERSE_FOLD_STATE_TYPE, N) typedef state0 result_type; result_type operator ()( typename reverse_fold_impl::expr_param e , typename reverse_fold_impl::state_param s , typename reverse_fold_impl::data_param d ) const { BOOST_PP_CAT(state, N) BOOST_PP_CAT(s, N) = typename when<_, State0>::template impl()(e, s, d); BOOST_PP_REPEAT(N, BOOST_PROTO_REVERSE_FOLD_STATE, N) return s0; } }; #undef N #endif