C++ Library Extensions 2022.12.09
To help learn modern C++ programming
for_workhorse.cpp
Go to the documentation of this file.
1/*
2 Author: Thomas Kim
3 First Edit: Feb. 04, 2021
4
5 Requirements: C++ compilers that supports C++17 Standards
6
7 clang++ -std=c++17 filename.cpp -ltbb -o output.exe
8 g++ -std=c++17 filename.cpp -ltbb -o output.exe
9 cl /EHsc /std:c++17 filename.cpp /Fe: output.exe
10 dpcpp -Qstd=c++17 filename.cpp -o output.exe
11*/
12
13#include <tpf_output.hpp>
14
15namespace types = tpf::types;
16
18auto& endl = tpf::endl; // one carriage-return and flush out to console
19auto& endL = tpf::endL; // two carriage-returns and flush out to console
20
22{
23 stream <<"\n--- for_workhorse(), Powerful Compile-Time Loop " << endL;
24
25 stream <<" 1. for_workhorse<10>(lambda) - for_workhorse<End>(lambda)" << endL;
26 stream <<"\t";
27
28 types::for_workhorse<10>(
29
30 [](auto i)
31 {
32 stream << i.Index <<", ";
33 }
34 );
35
36 stream <<" <- [ 0, 10 ), Span: +1, 10 not included" << endL;
37
38 stream <<" 2. for_workhorse<10, 20>(lambda) - for_workhorse<Start, End>(lambda)" << endL;
39 stream <<"\t";
40
41 types::for_workhorse<10, 20>(
42
43 [](auto i)
44 {
45 stream << i.Index <<", ";
46 }
47 );
48
49 stream <<" <- [ 10, 20 ), Span: +1, 20 not included" << endL;
50
51 stream <<" 3. for_workhorse<0, 20, 2>(lambda) - for_workhorse<Start, End, Increment>(lambda)" << endL;
52 stream <<"\t";
53
54 types::for_workhorse<0, 20, 2>(
55
56 [](auto i)
57 {
58 stream << i.Index <<", ";
59 }
60 );
61
62 stream <<" <- [ 0, 20 ), Span: +2, 20 not included" << endL;
63
64 stream <<" 4. for_workhorse<1, 20, 2>(lambda) - for_workhorse<Start, End, Increment>(lambda)" << endL;
65 stream <<"\t";
66
67 types::for_workhorse<1, 20, 2>(
68
69 [](auto i)
70 {
71 stream << i.Index <<", ";
72 }
73 );
74
75 stream <<" <- [ 1, 20 ), Span: +2, 20 not included" << endL;
76
77 stream <<" 5. for_workhorse<20, 0, -2>(lambda) - for_workhorse<Start, End, Increment>(lambda)" << endL;
78 stream <<"\t";
79
80 types::for_workhorse<20, 0, -2>(
81
82 [](auto i)
83 {
84 stream << i.Index <<", ";
85 }
86 );
87
88 stream <<" <- [ 20, 0 ), Span: -2, 0 not included" <<endL;
89
90 stream <<" 6. for_workhorse<19, 0, -2>(lambda) - for_workhorse<Start, End, Increment>(lambda)" << endL;
91 stream <<"\t";
92
93 types::for_workhorse<19, 0, -2>(
94
95 [](auto i)
96 {
97 stream << i.Index <<", ";
98 }
99 );
100
101 stream <<" <- [ 19, 0 ), Span: -2, 0 not included" <<endL;
102
103 stream <<" 7. for_workhorse<10, 0, -2>(lambda) - for_workhorse<Start, End, Increment>(lambda)" << endL;
104 stream <<"\t";
105
106 types::for_workhorse<10, 0, -2>(
107
108 [](auto i)
109 {
110 stream << i.Index <<", ";
111 }
112 );
113
114 stream <<" <- [ 10, 0 ), Span: -2, 0 not included" <<endL;
115
116 stream <<" 8. for_workhorse<9, 0, -2>(lambda) - for_workhorse<Start, End, Increment>(lambda)" << endL;
117 stream <<"\t";
118
119 types::for_workhorse<9, 0, -2>(
120
121 [](auto i)
122 {
123 stream << i.Index <<", ";
124 }
125 );
126
127 stream <<" <- [ 9, 0 ), Span: -2, 0 not included" <<endL;
128
129}
130
132{
133 stream <<"--- Iterating 2D Loop using for_workhorse() - 4 x 3 tuple matrix " << endL;
134
135 std::tuple M{ 0, 1, 2, // Row 1
136 3, 4, 5, // Row 2
137 6, 7, 8, // Row 3
138 9, 10, 11 /* Row 4 */ }; // 4 x 3 matrix
139
141
142 stream <<"\t{ ";
143
144 types::for_workhorse<indexer.Row>( [&M, &indexer](auto i, auto i_info)
145 {
146 stream <<"{ ";
147
148 types::for_workhorse<indexer.Column>([&](auto j, auto j_info)
149 {
150 // useful for clang++ and GNU g++
151 // constexpr auto index = indexer(i, j);
152
153 // for compatibility with Microsoft and oneAPI DPC++
154 constexpr auto index = indexer.Column * i.Index + j.Index;
155
156 stream << std::get<index>(M);
157
158 if constexpr(j.Index != j_info.Last)
159 stream <<", ";
160 });
161
162 stream <<" }";
163
164 if constexpr(i.Index != i_info.Last)
165 stream <<", ";
166 });
167
168 stream << " }" << endL;
169}
170
172{
173 stream <<"--- Printing tuple using for_workhorse() - IN ORDER" << endL;
174
175 std::tuple tuple{ 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
176
177 constexpr auto Size = types::tuple_size_v<decltype(tuple)>;
178
179 types::indexer_t<Size> dimension;
180
181 stream <<"\t{ ";
182
183 types::for_workhorse<dimension.Size>( [&tuple, &dimension](auto i, auto i_info)
184 {
185 stream << std::get<i.Index>(tuple);
186
187 if constexpr(i.Index != i_info.Last)
188 stream << ", ";
189 });
190
191 stream << " }" << endL;
192
193 stream <<"--- Printing tuple using for_workhorse() - IN REVERSE ORDER" << endL;
194
195 stream <<"\t{ ";
196
197 types::for_workhorse<dimension.Size-1, -1, -1>( [&tuple, &dimension](auto i, auto i_info)
198 {
199 stream << std::get<i.Index>(tuple);
200
201 if constexpr(i.Index != i_info.Last)
202 stream << ", ";
203 });
204
205 stream << " }" << endL;
206
207 stream <<"\t\t--- By Thomas Kim, Feb. 05, 2020. ---" << endl;
208}
209
210
211
212int main()
213{
215
217
219}
auto & endl
tpf::sstream stream
void test_workhorse_multi_level_loop()
auto & endL
void test_workhorse_basic()
int main()
void test_workhorse_print_tuple()
void for_workhorse(WorkType &&work, std::integer_sequence< T, Indices... >, ArgTypes &&... args)
Definition: cpg_types.hpp:3814
constexpr decltype(auto) get(T(&c_array)[N]) noexcept
constexpr auto tuple_size_v
Definition: tpf_types.hpp:1913
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
static constexpr T Row
Definition: tpf_types.hpp:2232
static constexpr T Column
Definition: tpf_types.hpp:2234
Stream output operators << are implemented.