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