From 60a7ae725aa3e4802489a8cafaba87e860da737b Mon Sep 17 00:00:00 2001 From: erius Date: Sat, 18 May 2024 16:16:14 +0300 Subject: [PATCH] ColumnIter --- src/math/matrix/iter.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/math/matrix/iter.rs b/src/math/matrix/iter.rs index 079eff7..6bbcc97 100644 --- a/src/math/matrix/iter.rs +++ b/src/math/matrix/iter.rs @@ -31,8 +31,8 @@ impl Matrix { self.data.chunks(self.width) } - pub fn iter_column(&self) -> () { - todo!() + pub fn iter_column(&self, j: usize) -> ColumnIter { + ColumnIter { row_start: 0, column_index: j, matrix: self } } pub fn iter_columns(&self) -> () { @@ -62,6 +62,10 @@ pub struct IterIndexedMut<'a, T: Num> { matrix_iter_mut: std::slice::IterMut<'a, T> } +pub struct ColumnIter<'a, T: Num> { + row_start: usize, column_index: usize, matrix: &'a Matrix +} + pub struct IterIndices { 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 { + 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 { type Item = (usize, usize); fn next(&mut self) -> Option {