-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.h
More file actions
217 lines (174 loc) · 4.23 KB
/
utils.h
File metadata and controls
217 lines (174 loc) · 4.23 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
/**
* @file utils.h
*
* @brief Supportive functions.
* @full Includes logger, conversion functions, etc.
*/
#pragma once
#include <cassert>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <ctime>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <limits.h>
#include <math.h>
typedef unsigned long long u64;
typedef unsigned int uint;
using std::string ;
using std::fstream;
using std::ostream;
using std::istream;
using std::stringstream;
using std::cout ;
using std::endl;
using std::cerr;
using std::cin ;
using std::ios;
using std::pair;
enum logLevel_e { LL_DEBUG, LL_WARNING, LL_ERROR, LL_INFO, LL_RAW, LL_DDEBUG };
#define STR_LOAD_FAIL "Fatal error occured while loading position."
#define MAX_THREADS 17
#define GRAND_MAX 0xFFFFFFFF
void logFunction(logLevel_e logLevel, const char* timestamp, const char* file, const char* function, int line, ...);
#define logInfo(...) logFunction(LL_INFO, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define logWarning(...) logFunction(LL_WARNING, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define logError(...) logFunction(LL_ERROR, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define logRaw(...) logFunction(LL_RAW, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#ifdef NDEBUG
#define logDebug(...) ((void)0)
#define logDDebug(...) ((void)0)
#else
#define logDebug(...) logFunction(LL_DEBUG, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#ifdef DEBUG
#define logDDebug(...) logFunction(LL_DDEBUG, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#else
#define logDDebug(...) ((void)0)
#endif
#endif
/**
* Simpler file read.
*/
class FileRead
{
public:
/**
* Constructor with filename.
*/
FileRead(string fn);
/**
* Reads whole file as string.
*/
string read();
/**
* Line read.
*
* @return True if read was successfull,
* False otherwise.
*/
bool getLine(string & s);
/**
* Line read.
*
* Expects line to be a pair of stuff, separated by sep.
* @param sep Separator - if not specified implicit ' ' is used.
*
* @return True if read was successfull,
* False otherwise.
*/
bool getLineAsPair(string & s1, string & s2, const char* sep=NULL);
/**
* Set ignore lines flag (for comments, etc.)
*/
void ignoreLines(const char * start);
/**
* Tells whether file is good to read.
*/
bool good() const;
private:
FileRead();
fstream f_;
string ignoreStart_;
};
/**
* (game) random generator.
*/
class Grand
{
public:
Grand();
Grand(unsigned int seed);
void seed(unsigned int seed);
unsigned int operator()();
unsigned int getOne();
float get01();
private:
unsigned int high_;
unsigned int low_;
};
/**
* This function is obsolete ! use grand->get01.
*/
inline double rand01()
{
return (double)rand()/((double)(RAND_MAX) + (double)(1));
}
/**
* Logistic sigmoid.
*/
inline double log_sig(double param, double value)
{
const double E = 2.71828182;
double ret = 1/(1 + pow(E, -1 * param * value));
assert(ret < 1 && ret > 0);
return ret;
}
template<typename T> T max(T a, T b){
return a > b ? a : b;
}
template<typename T> T min(T a, T b){
return a < b ? a : b;
}
#define SQRT_CACHE_SIZE 10000
#define LOG_CACHE_SIZE 10000
extern float sqrtCache[SQRT_CACHE_SIZE];
extern float logCache[LOG_CACHE_SIZE];
void initCachedFunctions();
float mylog(int arg);
float mysqrt(int arg);
inline float mylog(double arg) {return mylog(int(arg));}
inline float mysqrt(double arg){return mysqrt(int(arg));}
/**
String to int converter.
*/
int str2int(const string& str);
/**
* String to float converter.
*/
float str2float(const string& str);
/**
* Spaces trim from right.
*/
string trimRight(const string& str);
/**
* Spaces trim from right.
*/
string trimLeft(const string& str);
/**
* Spaces trim from both sides.
*/
string trim(const string& str);
/**
* Get stream rest.
*/
string getStreamRest(istream& is);
/**
* Tr analogy.
*
* Replaces all ocurences of c1 in s by c2.
*/
string replaceAllChars(string s, char c1, char c2);