• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  phoenix_style_control_structures.cpp  -- The Boost Lambda Library ------
2 //
3 // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4 // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // For more information, see www.boost.org
11 
12 // -----------------------------------------------------------------------
13 
14 
15 #include <boost/core/lightweight_test.hpp>
16 
17 #include "boost/lambda/lambda.hpp"
18 #include "boost/lambda/if.hpp"
19 #include "boost/lambda/loops.hpp"
20 
21 #include <iostream>
22 #include <vector>
23 #include <list>
24 #include <algorithm>
25 #include <cmath>
26 #include <cassert>
27 #include <functional>
28 
29 
30 
31 using namespace boost::lambda;
32 using namespace std;
33 
34 
35 
36 //  If-else, while, do-while, for statements
37 
38 
main()39 int main()
40 {
41     vector<int> v;
42     v.clear();
43     v.push_back(1);
44     v.push_back(2);
45     v.push_back(3);
46     v.push_back(4);
47     v.push_back(5);
48     v.push_back(6);
49     v.push_back(7);
50     v.push_back(8);
51     v.push_back(9);
52     v.push_back(10);
53 
54     int sum = 0;
55     //////////////////////////////////
56     for_each(v.begin(), v.end(),
57         if_(_1 > 3 && _1 <= 8)
58         [
59             sum += _1
60         ]
61     );
62 
63     BOOST_TEST_EQ(sum, 4+5+6+7+8);
64 
65     int gt = 0, eq = 0, lt = 0;
66     //////////////////////////////////
67     for_each(v.begin(), v.end(),
68         if_(_1 > 5)
69         [
70             ++var(gt)
71         ]
72         .else_
73         [
74             if_(_1 == 5)
75             [
76                 ++var(eq)
77             ]
78             .else_
79             [
80                 ++var(lt)
81             ]
82         ]
83     );
84 
85     BOOST_TEST_EQ(lt, 4);
86     BOOST_TEST_EQ(eq, 1);
87     BOOST_TEST_EQ(gt, 5);
88 
89     vector<int> t = v;
90 
91     int counta = 0;
92     int countb = 0;
93     //////////////////////////////////
94     for_each(v.begin(), v.end(),
95         (
96             while_(_1--)
97             [
98                 ++var(counta)
99             ],
100             ++var(countb)
101         )
102     );
103 
104     BOOST_TEST_EQ(counta, 55);
105     BOOST_TEST_EQ(countb, 10);
106 
107 
108     v = t;
109 
110     counta = 0; countb = 0;
111     //////////////////////////////////
112     for_each(v.begin(), v.end(),
113         (
114             do_
115             [
116              ++var(counta)
117             ]
118             .while_(_1--),
119             ++var(countb)
120         )
121     );
122 
123     BOOST_TEST_EQ(counta, (2+11)*10/2);
124     BOOST_TEST_EQ(countb, 10);
125 
126 
127     v = t;
128     counta = 0; countb = 0;
129     //////////////////////////////////
130     int iii;
131     for_each(v.begin(), v.end(),
132         (
133             for_(var(iii) = 0, var(iii) < _1, ++var(iii))
134             [
135               ++var(counta)
136             ],
137             ++var(countb)
138         )
139     );
140 
141     BOOST_TEST_EQ(counta, (1+10)*10/2);
142     BOOST_TEST_EQ(countb, 10);
143 
144     v = t;
145 
146     return boost::report_errors();
147 }
148 
149