Belofte version 2.1.8
A promising chess program using the UCI or Winboard interface
util.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: util.cpp
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#include "belofte.h"
9
10/** implementation of timing functions */
11
12#if defined(__GNUC__)
13#pragma GCC diagnostic push
14#pragma GCC diagnostic ignored "-Weffc++"
15#endif
16
18{
19 // make sure clockstart is initialized
20 ClockStart();
21}
22
23#if defined(__GNUC__)
24#pragma GCC diagnostic pop
25#endif
26
28{
29#if defined(CHRONO_MISSING)
30 gettimeofday(&execution_start, NULL);
31#else
32 execution_start = std::chrono::high_resolution_clock::now();
33#endif
34 m_isEnded = false;
35}
36
38{
39#if defined(CHRONO_MISSING)
40 gettimeofday(&execution_end, NULL);
41#else
42 execution_end = std::chrono::high_resolution_clock::now();
43#endif
44 m_isEnded = true; /// @todo TOCHECK: for multi-threaded, make sure there is no race condition
45}
46
47std::string TimedExecution::getDuration() const
48{
49 std::string duration;
50 if (getDurationMicroSec() < 99000LL)
51#if defined(BELOFTE_NOUNICODE)
52 duration = belofte::to_string(static_cast<int64_t>(getDurationMicroSec())) + " microsec";
53#else
54 duration = belofte::to_string(static_cast<int64_t>(getDurationMicroSec())) + " μs";
55#endif
56 else
57 duration = belofte::prettyTime(getDurationMilliSec());
58 return duration;
59}
60
62{
63 if (m_isEnded) {
64#if defined(CHRONO_MISSING)
65 return (execution_end.tv_sec * 1000000LL + execution_end.tv_usec) -
66 (execution_start.tv_sec * 1000000LL + execution_start.tv_usec);
67#else
68 return std::chrono::duration_cast<std::chrono::microseconds>(execution_end - execution_start).count();
69#endif
70 }
71#if defined(CHRONO_MISSING)
72 struct timeval now_end;
73 gettimeofday(&now_end, NULL);
74 return (now_end.tv_sec * 1000000LL + now_end.tv_usec) -
75 (execution_start.tv_sec * 1000000LL + execution_start.tv_usec);
76#else
77 std::chrono::high_resolution_clock::time_point now_end;
78 now_end = std::chrono::high_resolution_clock::now();
79 return std::chrono::duration_cast<std::chrono::microseconds>(now_end - execution_start).count();
80#endif
81}
82
84{
85 if (m_isEnded) {
86#if defined(CHRONO_MISSING)
87 return (execution_end.tv_sec * 1000LL + execution_end.tv_usec / 1000LL) -
88 (execution_start.tv_sec * 1000LL + execution_start.tv_usec / 1000LL);
89#else
90 return std::chrono::duration_cast<std::chrono::milliseconds>(execution_end - execution_start).count();
91#endif
92 }
93#if defined(CHRONO_MISSING)
94 struct timeval now_end;
95 gettimeofday(&now_end, NULL);
96 return (now_end.tv_sec * 1000LL + now_end.tv_usec / 1000LL) -
97 (execution_start.tv_sec * 1000LL + execution_start.tv_usec / 1000LL);
98#else
99 std::chrono::high_resolution_clock::time_point now_end;
100 now_end = std::chrono::high_resolution_clock::now();
101 return std::chrono::duration_cast<std::chrono::milliseconds>(now_end - execution_start).count();
102#endif
103}
104
106{
107 if (m_isEnded) {
108#if defined(CHRONO_MISSING)
109 return (execution_end.tv_sec + execution_end.tv_usec / 1000000LL) -
110 (execution_start.tv_sec + execution_start.tv_usec / 1000000LL);
111#else
112 return std::chrono::duration_cast<std::chrono::seconds>(execution_end - execution_start).count();
113#endif
114 }
115#if defined(CHRONO_MISSING)
116 struct timeval now_end;
117 gettimeofday(&now_end, NULL);
118 return (now_end.tv_sec + now_end.tv_usec / 1000000LL) -
119 (execution_start.tv_sec + execution_start.tv_usec / 1000000LL);
120#else
121 std::chrono::high_resolution_clock::time_point now_end;
122 now_end = std::chrono::high_resolution_clock::now();
123 return std::chrono::duration_cast<std::chrono::seconds>(now_end - execution_start).count();
124#endif
125}
126
127// --------------------------------------------------------------------
128// utility functions
129
130/** Split delimited long string into a vector
131 */
132belofte::stringList const belofte::stringSplit(std::string src, std::string const& delim)
133{
134 stringList v;
135 size_t delimPos = src.find(delim);
136 while (delimPos != std::string::npos) {
137 v.emplace_back(src.substr(0, delimPos));
138 src = src.substr(delimPos + delim.length());
139 delimPos = src.find(delim);
140 }
141 std::string s = alltrim(src, delim);
142 if (s != "") v.emplace_back(s);
143 return v;
144}
145
146/** Split delimited long string into a pair based on delimiter
147 * e.g. parameter1=x y z, "=" => parameter1, x y z
148 * 1) the chapter, " " => 1), the chapter
149 */
150std::pair<std::string, std::string> belofte::decompose(std::string const& src,
151 std::string const& delim)
152{
153 std::string first;
154 std::string second;
155 size_t delimPos = src.find(delim);
156 if (delimPos != std::string::npos) {
157 first = src.substr(0, delimPos);
158 second = src.substr(delimPos + delim.length());
159 } else {
160 first = src;
161 }
162 return std::make_pair(first, second);
163}
164
165/** std::to_string not compatible on Mac OS (Apple LLVM version 5.0)
166 * provide generic utility function
167 */
168std::string belofte::to_string(int32_t value)
169{
170 std::ostringstream ss;
171 ss << value;
172 return ss.str();
173}
174
175std::string belofte::to_string(int64_t value)
176{
177 std::ostringstream ss;
178 ss << value;
179 return ss.str();
180}
181
182/** trim left and right spaces or delim from string
183 */
184std::string belofte::alltrim(std::string s, std::string const& delim)
185{
186 if (!s.empty()) {
187 while (s.find(delim) == 0) s.erase(0, 1);
188 size_t len = s.size();
189 while (!s.empty() && (s.rfind(delim) == --len)) s.erase(len, len + 1);
190 }
191 return s;
192}
193
194bool belofte::is_number(std::string const& s)
195{
196 return !s.empty() && std::find_if(s.begin(), s.end(),
197 [](unsigned char c) { return !std::isdigit(c); }) == s.end();
198}
199
200/** return random in between 0 and nCeil - 1
201 */
202int belofte::getRandomInt(int const nCeil)
203{
204#if defined(BELOFTE_NORANDOM)
205 return 0;
206#else
207 if (App().getConfig("random", 0)) {
208 return std::rand() % nCeil;
209 } else {
210 return 0;
211 }
212#endif
213}
214
215int belofte::getRandomRange(int const nStart, int const nMax)
216{
217#if defined(BELOFTE_NORANDOM)
218 return (nStart + nMax) / 2;
219#else
220 if (App().getConfig("random", 0)) {
221 return nStart + std::rand() % (nMax + 1 - nStart);
222 } else {
223 return (nStart + nMax) / 2;
224 }
225#endif
226}
227
228/** convert executable name into name.rc, possibly dropping .exe suffix
229 */
230std::string belofte::getRCname(std::string const& basename)
231{
232 std::string rcname(basename);
233
234 std::size_t found = rcname.rfind(std::string(".exe"));
235 if (found != std::string::npos)
236 rcname = rcname.erase(found, std::string::npos);
237 return rcname + ".rc";
238}
239
240/**
241 * find position in which param has been found
242 * @param param vector of strings
243 * @param find string to find
244 * @return -1 in case of nothing found
245 */
246int belofte::positionParamIndex(stringList const& param,
247 std::string const& find)
248{
249 for (unsigned int i = 0; i < param.size(); i++) {
250 if (param[i] == find) return static_cast<int> (i);
251 }
252 return -1;
253}
254
255int belofte::positionParamValue(stringList const& param,
256 std::string const& find, unsigned long const nOffSet)
257{
258 for (unsigned int i = 0; i < param.size(); i++) {
259 if (param[i] == find) {
260 if (i < param.size() + nOffSet) return atoi(param[i + nOffSet].c_str());
261 }
262 }
263 return -1;
264}
265
266std::string belofte::currentDate()
267{
268 time_t now = time(nullptr);
269 struct tm tstruct;
270 char buf[40];
271 tstruct = *localtime(&now);
272 strftime(buf, sizeof (buf), "%Y.%m.%d", &tstruct);
273 return buf;
274}
275
276std::string belofte::scoreAsStr(long const sc)
277{
278#if defined(BELOFTE_NOUNICODE)
279 if (sc == SCORE_INFINITE) return "INF.";
280 if (sc == -SCORE_INFINITE) return "-INF.";
281 if ((sc & SCORE_POSITIVE) == SCORE_PUNDEFINED) return "UNDEF.";
282 if (sc == SCORE_THEORETIC_DRAW) return "1/2-1/2";
283#else
284 if (sc == SCORE_INFINITE) return "∞";
285 if (sc == -SCORE_INFINITE) return "-∞";
286 if ((sc & SCORE_POSITIVE) == SCORE_PUNDEFINED) return "∅";
287 if (sc == SCORE_THEORETIC_DRAW) return "½-½";
288#endif
289 if (sc == SCORE_PRACTICAL_DRAW) return belofte::to_string(static_cast<int32_t>(sc));
290 if (sc > SCORE_WINNING) {
291 // mate or mate in n moves
292 if (sc >= SCORE_MATE - SCORE_CONVERGE_BYDEPTH) return "mate";
293 else return "M-" + belofte::to_string(static_cast<int32_t>((SCORE_MATE - sc + 1) / 2));
294 }
295 if (sc < -SCORE_WINNING) {
296 // mate or mate in n moves
297 if (sc <= -SCORE_MATE + SCORE_CONVERGE_BYDEPTH) return "mate";
298 else return "-M" + belofte::to_string(static_cast<int32_t>(-(SCORE_MATE + sc) / 2));
299 }
300 return belofte::to_string(static_cast<int32_t>(sc));
301}
302
303std::string belofte::prettyTime(long const nTime)
304{
305 long nSeconds = nTime / 1000;
306 int ms = nTime % 1000;
307 std::stringstream sTime;
308 if (nSeconds < 60) {
309 sTime << belofte::to_string(static_cast<int32_t>(nSeconds));
310 } else {
311 if (nSeconds >= 86400) {
312 sTime << belofte::to_string(static_cast<int32_t>(nSeconds / 86400)) << "d ";
313 nSeconds %= 86400;
314 }
315 if (nSeconds >= 3600) {
316 sTime << belofte::to_string(static_cast<int32_t>(nSeconds / 3600)) << ":";
317 nSeconds %= 3600;
318 } else if (nTime > 86400000) {
319 sTime << "00:";
320 }
321 if (nSeconds >= 60) {
322 sTime << std::setw(2) << std::setfill('0')
323 << belofte::to_string(static_cast<int32_t>(nSeconds / 60)) << ":";
324 nSeconds %= 60;
325 } else {
326 sTime << "00:";
327 }
328 sTime << std::setw(2) << std::setfill('0') << belofte::to_string(static_cast<int32_t>(nSeconds));
329 }
330 if (ms) {
331 sTime << "." << std::setw(3) << std::setfill('0')
332 << belofte::to_string(static_cast<int32_t>(ms));
333 }
334 return sTime.str();
335}
336
337// eof
appInstance & App()
Definition belofte.cpp:480
This is the main include file, needs to be included before any other include.
long long getDurationSec() const
Definition util.cpp:105
void ClockEnd()
Definition util.cpp:37
std::string getDuration() const
Definition util.cpp:47
TimedExecution()
implementation of timing functions
Definition util.cpp:17
long long getDurationMilliSec() const
Definition util.cpp:83
void ClockStart()
Definition util.cpp:27
long long getDurationMicroSec() const
Definition util.cpp:61
constexpr bScore SCORE_MATE
Definition eval.h:23
constexpr bScore SCORE_PUNDEFINED
Definition eval.h:19
constexpr bScore SCORE_PRACTICAL_DRAW
Definition eval.h:18
constexpr bScore SCORE_CONVERGE_BYDEPTH
Definition eval.h:29
constexpr bScore SCORE_WINNING
Definition eval.h:24
constexpr bScore SCORE_POSITIVE
Definition eval.h:16
constexpr bScore SCORE_THEORETIC_DRAW
Definition eval.h:17
constexpr bScore SCORE_INFINITE
Definition eval.h:15