1 | /*=============================================================================
|
---|
2 | Copyright (c) 2003 Joel de Guzman
|
---|
3 | Copyright (c) 2004 Peder Holt
|
---|
4 |
|
---|
5 | Use, modification and distribution is subject to the Boost Software
|
---|
6 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
---|
7 | http://www.boost.org/LICENSE_1_0.txt)
|
---|
8 | ==============================================================================*/
|
---|
9 | #if !defined(FUSION_ALGORITHM_DETAIL_FOLD_IPP)
|
---|
10 | #define FUSION_ALGORITHM_DETAIL_FOLD_IPP
|
---|
11 |
|
---|
12 | #include <boost/mpl/bool.hpp>
|
---|
13 | #include <boost/type_traits/is_same.hpp>
|
---|
14 | #include <boost/spirit/fusion/sequence/begin.hpp>
|
---|
15 | #include <boost/spirit/fusion/sequence/end.hpp>
|
---|
16 | #include <boost/spirit/fusion/iterator/value_of.hpp>
|
---|
17 | #include <boost/spirit/fusion/iterator/next.hpp>
|
---|
18 |
|
---|
19 | namespace boost { namespace fusion { namespace detail
|
---|
20 | {
|
---|
21 | template <typename Iterator, typename State, typename F>
|
---|
22 | struct fold_apply
|
---|
23 | {
|
---|
24 | typedef typename fusion_apply2<F,
|
---|
25 | typename meta::value_of<Iterator>::type, State
|
---|
26 | >::type type;
|
---|
27 | };
|
---|
28 |
|
---|
29 | template <typename First, typename Last, typename State, typename F>
|
---|
30 | struct static_fold;
|
---|
31 |
|
---|
32 | template <typename First, typename Last, typename State, typename F>
|
---|
33 | struct next_result_of_fold
|
---|
34 | {
|
---|
35 | typedef typename
|
---|
36 | static_fold<
|
---|
37 | typename meta::next<First>::type
|
---|
38 | , Last
|
---|
39 | , typename fold_apply<First, State, F>::type
|
---|
40 | , F
|
---|
41 | >::type
|
---|
42 | type;
|
---|
43 | };
|
---|
44 |
|
---|
45 | template <typename First, typename Last, typename State, typename F>
|
---|
46 | struct static_fold
|
---|
47 | {
|
---|
48 | typedef typename
|
---|
49 | mpl::if_<
|
---|
50 | is_same<First, Last>
|
---|
51 | , mpl::identity<State>
|
---|
52 | , next_result_of_fold<First, Last, State, F>
|
---|
53 | >::type
|
---|
54 | result;
|
---|
55 |
|
---|
56 | typedef typename result::type type;
|
---|
57 | };
|
---|
58 |
|
---|
59 | // terminal case
|
---|
60 | template <typename First, typename Last, typename State, typename F>
|
---|
61 | inline State const&
|
---|
62 | fold(First const&, Last const&, State const& state, F const&, mpl::true_)
|
---|
63 | {
|
---|
64 | return state;
|
---|
65 | }
|
---|
66 |
|
---|
67 | // non-terminal case
|
---|
68 | template <typename First, typename Last, typename State, typename F>
|
---|
69 | inline typename static_fold<First, Last, State, F>::type
|
---|
70 | fold(
|
---|
71 | First const& first
|
---|
72 | , Last const& last
|
---|
73 | , State const& state
|
---|
74 | , F const& f
|
---|
75 | , mpl::false_)
|
---|
76 | {
|
---|
77 | return detail::fold(
|
---|
78 | fusion::next(first)
|
---|
79 | , last
|
---|
80 | , f(*first, state)
|
---|
81 | , f
|
---|
82 | , is_same<BOOST_DEDUCED_TYPENAME meta::next<First>::type, Last>()
|
---|
83 | );
|
---|
84 | }
|
---|
85 | }}}
|
---|
86 |
|
---|
87 | #endif
|
---|
88 |
|
---|