-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional.hpp
102 lines (89 loc) · 2.43 KB
/
functional.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
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
/**
* @file functional.hpp
* @brief Additonal functional structs and utilities not included in functional
* library.
*/
#ifndef FUNCTIONAL_HPP
#define FUNCTIONAL_HPP
#include <algorithm>
#include <functional>
#include <numeric>
namespace fn {
template <class T = void, class Proj = std::identity,
class Comp = std::ranges::less>
class minimum {
private:
Comp comp;
Proj proj;
public:
constexpr minimum(Comp comp = {}, Proj proj = {})
: comp(comp), proj(proj) {}
constexpr T operator()(const T &lhs, const T &rhs) const {
return std::ranges::min(lhs, rhs, comp, proj);
}
};
template <class Proj, class Comp> class minimum<void, Proj, Comp> {
private:
Comp comp;
Proj proj;
public:
constexpr minimum(Comp comp = {}, Proj proj = {})
: comp(comp), proj(proj) {}
template <class T>
constexpr T operator()(const T &lhs, const T &rhs) const {
return std::ranges::min(lhs, rhs, comp, proj);
}
};
template <class Comp, class Proj>
minimum(Comp, Proj) -> minimum<void, Proj, Comp>;
template <class T = void, class Proj = std::identity,
class Comp = std::ranges::less>
class maximum {
private:
Comp comp;
Proj proj;
public:
constexpr maximum(Comp comp = {}, Proj proj = {})
: comp(comp), proj(proj) {}
constexpr T operator()(const T &lhs, const T &rhs) const {
return std::ranges::max(lhs, rhs, comp, proj);
}
};
template <class Proj, class Comp> class maximum<void, Proj, Comp> {
private:
Comp comp;
Proj proj;
public:
constexpr maximum(Comp comp = {}, Proj proj = {})
: comp(comp), proj(proj) {}
template <class T>
constexpr T operator()(const T &lhs, const T &rhs) const {
return std::ranges::max(lhs, rhs, comp, proj);
}
};
template <class Comp, class Proj>
maximum(Comp, Proj) -> maximum<void, Proj, Comp>;
template <class T = void> struct gcd {
constexpr T operator()(T lhs, T rhs) const { return std::gcd(lhs, rhs); }
};
template <> struct gcd<void> {
template <class T1, class T2>
constexpr auto operator()(T1 &&lhs, T2 &&rhs) const
-> decltype(std::gcd(std::forward<T1>(lhs), std::forward<T2>(rhs))) {
return std::gcd(std::forward<T1>(lhs), std::forward<T2>(rhs));
}
};
struct assign {
template <class T1, class T2>
constexpr T2 &&operator()(T1 &&, T2 &&t) const noexcept {
return std::forward<T2>(t);
}
};
struct noop {
template <class T1, class T2>
constexpr T1 &&operator()(T1 &&t, T2 &&) const noexcept {
return std::forward<T1>(t);
}
};
} // namespace fn
#endif