• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Algorithms/Iteration Algorithms//iter_fold
2
3iter_fold
4=========
5
6Synopsis
7--------
8
9.. parsed-literal::
10
11    template<
12          typename Sequence
13        , typename State
14        , typename ForwardOp
15        >
16    struct iter_fold
17    {
18        typedef |unspecified| type;
19    };
20
21
22
23Description
24-----------
25
26Returns the result of the successive application of binary ``ForwardOp`` to the result
27of the previous ``ForwardOp`` invocation (``State`` if it's the first call) and each
28iterator in the range [``begin<Sequence>::type``, ``end<Sequence>::type``) in order.
29
30
31Header
32------
33
34.. parsed-literal::
35
36    #include <boost/mpl/iter_fold.hpp>
37
38
39
40Parameters
41----------
42
43+---------------+-------------------------------+---------------------------------------------------+
44| Parameter     | Requirement                   | Description                                       |
45+===============+===============================+===================================================+
46| ``Sequence``  | |Forward Sequence|            | A sequence to iterate.                            |
47+---------------+-------------------------------+---------------------------------------------------+
48| ``State``     | Any type                      | The initial state for the first ``ForwardOp``     |
49|               |                               | application.                                      |
50+---------------+-------------------------------+---------------------------------------------------+
51| ``ForwardOp`` | Binary |Lambda Expression|    | The operation to be executed on forward           |
52|               |                               | traversal.                                        |
53+---------------+-------------------------------+---------------------------------------------------+
54
55
56Expression semantics
57--------------------
58
59For any |Forward Sequence| ``s``, binary |Lambda Expression| ``op``, and an
60arbitrary type ``state``:
61
62
63.. parsed-literal::
64
65    typedef iter_fold<s,state,op>::type t;
66
67:Return type:
68    A type.
69
70:Semantics:
71    Equivalent to
72
73    .. parsed-literal::
74
75        typedef begin<s>::type i\ :sub:`1`;
76        typedef apply<op,state,i\ :sub:`1`>::type state\ :sub:`1`;
77        typedef next<i\ :sub:`1`>::type i\ :sub:`2`;
78        typedef apply<op,state\ :sub:`1`,i\ :sub:`2`>::type state\ :sub:`2`;
79        |...|
80        typedef apply<op,state\ :sub:`n-1`,i\ :sub:`n`>::type state\ :sub:`n`;
81        typedef next<i\ :sub:`n`>::type last;
82        typedef state\ :sub:`n` t;
83
84    where ``n == size<s>::value`` and ``last`` is identical to ``end<s>::type``; equivalent
85    to ``typedef state t;`` if ``empty<s>::value == true``.
86
87
88
89Complexity
90----------
91
92Linear. Exactly ``size<s>::value`` applications of ``op``.
93
94
95Example
96-------
97
98.. parsed-literal::
99
100    typedef vector_c<int,5,-1,0,7,2,0,-5,4> numbers;
101    typedef iter_fold<
102          numbers
103        , begin<numbers>::type
104        , if_< less< deref<_1>, deref<_2> >,_2,_1 >
105        >::type max_element_iter;
106
107    BOOST_MPL_ASSERT_RELATION( deref<max_element_iter>::type::value, ==, 7 );
108
109
110
111See also
112--------
113
114|Algorithms|, |reverse_iter_fold|, |fold|, |reverse_fold|, |copy|
115
116
117.. copyright:: Copyright �  2001-2009 Aleksey Gurtovoy and David Abrahams
118   Distributed under the Boost Software License, Version 1.0. (See accompanying
119   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
120