mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 04:00:01 +00:00
Add method to retrieve a specific row from the matrix and corresponding tests
This commit is contained in:
parent
04637ef4d0
commit
f749b2c921
@ -179,6 +179,21 @@ impl<T: Clone> Matrix<T> {
|
|||||||
self.cols -= 1;
|
self.cols -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn row(&self, r: usize) -> Vec<T> {
|
||||||
|
assert!(
|
||||||
|
r < self.rows,
|
||||||
|
"row index {} out of bounds for {} rows",
|
||||||
|
r,
|
||||||
|
self.rows
|
||||||
|
);
|
||||||
|
let mut row_data = Vec::with_capacity(self.cols);
|
||||||
|
for c in 0..self.cols {
|
||||||
|
row_data.push(self[(r, c)].clone()); // Clone each element
|
||||||
|
}
|
||||||
|
row_data
|
||||||
|
}
|
||||||
|
|
||||||
/// Deletes a row from the matrix. Panics on out-of-bounds.
|
/// Deletes a row from the matrix. Panics on out-of-bounds.
|
||||||
/// This is O(N) where N is the number of elements, as it rebuilds the data vec.
|
/// This is O(N) where N is the number of elements, as it rebuilds the data vec.
|
||||||
pub fn delete_row(&mut self, row: usize) {
|
pub fn delete_row(&mut self, row: usize) {
|
||||||
@ -1130,6 +1145,21 @@ mod tests {
|
|||||||
matrix[(0, 3)] = 99;
|
matrix[(0, 3)] = 99;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_row() {
|
||||||
|
let ma = static_test_matrix();
|
||||||
|
assert_eq!(ma.row(0), &[1, 4, 7]);
|
||||||
|
assert_eq!(ma.row(1), &[2, 5, 8]);
|
||||||
|
assert_eq!(ma.row(2), &[3, 6, 9]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic(expected = "row index 3 out of bounds for 3 rows")]
|
||||||
|
fn test_row_out_of_bounds() {
|
||||||
|
let ma = static_test_matrix();
|
||||||
|
ma.row(3);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_column() {
|
fn test_column() {
|
||||||
let matrix = static_test_matrix_2x4();
|
let matrix = static_test_matrix_2x4();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user