From 7cad1b8dfc8e9cd3bd72df7d4baa7c5d3d1e393c Mon Sep 17 00:00:00 2001 From: erius Date: Thu, 23 May 2024 02:15:53 +0300 Subject: [PATCH] Implemented row, column, column_iter and column_iters methods, added more tests --- .vscode/settings.json | 3 +++ src/math/matrix.rs | 17 ++++++++++++++++- src/math/matrix/iter.rs | 38 ++++++++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..179c99c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "codetogether.virtualCursorJoin": "sharedVirtualCursor" +} \ No newline at end of file diff --git a/src/math/matrix.rs b/src/math/matrix.rs index 65f0556..db4852c 100644 --- a/src/math/matrix.rs +++ b/src/math/matrix.rs @@ -34,7 +34,7 @@ impl Matrix { } pub fn column(&self, j: usize) -> Vec { - todo!() + self.iter_column(j).cloned().collect() } } @@ -160,4 +160,19 @@ mod tests { matrix[2][1] = 0; assert_eq!(matrix[2][1], 0); } + + #[test] + fn rows_and_columns() { + let matrix = matrix![3; + 1, 2, 3, + 4, 5, 6, + 7, 8, 9 + ]; + assert_eq!(matrix.row(0), vec![1, 2, 3]); + assert_eq!(matrix.row(1), vec![4, 5, 6]); + assert_eq!(matrix.row(2), vec![7, 8, 9]); + assert_eq!(matrix.column(0), vec![1, 4, 7]); + assert_eq!(matrix.column(1), vec![2, 5, 8]); + assert_eq!(matrix.column(2), vec![3, 6, 9]); + } } diff --git a/src/math/matrix/iter.rs b/src/math/matrix/iter.rs index 6bbcc97..f8d5323 100644 --- a/src/math/matrix/iter.rs +++ b/src/math/matrix/iter.rs @@ -1,5 +1,7 @@ use super::*; +use std::{slice::{Chunks, ChunksMut, Iter, IterMut}, vec::IntoIter}; + use num::Num; impl Matrix { @@ -7,7 +9,7 @@ impl Matrix { IterIndices { i: 0, j: 0, width: self.width, height: self.height() } } - pub fn into_iter(self) -> std::vec::IntoIter { + pub fn into_iter(self) -> IntoIter { self.data.into_iter() } @@ -19,7 +21,7 @@ impl Matrix { todo!() } - pub fn iter(&self) -> std::slice::Iter { + pub fn iter(&self) -> Iter { self.data.iter() } @@ -27,7 +29,7 @@ impl Matrix { IterIndexed { indices: self.indices(), matrix_iter: self.iter() } } - pub fn iter_rows(&self) -> std::slice::Chunks { + pub fn iter_rows(&self) -> Chunks { self.data.chunks(self.width) } @@ -35,11 +37,11 @@ impl Matrix { ColumnIter { row_start: 0, column_index: j, matrix: self } } - pub fn iter_columns(&self) -> () { - todo!() + pub fn iter_columns(&self) -> ColumnsIter { + ColumnsIter { current_column_index: 0, height: self.height(), matrix: self } } - pub fn iter_mut(&mut self) -> std::slice::IterMut { + pub fn iter_mut(&mut self) -> IterMut { self.data.iter_mut() } @@ -47,7 +49,7 @@ impl Matrix { IterIndexedMut { indices: self.indices(), matrix_iter_mut: self.iter_mut() } } - pub fn iter_rows_mut(&mut self) -> std::slice::ChunksMut { + pub fn iter_rows_mut(&mut self) -> ChunksMut { self.data.chunks_mut(self.width) } } @@ -63,7 +65,13 @@ pub struct IterIndexedMut<'a, T: Num> { } pub struct ColumnIter<'a, T: Num> { - row_start: usize, column_index: usize, matrix: &'a Matrix + row_start: usize, column_index: usize, + matrix: &'a Matrix +} + +pub struct ColumnsIter<'a, T: Num> { + current_column_index: usize, height: usize, + matrix: &'a Matrix } pub struct IterIndices { @@ -99,6 +107,20 @@ impl<'a, T: Num> Iterator for ColumnIter<'a, T> { } } + +impl<'a, T: Num> Iterator for ColumnsIter<'a, T> { + type Item = ColumnIter<'a, T>; + fn next(&mut self) -> Option { + if self.current_column_index >= self.height { return None; } + let column = ColumnIter { + row_start: 0, column_index: self.current_column_index, matrix: self.matrix + }; + self.current_column_index += 1; + Some(column) + } +} + + impl Iterator for IterIndices { type Item = (usize, usize); fn next(&mut self) -> Option {