licornea_tools
misc.h
Go to the documentation of this file.
1 #ifndef LICORNEA_UTILITY_MISC_H_
2 #define LICORNEA_UTILITY_MISC_H_
3 
4 #include <string>
5 #include <vector>
6 #include <random>
7 #include <memory>
8 #include <type_traits>
9 
10 namespace tlz {
11 
12 using default_random_engine = std::mt19937;
13 
14 template<typename...> using void_t = void;
15 
17 template<typename Numeric> Numeric sq(Numeric n) { return n * n; }
18 
20 template<typename T, typename T2, typename T3>
21 T clamp(T value, T2 minimum, T3 maximum);
22 
24 template<typename T> T gcd(T a, T b);
25 
27 template<typename T> T lcm(T a, T b);
28 
30 template<typename T>
31 bool is_power_of_two(T x) {
32  return (x != 0) && !(x & (x - 1));
33 }
34 
36 template<typename T, typename T2>
37 bool is_multiple_of(T x, T2 base) {
38  return (x % base == 0);
39 }
40 
42 template<typename T, typename T2>
43 bool is_nonzero_multiple_of(T x, T2 base) {
44  return (x != 0) && is_multiple_of(x, base);
45 }
46 
48 template<typename T>
49 bool is_odd(T x) { return (x % 2) != 0; }
50 
52 template<typename T>
53 bool is_even(T x) { return (x % 2) == 0; }
54 
55 
57 
58 
59 template<typename T>
60 T randint(T a, T b) {
62  std::uniform_int_distribution<T> dist(a, b);
63  return dist(engine);
64 }
65 
66 
67 
69 
70 template<typename T>
71 auto forward_make_shared(T&& t) {
72  using decayed_t = std::decay_t<T>;
73  return std::make_shared<decayed_t>(std::forward<T>(t));
74 }
75 
76 
78 
79 template<typename T>
81  using decayed_t = std::decay_t<T>;
82  return std::make_shared<const decayed_t>(std::forward<T>(t));
83 }
84 
85 }
86 
87 #include "misc.tcc"
88 
89 #endif
T gcd(T a, T b)
Compute greatest common divisor of a and b.
Numeric sq(Numeric n)
Compute square of a number.
Definition: misc.h:17
auto forward_make_shared(T &&t)
Get shared_ptr to new object copy- or move- constructed from t.
Definition: misc.h:71
bool is_multiple_of(T x, T2 base)
Check if x is a multiple of base, including zero.
Definition: misc.h:37
T lcm(T a, T b)
Compute least common multiple of a and b.
bool is_power_of_two(T x)
Check if x is a power of 2.
Definition: misc.h:31
void void_t
Definition: misc.h:14
std::mt19937 default_random_engine
Definition: misc.h:12
T randint(T a, T b)
Definition: misc.h:60
bool is_nonzero_multiple_of(T x, T2 base)
Check if x is a non-zero multiple of base.
Definition: misc.h:43
default_random_engine & random_engine()
T clamp(T value, T2 minimum, T3 maximum)
Clamp value between minimum and maximum value.
bool is_odd(T x)
Check if x is odd.
Definition: misc.h:49
auto forward_make_shared_const(T &&t)
Get shared_ptr to new const object copy- or move- constructed from t.
Definition: misc.h:80
bool is_even(T x)
Check if x is even.
Definition: misc.h:53