-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtools.cpp
More file actions
343 lines (294 loc) · 7.35 KB
/
tools.cpp
File metadata and controls
343 lines (294 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// (c) 2015-2018 Sebastian Kapfer <sebastian.kapfer@fau.de>, FAU Erlangen
// various utilities.
#include "tools.hpp"
#include <cstdlib>
#include <signal.h>
#include <fstream>
#include <stdexcept>
#include <iomanip>
#include <sstream>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <boost/algorithm/string.hpp>
uint64_t gclock ()
{
static uint64_t base = time (0);
struct timeval tv;
gettimeofday(&tv, 0);
return (tv.tv_sec-base) * 1000000ull + tv.tv_usec;
}
static bool sigterm_installed = false, sigterm_caught_flag;
static void sigterm_handler (int)
{
sigterm_caught_flag = true;
}
bool sigterm_caught ()
{
if (!sigterm_installed)
{
sigterm_caught_flag = false;
sigterm_installed = true;
signal (SIGTERM, sigterm_handler);
}
return sigterm_caught_flag;
}
void shell (string_ref command)
{
if (std::system (command.c_str ()) != 0)
rt_error ("command failed: " + command);
}
bool file_is_readable (string_ref path)
{
struct stat stat_result;
if (stat (path.c_str (), &stat_result) != 0)
return false;
return !! (stat_result.st_mode & S_IRUSR);
}
void rename_file (string_ref old_name, string_ref new_name)
{
if (::rename (old_name.c_str (), new_name.c_str ()) != 0)
{
rt_error ("rename " + old_name + " -> " + new_name + " failed");
}
}
bool remove_file (string_ref path, bool ignore_failure)
{
if (::unlink (path.c_str ()) != 0 && !ignore_failure)
{
rt_error ("unlink failed for " + path);
return false;
}
else
{
return true;
}
}
#ifndef HOST_NAME_MAX
// for MacOS. 255 is the limit according to POSIX.
#define HOST_NAME_MAX 255
#endif
string hostname ()
{
char buf[HOST_NAME_MAX+1];
gethostname (buf, HOST_NAME_MAX);
buf[HOST_NAME_MAX] = 0;
return buf;
}
void rt_error (string_ref msg)
{
throw std::runtime_error (msg);
}
std::vector <string> string_split (string_ref str)
{
std::vector <string> ret;
boost::algorithm::split (ret, str,
boost::is_any_of ("\t "), boost::token_compress_on);
return ret;
}
std::ostream &operator<< (std::ostream &os, const AbortObject &)
{
os << std::endl;
std::abort ();
}
static std::ostringstream cerr_buffer;
static std::ofstream cerr_logfile;
static std::ostream original_cerr_stream (0);
void buffer_cerr ()
{
// save the original stream buffer in case we need to restore it
original_cerr_stream.rdbuf (std::cerr.rdbuf ());
// redirect cerr to cerr_buffer
std::cerr.rdbuf (cerr_buffer.rdbuf ());
}
void redirect_cerr (string_ref filename, bool append)
{
std::ios::openmode mode = std::ios::out;
if (append)
mode |= std::ios::app;
cerr_logfile.open (filename, mode);
if (!cerr_logfile)
std::cerr << "cannot open log file " << filename << ABORT;
// OK, assume the log is open. flush the buffer to the log
std::cerr.flush ();
cerr_logfile << cerr_buffer.str ();
cerr_buffer.str ("");
// redirect cerr to the log file
std::cerr.rdbuf (cerr_logfile.rdbuf ());
}
void dont_redirect_cerr ()
{
// all in vain.
std::cerr.flush ();
original_cerr_stream << cerr_buffer.str ();
cerr_buffer.str ("");
// redirect cerr to the log file
std::cerr.rdbuf (original_cerr_stream.rdbuf ());
}
// argument helpers
static
std::pair <string, string> int_pop_arg (const char **argv)
{
assert (*argv);
string kw = *argv++;
if (!*argv)
rt_error ("missing argument for kw " + kw);
return std::make_pair (kw, string (*argv));
}
template <>
string read_arg (const char **argv)
{
string kw, value;
std::tie (kw, value) = int_pop_arg (argv);
return value;
}
template <>
unsigned long read_arg (const char **argv)
{
string kw, value;
std::tie (kw, value) = int_pop_arg (argv);
try
{
return std::stoul (value);
}
catch (...)
{
rt_error ("cannot convert value " + value + " for keyword " + kw);
}
}
template <>
double read_arg (const char **argv)
{
string kw, value;
std::tie (kw, value) = int_pop_arg (argv);
try
{
return std::stod (value);
}
catch (...)
{
rt_error ("cannot convert value " + value + " for keyword " + kw);
}
}
// RandomContext
RandomContext::RandomContext ()
{
#if RANDOM_EXPONENTIAL_BUFFER_SIZE != 0
num_exp_used_ = RANDOM_EXPONENTIAL_BUFFER_SIZE;
#endif
}
void RandomContext::seed (unsigned seed)
{
e_.seed (seed);
}
// FIXME RandomContext::seed_from
double RandomContext::real ()
{
std::uniform_real_distribution<> dis (0, 1);
return dis (e_);
}
double RandomContext::real (double high)
{
return high * real ();
}
double RandomContext::real (double low, double high)
{
assert (low < high);
std::uniform_real_distribution<> dis (low, high);
return dis (e_);
}
unsigned RandomContext::uint (unsigned max_excluded)
{
std::uniform_int_distribution <> dis (0, max_excluded-1u);
return dis (e_);
}
// standard exponential (lambda = 1)
double RandomContext::exponential ()
{
return -std::log (1-real ());
}
#if RANDOM_EXPONENTIAL_BUFFER_SIZE != 0
void RandomContext::refill_exp_buffer_ ()
{
for (int i = 0; i != RANDOM_EXPONENTIAL_BUFFER_SIZE; ++i)
exp_buffer_[i] = real ();
for (int i = 0; i != RANDOM_EXPONENTIAL_BUFFER_SIZE; ++i)
exp_buffer_[i] = -std::log (1-exp_buffer_[i]);
num_exp_used_ = 0;
}
#endif
double RandomContext::pareto (double paralpha, double minimum)
{
assert (paralpha > 0.);
assert (minimum > 0.);
double u = 1 - real ();
return pow (u, -1/paralpha) * minimum;
}
template <>
vector <2> RandomContext::unitbox ()
{
return {{ real (-.5, .5), real (-.5, .5) }};
}
template <>
vector <3> RandomContext::unitbox ()
{
return {{ real (-.5, .5), real (-.5, .5), real (-.5, .5) }};
}
template <>
vector <2> RandomContext::fromsphere <2> (double r)
{
double phi = real (2*M_PI);
return {{ r*std::cos (phi), r*std::sin (phi) }};
}
template <>
vector <3> RandomContext::fromsphere <3> (double r)
{
double x = real (-1, 1);
double y = real (-1, 1);
double z = real (-1, 1);
double s = pow (x*x + y*y + z*z, -0.5) * r;
return {{ s*x, s*y, s*z }};
}
template <>
vector <2> RandomContext::fromball <2> (double r)
{
double x = real () + real ();
if (x > 1.) x = 2-x;
return fromsphere <2> (x*r);
}
template <>
vector <3> RandomContext::fromball <3> (double r)
{
double s = std::cbrt (real ());
return fromsphere <3> (s*r);
}
template <size_t DIM>
vector <DIM> RandomContext::fromshell (double rmin, double rmax)
{
assert (rmin >= 0.);
assert (rmax > rmin);
double dDIM = DIM;
double u = real (pow (rmin, dDIM), pow (rmax, dDIM));
return fromsphere <DIM> (pow (u, 1/dDIM));
}
// instantiate
template vector <2> RandomContext::fromshell (double, double);
template vector <3> RandomContext::fromshell (double, double);
Histogram::Histogram (double bin_wid, double low, double high)
: data_ ((high-low)/bin_wid, 0), bin_wid_ (bin_wid), low_ (low), num_sampl_ (0)
{
}
Histogram::~Histogram ()
{
}
void Histogram::savetxt (string_ref filename) const
{
std::ofstream ofs (filename);
ofs.exceptions (std::ios::failbit | std::ios::badbit);
savetxt (ofs);
}
void Histogram::savetxt (std::ostream &os) const
{
for (unsigned i = 0; i != size (); ++i)
os << bin_center (i) << ' ' << bin_density (i) << '\n';
}