-
Notifications
You must be signed in to change notification settings - Fork 13
/
lgr_reduce.hpp
56 lines (47 loc) · 1.17 KB
/
lgr_reduce.hpp
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
#pragma once
#include <lgr_functional.hpp>
namespace lgr {
template <class Range, class T, class BinaryOp, class UnaryOp>
T
transform_reduce(Range const& range, T init, BinaryOp binary_op, UnaryOp unary_op)
{
auto first = range.begin();
auto const last = range.end();
for (; first != last; ++first) {
init = binary_op(std::move(init), unary_op(*first));
}
return init;
}
template <class Range, class T>
T
reduce(Range const& range, T init)
{
using input_value_type = typename Range::value_type;
auto const unop = [](input_value_type const i) { return T(i); };
return transform_reduce(range, init, plus<T>(), unop);
}
template <class Range, class UnaryPredicate>
bool
any_of(Range const& range, UnaryPredicate p)
{
return transform_reduce(range, false, logical_or(), p);
}
template <class Range, class UnaryPredicate>
bool
all_of(Range const& range, UnaryPredicate p)
{
return transform_reduce(range, true, logical_and(), p);
}
template <class Range>
bool
all_of(Range const& range)
{
return all_of(range, identity<bool>());
}
template <class Range>
bool
any_of(Range const& range)
{
return any_of(range, identity<bool>());
}
} // namespace lgr