-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
211 lines (183 loc) · 5.35 KB
/
util.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
#ifndef UTIL_H
#define UTIL_H
#ifdef __linux__
#include <bits/stdc++.h>
#elif _WIN32
#include <fstream>
#include <atomic>
#include <thread>
#include <queue>
#endif
// Uncovering some classes from std
using std::vector;
using std::string;
using std::array;
using std::pair;
using std::cerr;
using std::cout;
using std::map;
using std::set;
using std::stack;
using std::make_tuple;
using std::make_pair;
#define val(x) #x "=", x
// #define ENABLE_STEP_MODEL 1
// #define ENABLE_LOG 1
#ifdef ENABLE_LOG
#define print(...) debug_print(__VA_ARGS__)
#define println(...) \
{ \
debug_print(__VA_ARGS__); \
std::cout << "\n"; \
}
#else
#define print(os, ...) ;
#define println(os, ...) ;
#endif
#define fprint(os, ...) ;
#define fprintln(os, ...) ;
// #define fprint(os, ...) debug_print(os, __VA_ARGS__)
// #define fprintln(os, ...) \
// { \
// debug_print(os, __VA_ARGS__); \
// os << '\n'; \
// }
void debug_print() {
}
template <typename T, typename... Args>
void debug_print(T t, Args... args) {
cout << t << " ";
debug_print(args...);
}
template <class T>
int getIndex(const std::vector<T> &v, const T &elem) {
auto ptr = lower_bound(v.begin(), v.end(), elem);
if (ptr == v.end())
throw std::runtime_error("World is coming to an end with elem");
if (elem != *ptr)
throw std::runtime_error("World is coming to an end with no matching element");
return ptr - v.begin();
}
template <class T>
bool contains(const std::vector<T> &v, const T &elem) {
return std::binary_search(v.begin(), v.end(), elem);
}
template <class T>
bool contains(const std::set<T> &s, const T &elem) {
return s.find(elem) != s.end();
}
template <class T>
typename std::list<T>::iterator nextIterator(const std::list<T> &list, typename std::list<T>::iterator &itr) {
auto ret = std::next(itr);
if (ret == list.end())
ret = std::next(ret);
return ret;
}
template <class T>
typename std::list<T>::iterator prevIterator(const std::list<T> &list, typename std::list<T>::iterator &itr) {
auto ret = std::prev(itr);
if (ret == list.end())
ret = std::prev(ret);
return ret;
}
template <class T>
void deleteFromVector(std::vector<T> &v, const T &elem) {
auto itr = std::find(v.begin(), v.end(), elem);
if (itr != v.end())
v.erase(itr);
}
template <class T>
void sortAndRemoveDuplicate(std::vector<T> &v) {
std::sort(v.begin(), v.end()); // vector may have repeated elements like 1 1 2 2 3 3 3 4 4 5 5 6 7
auto last = unique(v.begin(), v.end()); // vector now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
v.erase(last, v.end());
v.shrink_to_fit();
}
template <typename T, typename T2>
int addToMap(std::map<T, int> &mapping, std::vector<T2> &vec, const T &object) {
if (mapping.find(object) == mapping.end()) {
mapping.insert({object, vec.size()});
vec.emplace_back();
}
return mapping[object];
}
template <typename T, int N>
void sortThis(std::array<T, N> &t) {
std::sort(t.begin(), t.end());
}
template <typename T>
using myPair = std::array<T, 2>;
template <typename T>
using myTriple = std::array<T, 3>;
class ourEdge {
int data[2];
public:
ourEdge(int a, int b) : data{a, b} {
if (data[0] > data[1]) std::swap(data[0], data[1]);
}
int &operator[](int index) {
//if (index < 0 || index > 1)
//throw std::out_of_range("Index tried to access=" + index);
return data[index];
}
const int &operator[](int index) const {
//if (index < 0 || index > 1)
//throw std::out_of_range("Index tried to access=" + index);
return data[index];
}
int other(int u) {
if (u == data[0]) return data[1];
if (u == data[1]) return data[0];
return -1;
}
};
bool operator<(const ourEdge &lhs, const ourEdge &rhs) {
if (lhs[0] < rhs[0] || (lhs[0] == rhs[0] && lhs[1] < rhs[1]))
return true;
return false;
}
bool operator!=(const ourEdge &lhs, const ourEdge &rhs) {
return lhs[0] != rhs[0] || lhs[1] != rhs[1];
}
bool operator==(const ourEdge &lhs, const ourEdge &rhs) {
return lhs[0] == rhs[0] && lhs[1] == rhs[1];
}
std::ostream &operator<<(std::ostream &os, const ourEdge &e) {
return os << "( " << e[0] << " " << e[1] << " )";
}
class ourFace {
int data[3];
public:
ourFace(int a, int b, int c) : data{a, b, c} {
if (data[0] > data[1]) std::swap(data[0], data[1]);
if (data[1] > data[2]) std::swap(data[1], data[2]);
if (data[0] > data[1]) std::swap(data[0], data[1]);
}
ourFace(ourEdge e, int p) : data{e[0], e[1], p} {
if (data[0] > data[1]) std::swap(data[0], data[1]);
if (data[1] > data[2]) std::swap(data[1], data[2]);
if (data[0] > data[1]) std::swap(data[0], data[1]);
}
int &operator[](int index) {
//if (index < 0 || index > 2)
//throw std::out_of_range("Index tried to access=" + index);
return data[index];
}
const int &operator[](int index) const {
//if (index < 0 || index > 2)
//throw std::out_of_range("Index tried to access=" + index);
return data[index];
}
};
bool operator<(const ourFace &lhs, const ourFace &rhs) {
if (lhs[0] < rhs[0] || (lhs[0] == rhs[0] && lhs[1] < rhs[1]) || (lhs[0] == rhs[0] && lhs[1] == rhs[1] && lhs[2] < rhs[2]))
return true;
return false;
}
bool operator!=(const ourFace &lhs, const ourFace &rhs) {
return lhs[0] != rhs[0] || lhs[1] != rhs[1] || lhs[2] != rhs[2];
}
std::ostream &operator<<(std::ostream &os, const ourFace &f) {
return os << "( " << f[0] << " " << f[1] << " " << f[2] << " )";
}
#endif // UTIL_H