4
4
* Copyright (c) 2011-2012, 2014-2015, Niklas Hauser
5
5
* Copyright (c) 2015, Sascha Schade
6
6
* Copyright (c) 2020, Christopher Durand
7
+ * Copyright (c) 2021, Thomas Sommer
7
8
*
8
9
* This file is part of the modm project.
9
10
*
@@ -60,65 +61,51 @@ pow(uint32_t base, uint8_t exponent)
60
61
}
61
62
62
63
/* *
63
- * @brief This does what you think it does.
64
- *
65
- * @param a A thing of arbitrary type.
66
- * @param b Another thing of arbitrary type.
67
- * @return The lesser of the parameters.
64
+ * @brief Variadic min for 2-∞ objects
68
65
*
69
- * This is the simple classic generic implementation. It will work on
70
- * temporary expressions, since they are only evaluated once, unlike a
71
- * preprocessor macro.
66
+ * @param a first object to compare
67
+ * @param b second object to compare
68
+ * @param cs More optional objects to compare
69
+ * @return The smallest object
70
+ *
71
+ * @see https://stackoverflow.com/questions/23815138/implementing-variadic-min-max-functions
72
72
*/
73
73
template <typename T>
74
- inline const T&
75
- min (const T& a, const T& b)
74
+ constexpr T vmin (T a, T b)
76
75
{
77
- if (b < a)
78
- return b;
79
- else
80
- return a;
76
+ return a < b ? a : b;
81
77
}
82
78
83
- /* *
84
- * @brief This does what you think it does.
85
- *
86
- * @param a A thing of arbitrary type.
87
- * @param b Another thing of arbitrary type.
88
- * @return The greater of the parameters.
89
- *
90
- * This is the simple classic generic implementation. It will work on
91
- * temporary expressions, since they are only evaluated once, unlike a
92
- * preprocessor macro.
93
- */
94
- template <typename T>
95
- inline const T&
96
- max (const T& a, const T& b)
79
+ template <typename T, typename ... Ts>
80
+ constexpr T vmin (T a, T b, Ts&&... cs)
97
81
{
98
- if (a < b)
99
- return b;
100
- else
101
- return a;
82
+ return a < b ?
83
+ vmin (a, std::forward<Ts>(cs)...) :
84
+ vmin (b, std::forward<Ts>(cs)...);
102
85
}
103
86
104
87
/* *
105
- * @brief This does what you think it does.
88
+ * @brief Variadic max for 2-∞ objects
106
89
*
107
- * @param a A thing of arbitrary type.
108
- * @param b Another thing of arbitrary type.
109
- * @param c Something else of arbitrary type.
110
- * @return The greater of the three parameters.
111
- *
112
- * This is the simple classic generic implementation. It will work on
113
- * temporary expressions, since they are only evaluated once, unlike a
114
- * preprocessor macro.
90
+ * @param a first object to compare
91
+ * @param b second object to compare
92
+ * @param cs More optional objects to compare
93
+ * @return The greatest object
94
+ *
95
+ * @see https://stackoverflow.com/questions/23815138/implementing-variadic-min-max-functions
115
96
*/
116
97
template <typename T>
117
- constexpr T
118
- max (const T a, const T b, const T c)
98
+ constexpr T vmax (T& a, T& b)
99
+ {
100
+ return a > b ? a : b;
101
+ }
102
+
103
+ template <typename T, typename ... Ts>
104
+ constexpr T vmax (T& a, T& b, Ts&... cs)
119
105
{
120
- return ( ( (b > c) ? b : c ) > a ) ?
121
- ( (b > c) ? b : c) : a;
106
+ return a > b ?
107
+ vmax (a, std::forward<Ts>(cs)...) :
108
+ vmax (b, std::forward<Ts>(cs)...);
122
109
}
123
110
124
111
/* *
@@ -136,10 +123,7 @@ template<typename T, typename Compare>
136
123
inline const T&
137
124
min (const T& a, const T& b, Compare compare)
138
125
{
139
- if (compare (b, a))
140
- return b;
141
- else
142
- return a;
126
+ return compare (b, a) ? b : a;
143
127
}
144
128
145
129
/* *
@@ -157,24 +141,16 @@ template<typename T, typename Compare>
157
141
inline const T&
158
142
max (const T& a, const T& b, Compare compare)
159
143
{
160
- if (compare (a, b))
161
- return b;
162
- else
163
- return a;
144
+ return compare (a, b) ? b : a;
164
145
}
165
146
166
147
/* *
167
148
* @brief constexpr implementation of fabs
168
149
*/
169
- template <typename Float>
170
- requires std::is_floating_point_v<Float>
150
+ template <std::floating_point Float>
171
151
constexpr Float constexpr_fabs (Float number)
172
152
{
173
- if (number >= 0 ) {
174
- return number;
175
- } else {
176
- return -number;
177
- }
153
+ return number >= 0 ? number : -number;
178
154
}
179
155
180
156
// / @}
0 commit comments