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
use super::*;

use aoc_pos::PosGeo;
use aoc_pos::PosGeoHexLat;
use aoc_pos::PosRowCol;
use aoc_pos::PosWXYZ;
use aoc_pos::PosXY;
use aoc_pos::PosXYZ;
use aoc_pos::PosXYZW;
use aoc_pos::PosYX;

/// Trait for values to use as indices for a [`GridView`].
///
/// For example, a two dimensional grid might be indexed with a struct containing an `x` and a `y`
/// coordinate.
///
/// This trait provides methods to translate whatever coordinate system is in use to and from a
/// single `usize` value.
///
pub trait GridPos <const DIMS: usize>: Copy + Debug + Default + Eq + Sized {

	type Coord: Int;

	fn to_array (self) -> [Self::Coord; DIMS];
	fn from_array (array: [Self::Coord; DIMS]) -> Self;

	#[ inline ]
	fn map <MapFn, Output> (self, map_fn: MapFn) -> [Output; DIMS]
			where MapFn: FnMut (Self::Coord) -> Output {
		self.to_array ().map (map_fn)
	}

	#[ inline ]
	fn to_native (self, start: Self) -> Option <Self> {
		let self_arr = self.to_array ();
		let start_arr = start.to_array ();
		let mut result_arr = [Self::Coord::ZERO; DIMS];
		for idx in 0 .. DIMS {
			let result = chk! (self_arr [idx] - start_arr [idx]).ok () ?;
			if result < Self::Coord::ZERO { return None }
			result_arr [idx] = result;
		}
		Some (Self::from_array (result_arr))
	}

	#[ inline ]
	#[ must_use ]
	fn native_to_index (self, size: Self) -> Option <isize> {
		self.to_array ().iter ()
			.zip (size.to_array ().iter ())
			.map (|(& val, & size)| (val.qck_isize (), size.qck_isize ()))
			.try_fold (0, |idx, (val, size)| {
				if size <= val { return Err (Overflow) }
				chk! (idx * size + val)
			})
			.ok ()
	}

	#[ inline ]
	fn from_native (native: Self, start: Self) -> Option <Self> {
		let native_arr = native.to_array ();
		let start_arr = start.to_array ();
		let mut result_arr = [Self::Coord::ZERO; DIMS];
		for idx in 0 .. DIMS {
			result_arr [idx] = chk! (native_arr [idx] + start_arr [idx]).ok () ?;
		}
		Some (Self::from_array (result_arr))
	}

}

impl <Val: Int> GridPos <2> for PosXY <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 2] {
		[ self.x, self.y ]
	}

	#[ inline ]
	fn from_array (array: [Val; 2]) -> Self {
		Self { x: array [0], y: array [1] }
	}

}

impl <Val: Int> GridPos <2> for PosYX <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 2] {
		[ self.y, self.x ]
	}

	#[ inline ]
	fn from_array (array: [Val; 2]) -> Self {
		Self { y: array [0], x: array [1] }
	}

}

impl <Val: Int> GridPos <2> for PosRowCol <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 2] {
		[ self.row, self.col ]
	}

	#[ inline ]
	fn from_array (array: [Val; 2]) -> Self {
		Self { row: array [0], col: array [1] }
	}

}

impl <Val: Int> GridPos <2> for PosGeo <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 2] {
		[ self.n, self.e ]
	}

	#[ inline ]
	fn from_array (array: [Val; 2]) -> Self {
		Self { n: array [0], e: array [1] }
	}

}

impl <Val: Int> GridPos <2> for PosGeoHexLat <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 2] {
		[ self.nw, self.e ]
	}

	#[ inline ]
	fn from_array (array: [Val; 2]) -> Self {
		Self { nw: array [0], e: array [1] }
	}

}

impl <Val: Int> GridPos <3> for PosXYZ <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 3] {
		[ self.x, self.y, self.z ]
	}

	#[ inline ]
	fn from_array (array: [Val; 3]) -> Self {
		Self { x: array [0], y: array [1], z: array [2] }
	}

}

impl <Val: Int> GridPos <4> for PosWXYZ <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 4] {
		[ self.w, self.x, self.y, self.z ]
	}

	#[ inline ]
	fn from_array (array: [Val; 4]) -> Self {
		Self { w: array [0], x: array [1], y: array [2], z: array [3] }
	}

}

impl <Val: Int> GridPos <4> for PosXYZW <Val> {

	type Coord = Val;

	#[ inline ]
	fn to_array (self) -> [Val; 4] {
		[ self.x, self.y, self.z, self.w ]
	}

	#[ inline ]
	fn from_array (array: [Val; 4]) -> Self {
		Self { x: array [0], y: array [1], z: array [2], w: array [3] }
	}

}