-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
275 lines (210 loc) · 6.06 KB
/
utils.cpp
File metadata and controls
275 lines (210 loc) · 6.06 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
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
#include "utils.h"
#define INITIAL_SEED 0x38F271A
#define PAIR_SEED 0x49616E42
string logLevelStr[10] = { "debug", "warning", "error", "info", "", "ddebug" };
string logSectionStr[10] = { "uct", "board", "hash", "test", "aei", "eval", "other"};
float sqrtCache[SQRT_CACHE_SIZE];
float logCache[LOG_CACHE_SIZE];
void logFunction(logLevel_e logLevel, const char* timestamp, const char* file, const char* function, int line, ...)
{
va_list args;
va_start(args, line);
char* msg = va_arg(args, char *);
char * fullmsg;
//compiler dependend function - allocates memory to fullmsg
vasprintf(&fullmsg, msg, args);
va_end(args);
if (logLevel == LL_RAW || logLevel == LL_DDEBUG)
std::cerr << fullmsg << std::endl;
else
std::cerr << timestamp << " --- " << "[" << logLevelStr[logLevel] << "] " << file << "/" << function << "/" << line << ":" << fullmsg << std::endl;
free(fullmsg);
}
//---------------------------------------------------------------------
// section FileRead
//---------------------------------------------------------------------
FileRead::FileRead(string fn){
f_.open(fn.c_str(), fstream::in);
ignoreStart_ = "";
}
//---------------------------------------------------------------------
FileRead::FileRead(){
assert(false);
}
//---------------------------------------------------------------------
string FileRead::read() {
string line;
string s = "";
while (getLine(line)){
s += line + '\n';
}
return s;
}
//---------------------------------------------------------------------
bool FileRead::getLine(string & s){
while (f_.good()){
getline(f_, s);
if (s != "" &&
(ignoreStart_ == ""
|| trimLeft(s).find(ignoreStart_.c_str(), 0) != 0))
return true;
}
return false;
}
//---------------------------------------------------------------------
bool FileRead::getLineAsPair(string & s1, string & s2, const char * sep){
string s;
stringstream ss;
ss.str("");
while (f_.good()){
getline(f_, s);
if (s != "" &&
( ignoreStart_ == ""
|| trimLeft(s).find(ignoreStart_.c_str(), 0) != 0)){
if (sep == NULL){
ss.str(s);
ss >> s1;
s2 = getStreamRest(ss);
return true;
}
else{ //valid separator
string::size_type loc = s.find(sep, 0);
if ( loc == string::npos) { //this should not happen !!!
logError("Wrong format in intput: %s", s.c_str());
exit(1);
}
s1 = trim(s.substr(0, loc));
s2 = trim(s.substr(loc + 1));
return true;
}
}
}
return false;
}
//---------------------------------------------------------------------
void FileRead::ignoreLines(const char* ignoreStart)
{
ignoreStart_ = string(ignoreStart);
}
//---------------------------------------------------------------------
bool FileRead::good() const
{
return f_.good();
}
//---------------------------------------------------------------------
Grand::Grand() {
seed(INITIAL_SEED);
}
//---------------------------------------------------------------------
Grand::Grand(unsigned int seed) {
this->seed(seed);
}
//---------------------------------------------------------------------
void Grand::seed(unsigned int seed) {
high_ = seed;
low_ = high_ ^ PAIR_SEED;
}
//---------------------------------------------------------------------
unsigned int Grand::operator()()
{
return getOne();
}
//---------------------------------------------------------------------
unsigned int Grand::getOne()
{
high_ = (high_ << 16) + (high_ >> 16);
high_ += low_;
low_ += high_;
return high_;
}
//---------------------------------------------------------------------
float Grand::get01()
{
return (double)getOne()/((double)(GRAND_MAX) + (double)(1));
}
//---------------------------------------------------------------------
void initCachedFunctions(){
for (int i = 0; i < SQRT_CACHE_SIZE; i++)
sqrtCache[i] = sqrt(i);
for (int i = 0; i < LOG_CACHE_SIZE; i++)
logCache[i] = log(i);
}
//---------------------------------------------------------------------
float mysqrt(int arg){
return arg < SQRT_CACHE_SIZE ? sqrtCache[arg] : sqrt(arg);
}
//---------------------------------------------------------------------
float mylog(int arg){
return arg < LOG_CACHE_SIZE ? logCache[arg] : log(arg);
}
//---------------------------------------------------------------------
int str2int(const string& str)
{
stringstream ss(str);
int n;
ss >> n;
return n;
}
//---------------------------------------------------------------------
float str2float(const string& str)
{
stringstream ss(str);
float f;
ss >> f;
return f;
}
//---------------------------------------------------------------------
string trimRight(const string& s)
{
string ret = s;
ret.erase(ret.find_last_not_of(' ') + 1);
return ret;
}
//---------------------------------------------------------------------
string trimLeft(const string& s)
{
string ret = s;
ret.erase(0, ret.find_first_not_of(' '));
return ret;
}
//---------------------------------------------------------------------
string trim(const string& s)
{
return trimLeft(trimRight(s));
}
//---------------------------------------------------------------------
string getStreamRest(istream& is)
{
stringstream ss ;
char * buffer;
int len;
int pos;
string s;
//initial position
pos = is.tellg();
//save buffer length
is.seekg (0, ios::end);
len = is.tellg();
is.seekg (pos, ios::beg);
buffer = new char[len - pos + 1];
is.read(buffer, len - pos);
//TODO why not bugger[len - pos + 1] = 0 ?
buffer[len - pos] = 0;
s = string(buffer);
delete[] buffer;
//set back g pointer
is.seekg (pos, ios::beg);
//cout << endl << s << endl;
return trimLeft(s);
}
//---------------------------------------------------------------------
string replaceAllChars(string s, char c1, char c2)
{
for (uint i = 0; i < s.length(); i++){
if (s[i] == c1){
s[i] = c2;
}
}
return s;
}
//---------------------------------------------------------------------