-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeshnyUtils.cpp
153 lines (131 loc) · 5.25 KB
/
NeshnyUtils.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "NeshnyUtils.h"
namespace Neshny {
////////////////////////////////////////////////////////////////////////////////
void RandomGenerator::AutoSeed(void) {
Seed(TimeSinceEpochMilliseconds());
}
////////////////////////////////////////////////////////////////////////////////
unsigned int RandomGenerator::Next(void) {
// Calculate output function (XSH RR), uses old state for max ILP
uint32_t xor_shifted = ((m_State >> 18) ^ m_State) >> 27;
uint32_t rot = m_State >> 59;
// Advance internal state
m_State = m_State * 6364136223846793005ULL + 1442695040888963407ULL;
return (xor_shifted >> rot) | (xor_shifted << (((rot ^ 0xFFFFFFFF) + 1) & 31));
}
////////////////////////////////////////////////////////////////////////////////
void RandomSeed(uint64_t seed) {
g_GlobalRandom.Seed(seed);
g_GlobalRandom.Next();
}
////////////////////////////////////////////////////////////////////////////////
double Random(void) {
return g_GlobalRandom.Next() * INV_UINT;
}
////////////////////////////////////////////////////////////////////////////////
double Random(double min_val, double max_val) {
return Random() * (max_val - min_val) + min_val;
}
////////////////////////////////////////////////////////////////////////////////
int RandomInt(int min_val, int max_val) {
return int(floor(Random(min_val, max_val))); // don't use modulo to avoid bias
}
////////////////////////////////////////////////////////////////////////////////
int RoundUpPowerTwo(int value) {
if (value <= 0) {
return 0;
}
int pow_two = int(ceil(log2(double(value))));
return 1 << pow_two;
}
////////////////////////////////////////////////////////////////////////////////
size_t HashMemory(unsigned char* mem, int size) {
return std::hash<std::string_view>()(std::string_view((char*)mem, size));
}
////////////////////////////////////////////////////////////////////////////////
uint64_t TimeSinceEpochMilliseconds() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
////////////////////////////////////////////////////////////////////////////////
bool NearlyEqual(double a, double b) {
return fabs(a - b) < ALMOST_ZERO;
}
////////////////////////////////////////////////////////////////////////////////
std::string ReplaceAll(std::string_view str, std::string_view before, std::string_view after) {
std::string result(str);
if (before.empty()) {
return result;
}
size_t start_pos = 0;
while ((start_pos = result.find(before, start_pos)) != std::string::npos) {
result.replace(start_pos, before.length(), after);
start_pos += after.length();
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
std::string JoinStrings(const std::vector<std::string>& list, std::string_view insert_between) {
std::string result;
int last_index = (int)list.size() - 1;
for (int i = 0; i <= last_index; i++) {
if (i == last_index) {
result += list[i];
} else {
result += std::format("{}{}", list[i], insert_between);
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
std::string JoinStrings(const std::list<std::string>& list, std::string_view insert_between) {
std::string result;
bool first = true;
for (const auto& str: list) {
if (first) {
result += str;
first = false;
} else {
result += std::format("{}{}", insert_between, str);
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
bool StringContains(std::string_view str, std::string_view substr, bool case_insensitive) {
auto case_insensitive_compare = [] (char a, char b) -> bool {
return std::tolower(a) == std::tolower(b);
};
auto case_sensitive_compare = [] (char a, char b) -> bool {
return a == b;
};
auto it = std::search(str.begin(), str.end(), substr.begin(), substr.end(), case_insensitive ? case_insensitive_compare : case_sensitive_compare);
return it != str.end();
}
////////////////////////////////////////////////////////////////////////////////
std::string SrcStr(const std::source_location location) {
return std::format("{}:{}", location.file_name(), location.line());
}
////////////////////////////////////////////////////////////////////////////////
void ImGuiTextColoredUnformatted(std::string str, ImVec4 text_col) {
ImGui::PushStyleColor(ImGuiCol_Text, text_col);
ImGui::TextUnformatted(str.c_str());
ImGui::PopStyleColor();
}
#ifdef __APPLE__
////////////////////////////////////////////////////////////////////////////////
std::string GetMacOSExecutableDir() {
char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) != 0)
return "";
std::string result(path, size);
auto end_slash = result.find_last_of('/');
if (end_slash == std::string::npos) {
return result;
}
return std::string(path, end_slash);
}
#endif
} // namespace Neshny