C++ Library Extensions 2022.12.09
To help learn modern C++ programming
apply_operations.cpp
Go to the documentation of this file.
1/*
2 Author: Thomas Kim
3 First Edit: Feb. 03, 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 Does CREATE a new type - the argument tuple is not changed after operation
13 std::apply_operation(operation, tuple)
14
15 Does NOT create a new type - INPLACE operation - the argument tuple is MODIFIED after operation
16 std::apply_operation_inplace(operation, tuple)
17
18 Does CREATE a new type - the argument array is not changed after operation
19 std::apply_operation(operation, array)
20
21 Does NOT create a new type - INPLACE operation - the argument array is MODIFIED after operation
22 std::apply_operation_inplace(operation, array)
23*/
24
25#include <tpf_output.hpp>
27#include <cmath>
28
29namespace types = tpf::types;
30
32auto& endl = tpf::endl; // one carriage-return and flush out to console
33auto& endL = tpf::endL; // two carriage-returns and flush out to console
34
36{
37 stream <<"\n--- Apply operations to std::array and std::tuple --- " << endL;
38
39 stream <<"1. std::apply_operation(operation, tuple) - NON-INPLACE operation" << endL;
40 stream <<"\tApply operation to elements of tuple." << endl;
41 stream <<"\tThe argument tuple is NOT modified after operation." << endl;
42 stream <<"\tA new instance of std::tuple is Created and Returned to its caller." << endL;
43
44 stream <<"2. std::apply_operation_inplace(operation, tuple) - INPLACE operation" << endL;
45 stream <<"\tApply operation to elements of tuple." << endl;
46 stream <<"\tThe argument tuple is MODIFIED after operation." << endL;
47
48 stream <<"3. std::apply_operation(operation, array) - NON-INPLACE operation" << endL;
49 stream <<"\tApply operation to elements of array." << endl;
50 stream <<"\tThe argument array is NOT modified after operation." << endl;
51 stream <<"\tA new instance of std::array is Created and Returned to its caller." << endL;
52
53 stream <<"4. std::apply_operation_inplace(operation, array) - INPLACE operation" << endL;
54 stream <<"\tApply operation to elements of array." << endl;
55 stream <<"\tThe argument array is MODIFIED after operation." << endL;
56}
57
59{
60 stream <<"--- Apply Batch Operation to Tuple Elements---" << endL;
61
62 std::tuple a{1, 2.4, 4};
63
64 stream<< "1. Apply Element-Wise Operation to SIMPLE Tuple - std::apply_operation(operation, tuple)" << endL;
65
66 stream <<" std::tuple a = " << a << endL;
67
68 stream << " WARNING: does CREATE a new type!!" << endL;
69
70 stream <<" a * a = " <<
71 std::apply_operation( [](auto&& x){ return x * x; }, a) << endL;
72
73 stream <<" sin(a) = " <<
74 std::apply_operation( [](auto&& x){ return std::sin(x); }, a) << endL;
75
76 stream<< "2. Apply Element-wise IN-PLACE Operation to SIMPLE Tuple - std::apply_operation_inplace(operation, tuple)" << endL;
77 stream << " WARNING: does NOT create a new type!!" << endL;
78
79 stream <<" Before a = " << a << endL;
80
81 // in-place operation - tuple a is modified
82 std::apply_operation_inplace( [](auto&& x){ return std::sin(x); }, a);
83
84 stream <<" After sin(a) = " << a << endL;
85
86 stream <<" !! WARNGING !! sin(1) == sin(4) == 0, because 1 and 4 are integral types" << endL;
87
88 stream<< "3. Apply Element-wise Operation to Nested Tuple - std::apply_operation(operation, tuple)" << endL;
89
90 std::tuple b{1.5, std::tuple{1, 2.4, 4}, std::tuple{1, 2.4, std::tuple{3.5, 5.6} } };
91
92 stream << " std::tuple b = " << b << endL;
93
94 stream <<" sin(b) = " <<
95 std::apply_operation( [](auto&& x){ return std::sin(x); }, b) << endL;
96
97 stream <<" WARNING: a new type is created!!" << endL;
98
99 stream<< "4. Apply Element-wise IN-PLACE Operation to Nested Tuple - std::apply_operation_inplace(operation, tuple)" << endL;
100 stream << " WARNING!! does NOT create a new type!!" << endL;
101
102 std::tuple c{1, std::tuple{2, 2.4, 4.3}, std::tuple{3, 2.4, std::tuple{3.5, 5.6} } };
103
104 stream <<" Before c = " << c << endL;
105
106 // in-place operation - tuple c is modified
107 std::apply_operation_inplace( [](auto&& x){ return std::sin(x); }, c);
108
109 stream <<" After sin(c) = " << c << endL;
110
111 stream << " WARNING!! does NOT create a new type!!" << endL;
112
113 stream <<" !! WARNGING !! sin(1) == sin(2) == sin(3) == 0, because 1, 2, and 3 are integral types" << endL;
114}
115
117{
118 stream << "--- Apply Batch Operation to Array Elements ---" << endL;
119
120 stream<< "0. WARNING!! The types of the elements of a std::array should be IDENTICAL!!" << endL;
121
122 std::array a{1, 2, 3};
123
124 stream<< "1. Apply Operation to SIMPLE, Not Nested array - std::apply_operation(lambda, a)" << endL;
125
126 stream << " WARNING: namespace std - does create a new type" << endL;
127
128 stream <<" std::array a = " << a << endL;
129
130 stream <<" a * 1.3 = " <<
131 std::apply_operation( [](auto&& x){ return x * 1.3; }, a) << endL;
132
133 stream <<" sin(a) = " <<
134 std::apply_operation( [](auto&& x){ return std::sin(x); }, a) << endL;
135
136 stream << " WARNING: a new type is created in the above!!" << endL;
137
138 stream<< "2. Apply Operation IN-PLACE to array - std::apply_operation_inplace(lambda, a)" << endL;
139 stream<< " WARNING: New type is not created!" << endL;
140
141 stream <<" Before a = " << a << endL;
142
143 // in-place operation - tuple a is modified
144 std::apply_operation_inplace( [](auto&& x){ return std::sin(x); }, a);
145
146 stream <<" After sin(a) = " << a << endL;
147
148 stream <<" !! WARNGING !! sin(1) == sin(2) == sin(3) == 0, because 1, 2, and 3 are integral types" << endL;
149
150 stream<< "3. Apply Operation to Nested Array - std::apply_operation(lambda, b)"<<endL;
151
152 std::array b{ std::array{1, 2, 3}, std::array{4, 5, 6} };
153
154 stream << " std::array b = " << b << endL;
155
156 stream <<" sin(b) = " <<
157 std::apply_operation( [](auto&& x){ return std::sin(x); }, b) << endL;
158
159 stream <<" WARNING: a new type is created!!" << endL;
160
161 stream<< "4. Apply IN-PLACE Operation to Nested Array - std::apply_operation_inplace(lambda, c)"<<endL;
162
163 std::array c{ std::array{1.2, 2.4, 3.5}, std::array{4.3, 5.6, 6.2} };
164
165 stream << " Before std::array c = " << c << endL;
166
167 std::apply_operation_inplace( [](auto&& x){ return std::sin(x); }, c);
168
169 stream <<" sin(c) = " << c << endL;
170
171 stream <<" WARNING: std::array c is modified!!" << endL;
172
173 stream <<"\t\t--- By Thomas Kim, Feb. 05, 2020. ---" << endl;
174}
175
176
177int main()
178{
180
182
184}
auto & endl
void test_apply_operation_to_tuple()
tpf::sstream stream
void summary_apply_binary_operation_to_array_tuple()
auto & endL
void test_apply_operation_to_array()
int main()
auto apply_operation(OperationType &&operation, const std::tuple< ArgTypes... > &tuple)
void apply_operation_inplace(OperationType &&operation, std::array< T, Size > &array)
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
Stream output operators << are implemented.
Type functions are implemented.