From 59301a5fc51acbf46c63c710a772000d9f986fbf Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 3 Jun 2024 22:48:12 +0200 Subject: [PATCH] add PixelGrid::iter_mut, tests --- crates/servicepoint/src/byte_grid.rs | 46 ++++++++++++++++++++++++ crates/servicepoint/src/pixel_grid.rs | 52 ++++++++++++++++++++++++--- 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/crates/servicepoint/src/byte_grid.rs b/crates/servicepoint/src/byte_grid.rs index e02217f..c9f8a63 100644 --- a/crates/servicepoint/src/byte_grid.rs +++ b/crates/servicepoint/src/byte_grid.rs @@ -256,4 +256,50 @@ mod tests { assert_eq!(vec.data, [1, 2, 3, 4]); assert_eq!(vec.get(1, 0), 2) } + + #[test] + fn iter() { + let mut vec = ByteGrid::new(2, 2); + vec.set(1, 1, 5); + + let mut iter = vec.iter(); + assert_eq!(*iter.next().unwrap(), 0); + assert_eq!(*iter.next().unwrap(), 0); + assert_eq!(*iter.next().unwrap(), 0); + assert_eq!(*iter.next().unwrap(), 5); + } + + #[test] + fn iter_mut() { + let mut vec = ByteGrid::new(2, 3); + for (index, cell) in vec.iter_mut().enumerate() { + *cell = index as u8; + } + + assert_eq!(vec.data_ref(), [0, 1, 2, 3, 4, 5]); + } + + #[test] + fn iter_rows() { + let vec = ByteGrid::load(2, 3, &[0, 1, 1, 2, 2, 3]); + for (y, row) in vec.iter_rows().enumerate() { + for (x, val) in row.enumerate() { + assert_eq!(*val, (x + y) as u8); + } + } + } + + #[test] + #[should_panic] + fn out_of_bounds_x() { + let mut vec = ByteGrid::load(2, 2, &[0, 1, 2, 3]); + vec.set(2, 1, 5); + } + + #[test] + #[should_panic] + fn out_of_bounds_y() { + let vec = ByteGrid::load(2, 2, &[0, 1, 2, 3]); + vec.get(1, 2); + } } diff --git a/crates/servicepoint/src/pixel_grid.rs b/crates/servicepoint/src/pixel_grid.rs index 1ff865d..bb8d4c2 100644 --- a/crates/servicepoint/src/pixel_grid.rs +++ b/crates/servicepoint/src/pixel_grid.rs @@ -1,7 +1,9 @@ -use crate::{BitVec, DataRef, Grid, SpBitVec, PIXEL_HEIGHT, PIXEL_WIDTH}; use bitvec::order::Msb0; use bitvec::prelude::BitSlice; -use bitvec::slice::Iter; +use bitvec::ptr::Mutability; +use bitvec::slice::IterMut; + +use crate::{BitVec, DataRef, Grid, SpBitVec, PIXEL_HEIGHT, PIXEL_WIDTH}; /// A grid of pixels stored in packed bytes. #[derive(Debug, Clone, PartialEq)] @@ -75,8 +77,35 @@ impl PixelGrid { /// } /// } /// ``` - pub fn iter(&self) -> Iter<'_, u8, Msb0> { - self.bit_vec.iter() + pub fn iter(&self) -> impl Iterator { + self.bit_vec.iter().by_refs() + } + + /// Iterate over all cells in `PixelGrid` mutably. + /// + /// Order is equivalent to the following loop: + /// ``` + /// # use servicepoint::{PixelGrid, Grid}; + /// # let mut grid = PixelGrid::new(8,2); + /// # let value = false; + /// for y in 0..grid.height() { + /// for x in 0..grid.width() { + /// grid.set(x, y, value) + /// } + /// } + /// ``` + /// + /// # Example + /// ``` + /// # use servicepoint::{PixelGrid, Grid}; + /// # let mut grid = PixelGrid::new(8,2); + /// # let value = false; + /// for (index, mut pixel) in grid.iter_mut().enumerate() { + /// pixel.set(index % 2 == 0) + /// } + /// ``` + pub fn iter_mut(&mut self) -> IterMut { + self.bit_vec.iter_mut() } /// Iterate over all rows in `PixelGrid` top to bottom. @@ -120,6 +149,7 @@ impl Grid for PixelGrid { } fn get(&self, x: usize, y: usize) -> bool { + self.check_indexes(x, y); self.bit_vec[x + y * self.width] } @@ -226,4 +256,18 @@ mod tests { let grid = PixelGrid::load(8, 3, &data); assert_eq!(grid.data_ref(), [0xAA, 0x55, 0xAA]); } + + #[test] + #[should_panic] + fn out_of_bounds_x() { + let vec = PixelGrid::new(8, 2); + vec.get(8, 1); + } + + #[test] + #[should_panic] + fn out_of_bounds_y() { + let mut vec = PixelGrid::new(8, 2); + vec.set(1, 2, false); + } }