48 #include <type_traits>
50 #include <unordered_set>
52 #include <forward_list>
55 #include <unordered_map>
60 template<
typename Set1,
typename Set2>
61 bool disjoint(Set1
const &set1, Set2
const &set2)
63 if (set1.empty() || set2.empty())
return true;
65 typename Set1::const_iterator it1 = set1.begin(), it1End = set1.end();
66 typename Set2::const_iterator it2 = set2.begin(), it2End = set2.end();
68 if (*it1 > *set2.rbegin() || *it2 > *set1.rbegin())
return true;
70 while (it1 != it1End && it2 != it2End) {
71 if (*it1 == *it2)
return false;
72 if (*it1 < *it2) { ++it1; }
79 template<
typename Set1,
typename Set2>
80 bool subsetOf(Set1
const & set1, Set2
const & set2)
82 if (set1.size() > set2.size())
return false;
83 for (
auto const & e : set1) {
84 if (set2.find(e) == set2.end())
91 template<
typename Set1,
typename Set2>
92 std::set<typename Set1::value_type> &
operator|=(Set1 & lhs, Set2
const & rhs)
94 lhs.insert(rhs.cbegin(), rhs.cend());
99 template<
typename Set1,
typename Set2>
100 std::set<typename Set1::value_type>
operator|(Set1 set1, Set2
const & set2)
107 template<
typename Set1,
typename Set2>
108 std::set<typename Set1::value_type> &
operator&=(Set1 & lhs, Set2
const & rhs)
110 typename Set1::iterator it = lhs.begin();
111 while (it != lhs.end()) {
112 if (rhs.find(*it) == rhs.end())
121 template<
typename Set1,
typename Set2>
122 std::set<typename Set1::value_type>
operator&(Set1 set1, Set2
const & set2)
129 template<
typename Set1,
typename Set2>
130 std::set<typename Set1::value_type> &
operator-=(Set1 & lhs, Set2
const & rhs)
132 for (
auto const & e : rhs) {
134 if (lhs.size() == 0)
break;
140 template<
typename Set1,
typename Set2>
141 std::set<typename Set1::value_type>
operator-(Set1 set1, Set2
const & set2)
148 template<
typename Set1,
typename Set2>
149 std::set<typename Set1::value_type> &
operator^=(Set1 & lhs, Set2
const & rhs)
151 for (
auto const & e : rhs) {
152 auto it = lhs.find(e);
162 template<
typename Set1,
typename Set2>
163 std::set<typename Set1::value_type>
operator^(Set1 set1, Set2
const & set2)
172 template<
template<
typename,
typename...>
class Container,
174 typename =
typename std::enable_if<std::is_same<std::vector<T>, Container<T> >::value ||
175 std::is_same<std::set<T>, Container<T> >::value ||
176 std::is_same<std::unordered_set<T>, Container<T> >::value ||
177 std::is_same<std::deque<T>, Container<T> >::value ||
178 std::is_same<std::list<T>, Container<T> >::value ||
179 std::is_same<std::forward_list<T>, Container<T> >::value
182 std::ostream & operator<<(std::ostream & os, Container<T, Rest...>
const & s)
186 char * psep = sep + 1;
187 for (T
const & e : s) {
196 template<
typename F,
typename S>
197 std::ostream & operator<<(std::ostream & os, std::pair<F, S>
const & p)
199 os <<
'(' << p.first <<
',' << p.second <<
')';
204 template<
template<
typename,
typename...>
class Container,
205 typename Key,
typename Val,
206 typename =
typename std::enable_if<std::is_same<std::map<Key, Val>, Container<Key, Val> >::value || std::is_same<std::unordered_map<Key, Val>, Container<Key, Val> >::value>::type,
208 std::ostream & operator<<(std::ostream & os, Container<Key, Val, Rest...>
const & m)
212 char * psep = sep + 1;
213 for (std::pair<Key, Val>
const e : m) {
222 template<
typename T,
size_t N>
223 std::ostream & operator<<(std::ostream & os, std::array<T, N>
const & a)
227 char * psep = sep + 1;
228 for (T
const & e : a) {
239 typename =
typename std::enable_if<!std::is_same<char, T>::value>::type,
241 std::ostream & operator<<(std::ostream & os, T
const (&a)[N])
245 char * psep = sep + 1;
246 for (T
const & e : a) {
256 template <
typename T,
size_t Size>
258 static std::ostream & print(std::ostream & os, T
const & t) {
260 << (Size != 1 ?
"," :
"") << std::get<Size-1>(t);
264 template <
typename T>
266 static std::ostream & print(std::ostream & os, T
const &) {
271 template <
typename ...Args>
272 std::ostream & operator<<(std::ostream & os, std::tuple<Args...>
const & t) {
273 using T = std::tuple<Args...>;