ColumnIter

This commit is contained in:
Egor 2024-05-18 16:16:14 +03:00
parent 0397811247
commit 60a7ae725a

View file

@ -31,8 +31,8 @@ impl<T: Num + Clone> Matrix<T> {
self.data.chunks(self.width) self.data.chunks(self.width)
} }
pub fn iter_column(&self) -> () { pub fn iter_column(&self, j: usize) -> ColumnIter<T> {
todo!() ColumnIter { row_start: 0, column_index: j, matrix: self }
} }
pub fn iter_columns(&self) -> () { pub fn iter_columns(&self) -> () {
@ -62,6 +62,10 @@ pub struct IterIndexedMut<'a, T: Num> {
matrix_iter_mut: std::slice::IterMut<'a, T> matrix_iter_mut: std::slice::IterMut<'a, T>
} }
pub struct ColumnIter<'a, T: Num> {
row_start: usize, column_index: usize, matrix: &'a Matrix<T>
}
pub struct IterIndices { pub struct IterIndices {
i: usize, j: usize, width: usize, height: usize i: usize, j: usize, width: usize, height: usize
} }
@ -84,6 +88,17 @@ impl<'a, T: Num> Iterator for IterIndexedMut<'a, T> {
} }
} }
impl<'a, T: Num> Iterator for ColumnIter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
let elem_index = self.row_start + self.column_index;
if elem_index >= self.matrix.data.len() { return None; }
let elem = &self.matrix.data[elem_index];
self.row_start += self.matrix.width;
Some(elem)
}
}
impl Iterator for IterIndices { impl Iterator for IterIndices {
type Item = (usize, usize); type Item = (usize, usize);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {