-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
236 lines (202 loc) · 4.23 KB
/
utils.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
#include <vector>
#include <set>
#include <map>
#include <string>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
typedef std::string String;
typedef std::filesystem::path Path;
typedef std::vector<String> StringVector;
typedef std::vector<int> IntVector;
typedef std::vector<uint64_t> ULongVector;
enum class EDirection
{
Up = 0,
Down,
Left,
Right,
Num
};
// Vec2
struct Vec2
{
int x;
int y;
Vec2(){}
Vec2(float _x, float _y)
: x(_x)
, y(_y)
{}
bool operator<(const Vec2& other) const {
if(x == other.x) return y < other.y;
else return x < other.x;
}
Vec2 operator-(const Vec2& v) const {
return Vec2(this->x - v.x, this->y - v.y);
}
Vec2 operator+(const Vec2& v) const {
return Vec2(this->x + v.x, this->y + v.y);
}
};
typedef std::vector<Vec2> Vec2Vector;
// File loading
StringVector GetFileAsLines(Path path)
{
StringVector lines;
std::ifstream infile;
infile.open(path);
if(infile.is_open())
{
String line;
while(std::getline(infile, line))
{
lines.push_back(line);
}
}
return lines;
}
String GetFileAsSingleLine(Path path)
{
String ret;
std::ifstream infile;
infile.open(path);
if(infile.is_open())
{
String line;
while(std::getline(infile, line))
{
ret.append(line);
}
}
return ret;
}
// String helpers
template <typename T>
int FindIndexOf(const std::vector<T>& vec, const T& element)
{
auto i = std::find(vec.begin(), vec.end(), element);
return i != vec.end() ? std::distance(vec.begin(), i) : -1;
}
StringVector SplitStringByChar(const String& str, char c)
{
StringVector ret;
String sub;
std::istringstream tokenStream(str);
while (std::getline(tokenStream, sub, c))
{
ret.push_back(sub);
}
return ret;
}
IntVector IntVecFromStringVec(const StringVector strvec)
{
IntVector ret;
for(auto s : strvec)
{
ret.push_back(std::stoi(s));
}
return ret;
}
ULongVector ULongVecFromStringVec(const StringVector strvec)
{
ULongVector ret;
for(auto s : strvec)
{
ret.push_back(std::stoll(s));
}
return ret;
}
IntVector PositionsOfString(String pattern, String data)
{
IntVector ret;
int search = 0;
while(search != std::string::npos)
{
search = data.find(pattern, search);
if(search != std::string::npos)
{
ret.push_back(search);
search++;
}
}
return ret;
}
bool DoesMatch2D(const StringVector& pattern, const StringVector& data, Vec2 pos, char ignore)
{
if(pattern.size() + pos.y > data.size())
return false;
if(pattern[0].size() + pos.x > data[0].size())
return false;
bool match = true;
for(int x=0 ; x<pattern[0].size() ; ++x)
{
for(int y=0 ; y<pattern.size() ; ++y)
{
if(pattern[y][x] != ignore)
{
if(data[y+pos.y][x+pos.x] != pattern[y][x])
{
match = false;
}
}
}
}
return match;
}
std::vector<int> IntToBaseDigits(int num, int base, int minDigits)
{
std::vector<int> ret;
while((num > 0) || (ret.size() < minDigits))
{
int remainder = num % base;
ret.push_back(remainder);
num /= base;
}
return ret;
}
template<class T>
class Grid2D
{
public:
Grid2D(int _width, int _height)
: width(_width)
, height(_height)
{
for(int y=0 ; y<height ; ++y)
{
std::vector<T> row;
row.resize(width);
e.push_back(row);
}
}
bool Inside(int x, int y) const
{
return x >= 0 && x < width && y >= 0 && y < height;
}
void Set(int x, int y, const T& val)
{
e[y][x] = val;
}
const T& Get(int x, int y) const
{
return e[y][x];
}
T& GetMutable(int x, int y)
{
return e[y][x];
}
int Height() const
{
return e.size();
}
int Width() const
{
return e[0].size();
}
private:
int width;
int height;
std::vector<std::vector<T>> e;
};