-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.hpp
131 lines (111 loc) · 3.57 KB
/
common.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
#ifndef COMMON_H_
#define COMMON_H_
#include <cstdint>
#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <optional>
// State of a mer. Either it is nil (unknown), no (absent), yes (present) or
// blocked (should not take part in an F-move).
enum tristate { nil, no, yes, blocked };
typedef int8_t tristate_t;
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& mds) {
for(size_t i = 0; i < mds.size(); ++i) {
if(i) os << ' ';
os << mds[i];
}
return os;
}
template<>
std::ostream& operator<<(std::ostream& os, const std::vector<tristate_t>& mds);
template<typename C>
std::ostream& print_set(std::ostream& os, const C& set) {
if(!set.empty()) {
auto it = set.cbegin();
os << *it;
for(++it; it != set.cend(); ++it)
os << ',' << *it;
}
return os;
}
template<typename T>
inline std::ostream& operator<<(std::ostream& os, const std::unordered_set<T>& set) { return print_set(os, set); }
template<typename T>
inline std::ostream& operator<<(std::ostream& os, const std::set<T>& set) { return print_set(os, set); }
template<typename T, typename V>
std::ostream& operator<<(std::ostream& os, const std::unordered_map<T, V>& map) {
if(!map.empty()) {
auto it = map.cbegin();
os << '<' << it->first << '>' << it->second;
for(++it; it != map.cend(); ++it)
os << '<' << it->first << '>' << it->second;
}
return os;
}
// For when we have concepts!
// template <typename T>
// concept Streamable =
// requires(std::ostream &os, T value) {
// { os << value } -> std::convertible_to<std::ostream &>;
// };
template<typename IT, typename S, typename T = typename IT::value_type>
struct joinit {
typedef T value_type;
const IT it, itend;
const S& s;
joinit(IT b, const IT e, const S& sep)
: it(b)
, itend(e)
, s(sep)
{}
};
template<typename C, typename S, typename T = typename C::value_type>
//requires Streamable<S> && Streamable<T>
struct join : public joinit<typename C::const_iterator, S, T> {
// typedef T value_type;
// const C& c;
// const S& s;
join(const C& cont, const S& sep)
: joinit<typename C::const_iterator, S, T>(begin(cont), end(cont), sep)
{}
};
// template<typename C, typename S, typename T>
// std::ostream& operator<<(std::ostream& os, const join<C, S, T>& j) {
// auto it = begin(j.c);
// if(it != end(j.c)) {
// os << static_cast<T>(*it);
// for(++it; it != end(j.c); ++it)
// os << j.s << static_cast<T>(*it);
// }
// return os;
// }
template<typename IT, typename S, typename T>
std::ostream& operator<<(std::ostream& os, const joinit<IT, S, T>& j) {
auto it = j.it;
if(it != j.itend) {
os << static_cast<T>(*it);
for(++it; it != j.itend; ++it)
os << j.s << static_cast<T>(*it);
}
return os;
}
template<typename T, typename S, typename C>
auto joinT(const C& cont, const S& sep) -> join<C, S, T> { return join<C, S, T>(cont, sep); }
template<typename T, typename S, typename IT>
auto joinitT(IT b, IT e, const S& sep) -> joinit<IT, S, T> { return joinit<IT, S, T>(b, e, sep); }
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
return os << p.first << ':' << p.second;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::optional<T>& x) {
if(x)
os << "Some(" << *x << ')';
else
os << "None";
return os;
}
#endif // COMMON_H_