qxLib
common.h
Go to the documentation of this file.
1 /**
2 
3  @file common.h
4  @author Khrapov
5  @date 7.08.2022
6  @copyright © Nick Khrapov, 2022. All right reserved.
7 
8 **/
9 #pragma once
10 
12 #include <qx/typedefs.h>
13 
14 #include <array>
15 #include <bitset>
16 #include <concepts>
17 #include <limits>
18 
19 namespace qx
20 {
21 
22 /**
23  @brief Constexpr absolute value
24  @tparam T - value type
25  @param value - value
26  @retval - absolute value
27 **/
28 template<class T>
29 constexpr T abs(T value);
30 
31 /**
32  @brief Constexpr comparison function for a user defined epsilon values
33  @tparam T - value type
34  @param left - left value
35  @param right - right value
36  @param eps - epsilon value
37  @retval - true if |left - right| < eps
38 **/
39 template<class T>
40 constexpr bool epsilon_equal(T left, T right, T eps = std::numeric_limits<T>::epsilon());
41 
42 /**
43  @brief Constexpr comparison with zero for a user defined epsilon values
44  @tparam T - value type
45  @param value - user value
46  @param eps - epsilon value
47  @retval - true if |value| < eps
48 **/
49 template<class T>
50 constexpr bool epsilon_zero(T value, T eps = std::numeric_limits<T>::epsilon());
51 
52 /**
53  @brief Constexpr comparison function for a user defined epsilon values
54  @tparam T - value type
55  @param left - left value
56  @param right - right value
57  @param eps - epsilon value
58  @retval - true if left < right or |left - right| < eps
59 **/
60 template<class T>
61 constexpr bool epsilon_less_equal(T left, T right, T eps = std::numeric_limits<T>::epsilon());
62 
63 /**
64  @brief Constexpr comparison function for a user defined epsilon values
65  @tparam T - value type
66  @param left - left value
67  @param right - right value
68  @param eps - epsilon value
69  @retval - true if left > right or |left - right| < eps
70 **/
71 template<class T>
72 constexpr bool epsilon_greater_equal(T left, T right, T eps = std::numeric_limits<T>::epsilon());
73 
74 /**
75  @brief Check if value is odd
76  @tparam T - integral type
77  @param val - value
78  @retval - true if value is odd
79 **/
80 template<std::integral T>
81 constexpr bool is_odd(T val);
82 
83 /**
84  @brief Check if value is even
85  @tparam T - integral type
86  @param val - value
87  @retval - true if value is even
88 **/
89 template<std::integral T>
90 constexpr bool is_even(T val);
91 
92 /**
93  @brief Greatest common divisor
94  @details Euclid's algorithm
95  based on fact gcd(A, B) == gcd(B, A mod B)
96  @complexity O(log(second))
97  @param nFirst - first num
98  @param nSecond - second num
99  @retval - greatest common divisor if first and second > 0, otherwise 0
100 **/
101 constexpr int gcd(int nFirst, int nSecond);
102 
103 /**
104  @brief Least common multiple
105  @complexity O(log(second))
106  @param nFirst - first num
107  @param nSecond - second num
108  @retval - least common multiple if first and second > 0, otherwise 0
109 **/
110 constexpr int lcm(int nFirst, int nSecond);
111 
112 /**
113  @brief Power function for integer power
114  @details About 2.22 times (positive powers)
115  1.7 times (positive and negative powers)
116  2.33 times (negative powers)
117  faster then std::pow
118  @complexity O(log(power))
119  @tparam T - Integral or floating point type
120  @param number - integral of floating point value
121  @param nPower - integral power
122  @retval - number ^ power
123 **/
124 template<class T>
125 inline double pow(T number, int nPower);
126 
127 /**
128  @brief Max power of two in integer
129  @tparam I - Integral type
130  @param nValue - number
131  @retval - max power of two in number
132 **/
133 template<std::integral I>
134 inline I maxpot(I nValue);
135 
136 /**
137  @brief Checks if value is between left and right
138  @tparam T - value type
139  @tparam compare_t - comparator type
140  @param left - left value
141  @param value - value
142  @param right - right value
143  @param compare - comparator function
144  @retval - true, left <= value <= right
145 **/
146 template<class T, class compare_t = std::less_equal<>>
147 constexpr bool between(T left, T value, T right, compare_t compare);
148 
149 /**
150  @brief Checks if value is between left and right
151  @details Overloading for disabling 4388 warning with Compare instantiation
152  @tparam T - value type
153  @tparam compare_t - comparator type
154  @param left - left value
155  @param value - value
156  @param right - right value
157  @retval - true, left <= value <= right
158 **/
159 template<class T, class compare_t = std::less_equal<>>
160 constexpr bool between(T left, T value, T right);
161 
162 } // namespace qx
163 
164 #include <qx/math/common.inl>
constexpr int lcm(int nFirst, int nSecond)
Least common multiple.
Definition: common.inl:70
constexpr bool between(T left, T value, T right, compare_t compare)
Checks if value is between left and right.
Definition: common.inl:153
constexpr bool epsilon_greater_equal(T left, T right, T eps=std::numeric_limits< T >::epsilon())
Constexpr comparison function for a user defined epsilon values.
Definition: common.inl:38
I maxpot(I nValue)
Max power of two in integer.
Definition: common.inl:131
constexpr bool is_odd(T val)
Check if value is odd.
Definition: common.inl:44
constexpr bool epsilon_zero(T value, T eps=std::numeric_limits< T >::epsilon())
Constexpr comparison with zero for a user defined epsilon values.
Definition: common.inl:26
constexpr T abs(T value)
Constexpr absolute value.
Definition: common.inl:14
constexpr bool epsilon_less_equal(T left, T right, T eps=std::numeric_limits< T >::epsilon())
Constexpr comparison function for a user defined epsilon values.
Definition: common.inl:32
constexpr bool is_even(T val)
Check if value is even.
Definition: common.inl:50
constexpr bool epsilon_equal(T left, T right, T eps=std::numeric_limits< T >::epsilon())
Constexpr comparison function for a user defined epsilon values.
Definition: common.inl:20
double pow(T number, int nPower)
Power function for integer power.
Definition: common.inl:82
constexpr int gcd(int nFirst, int nSecond)
Greatest common divisor.
Definition: common.inl:55