00001
00008 #pragma once
00009
00010 #include <cassert>
00011
00012
00013 #include <iostream>
00014 #include <fstream>
00015 #include <sstream>
00016 #include <string>
00017
00018 #include <ctime>
00019 #include <cstring>
00020 #include <cstdio>
00021 #include <cstdlib>
00022 #include <cstdarg>
00023 #include <limits.h>
00024 #include <math.h>
00025
00026 typedef unsigned long long u64;
00027
00028 typedef unsigned int uint;
00029
00030 using std::string ;
00031
00032 using std::fstream;
00033 using std::ostream;
00034 using std::istream;
00035 using std::stringstream;
00036
00037 using std::cout ;
00038 using std::endl;
00039 using std::cerr;
00040 using std::cin ;
00041 using std::ios;
00042
00043 using std::pair;
00044
00045
00046 enum logLevel_e { LL_DEBUG, LL_WARNING, LL_ERROR, LL_INFO, LL_RAW, LL_DDEBUG };
00047
00048 #define STR_LOAD_FAIL "Fatal error occured while loading position."
00049
00050 #define MAX_THREADS 17
00051
00052 #define GRAND_MAX 0xFFFFFFFF
00053
00054 void logFunction(logLevel_e logLevel, const char* timestamp, const char* file, const char* function, int line, ...);
00055
00056 #define logInfo(...) logFunction(LL_INFO, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
00057 #define logWarning(...) logFunction(LL_WARNING, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
00058 #define logError(...) logFunction(LL_ERROR, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
00059 #define logRaw(...) logFunction(LL_RAW, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
00060 #ifdef NDEBUG
00061 #define logDebug(...) ((void)0)
00062 #define logDDebug(...) ((void)0)
00063 #else
00064 #define logDebug(...) logFunction(LL_DEBUG, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
00065 #ifdef DEBUG
00066 #define logDDebug(...) logFunction(LL_DDEBUG, __TIME__, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
00067 #else
00068 #define logDDebug(...) ((void)0)
00069 #endif
00070 #endif
00071
00072
00076 class FileRead
00077 {
00078 public:
00082 FileRead(string fn);
00083
00087 string read();
00088
00095 bool getLine(string & s);
00096
00106 bool getLineAsPair(string & s1, string & s2, const char* sep=NULL);
00107
00111 void ignoreLines(const char * start);
00112
00116 bool good() const;
00117
00118 private:
00119 FileRead();
00120 fstream f_;
00121 string ignoreStart_;
00122 };
00123
00124
00125
00129 class Grand
00130 {
00131 public:
00132 Grand();
00133 Grand(unsigned int seed);
00134 void seed(unsigned int seed);
00135 unsigned int operator()();
00136 unsigned int getOne();
00137 float get01();
00138 private:
00139 unsigned int high_;
00140 unsigned int low_;
00141 };
00142
00143
00147 inline double rand01()
00148 {
00149 return (double)rand()/((double)(RAND_MAX) + (double)(1));
00150 }
00151
00155 inline double log_sig(double param, double value)
00156 {
00157 const double E = 2.71828182;
00158 double ret = 1/(1 + pow(E, -1 * param * value));
00159 assert(ret < 1 && ret > 0);
00160 return ret;
00161 }
00162
00163 template<typename T> T max(T a, T b){
00164 return a > b ? a : b;
00165 }
00166
00167 template<typename T> T min(T a, T b){
00168 return a < b ? a : b;
00169 }
00170
00171 #define SQRT_CACHE_SIZE 10000
00172 #define LOG_CACHE_SIZE 10000
00173 extern float sqrtCache[SQRT_CACHE_SIZE];
00174 extern float logCache[LOG_CACHE_SIZE];
00175
00176 void initCachedFunctions();
00177 float mylog(int arg);
00178 float mysqrt(int arg);
00179 inline float mylog(double arg) {return mylog(int(arg));}
00180 inline float mysqrt(double arg){return mysqrt(int(arg));}
00181
00185 int str2int(const string& str);
00186
00190 float str2float(const string& str);
00191
00195 string trimRight(const string& str);
00196
00200 string trimLeft(const string& str);
00201
00205 string trim(const string& str);
00206
00210 string getStreamRest(istream& is);
00211
00217 string replaceAllChars(string s, char c1, char c2);