diff --git a/src/base/edition.rs b/src/base/edition.rs index 0ae9c35f..4a99b77f 100644 --- a/src/base/edition.rs +++ b/src/base/edition.rs @@ -703,3 +703,51 @@ unsafe fn extend_rows( ); } } + +/// Extend the number of columns of the `Matrix` with elements from +/// a given iterator. +impl Extend for Matrix +where + N: Scalar, + R: Dim, + S: Extend, +{ + /// Extend the number of columns of the `Matrix` with elements + /// from the given iterator. + /// + /// # Example + /// ``` + /// use nalgebra::{Dynamic, Matrix, MatrixMN, Matrix3}; + /// + /// let data = vec![0, 1, 2, // column 1 + /// 3, 4, 5]; // column 2 + /// + /// let mut matrix : MatrixMN<_, Dynamic, Dynamic> = + /// Matrix::from_vec_generic(Dynamic::new(3), Dynamic::new(2), data); + /// + /// matrix.extend(vec![6, 7, 8]); // column 3 + /// + /// assert!(matrix.eq(&Matrix3::new(0, 3, 6, + /// 1, 4, 7, + /// 2, 5, 8))); + /// ``` + /// + /// # Panics + /// This function panics if the number of elements yielded by the + /// given iterator is not a multiple of the number of rows of the + /// `Matrix`. + /// + /// ```should_panic + /// # use nalgebra::{Dynamic, Matrix, MatrixMN, Matrix3, U3}; + /// let data = vec![0, 1, 2, // column 1 + /// 3, 4, 5]; // column 2 + /// + /// let mut matrix : MatrixMN<_, U3, Dynamic> = + /// Matrix::from_vec_generic(U3, Dynamic::new(2), data); + /// + /// matrix.extend(vec![6, 7, 8, 9]); // column 3 + /// ``` + fn extend>(&mut self, iter: I) { + self.data.extend(iter); + } +} diff --git a/src/base/matrix_vec.rs b/src/base/matrix_vec.rs index 73b77609..8dfd940e 100644 --- a/src/base/matrix_vec.rs +++ b/src/base/matrix_vec.rs @@ -240,3 +240,21 @@ unsafe impl ContiguousStorage for MatrixVe unsafe impl ContiguousStorageMut for MatrixVec where DefaultAllocator: Allocator {} + +impl Extend for MatrixVec +{ + /// Extends the number of columns of the `MatrixVec` with elements + /// from the given iterator. + /// + /// # Panics + /// This function panics if the number of elements yielded by the + /// given iterator is not a multiple of the number of rows of the + /// `MatrixVec`. + fn extend>(&mut self, iter: I) + { + self.data.extend(iter); + self.ncols = Dynamic::new(self.data.len() / self.nrows.value()); + assert!(self.data.len() % self.nrows.value() == 0, + "The number of elements produced by the given iterator was not a multiple of the number of rows."); + } +}