Enhance delete_column method to improve efficiency of column removal

This commit is contained in:
Palash Tyagi 2025-05-03 00:26:21 +01:00
parent 20727a2b91
commit 8574d86abc

View File

@ -170,12 +170,12 @@ impl<T: Clone> Matrix<T> {
}
}
/// Deletes a column from the matrix.
/// Deletes a column from the matrix. Panics on out-of-bounds.
/// This is O(N) where N is the number of elements.
pub fn delete_column(&mut self, col: usize) {
assert!(col < self.cols, "column index out of bounds");
for r in (0..self.rows).rev() {
self.data.remove(col * self.rows + r);
}
assert!(col < self.cols, "column index {} out of bounds for {} columns", col, self.cols);
let start = col * self.rows;
self.data.drain(start..start + self.rows); // Efficient removal
self.cols -= 1;
}