Created
May 4, 2021 19:56
-
-
Save B-Reif/30aba3a5781a1ddcff7095b5e653cc28 to your computer and use it in GitHub Desktop.
Grid Mutation Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct GridNeighborsMut<'a> { | |
grid: &'a mut Grid, | |
point_x: usize, | |
point_y: usize, | |
count: usize, | |
} | |
impl<'a> Iterator for GridNeighborsMut<'a> { | |
type Item = &'a mut Elevation; | |
fn next(&mut self) -> Option<Self::Item> { | |
unsafe { | |
let grid = self.grid; | |
match self.count { | |
0 => { | |
self.count += 1; | |
grid.get_mut(self.point_x, self.point_y + 1) | |
} | |
1 => { | |
self.count += 1; | |
grid.get_mut(self.point_x + 1, self.point_y) | |
} | |
2 => { | |
self.count += 1; | |
grid.get_mut(self.point_x, self.point_y - 1) | |
} | |
3 => { | |
self.count += 1; | |
grid.get_mut(self.point_x - 1, self.point_y) | |
} | |
_ => None, | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment