From a0a551c7d90e05e096c1c5d1d57e44a85f31969f Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Thu, 1 May 2025 23:13:57 +0100 Subject: [PATCH] update into_vec, from_vec --- src/matrix/mat.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/matrix/mat.rs b/src/matrix/mat.rs index 1523bf7..194bb4e 100644 --- a/src/matrix/mat.rs +++ b/src/matrix/mat.rs @@ -35,6 +35,7 @@ impl Matrix { Matrix { rows, cols, data } } + /// Build from a flat Vec, assuming column-major order. pub fn from_vec(data: Vec, rows: usize, cols: usize) -> Self { assert!( rows > 0 || cols == 0, @@ -44,7 +45,19 @@ impl Matrix { cols > 0 || rows == 0, "need at least one column if rows exist" ); - assert_eq!(data.len(), rows * cols, "data length mismatch"); + if rows * cols != 0 { + // Only assert length if matrix is non-empty + assert_eq!( + data.len(), + rows * cols, + "data length mismatch: expected {}, got {}", + rows * cols, + data.len() + ); + } else { + assert!(data.is_empty(), "data must be empty for 0-sized matrix"); + } + Matrix { rows, cols, data } } @@ -56,7 +69,13 @@ impl Matrix { &mut self.data } - pub fn as_vec(&self) -> Vec { + /// Consumes the Matrix and returns its underlying data Vec. + pub fn into_vec(self) -> Vec { + self.data + } + + /// Creates a new Vec containing the matrix data (cloned). + pub fn to_vec(&self) -> Vec { self.data.clone() }