diff --git a/crates/servicepoint/src/byte_grid.rs b/crates/servicepoint/src/byte_grid.rs index 5dbc654..40721ba 100644 --- a/crates/servicepoint/src/byte_grid.rs +++ b/crates/servicepoint/src/byte_grid.rs @@ -1,3 +1,5 @@ +use std::slice::{Iter, IterMut}; + use crate::grid::RefGrid; use crate::{DataRef, Grid}; @@ -43,6 +45,24 @@ impl ByteGrid { } } + pub fn iter(&self) -> Iter { + self.data.iter() + } + + pub fn iter_rows(&self) -> IterRows { + IterRows { + byte_grid: self, + row: 0, + } + } + + /// Returns an iterator that allows modifying each value. + /// + /// The iterator yields all cells from top left to bottom right. + pub fn iter_mut(&mut self) -> IterMut { + self.data.iter_mut() + } + fn check_indexes(&self, x: usize, y: usize) { assert!( x < self.width, @@ -92,21 +112,6 @@ impl Grid for ByteGrid { self.data[x + y * self.width] } - fn iter(&self) -> impl Iterator { - Iter { - byte_grid: self, - index: 0, - end: self.data.len(), - } - } - - fn iter_rows(&self) -> impl Iterator> { - IterRows { - byte_grid: self, - row: 0, - } - } - fn fill(&mut self, value: u8) { self.data.fill(value); } @@ -167,45 +172,24 @@ impl RefGrid for ByteGrid { } } -pub struct Iter<'t> { - byte_grid: &'t ByteGrid, - index: usize, - end: usize, -} - -impl<'t> Iterator for Iter<'t> { - type Item = u8; - - fn next(&mut self) -> Option { - if self.index >= self.end { - return None; - } - - let result = self.byte_grid.data[self.index]; - self.index += 1; - Some(result) - } -} - pub struct IterRows<'t> { byte_grid: &'t ByteGrid, row: usize, } impl<'t> Iterator for IterRows<'t> { - type Item = Iter<'t>; + type Item = Iter<'t, u8>; fn next(&mut self) -> Option { if self.row >= self.byte_grid.height { return None; } - let result = Some(Iter { - byte_grid: self.byte_grid, - index: self.row * self.byte_grid.width, - end: (self.row + 1) * self.byte_grid.width, - }); + + let start = self.row * self.byte_grid.width; + let end = start + self.byte_grid.width; + let result = self.byte_grid.data[start..end].iter(); self.row += 1; - result + Some(result) } } diff --git a/crates/servicepoint/src/grid.rs b/crates/servicepoint/src/grid.rs index c9d93e0..76c80db 100644 --- a/crates/servicepoint/src/grid.rs +++ b/crates/servicepoint/src/grid.rs @@ -54,13 +54,6 @@ pub trait Grid { } } - /// Get an iterator over every cell in the grid. - /// - /// Iteration is done in memory order, rows first. - fn iter(&self) -> impl Iterator; - - fn iter_rows(&self) -> impl Iterator>; - /// Sets all cells in the grid to the specified value fn fill(&mut self, value: T); diff --git a/crates/servicepoint/src/pixel_grid.rs b/crates/servicepoint/src/pixel_grid.rs index 3f55a70..6a24d79 100644 --- a/crates/servicepoint/src/pixel_grid.rs +++ b/crates/servicepoint/src/pixel_grid.rs @@ -60,6 +60,17 @@ impl PixelGrid { } } + pub fn iter(&self) -> crate::bit_vec::Iter { + self.bit_vec.iter() + } + + pub fn iter_rows(&self) -> IterRows { + IterRows { + pixel_grid: self, + row: 0, + } + } + fn check_indexes(&self, x: usize, y: usize) { assert!( x < self.width, @@ -96,17 +107,6 @@ impl Grid for PixelGrid { self.bit_vec.get(x + y * self.width) } - fn iter(&self) -> impl Iterator { - self.bit_vec.iter() - } - - fn iter_rows(&self) -> impl Iterator> { - IterRows { - pixel_grid: self, - row: 0, - } - } - /// Sets the state of all pixels in the `PixelGrid`. /// /// # Arguments