-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_template.hpp
More file actions
126 lines (125 loc) · 4.59 KB
/
Copy pathdebug_template.hpp
File metadata and controls
126 lines (125 loc) · 4.59 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
#ifndef __DEBUG_TEMPLATE_hpp__
#define __DEBUG_TEMPLATE_hpp__
#include <bits/stdc++.h>
#define cerr cout
namespace __DEBUG_UTIL__
{
using namespace std;
bool I_want_colored_output = false; /* ONLY WORKS WITH TERMINAL */
string white = I_want_colored_output ? "\033[0;m" : "";
string outer = I_want_colored_output ? "\033[0;31m" : ""; // red
string varName = I_want_colored_output ? "\033[1;34m" : ""; // blue
string varValue = I_want_colored_output ? "\033[1;32m" : ""; // green
template <typename T>
concept is_iterable = requires(T &&x) { begin(x); } &&
!is_same_v<remove_cvref_t<T>, string>;
void print(const char *x) { cerr << x; }
void print(char x) { cerr << "\'" << x << "\'"; }
void print(bool x) { cerr << (x ? "T" : "F"); }
void print(string x) { cerr << "\"" << x << "\""; }
void print(vector<bool> &&v)
{ /* Overloaded this because stl optimizes vector<bool> by using
_Bit_reference instead of bool to conserve space. */
int f = 0;
cerr << '{';
for (auto &&i : v)
cerr << (f++ ? "," : "") << (i ? "T" : "F");
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;
cerr << "\n~~~~~\n";
int w = max(0, (int)log10(size(x) - 1)) + 2;
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(); }) /* Stacks, Priority Queues, Queues */
{
auto temp = x;
int f = 0;
cerr << "{";
if constexpr (requires { x.top(); })
while (!temp.empty())
cerr << (f++ ? "," : ""), print(temp.top()), temp.pop();
else
while (!temp.empty())
cerr << (f++ ? "," : ""), print(temp.front()), temp.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;
}
template <typename T, typename... V>
void printer(const char *names, T &&head, V &&...tail)
{
int i = 0;
for (size_t bracket = 0; names[i] != '\0' and (names[i] != ',' or bracket != 0); i++)
if (names[i] == '(' or names[i] == '<' or names[i] == '{')
bracket++;
else if (names[i] == ')' or names[i] == '>' or names[i] == '}')
bracket--;
cerr << varName;
cerr.write(names, i) << outer << " = " << varValue;
print(head);
if constexpr (sizeof...(tail))
cerr << outer << " ||", printer(names + i + 1, tail...);
else
cerr << outer << "]\n"
<< white;
}
template <typename T, typename... V>
void printerArr(const char *names, T arr[], size_t N, V... tail)
{
size_t i = 0;
cerr << varName;
for (; names[i] and names[i] != ','; i++)
cerr << names[i];
for (i++; names[i] and names[i] != ','; i++)
;
cerr << outer << " = " << varValue << "{";
for (size_t ind = 0; ind < N; ind++)
cerr << (ind ? "," : ""), print(arr[ind]);
cerr << "}";
if constexpr (sizeof...(tail))
cerr << outer << " ||", printerArr(names + i + 1, tail...);
else
cerr << outer << "]\n"
<< white;
}
}
#ifndef ONLINE_JUDGE
#define debug(...) std::cerr << __DEBUG_UTIL__::outer << __LINE__ << ": [", __DEBUG_UTIL__::printer(#__VA_ARGS__, __VA_ARGS__)
#define debugArr(...) std::cerr << __DEBUG_UTIL__::outer << __LINE__ << ": [", __DEBUG_UTIL__::printerArr(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...)
#define debugArr(...)
#endif
#endif