-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug_Template.cpp
More file actions
154 lines (137 loc) · 4.55 KB
/
Debug_Template.cpp
File metadata and controls
154 lines (137 loc) · 4.55 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
#include <bits/stdc++.h>
using namespace std;
#define dbg(...) cerr << debug_util::outer << __LINE__ << ": [", \
debug_util::printer(#__VA_ARGS__, __VA_ARGS__), \
cerr << debug_util::outer << "]\n" << debug_util::reset
namespace debug_util {
const string WHITE = "\033[0;m";
const string RED = "\033[0;31m";
const string BLUE = "\033[0;34m";
const string GREEN = "\033[0;32m";
bool colored_output = isatty(fileno(stderr));
string reset = colored_output ? WHITE : "";
string outer = colored_output ? RED : "";
string var_name = colored_output ? BLUE : "";
string var_value = colored_output ? GREEN : "";
template <typename T>
concept is_iterable = requires(T&& x) { begin(x); } && !is_same_v<remove_cvref_t<T>, string>;
template <typename T>
void print(T* p);
void print(const char* x) { cerr << x; }
void print(char x) { cerr << "\'" << x << "\'"; }
void print(string x) { cerr << "\"" << x << "\""; }
void print(bool x) { cerr << (x ? "true" : "false"); }
// Print vector<bool>, stl optimizes this by using _Bit_reference
void print(vector<bool>& v) {
int f = 0;
cerr << "{";
for (auto&& i : v) {
cerr << (f++ ? ", " : "") << (i ? "true" : "false");
}
cerr << "}";
}
template <typename T>
void print(T&& x) {
if constexpr (is_iterable<T>) {
if (size(x) && is_iterable<decltype(*(begin(x)))>) {
// Iterable inside Iterable
int f = 0;
int w = max(0, (int)log10(size(x) - 1)) + 2;
cerr << "\n~~~~~~~~\n";
for (auto&& i : x) {
cerr << setw(w) << left << f++, print(i), cerr << "\n";
}
cerr << "~~~~~~~~\n";
} else {
// Normal Iterable
int f = 0;
cerr << "{";
for (auto&& i : x) {
cerr << (f++ ? ", " : ""), print(i);
}
cerr << "}";
}
} else if constexpr (requires { x.pop(); }) {
auto tmp = x;
int f = 0;
cerr << "{";
if constexpr (requires { x.top(); }) {
// Stack, Priority Queue
while (!tmp.empty()) {
cerr << (f++ ? ", " : ""), print(tmp.top()), tmp.pop();
}
} else {
// Queue
while (!tmp.empty()) {
cerr << (f++ ? ", " : ""), print(tmp.front()), tmp.pop();
}
}
cerr << "}";
} else if constexpr (requires { x.first; x.second; }) {
// Pair
cerr << "(", print(x.first), cerr << ", ", print(x.second), cerr << ")";
} else if constexpr (requires { get<0>(x); }) {
// Tuple
int f = 0;
cerr << "(";
apply([&f](auto... args) {
((cerr << (f++ ? ", " : ""), print(args)), ...);
}, x);
cerr << ")";
} else {
cerr << x;
}
}
// Print pointers address and, if not null, pointed value
template <typename T>
void print(T* p) {
if (!p) {
cerr << "(nullptr)";
} else {
cerr << "(" << p << ") -> ";
print(*p);
}
}
template <typename T, typename... V>
void printer(const char* names, T&& head, V&&... tail) {
int i = 0;
for (int bracket = 0; names[i] != '\0' && (names[i] != ',' || bracket != 0); i++) {
if (names[i] == '(' || names[i] == '<' || names[i] == '{') {
bracket++;
} else if (names[i] == ')' || names[i] == '>' || names[i] == '}') {
bracket--;
}
}
cerr << var_name;
cerr.write(names, i) << outer << " = " << var_value;
print(head);
if constexpr (sizeof...(tail)) {
cerr << outer << " ||";
printer(names + i + 1, tail...);
}
}
}
void solve() {
pair<int, bool> p1 = {1, true};
pair<char, string> p2 = {'a', "mmd"};
vector<int> v1(2);
vector<bool> v2(2);
dbg(p1, p2, v1, v2);
int n = 100;
int* ptr = &n;
int** pptr = &ptr;
dbg(ptr, pptr);
vector<vector<vector<int>>> vv;
vv = {{{111, 112}, {121, 122}}, {{211, 212}, {221, 222}}};
dbg(vv);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}