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 
11 #include <qx/typedefs.h>
12 
13 #include <array>
14 #include <bitset>
15 #include <concepts>
16 
17 namespace qx
18 {
19 
20 /**
21  @brief Constexpr absolute value
22  @tparam T - value type
23  @param value - value
24  @retval - absolute value
25 **/
26 template<class T>
27 constexpr T abs(T value);
28 
29 /**
30  @brief Check if value is odd
31  @tparam T - integral type
32  @param val - value
33  @retval - true if value is odd
34 **/
35 template<std::integral T>
36 constexpr bool is_odd(T val);
37 
38 /**
39  @brief Check if value is even
40  @tparam T - integral type
41  @param val - value
42  @retval - true if value is even
43 **/
44 template<std::integral T>
45 constexpr bool is_even(T val);
46 
47 /**
48  @brief Greatest common divisor
49  @details Euclid's algorithm
50  based on fact gcd(A, B) == gcd(B, A mod B)
51  @complexity O(log(second))
52  @param nFirst - first num
53  @param nSecond - second num
54  @retval - greatest common divisor if first and second > 0, otherwise 0
55 **/
56 constexpr int gcd(int nFirst, int nSecond);
57 
58 /**
59  @brief Least common multiple
60  @complexity O(log(second))
61  @param nFirst - first num
62  @param nSecond - second num
63  @retval - least common multiple if first and second > 0, otherwise 0
64 **/
65 constexpr int lcm(int nFirst, int nSecond);
66 
67 /**
68  @brief Power function for integer power
69  @details About 2.22 times (positive powers)
70  1.7 times (positive and negative powers)
71  2.33 times (negative powers)
72  faster than std::pow
73  @complexity O(log(power))
74  @tparam T - Integral or floating point type
75  @param number - integral of floating point value
76  @param nPower - integral power
77  @retval - number ^ power
78 **/
79 template<class T>
80 constexpr double pow(T number, int nPower);
81 
82 /**
83  @brief Max power of two in integer
84  @tparam I - Integral type
85  @param nValue - number
86  @retval - max power of two in the number
87 **/
88 template<std::integral I>
89 inline I maxpot(I nValue);
90 
91 } // namespace qx
92 
93 #include <qx/math/common.inl>
constexpr int lcm(int nFirst, int nSecond)
Least common multiple.
Definition: common.inl:46
I maxpot(I nValue)
Max power of two in integer.
Definition: common.inl:129
constexpr bool is_odd(T val)
Check if value is odd.
Definition: common.inl:20
constexpr T abs(T value)
Constexpr absolute value.
Definition: common.inl:14
constexpr bool is_even(T val)
Check if value is even.
Definition: common.inl:26
constexpr double pow(T number, int nPower)
Power function for integer power.
Definition: common.inl:58
constexpr int gcd(int nFirst, int nSecond)
Greatest common divisor.
Definition: common.inl:31