-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
250 lines (218 loc) · 6.35 KB
/
util.cpp
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
#include "util.hpp"
// From
// https://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c
#include <type_traits>
#include <typeinfo>
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
#include <memory>
#include <string>
#include <cstdlib>
template <class T> std::string type_name() {
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void (*)(void *)> own(
#ifndef _MSC_VER
abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr),
#else
nullptr,
#endif
std::free);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += " const";
if (std::is_volatile<TR>::value)
r += " volatile";
if (std::is_lvalue_reference<T>::value)
r += "&";
else if (std::is_rvalue_reference<T>::value)
r += "&&";
return r;
}
// Usage: std::cout << "decltype(it) is " << type_name<decltype(it)>() << '\n';
void handler(int sig) {
void *array[10];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 10);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
void __terminal_error(const char *file, const unsigned line, const char *func, const char *msg) {
printf("[%s:%u:%s] %s\n", file, line, func, msg);
fflush(stdout);
/*std::raise(SIGINT);*/
exit(1);
}
///////////////////////////////////
// From https://github.com/lemire/testingRNG/blob/master/source/widynski.h
#include <stdint.h>
// based on https://arxiv.org/pdf/1704.00358.pdf
static uint64_t g_widynski_x;
static uint64_t g_widynski_w;
static uint64_t g_widynski_s;
static inline void widynski_seed(uint64_t seed) {
g_widynski_w = 0;
g_widynski_x = 0;
g_widynski_s = seed;
g_widynski_s |= 1;
if ((g_widynski_s >> 32) == 0)
g_widynski_s = g_widynski_s | (g_widynski_s << 32);
}
static inline uint32_t widynski() {
g_widynski_x *= g_widynski_x;
g_widynski_x += (g_widynski_w += g_widynski_s);
g_widynski_x = (g_widynski_x >> 32) | (g_widynski_x << 32);
return g_widynski_x;
}
///////////////////////////////////
// inline int rand_int(int max_int) { return std::rand() % (max_int); }
// inline int rand_int_range(int min_int, int max_int) {
// return min_int + (std::rand() % (max_int - min_int + 1));
// }
#define CUSTOMRAND 1
#ifdef CUSTOMRAND
int _rand() {
int r = widynski() % std::numeric_limits<int>::max();
// printf("rnd=%d\n", r);
return r;
}
void rand_seed(int seed) { widynski_seed(seed); }
#else
int _rand() { return (int)std::rand(); }
void rand_seed(int seed) { srand(seed); }
#endif
int rand_int(int max_int) { return _rand() % (max_int); }
int rand_int_range(int min_int, int max_int) {
return min_int + (_rand() % (max_int - min_int + 1));
}
// #define RAND_FLOAT() (static_cast<double>(rand()) / static_cast<double>(RAND_MAX))
#define RAND_FLOAT() (static_cast<double>(_rand()) / static_cast<double>(RAND_MAX))
#define RAND_IN_RANGE(a, b) (a + (b - a) * RAND_FLOAT());
// Return integers from 0 to rangeEnd in random order.
int *getRandomOrderInts(int rangeEnd) {
int *randSample = (int *)calloc((rangeEnd + 1), sizeof(int)); // Init with zeros
int i, tmp;
int swapwith;
for (i = 0; i <= rangeEnd; i++) {
randSample[i] = i;
}
for (i = 0; i <= rangeEnd; i++) {
swapwith = _rand() % (rangeEnd + 1);
tmp = randSample[swapwith];
randSample[swapwith] = randSample[i];
randSample[i] = tmp;
}
return randSample;
}
// Copied from
// https://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf/6089413#6089413
std::istream &safeGetline(std::istream &is, std::string &t) {
t.clear();
// The characters in the stream are read one-by-one using a std::streambuf.
// That is faster than reading them one-by-one using the std::istream.
// Code that uses streambuf this way must be guarded by a sentry object.
// The sentry object performs various tasks,
// such as thread synchronization and updating the stream state.
std::istream::sentry se(is, true);
std::streambuf *sb = is.rdbuf();
for (;;) {
int c = sb->sbumpc();
switch (c) {
case '\n':
return is;
case '\r':
if (sb->sgetc() == '\n')
sb->sbumpc();
return is;
case std::streambuf::traits_type::eof():
// Also handle the case when the last line has no line ending
if (t.empty())
is.setstate(std::ios::eofbit);
return is;
default:
t += (char)c;
}
}
}
static void *safe_malloc(size_t n, unsigned int line) {
void *p = malloc(n);
if (!p) {
fprintf(stderr, "[%s:%u]Out of memory(%u bytes)\n", __FILE__, line, (unsigned int)n);
exit(EXIT_FAILURE);
}
return p;
}
inline int *rand_int_vector(int max_int, int n) {
int *int_v = (int *)safemalloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
int_v[i] = rand_int(max_int + 1);
}
return int_v;
}
int count_lines(FILE *fp) {
int num_lines = 0;
char ch;
fseek(fp, 0L, SEEK_SET);
while (!feof(fp)) {
ch = fgetc(fp);
if (ch == '\n') {
num_lines++;
}
}
fseek(fp, 0L, SEEK_SET);
return num_lines;
}
void write_ints_to_fp(FILE *fp, int *data, int N) {
int i;
for (int i = 0; i < N; i++) {
fprintf(fp, "%d", data[i]);
fprintf(fp, "\n");
}
}
void write_ints_to_file(const char *fn, int *data, int N) {
int i;
FILE *fp;
fp = fopen(fn, "w");
for (int i = 0; i < N; i++) {
fprintf(fp, "%d", data[i]);
fprintf(fp, "\n");
}
fclose(fp);
}
void write_flt_vec2_to_file(const char *fn, vector<vector<float>> *vec2) {
int i;
FILE *fp;
fp = fopen(fn, "w");
vector<vector<float>>::iterator row;
vector<float>::iterator col;
for (row = (*vec2).begin(); row != (*vec2).end(); row++) {
for (col = row->begin(); col != row->end(); col++) {
if (col != row->begin()) {
fprintf(fp, " ");
}
fprintf(fp, "%f", (*col));
}
fprintf(fp, "\n");
}
fclose(fp);
}
void write_dbl_vec2_to_file(const char *fn, vector<vector<double>> *vec2) {
int i;
FILE *fp;
fp = fopen(fn, "w");
vector<vector<double>>::iterator row;
vector<double>::iterator col;
for (row = (*vec2).begin(); row != (*vec2).end(); row++) {
for (col = row->begin(); col != row->end(); col++) {
if (col != row->begin()) {
fprintf(fp, " ");
}
fprintf(fp, "%f", (*col));
}
fprintf(fp, "\n");
}
fclose(fp);
}