add more iterators

This commit is contained in:
Vinzenz Schroeter 2024-06-02 14:34:22 +02:00
parent ee0c9dceab
commit 17d41ef6c6
4 changed files with 84 additions and 11 deletions

View file

@ -106,6 +106,7 @@ impl BitVec {
Iter {
bit_vec: self,
index: 0,
end: self.size,
}
}
@ -152,15 +153,16 @@ impl From<&[u8]> for BitVec {
}
pub struct Iter<'t> {
bit_vec: &'t BitVec,
index: usize,
pub(crate) bit_vec: &'t BitVec,
pub(crate) index: usize,
pub(crate) end: usize,
}
impl<'t> Iterator for Iter<'t> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.bit_vec.size {
if self.index >= self.end {
return None;
}

View file

@ -43,13 +43,6 @@ impl ByteGrid {
}
}
pub fn iter(&self) -> Iter {
Iter {
byte_grid: self,
index: 0,
}
}
fn check_indexes(&self, x: usize, y: usize) {
assert!(
x < self.width,
@ -99,6 +92,21 @@ impl Grid<u8> for ByteGrid {
self.data[x + y * self.width]
}
fn iter(&self) -> impl Iterator<Item = u8> {
Iter {
byte_grid: self,
index: 0,
end: self.data.len(),
}
}
fn iter_rows(&self) -> impl Iterator<Item = impl Iterator<Item = u8>> {
IterRows {
byte_grid: self,
row: 0,
}
}
fn fill(&mut self, value: u8) {
self.data.fill(value);
}
@ -162,13 +170,14 @@ impl RefGrid<u8> 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<Self::Item> {
if self.index >= self.byte_grid.data.len() {
if self.index >= self.end {
return None;
}
@ -178,6 +187,28 @@ impl<'t> Iterator for Iter<'t> {
}
}
pub struct IterRows<'t> {
byte_grid: &'t ByteGrid,
row: usize,
}
impl<'t> Iterator for IterRows<'t> {
type Item = Iter<'t>;
fn next(&mut self) -> Option<Self::Item> {
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,
});
self.row += 1;
result
}
}
#[cfg(test)]
mod tests {
use crate::{ByteGrid, DataRef, Grid};

View file

@ -54,6 +54,13 @@ pub trait Grid<T> {
}
}
/// Get an iterator over every cell in the grid.
///
/// Iteration is done in memory order, rows first.
fn iter(&self) -> impl Iterator<Item = T>;
fn iter_rows(&self) -> impl Iterator<Item = impl Iterator<Item = T>>;
/// Sets all cells in the grid to the specified value
fn fill(&mut self, value: T);

View file

@ -96,6 +96,17 @@ impl Grid<bool> for PixelGrid {
self.bit_vec.get(x + y * self.width)
}
fn iter(&self) -> impl Iterator<Item = bool> {
self.bit_vec.iter()
}
fn iter_rows(&self) -> impl Iterator<Item = impl Iterator<Item = bool>> {
IterRows {
pixel_grid: self,
row: 0,
}
}
/// Sets the state of all pixels in the `PixelGrid`.
///
/// # Arguments
@ -132,6 +143,28 @@ impl From<PixelGrid> for Vec<u8> {
}
}
pub struct IterRows<'t> {
pixel_grid: &'t PixelGrid,
row: usize,
}
impl<'t> Iterator for IterRows<'t> {
type Item = crate::bit_vec::Iter<'t>;
fn next(&mut self) -> Option<Self::Item> {
if self.row >= self.pixel_grid.height {
return None;
}
let result = Some(crate::bit_vec::Iter {
bit_vec: &self.pixel_grid.bit_vec,
index: self.row * self.pixel_grid.width,
end: (self.row + 1) * self.pixel_grid.width,
});
self.row += 1;
result
}
}
#[cfg(test)]
mod tests {
use crate::{DataRef, Grid, PixelGrid};