-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional-utils.h
323 lines (279 loc) · 12.5 KB
/
functional-utils.h
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#ifndef ZAUBERON_FUNCTIONAL_UTILS_H
#define ZAUBERON_FUNCTIONAL_UTILS_H
#include <algorithm>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <numeric>
#include <string>
#include "global-types.h"
#define NOW std::chrono::steady_clock::now()
#define ELAPSED_MILLISECONDS(now, then) (std::chrono::duration<TNumber>(now - then).count() * 1000.0)
#define ELAPSED_SECONDS(now, then) std::chrono::duration<TNumber>(now - then).count()
#define ELAPSED_TIME(now, then) std::chrono::duration<TNumber>(now - then).count()
#define BINARY_FN(function) \
(const TNumber n1, const TNumber n2) \
{ \
return function(n1, n2); \
}
#define TERNARY_FN(function) \
(const TNumber n1, const TNumber n2, const TNumber n3) \
{ \
return function(n1, n2, n3); \
}
#define UNARY_FN(function) \
(const TNumber n) \
{ \
return function(n); \
}
#define UNARY_TYPE_NAME(type, name) (const type name)
#define DOTIMES_FN(index) (const TIndex index)
#define OPEN_FN(streamName) (TStream & streamName)
#define FUNCTIONS_H(name) \
TNumber Binary##name(TNumber n1, TNumber n2) noexcept; \
\
template <typename TNS> \
auto name(const TNumber number, const TNS& numbers) noexcept \
{ \
return Map([number](const TNumber n) { return Binary##name(n, number); }, numbers); \
} \
\
template <typename TNS> \
auto name(const TNS& numbers1, const TNS& numbers2) noexcept \
{ \
return Map(Binary##name, numbers1, numbers2); \
} \
\
template <typename TN> \
auto name(std::initializer_list<TN> numbers1, std::initializer_list<TN> numbers2) noexcept \
{ \
return Map(Binary##name, numbers1, numbers2); \
} \
\
template <typename TNS> \
auto name(const TNS& numbers) noexcept \
{ \
return Reduce(Binary##name, numbers); \
} \
\
template <typename TN> \
auto name(std::initializer_list<TN> numbers) noexcept \
{ \
return Reduce(Binary##name, numbers); \
}
namespace functional_utils
{
using TStream = std::fstream;
using TString = std::string;
using TTimePoint = std::chrono::time_point<std::chrono::steady_clock>;
using TBinaryFunction = std::function<TNumber(const TNumber, const TNumber)>;
using TOpenFunction = std::function<void(TStream&)>;
using TTernaryFunction = std::function<TNumber(const TNumber, const TNumber, const TNumber)>;
using TUnaryFunction = std::function<TNumber(const TNumber)>;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// internal
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// https://en.cppreference.com/w/cpp/types/numeric_limits/epsilon
template <typename T>
auto InternalAlmostEqual(T x, T y, int ulp)
{
// the machine epsilon has to be scaled to the magnitude of the values used
// and multiplied by the desired precision in ULPs (units in the last place)
return std::fabs(x - y) <= std::numeric_limits<T>::epsilon() * std::fabs(x + y) * ulp
// unless the result is subnormal
|| std::fabs(x - y) < std::numeric_limits<T>::min();
}
template <typename TNS>
auto InternalMap(TUnaryFunction uf, const TNS& numbers) noexcept
{
auto result{TNumbers(numbers.size())};
std::transform(numbers.begin(), numbers.end(), result.begin(), uf);
return result;
}
template <typename TNS>
auto InternalMap(TBinaryFunction bf, const TNS& numbers1, const TNS& numbers2) noexcept
{
auto result{TNumbers(numbers1.size())};
std::transform(numbers1.begin(), numbers1.end(), numbers2.begin(), result.begin(), bf);
return result;
}
template <typename TNS>
auto InternalMap(TTernaryFunction& tf, const TNS& numbers1, const TNS& numbers2, const TNS& numbers3) noexcept
{
auto result{TNumbers(numbers1.size())};
auto n3{numbers3.begin()};
std::transform(numbers1.begin(),
numbers1.end(),
numbers2.begin(),
result.begin(),
[tf, &n3](const TNumber n1, const TNumber n2) { return tf(n1, n2, *n3++); });
return result;
}
template <typename TNS>
auto InternalReduce(TBinaryFunction bf, const TNS& numbers) noexcept
{
return std::accumulate(numbers.begin() + 1, numbers.end(), *numbers.begin(), bf);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// internal
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNS>
auto Count(const TNS& ns) noexcept
{
return ns.size();
}
template <typename TTIMES, typename TUF>
void DoTimes(const TTIMES times, TUF uf) noexcept
{
for (TTIMES i = 0; i < times; ++i)
uf(i);
}
template <typename TN>
auto FormatElapsedTime(const TN elapsedTime) noexcept
{
return (elapsedTime >= 1.0) ? std::to_string(elapsedTime) + "s" : std::to_string(1000.0 * elapsedTime) + "ms";
}
template <typename TNS>
auto FormatNumbers(const TNS& numbers) noexcept
{
static const auto MAX_WIDTH(80);
auto result{std::string("")};
auto lineLength{result.length()};
for (auto i : numbers)
{
auto iStr{std::to_string(i) + " "};
if ((lineLength + iStr.length()) > MAX_WIDTH)
{
iStr = "\n" + iStr;
lineLength = 0;
}
result += iStr;
lineLength += iStr.length();
}
result.pop_back();
return result;
}
template <typename TNS>
auto IndexOf(const TNS& numbers, TNumber number)
{
auto numbersIt{std::find_if(
numbers.begin(), numbers.end(), [number](const TNumber n) { return InternalAlmostEqual(n, number, 2); })};
return (numbersIt == numbers.end()) ? -1 : std::distance(numbers.begin(), numbersIt);
}
template <typename TUF, typename TVALUES>
void IMap(TUF uf, TVALUES& values) noexcept
{
std::transform(values.begin(), values.end(), values.begin(), uf);
}
template <typename TNS>
auto Map(TUnaryFunction uf, const TNS& numbers) noexcept
{
return InternalMap(uf, numbers);
}
template <typename TN>
auto Map(TUnaryFunction uf, std::initializer_list<TN> numbers) noexcept
{
return InternalMap(uf, numbers);
}
template <typename TNS>
auto Map(TBinaryFunction bf, const TNS& numbers1, const TNS& numbers2) noexcept
{
return InternalMap(bf, numbers1, numbers2);
}
template <typename TN>
auto Map(TBinaryFunction bf, std::initializer_list<TN> numbers1, std::initializer_list<TN> numbers2) noexcept
{
return InternalMap(bf, numbers1, numbers2);
}
template <typename TNS>
auto Map(TTernaryFunction tf, const TNS& numbers1, const TNS& numbers2, const TNS& numbers3) noexcept
{
return InternalMap(tf, numbers1, numbers2, numbers3);
}
template <typename TN>
auto Map(TTernaryFunction tf,
std::initializer_list<TN> numbers1,
std::initializer_list<TN> numbers2,
std::initializer_list<TN> numbers3) noexcept
{
return InternalMap(tf, numbers1, numbers2, numbers3);
}
template <typename I>
auto Numbers(const I count) noexcept
{
return TNumbers(count);
}
template <typename TN>
auto Numbers(std::initializer_list<TN> numbers) noexcept
{
TNumbers result;
result.insert(result.end(), numbers.begin(), numbers.end());
return result;
}
template <typename STRING>
void Print(const STRING& s, TStream& os) noexcept
{
os << s;
}
template <typename STRING>
void PrintLn(const STRING& s, TStream& os) noexcept
{
os << s << "\n";
}
template <typename STRING>
void Print(const STRING& s) noexcept
{
std::cout << s;
}
template <typename STRING>
void PrintLn(const STRING& s) noexcept
{
std::cout << s << "\n";
}
template <typename STRING>
void PrintErr(const STRING& s) noexcept
{
std::cerr << s;
}
template <typename STRING>
void PrintLnErr(const STRING& s) noexcept
{
std::cerr << s << "\n";
}
template <typename I>
auto Range(const I start, const I end) noexcept
{
auto result{TNumbers(end - start)};
std::iota(result.begin(), result.end(), start);
return result;
}
template <typename I>
auto Range(const I count) noexcept
{
return Range(0, count);
}
template <typename TNS>
auto Reduce(TBinaryFunction bf, const TNS& numbers) noexcept
{
return InternalReduce(bf, numbers);
}
template <typename TN>
auto Reduce(TBinaryFunction bf, std::initializer_list<TN> numbers) noexcept
{
return InternalReduce(bf, numbers);
}
template <typename STRING>
void WithOpen(const STRING& fileSpec, TOpenFunction of) noexcept
{
auto outputStream{TStream{fileSpec, std::ios::binary}};
of(outputStream);
}
FUNCTIONS_H(Add)
FUNCTIONS_H(Divide)
FUNCTIONS_H(Multiply)
FUNCTIONS_H(Subtract)
} // namespace functional_utils
#endif // ZAUBERON_FUNCTIONAL_UTILS_H