forked from user1095108/generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.hpp
104 lines (91 loc) · 1.84 KB
/
algorithm.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
103
104
#ifndef GNR_ALGORITHM_HPP
# define GNR_ALGORITHM_HPP
# pragma once
#include <cstddef>
#include <type_traits>
#include <utility>
#include "meta.hpp"
namespace gnr
{
// min, max
template <typename T>
constexpr inline T const& max(T const& a, T const& b) noexcept
{
return b < a ? a : b;
}
template <typename T>
constexpr inline T const& min(T const& a, T const& b) noexcept
{
return a < b ? a : b;
}
template <typename T, typename ...A>
constexpr inline std::enable_if_t<
bool(sizeof...(A)) &&
all_of<
std::is_same<
typename std::decay_t<T>,
typename std::decay_t<A>
>...
>{},
T
>
max(T const& a, T const& b, A&& ...args) noexcept
{
return b < a ?
max(a, std::forward<A>(args)...) :
max(b, std::forward<A>(args)...);
}
template <typename T, typename ...A>
constexpr inline std::enable_if_t<
bool(sizeof...(A)) &&
all_of<
std::is_same<
typename std::decay_t<T>,
typename std::decay_t<A>
>...
>{},
T
>
min(T const& a, T const& b, A&& ...args) noexcept
{
return a < b ?
min(a, std::forward<A>(args)...) :
min(b, std::forward<A>(args)...);
}
template <typename ...A>
constexpr inline std::enable_if_t<
bool(sizeof...(A)) &&
all_of<
std::is_same<
typename std::decay_t<typename front<A...>::type>,
typename std::decay_t<A>
>...
>{},
std::pair<
typename std::decay_t<typename front<A...>::type>,
typename std::decay_t<typename front<A...>::type>
>
>
minmax(A&& ...args) noexcept
{
return {
min(std::forward<A>(args)...),
max(std::forward<A>(args)...)
};
}
template<typename T, typename U, typename V>
constexpr inline std::enable_if_t<
std::is_same<T, U>{} &&
std::is_same<U, V>{},
T const&
>
clamp(T const& v, U const& lo, V const& hi) noexcept
{
return v < lo ?
lo :
hi < v ?
hi :
v;
}
}
#endif // GNR_ALGORITHM_HPP