Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #include "Util.h" 00010 00011 namespace grafalgo { 00012 00020 bool Util::skipSpace(istream& in, bool sameline) { 00021 while (true) { 00022 char c = in.get(); 00023 if (!in.good()) return false; 00024 if (sameline && c == '\n') { in.unget(); return false; } 00025 if (!isspace(c)) { in.unget(); return true; } 00026 } 00027 } 00028 00034 bool Util::skipBlank(istream& in) { 00035 char c; bool com; 00036 com = false; 00037 while (1) { 00038 in.get(c); if (!in.good()) return false; 00039 if (c == '#') {com = true; continue; } 00040 if (c == '\n') {com = false; continue; } 00041 if (com || isspace(c)) continue; 00042 in.unget(); return true; 00043 } 00044 } 00045 00055 bool Util::readWord(istream& in, string& s, bool sameline) { 00056 s = ""; 00057 skipSpace(in,sameline); 00058 char c = in.peek(); 00059 if (!in.good() || !isalpha(c)) return false; 00060 while (1) { 00061 c = in.get(); 00062 if (!in.good()) return false; 00063 if (isalpha(c) || isdigit(c) || c == '_' || c == '/') s += c; 00064 else { in.unget(); return true; } 00065 } 00066 } 00067 00075 bool Util::readInt(istream& in, int& i, bool sameline) { 00076 if (skipSpace(in,sameline)) { 00077 char c = in.get(); 00078 if (in.good() && (isdigit(c) || c != '-')) { 00079 in.unget(); 00080 in >> i; 00081 if (in.good()) return true; 00082 } 00083 } 00084 return false; 00085 } 00086 00096 bool Util::verify(istream& in, char c, bool strict) { 00097 skipSpace(in); 00098 char c1 = in.get(); 00099 if (!in.good()) return false; 00100 if (c == c1) return true; 00101 in.unget(); 00102 return false; 00103 } 00104 00110 bool Util::prefix(string& s1, string& s2) { 00111 return s1.length() > 0 && s2.find(s1) == 0; 00112 } 00113 00116 int Util::strnlen(char* s, int n) { 00117 for (int i = 0; i < n; i++) 00118 if (*s++ == '\0') return i; 00119 return n; 00120 } 00121 00124 void Util::genPerm(int n, int p[]) { 00125 int i, j, k; 00126 for (i = 1; i <= n; i++) p[i] = i; 00127 for (i = 1; i <= n; i++) { 00128 j = randint(i,n); 00129 k = p[i]; p[i] = p[j]; p[j] = k; 00130 } 00131 } 00132 00138 uint32_t Util::getTime() { 00139 // note use of static variables 00140 static uint32_t now; 00141 static struct timeval prevTimeval = { 0, 0 }; 00142 00143 if (prevTimeval.tv_sec == 0 && prevTimeval.tv_usec == 0) { 00144 // first call to getTime(); initialize and return 0 00145 if (gettimeofday(&prevTimeval, NULL) < 0) 00146 fatal("Util::getTime: gettimeofday failure"); 00147 now = 0; 00148 return 0; 00149 } 00150 // normal case 00151 struct timeval nowTimeval; 00152 if (gettimeofday(&nowTimeval, NULL) < 0) 00153 fatal("Util::getTime: gettimeofday failure"); 00154 uint32_t dsec = nowTimeval.tv_sec; dsec -= prevTimeval.tv_sec; 00155 uint32_t dusec = nowTimeval.tv_usec - prevTimeval.tv_usec; 00156 if (nowTimeval.tv_usec < prevTimeval.tv_usec) { 00157 dusec = nowTimeval.tv_usec + (1000000 - prevTimeval.tv_usec); 00158 dsec--; 00159 } 00160 now += 1000000*dsec + dusec; 00161 prevTimeval = nowTimeval; 00162 00163 return now; 00164 } 00165 00166 } // ends namespace