-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.h
373 lines (312 loc) · 8.69 KB
/
print.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#ifndef PRINT_PRINT_H
#define PRINT_PRINT_H
#include <type_traits>
#include <iostream>
#include <vector>
#include <deque>
#include <array>
#include <list>
#include <iterator>
#include <forward_list>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <memory>
#include <bitset>
#include <stack>
const int enum_min = -127;
const int enum_max = 127;
struct Params {
std::string sep = " ";
std::string end = "\n";
std::ostream *file = &std::cout;
};
// Check if it is a sequence container
template<typename T>
struct isSequenceContainer : public std::false_type {
};
template<typename T>
struct isSequenceContainer<std::vector<T>> : public std::true_type {
};
template<typename T>
struct isSequenceContainer<std::deque<T>> : public std::true_type {
};
template<typename T, std::size_t N>
struct isSequenceContainer<std::array<T, N>> : public std::true_type {
};
template<typename T>
struct isSequenceContainer<std::list<T>> : public std::true_type {
};
template<typename T>
struct isSequenceContainer<std::forward_list<T>> : public std::true_type {
};
template<typename T, std::size_t N>
struct isSequenceContainer<T[N]> : public std::true_type {
};
// Check if it is a map container
template<typename T>
struct isMapContainer : public std::false_type {
};
template<typename K, typename V>
struct isMapContainer<std::map<K, V>> : public std::true_type {
};
template<typename K, typename V>
struct isMapContainer<std::multimap<K, V>> : public std::true_type {
};
template<typename K, typename V>
struct isMapContainer<std::unordered_map<K, V>> : public std::true_type {
};
template<typename K, typename V>
struct isMapContainer<std::unordered_multimap<K, V>> : public std::true_type {
};
// Check if it is a set container
template<typename T>
struct isSetContainer : public std::false_type {
};
template<typename K, typename V>
struct isSetContainer<std::set<K, V>> : public std::true_type {
};
template<typename K, typename V>
struct isSetContainer<std::multiset<K, V>> : public std::true_type {
};
template<typename K, typename V>
struct isSetContainer<std::unordered_set<K, V>> : public std::true_type {
};
template<typename K, typename V>
struct isSetContainer<std::unordered_multiset<K, V>> : public std::true_type {
};
//concept
template<typename T>
concept Iterable = isSequenceContainer<T>::value;
template<typename T>
concept Map = isMapContainer<T>::value;
template<typename T>
concept Set = isSetContainer<T>::value;
template<typename T>
concept Ptr = std::is_pointer<T>::value ||
std::is_same<T, std::unique_ptr<typename T::element_type>>::value ||
std::is_same<T, std::shared_ptr<typename T::element_type>>::value ||
std::is_same<T, std::weak_ptr<typename T::element_type>>::value;
template<typename T>
void _print(const T &obj, std::ostream &out);
void _print(const char *obj, std::ostream &out);
void _print(const bool &obj, std::ostream &out);
template<Iterable T>
void _print(const T &obj, std::ostream &out);
template<Map T>
void _print(const T &obj, std::ostream &out);
template<Set T>
void _print(const T &obj, std::ostream &out);
template<typename... T>
void _print(const std::tuple<T...> &obj, std::ostream &out);
template<typename T1, typename T2>
void _print(const std::pair<T1, T2> &obj, std::ostream &out);
template<Ptr T>
void _print(const T &obj, std::ostream &out);
template<typename T>
requires std::is_enum_v<T>
void _print(const T &obj, std::ostream &out);
void _print(const __int128 &obj, std::ostream &out);
template<typename... Args>
void print(const Args &... args);
template<typename... Args>
void print(Params params, const Args &... args);
// fundamental types
template<typename T>
void _print(const T &obj, std::ostream &out) {
out << obj;
}
// c-style string
void _print(const char *obj, std::ostream &out) {
out << obj;
}
// bool
void _print(const bool &obj, std::ostream &out) {
if (obj)
out << "True";
else
out << "False";
}
// sequence container
template<Iterable T>
void _print(const T &obj, std::ostream &out) {
bool isFirst = true;
out << '[';
for (auto const &entry: obj) {
if (!isFirst)
out << ", ";
isFirst = false;
_print(entry, out);
}
out << ']';
}
//map container
template<Map T>
void _print(const T &obj, std::ostream &out) {
bool isFirst = true;
out << '{';
for (auto const &entry: obj) {
if (!isFirst)
out << ", ";
isFirst = false;
_print(entry.first, out);
out << ": ";
_print(entry.second, out);
}
out << '}';
}
//set container
template<Set T>
void _print(const T &obj, std::ostream &out) {
bool isFirst = true;
out << '{';
for (auto const &entry: obj) {
if (!isFirst)
out << ", ";
isFirst = false;
_print(entry, out);
}
out << '}';
}
// tuple
template<typename... T>
void _print(const std::tuple<T...> &obj, std::ostream &out) {
bool isFirst = true;
out << "(";
std::apply(
[&isFirst, &out](const T &... entries) {
([&isFirst, &out](const T &entry) {
if (!isFirst)
out << ", ";
isFirst = false;
_print(entry, out);
}(entries), ...);
}, obj);
out << ")";
}
// pair
template<typename T1, typename T2>
void _print(const std::pair<T1, T2> &obj, std::ostream &out) {
out << "(";
out << obj.first;
out << ", ";
out << obj.second;
out << ")";
}
// pointer, unique_ptr and shared_ptr
template<Ptr T>
void _print(const T &obj, std::ostream &out) {
_print(*obj, out);
}
// get enum name of compile-time
template<auto value>
constexpr auto getName() {
std::string_view name;
#ifdef __clang__
name = __PRETTY_FUNCTION__;
auto end = name.find_last_of(']');
auto start = end;
int cnt = 0;
for (; start >= 0; --start) {
if (name[start] == ':')
cnt++;
if (name[start] == ' ' || cnt == 3)
break;
}
start++;
return std::string_view{name.data() + start, end - start};
#elif defined(__GNUC__)
name = __PRETTY_FUNCTION__;
auto end = name.find_last_of(']');
auto start = end;
int cnt = 0;
for (; start >= 0; --start) {
if (name[start] == ':')
cnt++;
if (name[start] == ' ' || cnt == 3)
break;
}
start++;
return std::string_view{name.data() + start, end - start};
// auto start = name.find("value = ") + 8; // 8 is length of "value = "
// name = std::string_view{name.data() + start};
// start = name.find("::");
// name = std::string_view{name.data() + start};
#elif defined(_MSC_VER)
name = __FUNCSIG__;
auto end = name.find_last_of('>');
auto start = end;
int cnt = 0;
for (; start >= 0; --start) {
if (name[start] == ':')
cnt++;
if (name[start] == '<' || name[start] == ' ' || cnt == 3)
break;
}
start++;
return std::string_view{name.data() + start, end - start};
#endif
}
// get enum name of runtime
template<typename T>
requires std::is_enum_v<T>
constexpr std::string_view getName(T value) {
constexpr auto num = enum_max - enum_min + 1;
std::array<std::string_view, num> names
{
[] <std::size_t... Is>(std::index_sequence<Is...>)
{
return std::array<std::string_view, num>{getName<static_cast<T>(Is + enum_min)>()...};
}(std::make_index_sequence<num>{})
};
return names[static_cast<std::size_t>(value) - enum_min];
}
// enum
template<typename T>
requires std::is_enum_v<T>
void _print(const T &obj, std::ostream &out) {
out << getName(obj);
}
//__int128
void _print(const __int128 &obj, std::ostream &out) {
__int128 temp = obj;
std::stack<int> s;
while (temp != 0) {
s.push(temp % 10);
temp /= 10;
}
while (!s.empty()) {
out << s.top();
s.pop();
}
}
template<typename... Args>
void print(const Args &... args) {
// initialization
Params params;
std::ostream &out = *params.file;
bool isFirst = true;
([&isFirst, &out, ¶ms](const Args &args) {
if (!isFirst)
out << params.sep;
isFirst = false;
_print(args, out);
}(args), ...);
out << params.end;
}
template<typename... Args>
void print(Params params, const Args &... args) {
// initialization
std::ostream &out = *params.file;
bool isFirst = true;
([&isFirst, &out, ¶ms](const Args &args) {
if (!isFirst)
out << params.sep;
isFirst = false;
_print(args, out);
}(args), ...);
out << params.end;
}
#endif //PRINT_PRINT_H