-
Notifications
You must be signed in to change notification settings - Fork 4
/
part-04-no-recurse-iteration.cpp
199 lines (181 loc) · 8.38 KB
/
part-04-no-recurse-iteration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright 2017 Nils Deppe
Distributed under the Boost Software License.
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/*
* Compile the code using:
* clang++ -std=c++14 -stdlib=libc++ ./part-03-iteration-cxx14.cpp
*/
#include <iostream>
#include <tuple>
#include <utility>
template <typename... Elements, size_t... Is>
void print_helper(std::ostream& os, const std::tuple<Elements...>& t, std::index_sequence<Is...> /*meta*/) {
static_cast<void>(std::initializer_list<char>{
(static_cast<void>(os << std::get<Is>(t) << ", "), '0')...});
}
template <typename... Elements>
std::ostream& operator<<(std::ostream& os, const std::tuple<Elements...>& t) {
os << "(";
print_helper(os, t, std::make_index_sequence<sizeof...(Elements) - 1>{});
os << std::get<sizeof...(Elements) - 1>(t);
os << ")";
return os;
}
namespace tuple_impl_detail {
template <bool ReverseIteration, typename... Elements, typename N_aryOp,
typename... Args, size_t... Is>
constexpr void tuple_fold_impl(
const std::tuple<Elements...>& tupull, N_aryOp&& op,
std::index_sequence<Is...> /*meta*/,
Args&... args) noexcept(noexcept(static_cast<void>(std::initializer_list<char>{
(static_cast<void>(
op(std::get<(ReverseIteration ? sizeof...(Elements) - 1 - Is : Is)>(
tupull),
args...)),
'0')...}))) {
constexpr size_t tuple_size = sizeof...(Elements);
static_cast<void>(std::initializer_list<char>{
(static_cast<void>(
op(std::get<(ReverseIteration ? tuple_size - 1 - Is : Is)>(tupull),
args...)),
'0')...});
}
template <bool ReverseIteration, typename... Elements, typename N_aryOp,
typename... Args, size_t... Is>
constexpr void tuple_counted_fold_impl(
const std::tuple<Elements...>& tupull, N_aryOp&& op,
std::index_sequence<Is...> /*meta*/,
Args&... args) noexcept(noexcept(static_cast<void>(std::initializer_list<char>{
(static_cast<void>(
op(std::get<(ReverseIteration ? sizeof...(Elements) - 1 - Is : Is)>(
tupull),
(ReverseIteration ? sizeof...(Elements) - 1 - Is : Is), args...)),
'0')...}))) {
constexpr size_t tuple_size = sizeof...(Elements);
static_cast<void>(std::initializer_list<char>{
(static_cast<void>(
op(std::get<(ReverseIteration ? tuple_size - 1 - Is : Is)>(tupull),
(ReverseIteration ? tuple_size - 1 - Is : Is), args...)),
'0')...});
}
template <bool ReverseIteration, typename... Elements, typename N_aryOp,
typename... Args, size_t... Is>
constexpr inline void tuple_transform_impl(
const std::tuple<Elements...>& tupull, N_aryOp&& op,
std::index_sequence<Is...> /*meta*/,
Args&... args) noexcept(noexcept(static_cast<void>(std::initializer_list<char>{
(static_cast<void>(op(
std::get<(ReverseIteration ? sizeof...(Elements) - 1 - Is : Is)>(
tupull),
std::integral_constant<
size_t, (ReverseIteration ? sizeof...(Elements) - 1 - Is : Is)>{},
args...)),
'0')...}))) {
constexpr size_t tuple_size = sizeof...(Elements);
static_cast<void>(std::initializer_list<char>{(
static_cast<void>(op(
std::get<(ReverseIteration ? tuple_size - 1 - Is : Is)>(tupull),
std::integral_constant<size_t, (ReverseIteration ? tuple_size - 1 - Is
: Is)>{},
args...)),
'0')...});
}
} // namespace tuple_impl_detail
template <bool ReverseIteration = false, typename... Elements, typename N_aryOp,
typename... Args>
constexpr void tuple_fold(
const std::tuple<Elements...>& tuple, N_aryOp&& op,
Args&&... args) noexcept(noexcept(tuple_impl_detail::
tuple_fold_impl<ReverseIteration>(
tuple, std::forward<N_aryOp>(op),
std::make_index_sequence<
sizeof...(Elements)>{},
args...))) {
tuple_impl_detail::tuple_fold_impl<ReverseIteration>(
tuple, std::forward<N_aryOp>(op),
std::make_index_sequence<sizeof...(Elements)>{}, args...);
}
template <bool ReverseIteration = false, typename... Elements, typename N_aryOp,
typename... Args>
constexpr void tuple_counted_fold(
const std::tuple<Elements...>& tuple, N_aryOp&& op,
Args&&... args) noexcept(noexcept(tuple_impl_detail::
tuple_counted_fold_impl<
ReverseIteration>(
tuple, std::forward<N_aryOp>(op),
std::make_index_sequence<
sizeof...(Elements)>{},
args...))) {
tuple_impl_detail::tuple_counted_fold_impl<ReverseIteration>(
tuple, std::forward<N_aryOp>(op),
std::make_index_sequence<sizeof...(Elements)>{}, args...);
}
template <bool ReverseIteration = false, typename... Elements, typename N_aryOp,
typename... Args>
constexpr void tuple_transform(
const std::tuple<Elements...>& tuple, N_aryOp&& op,
Args&&... args) noexcept(noexcept(tuple_impl_detail::
tuple_transform_impl<
ReverseIteration>(
tuple, std::forward<N_aryOp>(op),
std::make_index_sequence<
sizeof...(Elements)>{},
args...))) {
tuple_impl_detail::tuple_transform_impl<ReverseIteration>(
tuple, std::forward<N_aryOp>(op),
std::make_index_sequence<sizeof...(Elements)>{}, args...);
}
void tuple_fold_and_counted_fold_example() {
const auto my_tupull = std::make_tuple(2, 7, -3.8, 20.9);
double sum_value = 0.0;
tuple_fold(my_tupull,
[](const auto& element, double& state) { state += element; },
sum_value);
std::cout << "Expected: 26.1 Computed: " << sum_value << "\n";
sum_value = 0.0;
tuple_counted_fold(my_tupull,
[](const auto& element, size_t index, double& state) {
if (index != 1) {
state += element;
}
},
sum_value);
std::cout << "Expected: 19.1 Computed: " << sum_value << "\n";
}
void tuple_transform_example() {
const auto my_tupull = std::make_tuple(2, 7, -3.8, 20.9);
std::decay_t<decltype(my_tupull)> out_tupull;
tuple_transform(my_tupull,
[](const auto& element, auto index, auto& out_tuple) {
constexpr size_t index_v = decltype(index)::value;
std::get<index_v>(out_tuple) = -element;
},
out_tupull);
std::cout << "Expected: (-2, -7, 3.8, -20.9) Computed: " << out_tupull
<< "\n";
}
int main() {
tuple_fold_and_counted_fold_example();
tuple_transform_example();
}