-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
338 lines (299 loc) · 7.92 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// -*- C++ Header -*-
#pragma once
#include <iostream>
#include <string>
#include <cassert>
#include <cmath>
#include <optional>
#include <vector>
#include <algorithm>
#include <filesystem>
namespace utils {
namespace stdfs = std::filesystem;
using namespace std::string_literals;
// Floating point precision helpers
constexpr float epsilonf = 1e-4;
inline bool isEqual(float a, float b)
{
return std::abs(a - b) < epsilonf;
}
inline float getRound2(float value)
{
const float round1{ std::round(value * 1000.f) };
const float round2{ std::round(round1/10.f) };
return (round2/100.f);
}
inline float getRoundN(float value, int places)
{
const float round1 = std::round(value * std::pow(10.f, places+1));
const float round2{ std::round(round1/10.f) };
return (round2/std::pow(10.f, places));
}
// Console write related helpers
const std::string Line_Str = std::string{}.assign(30, '-');
const std::string Whitespace_Str = " \n\r\t\f\v";
inline void printNewLine(int count = 1, std::ostream& stream = std::cout)
{
assert("[Error] (printNewLine): non-positive value given as count" &&
(count > 0));
const std::string newline_str =
std::string{}.assign(static_cast<std::size_t>(count), '\n');
stream << newline_str;
}
inline std::string subtituteStr(const std::string& text,
const std::string& pattern,
const std::string& pattern_new)
{
typedef std::string::size_type ssize_type;
const ssize_type n_old = pattern.length();
const ssize_type n_new = pattern_new.length();
ssize_type pos_begin = 0;
ssize_type pos_found = text.find(pattern, pos_begin);
if (pos_found == std::string::npos) {
return text;
}
std::string text_new{ text };
do {
text_new = text_new.substr(0, pos_found) + pattern_new +
text_new.substr(pos_found + n_old);
pos_begin = pos_found + n_new;
pos_found = text_new.find(pattern, pos_begin);
} while (pos_found != std::string::npos);
return text_new;
}
inline void printInfo(const std::string& msg, const std::string& fname = "")
{
bool split_newlines = (msg.find('\n') != std::string::npos);
std::string prefix{ "[Info]: " };
std::cout << Line_Str << '\n';
if (not fname.empty()) {
prefix = "\n[Info] (" + fname + "): ";
}
std::cout << prefix;
std::cout << (split_newlines ? subtituteStr(msg, "\n", "\n" + prefix) : msg)
<< '\n';
std::cout << Line_Str << '\n';
}
inline void printErr(const std::string& msg, const std::string& fname = "")
{
bool split_newlines = (msg.find('\n') != std::string::npos);
std::string prefix{ "[Error]: " };
std::cerr << Line_Str << '\n';
if (not fname.empty()) {
prefix = "\n[Error] (" + fname + "): ";
}
std::cerr << prefix;
std::cerr << (split_newlines ? subtituteStr(msg, "\n", prefix) : msg)
<< '\n';
std::cerr << Line_Str << '\n';
}
template <typename F,
typename std::enable_if<std::is_convertible_v<F, stdfs::path>>::type* =
nullptr>
stdfs::path getCleanPath(const F& src)
{
const stdfs::path tmp_src{ src };
const stdfs::path lexical_src{ tmp_src.lexically_normal() };
const stdfs::path abs_src{ stdfs::absolute(lexical_src) };
return stdfs::weakly_canonical(abs_src);
}
// "Expected" type helpers
template <typename T, typename E> class Expected
{
public:
Expected()
: m_opt{ std::nullopt }
, m_err{}
{
}
Expected(const T& val, const E& err)
: m_opt{ val }
, m_err{ err }
{
}
Expected(const std::optional<T>& opt, const E& err)
: m_opt{ opt }
, m_err{ err }
{
}
bool has_value() const
{
return m_opt.has_value();
}
std::optional<T>& opt()
{
return m_opt;
}
const std::optional<T>& opt() const
{
return m_opt;
}
T& value()
{
return *m_opt;
}
const T& value() const
{
return *m_opt;
}
E& err()
{
return m_err;
}
const E& err() const
{
return m_err;
}
private:
std::optional<T> m_opt;
E m_err;
};
template <typename T> using ErrorMsg = Expected<T, std::string>;
template <typename T> using ErrorBool = Expected<T, bool>;
inline std::string trimLeft(const std::string& text)
{
if (text.empty()) {
return text;
}
std::string::size_type pos = text.find_first_not_of(" \n\r\t\f\v");
if (pos == std::string::npos) {
return "";
}
return text.substr(pos);
}
inline std::string trimRight(const std::string& text)
{
if (text.empty()) {
return text;
}
std::string::size_type pos = text.find_last_not_of(" \n\r\t\f\v");
if (pos == std::string::npos) {
return "";
}
return text.substr(0, pos + 1);
}
inline bool isNumber(const std::string& str)
{
if (str.empty())
return false;
const std::string& number_str{ trimRight(str) };
char* p;
std::strtod(number_str.c_str(), &p);
return (*p == '\0');
}
class Str2Num
{
public:
Str2Num(const std::string& str)
{
if (str.empty()) {
m_val = -1.0;
m_state = false;
} else {
char* p;
const std::string& number_str{ trimRight(str) };
m_val = std::strtod(number_str.c_str(), &p);
m_state = (*p == '\0');
}
}
bool has_value()
{
return m_state;
}
double value()
{
return m_val;
}
private:
double m_val;
bool m_state{ false };
};
template <typename T> class MatchItem
{
public:
MatchItem(T item)
: m_item{ item }
{
}
bool operator()(const T& item)
{
return (item == m_item);
}
T& item()
{
return m_item;
}
const T& item() const
{
return m_item;
}
private:
T m_item;
};
template <typename T>
bool hasRepeatingPattern(const std::vector<T>& vect, int n)
{
if (vect.size() <
static_cast<std::size_t>(
2 * n)) { // If there aren't enough elements for a duplicate
return false;
}
const auto it_begin_pattern = vect.end() - n;
const auto it_end = vect.end();
const auto it_end_check = it_end - (2 * n);
for (auto it = vect.begin(); it != it_end_check; ++it) {
if (std::equal(it_begin_pattern, it_end, it)) {
return true;
}
}
return false;
}
template <typename T>
bool vectContains(const T& item, const std::vector<T>& vect)
{
for (const T& elmnt : vect) {
if (elmnt == item) {
return true;
}
}
return false;
}
template <typename T> int vectFind(const T& item, const std::vector<T>& vect)
{
const auto& it_begin{ vect.begin() };
const auto& it_end{ vect.end() };
const auto& it =
std::find_if(it_begin, it_end, utils::MatchItem<T>{ item });
return (it == it_end) ? -1 : std::distance(it_begin, it);
}
typedef std::uint32_t hash_type;
inline std::uint32_t calcCRC32(const std::string& str)
{
const unsigned char *pData{ reinterpret_cast<const unsigned char*>(str.c_str()) };
std::size_t ulByteCount{ str.length() };
std::uint32_t d, ind;
std::uint32_t acc = 0xFFFFFFFF;
const std::uint32_t ulCrcRand32Lut[] =
{
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC,
0x76DC4190, 0x6B6B51F4, 0x4DB26158, 0x5005713C,
0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C,
0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C
};
while ( ulByteCount > 0 )
{
ulByteCount--;
d = *pData++;
ind = (acc & 0x0F) ^ (d & 0x0F);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
ind = (acc & 0x0F) ^ (d >> 4);
acc = (acc >> 4) ^ ulCrcRand32Lut[ind];
}
return (acc ^ 0xFFFFFFFF);
}
inline hash_type getHash(const std::string& str)
{
return calcCRC32(str);
// return std::hash<std::string>{}(str);
}
} // namespace utils