• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Algorithms/Iteration Algorithms//reverse_fold
2
3reverse_fold
4============
5
6Synopsis
7--------
8
9.. parsed-literal::
10
11    template<
12          typename Sequence
13        , typename State
14        , typename BackwardOp
15        , typename ForwardOp = _1
16        >
17    struct reverse_fold
18    {
19        typedef |unspecified| type;
20    };
21
22
23
24Description
25-----------
26
27Returns the result of the successive application of binary ``BackwardOp`` to the
28result of the previous ``BackwardOp`` invocation (``State`` if it's the first call)
29and every element in the range [``begin<Sequence>::type``, ``end<Sequence>::type``) in
30reverse order. If ``ForwardOp`` is provided, then it is applied on forward
31traversal to form the result that is passed to the first ``BackwardOp`` call.
32
33
34Header
35------
36
37.. parsed-literal::
38
39    #include <boost/mpl/reverse_fold.hpp>
40
41
42
43Parameters
44----------
45
46Parameters
47----------
48
49+---------------+-------------------------------+-----------------------------------------------+
50| Parameter     | Requirement                   | Description                                   |
51+===============+===============================+===============================================+
52| ``Sequence``  | |Forward Sequence|            | A sequence to iterate.                        |
53+---------------+-------------------------------+-----------------------------------------------+
54| ``State``     | Any type                      | The initial state for the first ``BackwardOp``|
55|               |                               | / ``ForwardOp`` application.                  |
56+---------------+-------------------------------+-----------------------------------------------+
57| ``BackwardOp``| Binary |Lambda Expression|    | The operation to be executed on backward      |
58|               |                               | traversal.                                    |
59+---------------+-------------------------------+-----------------------------------------------+
60| ``ForwardOp`` | Binary |Lambda Expression|    | The operation to be executed on forward       |
61|               |                               | traversal.                                    |
62+---------------+-------------------------------+-----------------------------------------------+
63
64
65Expression semantics
66--------------------
67
68For any |Forward Sequence| ``s``, binary |Lambda Expression| ``backward_op`` and ``forward_op``,
69and arbitrary type ``state``:
70
71.. parsed-literal::
72
73    typedef reverse_fold< s,state,backward_op >::type t;
74
75:Return type:
76    A type
77
78:Semantics:
79    Equivalent to
80
81    .. parsed-literal::
82
83        typedef lambda<backward_op>::type op;
84        typedef reverse_iter_fold<
85              s
86            , state
87            , apply_wrap2< op, _1, deref<_2> >
88            >::type t;
89
90
91.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92
93
94.. parsed-literal::
95
96    typedef reverse_fold< s,state,backward_op,forward_op >::type t;
97
98
99:Return type:
100    A type.
101
102:Semantics:
103    Equivalent to
104
105    .. parsed-literal::
106
107        typedef reverse_fold<
108              Sequence
109            , fold<s,state,forward_op>::type
110            , backward_op
111            >::type t;
112
113
114Complexity
115----------
116
117Linear. Exactly ``size<s>::value`` applications of ``backward_op`` and ``forward_op``.
118
119
120Example
121-------
122
123Remove non-negative elements from a sequence [#reverse_fold_note]_.
124
125.. parsed-literal::
126
127    typedef list_c<int,5,-1,0,-7,-2,0,-5,4> numbers;
128    typedef list_c<int,-1,-7,-2,-5> negatives;
129    typedef reverse_fold<
130          numbers
131        , list_c<int>
132        , if_< less< _2,int_<0> >, push_front<_1,_2,>, _1 >
133        >::type result;
134
135    BOOST_MPL_ASSERT(( equal< negatives,result > ));
136
137
138.. [#reverse_fold_note] See ``remove_if`` for a more compact way to do this.
139
140
141See also
142--------
143
144|Algorithms|, |fold|, |reverse_iter_fold|, |iter_fold|
145
146
147.. copyright:: Copyright �  2001-2009 Aleksey Gurtovoy and David Abrahams
148   Distributed under the Boost Software License, Version 1.0. (See accompanying
149   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
150