Belofte version 2.1.9
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 check 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
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/**
131 * Split delimited long string into a vector
132 */
133belofte::stringList const belofte::stringSplit(std::string src, std::string const& delim)
134{
135 stringList v;
136 size_t delimPos = src.find(delim);
137 while (delimPos != std::string::npos) {
138 v.emplace_back(src.substr(0, delimPos));
139 src = src.substr(delimPos + delim.length());
140 delimPos = src.find(delim);
141 }
142 std::string s = alltrim(src, delim);
143 if (s != "") v.emplace_back(s);
144 return v;
145}
146
147/**
148 * Split delimited long string into a pair based on delimiter
149 * e.g. parameter1=x y z, "=" => parameter1, x y z
150 * 1) the chapter, " " => 1), the chapter
151 */
152std::pair<std::string, std::string> belofte::decompose(std::string const& src,
153 std::string const& delim)
154{
155 std::string first;
156 std::string second;
157 size_t delimPos = src.find(delim);
158 if (delimPos != std::string::npos) {
159 first = src.substr(0, delimPos);
160 second = src.substr(delimPos + delim.length());
161 } else {
162 first = src;
163 }
164 return std::make_pair(first, second);
165}
166
167/**
168 * std::to_string not compatible on Mac OS (Apple LLVM version 5.0)
169 * provide generic utility function
170 */
171std::string belofte::to_string(int16_t value)
172{
173 std::ostringstream ss;
174 ss << value;
175 return ss.str();
176}
177
178std::string belofte::to_string(int32_t value)
179{
180 std::ostringstream ss;
181 ss << value;
182 return ss.str();
183}
184
185std::string belofte::to_string(int64_t value)
186{
187 std::ostringstream ss;
188 ss << value;
189 return ss.str();
190}
191
192/**
193 * trim left and right spaces or delim from string
194 */
195std::string belofte::alltrim(std::string s, std::string const& delim)
196{
197 if (!s.empty()) {
198 while (s.find(delim) == 0) s.erase(0, 1);
199 size_t len = s.size();
200 while (!s.empty() && (s.rfind(delim) == --len)) s.erase(len, len + 1);
201 }
202 return s;
203}
204
205bool belofte::is_number(std::string const& s)
206{
207 return !s.empty() && std::find_if(s.begin(), s.end(),
208 [](unsigned char c) { return !std::isdigit(c); }) == s.end();
209}
210
211/**
212 * return random in between 0 and nCeil - 1
213 */
214int belofte::getRandomInt(int const nCeil)
215{
216#if defined(BELOFTE_NORANDOM)
217 return 0;
218#else
219 if (App().getConfig("random", 0)) {
220 return std::rand() % nCeil;
221 } else {
222 return 0;
223 }
224#endif
225}
226
227int belofte::getRandomRange(int const nStart, int const nMax)
228{
229#if defined(BELOFTE_NORANDOM)
230 return (nStart + nMax) / 2;
231#else
232 if (App().getConfig("random", 0)) {
233 return nStart + std::rand() % (nMax + 1 - nStart);
234 } else {
235 return (nStart + nMax) / 2;
236 }
237#endif
238}
239
240/**
241 * convert executable name into name.rc, possibly dropping .exe suffix
242 */
243std::string belofte::getRCname(std::string const& basename)
244{
245 std::string rcname(basename);
246
247 std::size_t found = rcname.rfind(std::string(".exe"));
248 if (found != std::string::npos)
249 rcname = rcname.erase(found, std::string::npos);
250 return rcname + ".rc";
251}
252
253/**
254 * find position in which param has been found
255 * @param param vector of strings
256 * @param find string to find
257 * @return -1 in case of nothing found
258 */
260 std::string const& find)
261{
262 for (unsigned int i = 0; i < param.size(); ++i) {
263 if (param[i] == find) return static_cast<int> (i);
264 }
265 return -1;
266}
267
269 std::string const& find, unsigned long const nOffSet)
270{
271 for (unsigned int i = 0; i < param.size(); ++i) {
272 if (param[i] == find) {
273 if (i < param.size() + nOffSet) return atoi(param[i + nOffSet].c_str());
274 }
275 }
276 return -1;
277}
278
280{
281 time_t now = time(nullptr);
282 struct tm tstruct;
283 char buf[40];
284 tstruct = *localtime(&now);
285 strftime(buf, sizeof (buf), "%Y.%m.%d", &tstruct);
286 return buf;
287}
288
289std::string belofte::prettyTime(long const nTime)
290{
291 long nSeconds = nTime / 1000;
292 int ms = nTime % 1000;
293 std::stringstream sTime;
294 if (nSeconds < 60) {
295 sTime << belofte::to_string(static_cast<int32_t>(nSeconds));
296 } else {
297 if (nSeconds >= 86400) {
298 sTime << belofte::to_string(static_cast<int32_t>(nSeconds / 86400)) << "d ";
299 nSeconds %= 86400;
300 }
301 if (nSeconds >= 3600) {
302 sTime << belofte::to_string(static_cast<int32_t>(nSeconds / 3600)) << ":";
303 nSeconds %= 3600;
304 } else if (nTime > 86400000) {
305 sTime << "00:";
306 }
307 if (nSeconds >= 60) {
308 sTime << std::setw(2) << std::setfill('0')
309 << belofte::to_string(static_cast<int32_t>(nSeconds / 60)) << ":";
310 nSeconds %= 60;
311 } else {
312 sTime << "00:";
313 }
314 sTime << std::setw(2) << std::setfill('0') << belofte::to_string(static_cast<int32_t>(nSeconds));
315 }
316 if (ms) {
317 sTime << "." << std::setw(3) << std::setfill('0')
318 << belofte::to_string(static_cast<int32_t>(ms));
319 }
320 return sTime.str();
321}
322
323// eof
appInstance & App()
Definition belofte.cpp:225
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
int getRandomRange(int const nStart, int const nMax)
Definition util.cpp:227
int positionParamValue(stringList const &param, std::string const &find, unsigned long const nOffSet=0)
Definition util.cpp:268
std::string alltrim(std::string s, std::string const &delim=" ")
trim left and right spaces or delim from string
Definition util.cpp:195
std::pair< std::string, std::string > decompose(std::string const &src, std::string const &delim)
Split delimited long string into a pair based on delimiter e.g.
Definition util.cpp:152
std::string to_string(int16_t value)
std::to_string not compatible on Mac OS (Apple LLVM version 5.0) provide generic utility function
Definition util.cpp:171
stringList const stringSplit(std::string src, std::string const &delim)
Split delimited long string into a vector.
Definition util.cpp:133
std::vector< std::string > stringList
Definition util.h:46
std::string currentDate()
Definition util.cpp:279
std::string prettyTime(long const t)
Definition util.cpp:289
std::string getRCname(std::string const &basename)
convert executable name into name.rc, possibly dropping .exe suffix
Definition util.cpp:243
bool is_number(std::string const &s)
Definition util.cpp:205
int getRandomInt(int const nCeil)
return random in between 0 and nCeil - 1
Definition util.cpp:214
int positionParamIndex(stringList const &param, std::string const &find)
find position in which param has been found
Definition util.cpp:259