From e705720723585af157ef48a7599b53f8e531db19 Mon Sep 17 00:00:00 2001 From: ShazidMahsrafi Date: Tue, 23 Apr 2024 20:51:35 +0600 Subject: [PATCH] update debug. --- Debug.h | 133 +++++++++++++++++++++++++++++++++++++++-------- Template/Debug.h | 21 -------- 2 files changed, 112 insertions(+), 42 deletions(-) delete mode 100644 Template/Debug.h diff --git a/Debug.h b/Debug.h index a796ca9..bf23ce1 100644 --- a/Debug.h +++ b/Debug.h @@ -1,21 +1,112 @@ -#include -using namespace std; -void __print(int x) {cerr << x;} -void __print(long x) {cerr << x;} -void __print(long long x){cerr << x;} -void __print(unsigned x){cerr << x;} -void __print(unsigned long x){cerr << x;} -void __print(unsigned long long x){cerr << x;} -void __print(float x){cerr << x;} -void __print(double x){cerr << x;} -void __print(long double x){cerr << x;} -void __print(char x){cerr << x;} -void __print(const char *x){cerr << x;} -void __print(const string &x){cerr << x;} -void __print(bool x){cerr << (x ? "true" : "false");} -void _print() { cerr << "\n"; } -template void __print(const A &x); template void __print(const pair &p); -template void __print(const A &x) {bool f=1; cerr << '['; for (const auto &i : x) {cerr << (f ? "" : ","), __print(i); f = 0;} cerr << ']';} -template void __print(const pair &p) {cerr << '('; __print(p.first); cerr << ','; __print(p.second); cerr << ')';} -template void _print(const Head &H, const Tail &...T) { __print(H); if (sizeof...(T)) cerr << ", "; _print(T...);} -#define dbg(x...) cerr << #x << " : "; _print(x) \ No newline at end of file +#include +namespace __DEBUG_UTIL__ +{ + using namespace std; + template + concept is_iterable = requires(T &&x) { begin(x); } && + !is_same_v, 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 &v) + { /* Overloaded this because stl optimizes vector 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 + void print(T &&x) + { + if constexpr (is_iterable) + if (size(x) && is_iterable) + { /* Iterable inside Iterable */ + int f = 0; + cerr << "\n~~~~~\n"; + for (auto &&i : x) + { + cerr << setw(2) << 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 + 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.write(names, i) << " = "; + print(head); + if constexpr (sizeof...(tail)) + cerr << " ||", printer(names + i + 1, tail...); + else + cerr << "]\n"; + } + template + void printerArr(const char *names, T arr[], size_t N, V... tail) + { + size_t i = 0; + for (; names[i] and names[i] != ','; i++) + cerr << names[i]; + for (i++; names[i] and names[i] != ','; i++) + ; + cerr << " = {"; + for (size_t ind = 0; ind < N; ind++) + cerr << (ind ? "," : ""), print(arr[ind]); + cerr << "}"; + if constexpr (sizeof...(tail)) + cerr << " ||", printerArr(names + i + 1, tail...); + else + cerr << "]\n"; + } + +} +#ifdef ONLINE_JUDGE +#define dbg(...) +#define dbgArr(...) +#else +#define dbg(...) std::cerr << __LINE__ << ": [", __DEBUG_UTIL__::printer(#__VA_ARGS__, __VA_ARGS__) +#define dbgArr(...) std::cerr << __LINE__ << ": [", __DEBUG_UTIL__::printerArr(#__VA_ARGS__, __VA_ARGS__) +#endif \ No newline at end of file diff --git a/Template/Debug.h b/Template/Debug.h deleted file mode 100644 index a796ca9..0000000 --- a/Template/Debug.h +++ /dev/null @@ -1,21 +0,0 @@ -#include -using namespace std; -void __print(int x) {cerr << x;} -void __print(long x) {cerr << x;} -void __print(long long x){cerr << x;} -void __print(unsigned x){cerr << x;} -void __print(unsigned long x){cerr << x;} -void __print(unsigned long long x){cerr << x;} -void __print(float x){cerr << x;} -void __print(double x){cerr << x;} -void __print(long double x){cerr << x;} -void __print(char x){cerr << x;} -void __print(const char *x){cerr << x;} -void __print(const string &x){cerr << x;} -void __print(bool x){cerr << (x ? "true" : "false");} -void _print() { cerr << "\n"; } -template void __print(const A &x); template void __print(const pair &p); -template void __print(const A &x) {bool f=1; cerr << '['; for (const auto &i : x) {cerr << (f ? "" : ","), __print(i); f = 0;} cerr << ']';} -template void __print(const pair &p) {cerr << '('; __print(p.first); cerr << ','; __print(p.second); cerr << ')';} -template void _print(const Head &H, const Tail &...T) { __print(H); if (sizeof...(T)) cerr << ", "; _print(T...);} -#define dbg(x...) cerr << #x << " : "; _print(x) \ No newline at end of file