src/generics/functions.h

changeset 1429
80e8aaabeeed
parent 1419
f7c53002a990
child 1430
6ce6d3da584f
equal deleted inserted replaced
1428:ece049033adc 1429:80e8aaabeeed
23 23
24 /* 24 /*
25 * Returns whether the argument is reasonably close to zero. 25 * Returns whether the argument is reasonably close to zero.
26 */ 26 */
27 template<typename T> 27 template<typename T>
28 bool isZero(T a) 28 constexpr bool isZero(T a)
29 { 29 {
30 return qFuzzyCompare(a + 1.0, 1.0); 30 return qFuzzyCompare(a + 1.0, 1.0);
31 } 31 }
32 32
33 template<typename T> 33 template<typename T>
41 { 41 {
42 return ::pow(value, 2); 42 return ::pow(value, 2);
43 } 43 }
44 44
45 template<> 45 template<>
46 inline int squared<int>(int value) 46 constexpr int squared<int>(int value)
47 { 47 {
48 return value * value; 48 return value * value;
49 } 49 }
50 50
51 // 51 //
66 return false; 66 return false;
67 } 67 }
68 68
69 // http://stackoverflow.com/a/18204188/3629665 69 // http://stackoverflow.com/a/18204188/3629665
70 template<typename T> 70 template<typename T>
71 inline int rotl10(T x) 71 constexpr int rotl10(T x)
72 { 72 {
73 return (((x) << 10) | (((x) >> 22) & 0x000000ff)); 73 return (((x) << 10) | (((x) >> 22) & 0x000000ff));
74 } 74 }
75 75
76 template<typename T> 76 template<typename T>
77 inline int rotl20(T x) 77 constexpr int rotl20(T x)
78 { 78 {
79 return (((x) << 20) | (((x) >> 12) & 0x000000ff)); 79 return (((x) << 20) | (((x) >> 12) & 0x000000ff));
80 } 80 }
81 81
82 // 82 //
83 // Get the amount of elements in something. 83 // Get the amount of elements in something.
84 // 84 //
85 template<typename T, size_t N> 85 template<typename T, int N>
86 int countof(T(&)[N]) 86 constexpr int countof(T(&)[N])
87 { 87 {
88 return N; 88 return N;
89 } 89 }
90 90
91 [[maybe_unused]]
91 static inline int countof(const QString& string) 92 static inline int countof(const QString& string)
92 { 93 {
93 return string.length(); 94 return string.length();
94 } 95 }
95 96
120 /* 121 /*
121 * Extracts the sign of 'value'. 122 * Extracts the sign of 'value'.
122 * From: https://stackoverflow.com/q/1903954 123 * From: https://stackoverflow.com/q/1903954
123 */ 124 */
124 template<typename T> 125 template<typename T>
125 int sign(T value) 126 constexpr int sign(T value)
126 { 127 {
127 return (0 < value) - (value < 0); 128 return (0 < value) - (value < 0);
128 } 129 }
129 130
130 /* 131 /*
131 * Returns the maximum of a single parameter (the parameter itself). 132 * Returns the maximum of a single parameter (the parameter itself).
132 */ 133 */
133 template <typename T> 134 template <typename T>
134 T max(T a) 135 constexpr T max(T a)
135 { 136 {
136 return a; 137 return a;
137 } 138 }
138 139
139 /* 140 /*
140 * Returns the maximum of two parameters. 141 * Returns the maximum of two parameters.
141 */ 142 */
142 template <typename T> 143 template <typename T>
143 T max(T a, T b) 144 constexpr T max(T a, T b)
144 { 145 {
145 return a > b ? a : b; 146 return a > b ? a : b;
146 } 147 }
147 148
148 /* 149 /*
149 * Returns the maximum of n parameters. 150 * Returns the maximum of n parameters.
150 */ 151 */
151 template <typename T, typename... Rest> 152 template <typename T, typename... Rest>
152 T max(T a, Rest&&... rest) 153 constexpr T max(T a, Rest&&... rest)
153 { 154 {
154 return max(a, max(rest...)); 155 return max(a, max(rest...));
155 } 156 }
156 157
157 /* 158 /*
158 * Returns the minimum of a single parameter (the parameter itself). 159 * Returns the minimum of a single parameter (the parameter itself).
159 */ 160 */
160 template <typename T> 161 template <typename T>
161 T min(T a) 162 constexpr T min(T a)
162 { 163 {
163 return a; 164 return a;
164 } 165 }
165 166
166 /* 167 /*
167 * Returns the minimum of two parameters. 168 * Returns the minimum of two parameters.
168 */ 169 */
169 template <typename T> 170 template <typename T>
170 T min(T a, T b) 171 constexpr T min(T a, T b)
171 { 172 {
172 return a < b ? a : b; 173 return a < b ? a : b;
173 } 174 }
174 175
175 /* 176 /*
176 * Returns the minimum of n parameters. 177 * Returns the minimum of n parameters.
177 */ 178 */
178 template <typename T, typename... Rest> 179 template <typename T, typename... Rest>
179 T min(T a, Rest&&... rest) 180 constexpr T min(T a, Rest&&... rest)
180 { 181 {
181 return min(a, min(rest...)); 182 return min(a, min(rest...));
182 } 183 }
183 184
184 /* 185 /*
215 216
216 /* 217 /*
217 * Returns the empty sum. (recursion base) 218 * Returns the empty sum. (recursion base)
218 */ 219 */
219 template<typename T> 220 template<typename T>
220 T sum() 221 constexpr T sum()
221 { 222 {
222 return {}; 223 return {};
223 } 224 }
224 225
225 /* 226 /*
226 * Returns the sum of n arguments. 227 * Returns the sum of n arguments.
227 */ 228 */
228 template<typename T, typename... Rest> 229 template<typename T, typename... Rest>
229 T sum(const T& arg, Rest&&... rest) 230 constexpr T sum(const T& arg, Rest&&... rest)
230 { 231 {
231 return arg + sum<T>(rest...); 232 return arg + sum<T>(rest...);
232 } 233 }
233 234
234 // Copy of qOverload so as to drop Qt version requirement from 5.7 to 5.5. 235 // Copy of qOverload so as to drop Qt version requirement from 5.7 to 5.5.

mercurial