-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.h
156 lines (136 loc) · 3.22 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
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <math.h>
#define uint unsigned int
using namespace std;
namespace Util
{
vector<string> split(string s, string del)
{
vector<string> res;
size_t pos;
while ((pos = s.find(del)) != string::npos)
{
res.push_back(s.substr(0, pos));
s.erase(0, pos + del.length());
}
res.push_back(s);
return res;
}
vector<string> splitMulti(string s, vector<string> dels)
{
vector<string> res;
res.push_back(s);
size_t pos;
size_t size;
for (string del : dels)
{
size = res.size();
for (int i = 0; i < size; i++)
{
while ((pos = res[i].find(del)) != string::npos)
{
res.push_back(res[i].substr(0, pos));
res[i].erase(0, pos + del.length());
}
if (res[i] != "")
res.push_back(res[i]);
}
res.erase(res.begin(), res.begin() + size);
}
return res;
}
void printStacks(vector<stack<char>*> stacks)
{
for (auto stack : stacks)
{
while (stack->size() > 0)
{
cout << stack->top();
stack->pop();
}
cout << "]" << endl;
}
}
struct vec2
{
public:
long x, y;
vec2(long x, long y): x(x), y(y) {}
vec2(): x(0), y(0) {}
vec2 operator+ (vec2 other)
{
return vec2(this->x + other.x, this->y + other.y);
}
vec2& operator+=(vec2& other)
{
this->x += other.x;
this->y += other.y;
return *this;
}
vec2 operator-(vec2 other)
{
return vec2(this->x - other.x, this->y - other.y);
}
float operator/(vec2 other)
{
vec2 delta = other - *this;
return sqrt(delta.x * delta.x + delta.y * delta.y);
}
bool operator==(vec2 other)
{
return this->x == other.x && this->y == other.y;
}
bool operator!=(vec2 other)
{
return !(*this == other);
}
static vec2 min(vec2 a, vec2 b)
{
return vec2(std::min(a.x, b.x), std::min(a.y, b.y));
}
static vec2 max(vec2 a, vec2 b)
{
return vec2(std::max(a.x, b.x), std::max(a.y, b.y));
}
long sqrMagnitude()
{
return x * y;
}
vec2 oneDown()
{
return vec2(x, y - 1);
}
};
/*
long clamp(long a, long min, long max)
{
if (a < min)
return min;
if (a > max)
return max;
return a;
}
long min(long a, long b)
{
if (a > b)
return b;
return a;
}
long max(long a, long b)
{
if (a < b)
return b;
return a;
}
*/
ostream& operator<< (ostream& os, vec2& v)
{
os << "(" << v.x << ", " << v.y << ")";
return os;
}
};