-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.h
139 lines (113 loc) · 3.5 KB
/
debug.h
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
// author: KleiberXD
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <regex>
using namespace std;
#define debug(...) cerr<<"debug:"<<__LINE__<<" "<<#__VA_ARGS__<<": "<<to_string(__VA_ARGS__)<<endl
#define debugm(...) cerr<<"debug:"<<__LINE__<<" ", debug_out_multiple(#__VA_ARGS__, __VA_ARGS__)
#define print(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define debugt(f) \
for ( \
auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \
blockTime.second; \
print("debug:%d Time execution %s(): %lld ms\n", __LINE__, f, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false \
)
string to_string(bool b) {
return (b?"1":"0");
}
string to_string(char c){
return "'" + string({c}) + "'";
}
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + "," + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
template <typename A>
string to_string(vector<A> a) {
string output = "\n[";
for (int i = 0; i < a.size(); i++) {
if(i > 0) output += " ";
output += to_string(a[i]);
}
output += "]";
return output;
}
template <typename A>
string to_string(queue<A>& a) {
typedef typename std::queue<A>::container_type Container;
const auto containerPtr = reinterpret_cast<const Container*>(&a);
vector<A> tmp;
for(auto v : *containerPtr) tmp.push_back(v);
return to_string(tmp);
}
template <typename A>
string to_string(stack<A>& a) {
typedef typename std::stack<A>::container_type Container;
const auto containerPtr = reinterpret_cast<const Container*>(&a);
vector<A> tmp;
for(auto v : *containerPtr) tmp.push_back(v);
reverse(tmp.begin(), tmp.end());
return to_string(tmp);
}
template <typename A, size_t R>
string to_string(A (&a)[R]){
string output = "\n[";
for (int i = 0; i < R; i++) {
if(i > 0) output += " ";
output += to_string(a[i]);
}
output += "]";
return output;
}
template <typename A, size_t R, size_t C>
string to_string(A (&a)[R][C]) {
string output = "";
for (int i = 0; i < R; i++) {
output += to_string(a[i]);
}
output += "";
return output;
}
template <typename A>
string to_string(A a) {
string output = "[";
bool first = true;
for (const auto &v : a) {
if(!first) output += " ";
output += to_string(v);
first = false;
}
output += "]";
return output;
}
void debug_out_multiple(string names) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out_multiple(string names, Head H, Tail... T) {
auto pos = names.find(',');
auto name = names.substr(0, pos);
names = names.substr(pos + 1);
while(names.front() == ' '){
names = names.substr(1);
}
auto output = to_string(H);
regex remove_newlines("\n+");
output = regex_replace(output, remove_newlines, "");
cerr<<name<<": "<<output<<" ";
debug_out_multiple(names, T...);
}