-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugging.h
More file actions
139 lines (127 loc) · 3.68 KB
/
debugging.h
File metadata and controls
139 lines (127 loc) · 3.68 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
#include <iostream>
#include <vector>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& vec) {
if (vec.empty()) {
out << "[]";
return out;
}
out << '[';
for (int i = 0; i < vec.size() - 1; i++) {
out << vec[i] << ", ";
}
return out << vec.back() << ']';
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::pair<T1, T2>& pair) {
return out << '(' << pair.first << ", " << pair.second << ')';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::deque<T>& deq) {
if (deq.empty()) {
out << "[]";
return out;
}
out << '[';
for (int i = 0; i < deq.size() - 1; i++) {
out << deq[i] << ", ";
}
return out << deq.back() << ']';
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::unordered_map<T1, T2>& map) {
out << '{';
for (auto it = map.begin(); it != map.end(); it++) {
std::pair<T1, T2> element = *it;
out << element.first << ": " << element.second;
if (std::next(it) != map.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& out, const std::map<T1, T2>& map) {
out << '{';
for (auto it = map.begin(); it != map.end(); it++) {
std::pair<T1, T2> element = *it;
out << element.first << ": " << element.second;
if (std::next(it) != map.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::unordered_set<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::multiset<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::unordered_multiset<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::set<T>& set) {
out << '{';
for (auto it = set.begin(); it != set.end(); it++) {
T element = *it;
out << element;
if (std::next(it) != set.end()) {
out << ", ";
}
}
return out << '}';
}
// Source: https://stackoverflow.com/a/31116392/12128483
template<typename Type, unsigned N, unsigned Last>
struct TuplePrinter {
static void print(std::ostream& out, const Type& value) {
out << std::get<N>(value) << ", ";
TuplePrinter<Type, N + 1, Last>::print(out, value);
}
};
template<typename Type, unsigned N>
struct TuplePrinter<Type, N, N> {
static void print(std::ostream& out, const Type& value) {
out << std::get<N>(value);
}
};
template<typename... Types>
std::ostream& operator<<(std::ostream& out, const std::tuple<Types...>& value) {
out << '(';
TuplePrinter<std::tuple<Types...>, 0, sizeof...(Types) - 1>::print(out, value);
return out << ')';
}