5#include "qx/core/qx_core_export.h"
9#include <unordered_set>
20QX_CORE_EXPORT
int abs(
int n);
21QX_CORE_EXPORT
unsigned int abs(
unsigned int n);
22QX_CORE_EXPORT
long abs(
long n);
23QX_CORE_EXPORT
unsigned long abs(
unsigned long n);
24QX_CORE_EXPORT
long long abs (
long long n);
25QX_CORE_EXPORT
unsigned long long abs(
unsigned long long n);
28 requires std::integral<T>
29T
length(T start, T end) {
return (end - start) + 1; }
32 requires arithmetic<T>
33bool isOdd(T num) {
return num % 2; }
36 requires arithmetic<T>
39template <
class InputIt>
40 requires std::input_iterator<InputIt> && std::equality_comparable<InputIt>
44 using T =
typename std::iterator_traits<InputIt >::value_type;
47 std::unordered_set<T> values(begin, end);
50 std::size_t size = std::distance(begin,end);
54 return size != values.size();
58 requires arithmetic<T>
59T
distance(T x, T y) {
return std::max(x,y) - std::min(x,y); }
64 requires std::signed_integral<T>
65T
constrainedAdd(T a, T b, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
67 if((b >= 0) && (a > (max - b)))
69 else if((b < 0) && (a < (min - b)))
76 requires std::unsigned_integral<T>
86 requires std::signed_integral<T>
87T
constrainedSub(T a, T b, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
89 if(b >= 0 && a < min + b)
91 else if(b < 0 && a > max + b)
98 requires std::unsigned_integral<T>
108 requires std::signed_integral<T>
109T
constrainedMult(T a, T b, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
113 if(b > 0 && a > (max / b))
115 else if(b < (min / a))
120 if(b > 0 && a < (min / b))
122 else if(b < (max / a))
130 requires std::unsigned_integral<T>
140 requires std::signed_integral<T>
141T
constrainedDiv(T a, T b, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
144 qFatal(
"Divide by zero");
146 if((a == std::numeric_limits<T>::min()) && (b == -1))
154 else if(result < min)
162 requires std::unsigned_integral<T>
166 qFatal(
"Divide by zero");
177 requires std::integral<T>
186 if(mult == 1 || mult == num)
190 return (num / mult) * mult;
193 T previousMultiple = (num / mult) * mult;
194 return previousMultiple == num ? num :
constrainedAdd(previousMultiple, mult);
199 requires std::integral<T>
208 if(mult == 1 || mult == num)
212 return (num / mult) * mult;
215 T nextMultiple = (num / mult) * mult;
216 return nextMultiple == num ? num :
constrainedSub(nextMultiple, mult);
221 requires std::integral<T>
228 return above - num <= num - below ? above : below;
232 requires std::integral<T>
236 if (num && !(num & (num - 1)))
241 while(powOfTwo < num)
248 requires std::integral<T>
252 if (num && !(num & (num - 1)))
256 T powOfTwo = (std::numeric_limits<T>::max() >> 1) + 1;
258 while(powOfTwo > num)
265 requires std::integral<T>
271 return above - num <= num - below ? above : below;
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5
bool isOdd(T num)
Definition qx-algorithm.h:33
T ceilPowOfTwo(T num)
Definition qx-algorithm.h:233
T constrainedAdd(T a, T b, T min=std::numeric_limits< T >::min(), T max=std::numeric_limits< T >::max())
Definition qx-algorithm.h:65
T floorNearestMultiple(T num, T mult)
Definition qx-algorithm.h:200
T constrainedDiv(T a, T b, T min=std::numeric_limits< T >::min(), T max=std::numeric_limits< T >::max())
Definition qx-algorithm.h:141
T roundPowOfTwo(T num)
Definition qx-algorithm.h:266
T constrainedSub(T a, T b, T min=std::numeric_limits< T >::min(), T max=std::numeric_limits< T >::max())
Definition qx-algorithm.h:87
T length(T start, T end)
Definition qx-algorithm.h:29
bool isEven(T num)
Definition qx-algorithm.h:37
T floorPowOfTwo(T num)
Definition qx-algorithm.h:249
bool containsDuplicates(InputIt begin, InputIt end)
Definition qx-algorithm.h:41
int abs(int n)
Definition qx-algorithm.cpp:20
T distance(T x, T y)
Definition qx-algorithm.h:59
T ceilNearestMultiple(T num, T mult)
Definition qx-algorithm.h:178
T constrainedMult(T a, T b, T min=std::numeric_limits< T >::min(), T max=std::numeric_limits< T >::max())
Definition qx-algorithm.h:109
T roundToNearestMultiple(T num, T mult)
Definition qx-algorithm.h:222
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...