-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoutput_extended.h
211 lines (173 loc) · 6.18 KB
/
output_extended.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* output-extended header
* written by Shuangquan Li, [email protected]
* created on 2016-5-10
*/
#ifndef __OUTPUT_EXTENDED_H__
#define __OUTPUT_EXTENDED_H__
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <array>
#include <deque>
#include <forward_list>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <initializer_list>
#include <utility>
// translate an IntegerType to binary string
// split each `group_size` chars with a `delim`
template<typename IntegerType> std::string to_binary(IntegerType a, int group_size = 0, char delim = ' ') {
std::string ret;
for (int i = sizeof(IntegerType) * 8 - 1; i >= 0; --i) {
ret += a & (1LL << i) ? '1' : '0';
if (group_size != 0 && i % group_size == 0) ret += delim;
}
return ret;
}
// overload operator<< for std::pair and all containters
//////////////////// declarations ////////////////////
// std::pair
template<typename T, typename U>
std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& p);
// array_printer
template<typename T>
std::ostream& array_printer(std::ostream& os, const T* a, const size_t n,
const char* delim = " ", const char* leftbound = "", const char* rightbound = "");
// container_printer
template<typename Container>
std::ostream& container_printer(std::ostream& os, const Container& c,
const char* delim = " ", const char* leftbound = "", const char* rightbound = "");
// std::array
template<typename T, size_t N>
std::ostream& operator << (std::ostream& os, const std::array<T, N>& a);
// std::deque
template<typename T>
std::ostream& operator << (std::ostream& os, const std::deque<T>& d);
// std::forward_list
template<typename T>
std::ostream& operator << (std::ostream& os, const std::forward_list<T>& f);
// std::list
template<typename T>
std::ostream& operator << (std::ostream& os, const std::list<T>& l);
// std::set
template<typename T>
std::ostream& operator << (std::ostream& os, const std::set<T>& s);
// std::multiset
template<typename T>
std::ostream& operator << (std::ostream& os, const std::multiset<T>& s);
// std::unordered_set
template<typename T>
std::ostream& operator << (std::ostream& os, const std::unordered_set<T>& s);
// std::vector
template<typename T>
std::ostream& operator << (std::ostream& os, const std::vector<T>& v);
// std::initializer_list
template<typename T>
std::ostream& operator << (std::ostream &os, const std::initializer_list<T>& il);
// std::map
template<typename T, typename U>
std::ostream& operator << (std::ostream& os, const std::map<T, U>& m);
// std::multimap
template<typename T, typename U>
std::ostream& operator << (std::ostream& os, const std::multimap<T, U>& m);
// std::unordered_map
template<typename T, typename U>
std::ostream& operator << (std::ostream& os, const std::unordered_map<T, U>& m);
//////////////////// implementations ////////////////////
template<typename T, typename U>
std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& p) {
os << "(" << p.first << ", " << p.second << ")" << std::flush;
return os;
}
template<typename T>
std::ostream& array_printer(std::ostream& os, const T* a, const size_t n,
const char* delim, const char* leftbound, const char* rightbound) {
os << leftbound;
if (n) {
os << a[0];
for (size_t i = 1; i < n; ++i) os << delim << a[i];
}
os << rightbound;
return os << std::flush;
}
template<typename Container>
std::ostream& container_printer(std::ostream& os, const Container& c,
const char* delim, const char* leftbound, const char* rightbound) {
auto b = c.begin(), e = c.end();
os << leftbound;
if (b != e) {
os << *b++;
for (; b != e; ++b) os << delim << *b;
}
os << rightbound;
return os << std::flush;
}
template<typename T, size_t N>
std::ostream& operator << (std::ostream& os, const std::array<T, N>& a) {
return container_printer(os, a, " ", "[ ", " ]");
}
template<typename T>
std::ostream& operator << (std::ostream& os, const std::deque<T>& d) {
return container_printer(os, d, " ", "[ ", " ]");
}
template<typename T>
std::ostream& operator << (std::ostream& os, const std::forward_list<T>& f) {
return container_printer(os, f, " ", "[ ", " ]");
}
template<typename T>
std::ostream& operator << (std::ostream& os, const std::list<T>& l) {
return container_printer(os, l, " ", "[ ", " ]");
}
template<typename T>
std::ostream& operator << (std::ostream& os, const std::set<T>& s) {
return container_printer(os, s, " ", "{ ", " }");
}
template<typename T>
std::ostream& operator << (std::ostream& os, const std::multiset<T>& s) {
return container_printer(os, s, " ", "{ ", " }");
}
template<typename T>
std::ostream& operator << (std::ostream& os, const std::unordered_set<T>& s) {
return container_printer(os, s, " ", "{ ", " }");
}
template<typename T>
std::ostream& operator << (std::ostream& os, const std::vector<T>& v) {
return container_printer(os, v, " ", "[ ", " ]");
}
template<typename T>
std::ostream& operator << (std::ostream &os, const std::initializer_list<T>& il) {
return container_printer(os, il, " ", "{ ", " }");
}
template<typename T, typename U>
std::ostream& operator << (std::ostream& os, const std::map<T, U>& m) {
typename std::map<T, U>::const_iterator b = m.cbegin(), e = m.cend();
os << "{";
for (; b != e; ++b) os << " " << b->first << ":" << b->second;
os << " }" << std::flush;
return os;
}
template<typename T, typename U>
std::ostream& operator << (std::ostream& os, const std::unordered_map<T, U>& m) {
typename std::unordered_map<T, U>::const_iterator b = m.cbegin(), e = m.cend();
os << "{";
for (; b != e; ++b) os << " " << b->first << ":" << b->second;
os << " }" << std::flush;
return os;
}
template<typename T, typename U>
std::ostream& operator << (std::ostream& os, const std::multimap<T, U>& m) {
typename std::multimap<T, U>::const_iterator b = m.cbegin(), e = m.cend();
os << "{";
for (; b != e; ++b) os << " " << b->first << ":" << b->second;
os << " }" << std::flush;
return os;
}
/* eof */
#endif