forked from atx/kvak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.hpp
65 lines (51 loc) · 1.13 KB
/
utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#include <complex>
#include <functional>
#include <cstdlib>
namespace kvak::utils {
template <typename T>
T modular_clamp(T v, const T lo, const T hi, const T step)
{
// TODO: We might want to specialize this for floats and use fmod instead
// However, for our current application, we are never going to walk too far
// away, so it does not matter.
while (v > hi) {
v -= step;
}
while (v < lo) {
v += step;
}
return v;
}
template<typename T>
T modular_inc(T a, T b, T c=1)
{
return (a + c) % b;
}
template<typename T>
T bit(T a, unsigned int n)
{
return (a >> n) & 0x1;
}
template<typename T>
std::complex<T> expj(T phase)
{
return std::exp(std::complex<T>(0.0, phase));
}
std::string replace_first(const std::string &haystack,
const std::string &needle,
const std::string &with);
// This is a hacky method for injecting constants through environment
// variables.
template <typename T>
class env_initializer {
public:
env_initializer(const std::string &name, T &value)
{
const char *envval = std::getenv(name.c_str());
if (envval != nullptr) {
std::istringstream(envval) >> value;
}
}
};
}