-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
157 lines (136 loc) · 3.6 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
#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);
}
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));
}
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);
}