Skip to content

Instantly share code, notes, and snippets.

@B-Reif
Created May 4, 2021 19:56
Show Gist options
  • Save B-Reif/30aba3a5781a1ddcff7095b5e653cc28 to your computer and use it in GitHub Desktop.
Save B-Reif/30aba3a5781a1ddcff7095b5e653cc28 to your computer and use it in GitHub Desktop.
Grid Mutation Iterator
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