-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.hpp
143 lines (134 loc) · 4.07 KB
/
debug.hpp
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
#include <bits/stdc++.h>
namespace debug {
template<class Tuple, size_t... I> void print_tuple_helper(std::ostream &out, const Tuple& tuple, std::index_sequence<I...>) {
out << '(';
(..., (operator<<(out, I == 0 ? "" : ", "), operator<<(out, std::get<I>(tuple))));
out << ')';
}
template<class... T> std::ostream& operator<<(std::ostream &out, const std::tuple<T...>& tuple) {
print_tuple_helper(out, tuple, std::make_index_sequence<sizeof...(T)>());
return out;
}
template <typename T, typename U> std::ostream& operator<<(std::ostream &out, const std::pair<T, U> &pair) {
return out << '(' << pair.first << ", " << pair.second << ')';
}
std::ostream& operator<<(std::ostream &out, const std::vector<bool> &vector) {
out << '[';
bool is_first = true;
for (auto flag : vector) {
if (!is_first) {
out << ", ";
}
out << (flag ? '1' : '0');
is_first = false;
}
return out << ']';
}
template<typename T> std::ostream& operator<<(std::ostream &out, const std::vector<T> &vector) {
out << '[';
bool is_first = true;
for (auto element : vector) {
if (!is_first) {
out << ", ";
}
out << element;
is_first = false;
}
return out << ']';
}
template<typename T, std::size_t N> std::ostream& operator<<(std::ostream &out, const std::array<T, N> &array) {
out << '[';
bool is_first = true;
for (auto element : array) {
if (!is_first) {
out << ", ";
}
out << element;
is_first = false;
}
return out << ']';
}
template <typename T> std::ostream& operator<<(std::ostream &out, const std::set<T> &set) {
out << '{';
bool is_first = true;
for (auto x : set) {
if (!is_first) {
out << ", ";
}
out << x;
is_first = false;
}
return out << '}';
}
template <typename T> std::ostream& operator<<(std::ostream &out, const std::multiset<T> &multiset) {
out << '{';
bool is_first = true;
for (auto x : multiset) {
if (!is_first) {
out << ", ";
}
out << x;
is_first = false;
}
return out << '}';
}
template <typename T> std::ostream& operator<<(std::ostream &out, const std::unordered_set<T> &unordered_set) {
out << '{';
bool is_first = true;
for (auto x : unordered_set) {
if (!is_first) {
out << ", ";
}
out << x;
is_first = false;
}
return out << '}';
}
template <typename T, typename U> std::ostream& operator<<(std::ostream &out, const std::map<T, U> &map) {
out << '{';
bool is_first = true;
for (auto &kv : map) {
if (!is_first) {
out << ", ";
}
out << kv;
is_first = false;
}
return out << '}';
}
template <typename T, typename U> std::ostream& operator<<(std::ostream &out, const std::unordered_map<T, U> &unordered_map) {
out << '{';
bool is_first = true;
for (auto &kv : unordered_map) {
if (!is_first) {
out << ", ";
}
out << kv;
is_first = false;
}
return out << '}';
}
template <typename T, typename U> std::ostream& operator<<(std::ostream &out, const std::multimap<T, U> &multimap) {
out << '{';
bool is_first = true;
for (auto &kv : multimap) {
if (!is_first) {
out << ", ";
}
out << kv;
is_first = false;
}
return out << '}';
}
std::ostream& operator<<(std::ostream &out, bool flag) { return out << (flag ? "True" : "False"); }
std::ostream& operator<<(std::ostream &out, const char *s) { return out << (std::string)s; }
template<typename T> concept Printable = requires(T t) { { std::cout << t } -> std::same_as<std::ostream&>; };
template <Printable T> std::ostream& operator<<(std::ostream &out, const T &unknown) { return out << unknown; }
void print() { std::cerr << std::endl; }
template <typename Head, typename... Tail> void print(Head H, Tail... T) {
std::cerr << " ";
operator<<(std::cerr, H);
print(T...);
}
}
#define dbg(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug::print(__VA_ARGS__)