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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use crate::{
ArrGrid, Col, ColsIter, Coord, CoordComponent, GridBuf, GridIter, GridMut, Row, RowsIter,
VecGrid, View,
};
use fey_math::{RectU, Vec2U, rect, vec2};
use std::fmt::{Debug, Write};
use std::hash::{Hash, Hasher};
/// A type representing an immutable 2D array.
pub trait Grid {
/// The type of item this grid contains.
type Item;
/// The root grid type. [Views](View) use this to store a reference to the root
/// grid so they can read and modify it.
type Root: Grid<Item = Self::Item>;
/// The root grid for this one. If this grid is the root, this returns `self`.
fn root(&self) -> &Self::Root;
/// This grid's x-offset from the root grid. For the root grid this is `0`.
fn root_x(&self) -> u32;
/// This grid's y-offset from the root grid. For the root grid this is `0`.
fn root_y(&self) -> u32;
/// Width of the grid (how many columns it has).
fn width(&self) -> u32;
/// Height of the grid (how many rows it has).
fn height(&self) -> u32;
/// Size of the grid as a `(width, height)` pair.
#[inline]
fn size(&self) -> Vec2U {
vec2(self.width(), self.height())
}
/// Surface area of the grid, equal to `width * height`.
#[inline]
fn area(&self) -> u32 {
self.width() * self.height()
}
/// Returns a reference to the value stored at `(x, y)` in the grid, or `None` if
/// the provided coordinate is out of bounds.
fn get(&self, x: u32, y: u32) -> Option<&Self::Item>;
/// Returns a reference to the value stored at `(x, y)` in the grid, skipping
/// any bounds checks.
unsafe fn get_unchecked(&self, x: u32, y: u32) -> &Self::Item;
/// Returns a reference to the value stored at the provided coordinate in the grid,
/// or `None` if the provided coordinate is out of bounds.
#[inline]
fn get_at(&self, coord: impl Coord) -> Option<&Self::Item> {
self.get(
coord.x().to_grid(self.width())?,
coord.y().to_grid(self.height())?,
)
}
/// Returns a reference to the value stored at the provided coordinate in the grid,
/// skipping any bounds checks.
#[inline]
unsafe fn get_unchecked_at(&self, coord: impl Coord) -> &Self::Item {
unsafe {
self.get_unchecked(
coord.x().to_grid(self.width()).unwrap_unchecked(),
coord.y().to_grid(self.height()).unwrap_unchecked(),
)
}
}
/// Returns row `y` of the grid as a slice if it is able to do so. Algorithms that work
/// on large portions of the grid may use this to look for performance gain. For example,
/// [`Row::draw_copied`] uses this internally to call `copy_from_slice` when possible,
/// which can be faster than manually copying elements one-by-one.
fn row_slice(&self, y: u32) -> Option<&[Self::Item]>;
/// Returns row `y` of the grid as a slice if it is able to do so. This variation
/// can take signed integers, or y-values in a [`Wrap`](super::Wrap) or
/// [`Clamp`](super::Clamp).
#[inline]
fn row_slice_at(&self, y: impl CoordComponent) -> Option<&[Self::Item]> {
self.row_slice(y.to_grid(self.height())?)
}
/// Returns true if both grids are the same size.
#[inline]
fn same_size<G2: Grid>(&self, other: &G2) -> bool {
self.width() == other.width() && self.height() == other.height()
}
/// Get an immutable [`View`] into this grid, or `None` if the provided region is
/// out of bounds.
#[inline]
fn try_view(&self, x: u32, y: u32, w: u32, h: u32) -> Option<View<&Self::Root>> {
if x.checked_add(w)? <= self.width() && y.checked_add(h)? <= self.height() {
let x = self.root_x() + x;
let y = self.root_y() + y;
Some(View::new(self.root(), x, y, w, h))
} else {
None
}
}
#[inline]
fn try_view_at(&self, region: impl Into<RectU>) -> Option<View<&Self::Root>> {
let RectU { x, y, w, h } = region.into();
self.try_view(x, y, w, h)
}
/// Get an immutable [`View`] into this grid. Panicks if the provided region is out
/// of bounds.
#[inline]
fn view(&self, x: u32, y: u32, w: u32, h: u32) -> View<&Self::Root> {
self.try_view(x, y, w, h)
.expect("view does not overlap grid's bounds")
}
#[inline]
fn view_at(&self, region: impl Into<RectU>) -> View<&Self::Root> {
let RectU { x, y, w, h } = region.into();
self.view(x, y, w, h)
}
/// Create a [`GridBuf`] using the provided storage and clone this entire
/// grid into it. The resulting grid will be the same size as this one.
#[inline]
fn to_grid_buf<S>(&self, store: S) -> GridBuf<Self::Item, S>
where
S: AsRef<[Self::Item]> + AsMut<[Self::Item]>,
Self::Item: Clone,
Self: Sized,
{
let mut buf = GridBuf::with_store(self.size(), store);
buf.draw_cloned(self);
buf
}
/// Create a stack-allocated [`GridBuf`], using an `N`-sized array for storage,
/// and clone this entire grid into it. Panics if `N` is not exactly the area
/// of the grid (`width * height`).
#[inline]
fn to_arr_grid<const N: usize>(&self) -> ArrGrid<Self::Item, N>
where
Self::Item: Default + Clone,
Self: Sized,
{
assert_eq!(self.width() * self.height(), N as u32);
let mut arr = std::array::from_fn(|_| Self::Item::default());
for (dst, src) in arr.chunks_exact_mut(self.width() as usize).zip(self.rows()) {
if let Some(src) = src.as_slice() {
dst.clone_from_slice(src);
} else {
for (dst, src) in dst.iter_mut().zip(&src) {
*dst = src.clone();
}
}
}
GridBuf::with_store(self.size(), arr)
}
/// Create a stack-allocated [`GridBuf`], using a [`Vec`] for storage, and
/// clone this entire grid into it.
fn to_vec_grid(&self) -> VecGrid<Self::Item>
where
Self::Item: Clone,
Self: Sized,
{
let mut vec = Vec::with_capacity(self.area() as usize);
for row in self.rows() {
if let Some(row) = row.as_slice() {
vec.extend_from_slice(row);
} else {
vec.extend(row.iter().cloned())
}
}
GridBuf::with_store(self.size(), vec)
}
/// Iterate over all values in the grid, with their positions.
#[inline]
fn iter(&self) -> GridIter<&Self>
where
Self: Sized,
{
GridIter::new(self)
}
/// Iterate over all columns in the grid.
#[inline]
fn cols(&self) -> ColsIter<&Self>
where
Self: Sized,
{
ColsIter::new(self, self.width())
}
/// Return the column `x`, or `None` if `x` is out of bounds.
#[inline]
fn try_col(&self, x: u32) -> Option<Col<&Self>> {
(x < self.width()).then(|| Col::new(self, x))
}
/// Return the column `x`. Panics if `x` is out of bounds.
#[inline]
fn col(&self, x: u32) -> Col<&Self> {
self.try_col(x).expect("column index out of bounds")
}
/// Iterate over the rows of the grid.
#[inline]
fn rows(&self) -> RowsIter<&Self>
where
Self: Sized,
{
RowsIter::new(self, self.height())
}
/// Return the row `y`, or `None` if `y` is out of bounds.
#[inline]
fn try_row(&self, y: u32) -> Option<Row<&Self>> {
(y < self.height()).then(|| Row::new(self, y))
}
/// Return the row `y`. Panics if `y` is out of bounds.
#[inline]
fn row(&self, y: u32) -> Row<&Self> {
self.try_row(y).expect("row index out of bounds")
}
/// Return the bounds of the grid containing all elements match the predicate.
#[inline]
fn get_bounds<F: FnMut(&Self::Item) -> bool>(&self, mut cond: F) -> Option<RectU>
where
Self: Sized,
{
let mut min = Vec2U::MAX;
let mut max = Vec2U::MIN;
for (val, p) in self.iter() {
if cond(val) {
min = min.min(p);
max = max.max(p);
}
}
(min.x <= max.x && min.y <= max.y)
.then(|| rect(min.x, min.y, (max.x - min.x) + 1, (max.y - min.y) + 1))
}
#[inline]
fn eq_grid<'a, H: Grid>(&'a self, other: &'a H) -> bool
where
Self::Item: PartialEq<H::Item>,
Self: Sized,
{
self.same_size(other)
&& self.rows().zip(other.rows()).all(|(a, b)| {
if let (Some(a), Some(b)) = (a.as_slice(), b.as_slice()) {
a == b
} else {
a.iter().zip(b.iter()).all(|(a, b)| a == b)
}
})
}
#[inline]
fn debug_fmt<W: Write>(&self, mut f: W) -> std::fmt::Result
where
Self::Item: Debug,
{
let mut s = String::new();
let mut len = 0;
for y in 0..self.height() {
for x in 0..self.width() {
let val = self.get(x, y).unwrap();
s.clear();
write!(s, "{:?}", val)?;
len = len.max(s.len());
}
}
for y in 0..self.height() {
for x in 0..self.width() {
let val = self.get(x, y).unwrap();
s.clear();
write!(s, "{:?}", val)?;
while s.len() < len {
s.push(' ');
}
write!(f, "[{}]", s)?;
}
writeln!(f)?;
}
writeln!(f)
}
fn hash_grid<H: Hasher>(&self, hasher: &mut H)
where
Self: Sized,
Self::Item: Hash,
{
self.width().hash(hasher);
self.height().hash(hasher);
for row in self.rows() {
if let Some(row) = row.as_slice() {
row.hash(hasher);
} else {
for val in row {
val.hash(hasher);
}
}
}
}
}
impl<T, const W: usize, const H: usize> Grid for [[T; W]; H] {
type Item = T;
type Root = Self;
#[inline]
fn root(&self) -> &Self::Root {
self
}
#[inline]
fn root_x(&self) -> u32 {
0
}
#[inline]
fn root_y(&self) -> u32 {
0
}
#[inline]
fn width(&self) -> u32 {
W as u32
}
#[inline]
fn height(&self) -> u32 {
H as u32
}
#[inline]
fn get(&self, x: u32, y: u32) -> Option<&Self::Item> {
let x = x as usize;
let y = y as usize;
(x < W && y < H).then(|| &self[y][x])
}
#[inline]
unsafe fn get_unchecked(&self, x: u32, y: u32) -> &Self::Item {
unsafe {
self.as_slice()
.get_unchecked(y as usize)
.get_unchecked(x as usize)
}
}
#[inline]
fn row_slice(&self, y: u32) -> Option<&[Self::Item]> {
let y = y as usize;
(y < H).then(|| self[y].as_slice())
}
}