mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 07:09:59 +00:00
Allow creation of 0-row matrices from empty column data and improve data flattening in from_cols method.
This commit is contained in:
parent
38c5c28454
commit
7cf41171a8
@ -13,13 +13,15 @@ impl<T: Clone> Matrix<T> {
|
|||||||
pub fn from_cols(cols_data: Vec<Vec<T>>) -> Self {
|
pub fn from_cols(cols_data: Vec<Vec<T>>) -> Self {
|
||||||
let cols = cols_data.len();
|
let cols = cols_data.len();
|
||||||
assert!(cols > 0, "need at least one column");
|
assert!(cols > 0, "need at least one column");
|
||||||
let rows = cols_data[0].len();
|
// Handle empty cols_data
|
||||||
// Allow 0-row matrices if columns are empty, but not 0-col matrices if rows > 0
|
let rows = cols_data.get(0).map_or(0, |c| c.len());
|
||||||
|
// Allow 0-row matrices if columns are empty, but not 0-col matrices if rows > 0
|
||||||
assert!(
|
assert!(
|
||||||
rows > 0 || cols == 0,
|
rows > 0 || cols == 0,
|
||||||
"need at least one row if columns exist"
|
"need at least one row if columns exist"
|
||||||
);
|
);
|
||||||
for (i, col) in cols_data.iter().enumerate().skip(1) {
|
|
||||||
|
for (i, col) in cols_data.iter().enumerate() {
|
||||||
assert!(
|
assert!(
|
||||||
col.len() == rows,
|
col.len() == rows,
|
||||||
"col {} has len {}, expected {}",
|
"col {} has len {}, expected {}",
|
||||||
@ -28,10 +30,8 @@ impl<T: Clone> Matrix<T> {
|
|||||||
rows
|
rows
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let mut data = Vec::with_capacity(rows * cols);
|
// Flatten column data directly
|
||||||
for col in cols_data {
|
let data = cols_data.into_iter().flatten().collect();
|
||||||
data.extend(col);
|
|
||||||
}
|
|
||||||
Matrix { rows, cols, data }
|
Matrix { rows, cols, data }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user