Implemented row, column, column_iter and column_iters methods, added more tests

This commit is contained in:
Egor 2024-05-23 02:15:53 +03:00
parent 60a7ae725a
commit 7cad1b8dfc
3 changed files with 49 additions and 9 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"codetogether.virtualCursorJoin": "sharedVirtualCursor"
}

View file

@ -34,7 +34,7 @@ impl<T: Num + Clone> Matrix<T> {
}
pub fn column(&self, j: usize) -> Vec<T> {
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]);
}
}

View file

@ -1,5 +1,7 @@
use super::*;
use std::{slice::{Chunks, ChunksMut, Iter, IterMut}, vec::IntoIter};
use num::Num;
impl<T: Num + Clone> Matrix<T> {
@ -7,7 +9,7 @@ impl<T: Num + Clone> Matrix<T> {
IterIndices { i: 0, j: 0, width: self.width, height: self.height() }
}
pub fn into_iter(self) -> std::vec::IntoIter<T> {
pub fn into_iter(self) -> IntoIter<T> {
self.data.into_iter()
}
@ -19,7 +21,7 @@ impl<T: Num + Clone> Matrix<T> {
todo!()
}
pub fn iter(&self) -> std::slice::Iter<T> {
pub fn iter(&self) -> Iter<T> {
self.data.iter()
}
@ -27,7 +29,7 @@ impl<T: Num + Clone> Matrix<T> {
IterIndexed { indices: self.indices(), matrix_iter: self.iter() }
}
pub fn iter_rows(&self) -> std::slice::Chunks<T> {
pub fn iter_rows(&self) -> Chunks<T> {
self.data.chunks(self.width)
}
@ -35,11 +37,11 @@ impl<T: Num + Clone> Matrix<T> {
ColumnIter { row_start: 0, column_index: j, matrix: self }
}
pub fn iter_columns(&self) -> () {
todo!()
pub fn iter_columns(&self) -> ColumnsIter<T> {
ColumnsIter { current_column_index: 0, height: self.height(), matrix: self }
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
pub fn iter_mut(&mut self) -> IterMut<T> {
self.data.iter_mut()
}
@ -47,7 +49,7 @@ impl<T: Num + Clone> Matrix<T> {
IterIndexedMut { indices: self.indices(), matrix_iter_mut: self.iter_mut() }
}
pub fn iter_rows_mut(&mut self) -> std::slice::ChunksMut<T> {
pub fn iter_rows_mut(&mut self) -> ChunksMut<T> {
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<T>
row_start: usize, column_index: usize,
matrix: &'a Matrix<T>
}
pub struct ColumnsIter<'a, T: Num> {
current_column_index: usize, height: usize,
matrix: &'a Matrix<T>
}
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<Self::Item> {
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<Self::Item> {