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
use direction::Orientation;
use std::iter;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct XY<T> {
pub x: T,
pub y: T,
}
impl<T> IntoIterator for XY<T> {
type Item = T;
type IntoIter = iter::Chain<iter::Once<T>, iter::Once<T>>;
fn into_iter(self) -> Self::IntoIter {
iter::once(self.x).chain(iter::once(self.y))
}
}
impl<T> XY<T> {
pub fn new(x: T, y: T) -> Self {
XY { x, y }
}
pub fn swap(self) -> Self {
XY::new(self.y, self.x)
}
pub fn fold<U, F>(self, f: F) -> U
where
F: FnOnce(T, T) -> U,
{
f(self.x, self.y)
}
pub fn map<U, F>(self, f: F) -> XY<U>
where
F: Fn(T) -> U,
{
XY::new(f(self.x), f(self.y))
}
pub fn map_if<F>(self, condition: XY<bool>, f: F) -> Self
where
F: Fn(T) -> T,
{
self.zip_map(condition, |v, c| if c { f(v) } else { v })
}
pub fn run_if<F, U>(self, condition: XY<bool>, f: F) -> XY<Option<U>>
where
F: Fn(T) -> U,
{
self.zip_map(condition, |v, c| if c { Some(f(v)) } else { None })
}
pub fn map_x<F>(self, f: F) -> Self
where
F: FnOnce(T) -> T,
{
XY::new(f(self.x), self.y)
}
pub fn map_y<F>(self, f: F) -> Self
where
F: FnOnce(T) -> T,
{
XY::new(self.x, f(self.y))
}
pub fn pair(self) -> (T, T) {
(self.x, self.y)
}
pub fn as_ref(&self) -> XY<&T> {
XY::new(&self.x, &self.y)
}
pub fn iter(&self) -> iter::Chain<iter::Once<&T>, iter::Once<&T>> {
iter::once(&self.x).chain(iter::once(&self.y))
}
pub fn get(&self, o: Orientation) -> &T {
match o {
Orientation::Horizontal => &self.x,
Orientation::Vertical => &self.y,
}
}
pub fn get_mut(&mut self, o: Orientation) -> &mut T {
match o {
Orientation::Horizontal => &mut self.x,
Orientation::Vertical => &mut self.y,
}
}
pub fn zip<U>(self, other: XY<U>) -> XY<(T, U)> {
XY::new((self.x, other.x), (self.y, other.y))
}
pub fn zip3<U, V>(self, a: XY<U>, b: XY<V>) -> XY<(T, U, V)> {
XY::new((self.x, a.x, b.x), (self.y, a.y, b.y))
}
pub fn zip4<U, V, W>(
self, a: XY<U>, b: XY<V>, c: XY<W>,
) -> XY<(T, U, V, W)> {
XY::new((self.x, a.x, b.x, c.x), (self.y, a.y, b.y, c.y))
}
pub fn zip5<U, V, W, Z>(
self, a: XY<U>, b: XY<V>, c: XY<W>, d: XY<Z>,
) -> XY<(T, U, V, W, Z)> {
XY::new((self.x, a.x, b.x, c.x, d.x), (self.y, a.y, b.y, c.y, d.y))
}
pub fn zip_map<U, V, F>(self, other: XY<U>, f: F) -> XY<V>
where
F: Fn(T, U) -> V,
{
XY::new(f(self.x, other.x), f(self.y, other.y))
}
pub fn keep(self, keep: XY<bool>) -> XY<Option<T>> {
keep.select(self)
}
}
impl<T: Clone> XY<T> {
pub fn with_axis(&self, o: Orientation, value: T) -> Self {
let mut new = self.clone();
*o.get_ref(&mut new) = value;
new
}
pub fn with_axis_from(&self, o: Orientation, other: &Self) -> Self {
let mut new = self.clone();
new.set_axis_from(o, other);
new
}
pub fn set_axis_from(&mut self, o: Orientation, other: &Self) {
*o.get_ref(self) = o.get(other);
}
}
impl<T> XY<Option<T>> {
pub fn unwrap_or(self, other: XY<T>) -> XY<T> {
self.zip_map(other, Option::unwrap_or)
}
}
impl XY<bool> {
pub fn any(self) -> bool {
use std::ops::BitOr;
self.fold(BitOr::bitor)
}
pub fn both(self) -> bool {
use std::ops::BitAnd;
self.fold(BitAnd::bitand)
}
pub fn select<T>(self, other: XY<T>) -> XY<Option<T>> {
self.zip_map(other, |keep, o| if keep { Some(o) } else { None })
}
pub fn select_or<T>(self, if_true: XY<T>, if_false: XY<T>) -> XY<T> {
self.select(if_true).unwrap_or(if_false)
}
pub fn and(self, other: Self) -> Self {
self.zip_map(other, |s, o| s && o)
}
pub fn or(self, other: Self) -> Self {
self.zip_map(other, |s, o| s || o)
}
}
impl<T: Copy> XY<T> {
pub fn both_from(value: T) -> Self {
XY::new(value, value)
}
}
impl<T> From<(T, T)> for XY<T> {
fn from((x, y): (T, T)) -> Self {
XY::new(x, y)
}
}
impl<T, U> From<(XY<T>, XY<U>)> for XY<(T, U)> {
fn from((t, u): (XY<T>, XY<U>)) -> Self {
t.zip(u)
}
}