-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.cpp
More file actions
166 lines (143 loc) · 3.86 KB
/
global.cpp
File metadata and controls
166 lines (143 loc) · 3.86 KB
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
#include "global.h"
#include <random>
#include <Windows.h>
//ref: https://leetcode.com/problems/wildcard-matching/solution/
bool YGO::wildCardMatch(const t_string& s, const t_string& p)
{
int sLen = s.size(), pLen = p.size();
int sIdx = 0, pIdx = 0;
int starIdx = -1, sTmpIdx = -1;
while (sIdx < sLen)
{
if (pIdx < pLen && (p[pIdx] == '?' || p[pIdx] == s[sIdx]))
{
++sIdx;
++pIdx;
}
else if (pIdx < pLen && p[pIdx] == '*')
{
starIdx = pIdx;
sTmpIdx = sIdx;
++pIdx;
}
else if (starIdx == -1)
{
return false;
}
else
{
pIdx = starIdx + 1;
sIdx = sTmpIdx + 1;
sTmpIdx = sIdx;
}
}
for (int i = pIdx; i < pLen; i++)
{
if (p[i] != '*')
{
return false;
}
}
return true;
}
//https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
std::vector<std::string> YGO::split(std::string s, std::string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
res.push_back(s.substr(pos_start));
return res;
}
//https://stackoverflow.com/questions/216823/how-to-trim-an-stdstring
// trim from start (in place)
void YGO::ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
void YGO::rtrim(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
void YGO::trim(std::string& s) {
rtrim(s);
ltrim(s);
}
void YGO::remove_space(std::stringstream& ss) {
while (std::isspace(ss.peek())) {
ss.ignore();
}
}
std::string YGO::read_while(std::stringstream& s, std::function<bool(char c)> pred) {
std::string w;
while (true) {
char c = s.peek();
if (c == std::char_traits<char>::eof() || !pred(c)) {
break;
}
w += c;
s.get();
}
return w;
}
int YGO::random_int(int low, int high) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(low, high);
return dis(gen);
}
double YGO::compute_ci(double p, int n) {
return 1.96 * sqrt(p * (1 - p) / n);
}
double YGO::compute_mean(const std::vector<double> data) {
if (data.empty()) {
return 0;
}
double sum = 0;
for (auto x : data) {
sum += x;
}
return sum / data.size();
}
double YGO::compute_std(const std::vector<double> data) {
if (data.size() <= 1) {
return 0;
}
double mean = compute_mean(data);
double sum = 0;
int n = data.size();
for (auto x : data) {
sum += (x - mean) * (x - mean);
}
return sqrt(sum / (n - 1));
}
std::string YGO::utf8_to_local_encoding(const std::string& utf8String) {
// Calculate the required buffer size for the converted string
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, utf8String.c_str(), -1, nullptr, 0);
// Allocate a wide-character buffer
std::wstring wideString(requiredSize, 0);
// Convert the UTF-8 string to wide-character
MultiByteToWideChar(CP_UTF8, 0, utf8String.c_str(), -1, &wideString[0], requiredSize);
// Calculate the required buffer size for the console encoding
int consoleSize = WideCharToMultiByte(CP_ACP, 0, wideString.c_str(), -1, nullptr, 0, nullptr, nullptr);
// Allocate a buffer for the console-encoded string
std::string consoleString(consoleSize, 0);
// Convert the wide-character string to the console encoding
WideCharToMultiByte(CP_ACP, 0, wideString.c_str(), -1, &consoleString[0], consoleSize, nullptr, nullptr);
return consoleString;
}
YGO::UTF8CodePage::UTF8CodePage() : m_old_code_page(::GetConsoleOutputCP()) {
::SetConsoleOutputCP(CP_UTF8);
}
YGO::UTF8CodePage::~UTF8CodePage()
{
::SetConsoleOutputCP(m_old_code_page);
}