-
Notifications
You must be signed in to change notification settings - Fork 0
/
PosDir.hh
162 lines (134 loc) · 3.08 KB
/
PosDir.hh
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
#ifndef PosDir_hh
#define PosDir_hh
#include <cstdlib>
#include "Utils.hh"
using namespace std;
/**
* Enum to encode directions.
*/
enum Dir {
None, Top, Bottom, Left, Right
};
inline bool ok(Dir d) {
return d == None or d == Top or d == Bottom or d == Left or d == Right;
}
/**
* Conversion from Dir to char.
*/
inline char d2c (Dir d) {
switch (d) {
case None: return 'n';
case Top: return 't';
case Bottom: return 'b';
case Left: return 'l';
case Right: return 'r';
}
_unreachable();
return 'n';
}
/**
* Conversion from char to Dir.
*/
inline Dir c2d (char c) {
switch (c) {
case 'n': return None;
case 't': return Top;
case 'b': return Bottom;
case 'l': return Left;
case 'r': return Right;
}
return None;
}
/**
* Simple struct to handle positions.
*/
struct Pos {
/**
* Plain old data.
*/
int i, j;
/**
* Default constructor (0,0).
*/
inline Pos ()
: i(0),
j(0)
{ }
/**
* Given constructor.
*/
inline Pos (int i, int j)
: i(i),
j(j)
{ }
/**
* Put operator .
*/
inline friend ostream& operator<< (ostream& os, const Pos& p) {
return os << "(" << p.i << "," << p.j << ")";
}
/**
* Comparison operator.
*/
inline friend bool operator< (const Pos& a, const Pos& b) {
if (a.i != b.i) return a.i < b.i;
return a.j < b.j;
}
/**
* Comparison operator.
*/
inline friend bool operator== (const Pos& a, const Pos& b) {
return a.i == b.i and a.j == b.j;
}
/**
* Comparison operator.
*/
inline friend bool operator!= (const Pos& a, const Pos& b) {
return not (a == b);
}
/**
* Comparison operator.
*/
inline friend bool operator<= (const Pos& a, const Pos& b) {
return a < b or a == b;
}
/**
* Comparison operator.
*/
inline friend bool operator>= (const Pos& a, const Pos& b) {
return not (a < b);
}
/**
* Comparison operator.
*/
inline friend bool operator> (const Pos& a, const Pos& b) {
return not (a <= b);
}
/**
* Increment operator: moves a position according to a direction.
*/
inline Pos& operator+= (Dir d) {
switch (d) {
case None: break;
case Top: --i; break;
case Bottom: ++i; break;
case Left: --j; break;
case Right: ++j; break;
}
return *this;
}
/**
* Addition operator: Returns a position by adding a direction.
*/
inline Pos operator+ (Dir d) const {
switch (d) {
case None: return Pos(i , j );
case Top: return Pos(i - 1, j );
case Bottom: return Pos(i + 1, j );
case Left: return Pos(i , j - 1);
case Right: return Pos(i , j + 1);
}
_unreachable();
}
};
#endif