mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 10:00:14 +01:00
add some iterators
This commit is contained in:
parent
b8baf8127d
commit
ee0c9dceab
|
@ -101,6 +101,14 @@ impl BitVec {
|
|||
self.data.is_empty()
|
||||
}
|
||||
|
||||
/// Get an iterator over every bit in the vector
|
||||
pub fn iter(&self) -> Iter {
|
||||
Iter {
|
||||
bit_vec: self,
|
||||
index: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates the byte index and bitmask for a specific bit in the vector
|
||||
fn get_indexes(&self, bit_index: usize) -> (usize, u8) {
|
||||
assert!(
|
||||
|
@ -143,6 +151,25 @@ impl From<&[u8]> for BitVec {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Iter<'t> {
|
||||
bit_vec: &'t BitVec,
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl<'t> Iterator for Iter<'t> {
|
||||
type Item = bool;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.index >= self.bit_vec.size {
|
||||
return None;
|
||||
}
|
||||
|
||||
let result = Some(self.bit_vec.get(self.index));
|
||||
self.index += 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{BitVec, DataRef};
|
||||
|
|
|
@ -43,6 +43,13 @@ 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,
|
||||
|
@ -152,6 +159,25 @@ impl RefGrid<u8> for ByteGrid {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Iter<'t> {
|
||||
byte_grid: &'t ByteGrid,
|
||||
index: 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() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let result = self.byte_grid.data[self.index];
|
||||
self.index += 1;
|
||||
Some(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{ByteGrid, DataRef, Grid};
|
||||
|
|
Loading…
Reference in a new issue