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. |