lib: implement column iterator
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
c14c785355
commit
43c39a3ec0
3 changed files with 30 additions and 0 deletions
25
src/iterators/column_iterator.rs
Normal file
25
src/iterators/column_iterator.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
pub struct ColumnIterator<'a, T> {
|
||||||
|
map: &'a [Vec<T>],
|
||||||
|
column: usize,
|
||||||
|
|
||||||
|
i: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> ColumnIterator<'a, T> {
|
||||||
|
pub fn new(map: &'a [Vec<T>], column: usize) -> ColumnIterator<'a, T> {
|
||||||
|
Self { map, column, i: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T> Iterator for ColumnIterator<'a, T> {
|
||||||
|
type Item = &'a T;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.i >= self.map.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.i += 1;
|
||||||
|
Some(&self.map[self.i - 1][self.column])
|
||||||
|
}
|
||||||
|
}
|
2
src/iterators/mod.rs
Normal file
2
src/iterators/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
mod column_iterator;
|
||||||
|
pub use column_iterator::*;
|
|
@ -10,5 +10,8 @@ pub use testing::*;
|
||||||
mod vectors;
|
mod vectors;
|
||||||
pub use vectors::*;
|
pub use vectors::*;
|
||||||
|
|
||||||
|
mod iterators;
|
||||||
|
pub use iterators::*;
|
||||||
|
|
||||||
mod data_structures;
|
mod data_structures;
|
||||||
pub use data_structures::*;
|
pub use data_structures::*;
|
||||||
|
|
Loading…
Reference in a new issue