📄 fold.hpp
字号:
/**/ #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PROTO_MAX_ARITY, <boost/proto/transform/fold.hpp>)) #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 <tt>fusion::fold\<\></tt> /// algorithm to accumulate template<typename Sequence, typename State0, typename Fun> struct fold : transform<fold<Sequence, State0, Fun> > { template<typename Expr, typename State, typename Data> struct impl : transform_impl<Expr, State, Data> { /// \brief A Fusion sequence. typedef typename remove_reference< typename when<_, Sequence>::template impl<Expr, State, Data>::result_type >::type sequence; /// \brief An initial state for the fold. typedef typename remove_reference< typename when<_, State0>::template impl<Expr, State, Data>::result_type >::type state0; /// \brief <tt>fun(v)(e,s) == when\<_,Fun\>()(e,s,v)</tt> typedef detail::as_callable<Fun, Data> 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 <tt>when\<_, Sequence\>()(expr, state, data)</tt>, let /// \c state0 be <tt>when\<_, State0\>()(expr, state, data)</tt>, and /// let \c fun(data) be an object such that <tt>fun(data)(expr, state)</tt> /// is equivalent to <tt>when\<_, Fun\>()(expr, state, data)</tt>. Then, this /// function returns <tt>fusion::fold(seq, state0, fun(data))</tt>. /// /// \param expr The current expression /// \param state The current state /// \param data An arbitrary data result_type operator ()( typename impl::expr_param expr , typename impl::state_param state , typename impl::data_param data ) const { typename when<_, Sequence>::template impl<Expr, State, Data> sequence; detail::as_callable<Fun, Data> fun(data); return fusion::fold( BOOST_PROTO_AS_FUSION_SEQUENCE(sequence(expr, state, data)) , typename when<_, State0>::template impl<Expr, State, Data>()(expr, state, data) , fun ); } }; }; /// \brief A PrimitiveTransform that is the same as the /// <tt>fold\<\></tt> transform, except that it folds /// back-to-front instead of front-to-back. It uses /// the \c _reverse callable PolymorphicFunctionObject /// to create a <tt>fusion::reverse_view\<\></tt> of the /// sequence before invoking <tt>fusion::fold\<\></tt>. template<typename Sequence, typename State0, typename Fun> struct reverse_fold : fold<call<_reverse(Sequence)>, 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<typename State0, typename Fun> struct fold<_, State0, Fun> : transform<fold<_, State0, Fun> > { template<typename Expr, typename State, typename Data> struct impl : detail::fold_impl<State0, Fun, Expr, State, Data> {}; }; // This specialization is only for improved compile-time performance // in the commom case when the Sequence transform is \c proto::_. // /// INTERNAL ONLY /// template<typename State0, typename Fun> struct reverse_fold<_, State0, Fun> : transform<reverse_fold<_, State0, Fun> > { template<typename Expr, typename State, typename Data> struct impl : detail::reverse_fold_impl<State0, Fun, Expr, State, Data> {}; }; /// INTERNAL ONLY /// template<typename Sequence, typename State, typename Fun> struct is_callable<fold<Sequence, State, Fun> > : mpl::true_ {}; /// INTERNAL ONLY /// template<typename Sequence, typename State, typename Fun> struct is_callable<reverse_fold<Sequence, State, Fun> > : mpl::true_ {}; }} #endif#else #define N BOOST_PP_ITERATION() template<typename State0, typename Fun, typename Expr, typename State, typename Data> struct fold_impl<State0, Fun, Expr, State, Data, N> : transform_impl<Expr, State, Data> { typedef typename when<_, State0>::template impl<Expr, State, Data>::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 expr , typename fold_impl::state_param state , typename fold_impl::data_param data ) const { state0 s0 = typename when<_, State0>::template impl<Expr, State, Data>()(expr, state, data); BOOST_PP_REPEAT(N, BOOST_PROTO_FOLD_STATE, N) return BOOST_PP_CAT(s, N); } }; template<typename State0, typename Fun, typename Expr, typename State, typename Data> struct reverse_fold_impl<State0, Fun, Expr, State, Data, N> : transform_impl<Expr, State, Data> { typedef typename when<_, State0>::template impl<Expr, State, Data>::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 expr , typename reverse_fold_impl::state_param state , typename reverse_fold_impl::data_param data ) const { BOOST_PP_CAT(state, N) BOOST_PP_CAT(s, N) = typename when<_, State0>::template impl<Expr, State, Data>()(expr, state, data); BOOST_PP_REPEAT(N, BOOST_PROTO_REVERSE_FOLD_STATE, N) return s0; } }; #undef N#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -