Implement Extend<Vector<_,_,_>> for Matrix.

Extend a `Matrix` with columns drawn from an iterator of `Vector`s.
This commit is contained in:
Jack Wrenn 2018-11-13 16:17:00 -05:00 committed by Sébastien Crozet
parent 0f29a3ae0a
commit 0ed67d0470
2 changed files with 92 additions and 1 deletions

View File

@ -772,3 +772,66 @@ where
self.data.extend(iter);
}
}
impl<N, R, S, RV, SV> Extend<Vector<N, RV, SV>> for Matrix<N, R, Dynamic, S>
where
N: Scalar,
R: Dim,
S: Extend<Vector<N, RV, SV>>,
RV: Dim,
SV: Storage<N, RV>
{
/// Extends the number of columns of a `Matrix` with `Vector`s
/// from a given iterator.
///
/// # Example
/// ```
/// # use nalgebra::{DMatrix, Vector3, Matrix3x4};
///
/// let data = vec![0, 1, 2, // column 1
/// 3, 4, 5]; // column 2
///
/// let mut matrix = DMatrix::from_vec(3, 2, data);
///
/// matrix.extend(
/// vec![Vector3::new(6, 7, 8), // column 3
/// Vector3::new(9, 10, 11)]); // column 4
///
/// assert!(matrix.eq(&Matrix3x4::new(0, 3, 6, 9,
/// 1, 4, 7, 10,
/// 2, 5, 8, 11)));
/// ```
///
/// # Panics
/// This function panics if the dimension of each `Vector` yielded
/// by the given iterator is not equal to the number of rows of
/// this `Matrix`.
///
/// ```should_panic
/// # use nalgebra::{DMatrix, Vector2, Matrix3x4};
/// let mut matrix =
/// DMatrix::from_vec(3, 2,
/// vec![0, 1, 2, // column 1
/// 3, 4, 5]); // column 2
///
/// // The following panics because this matrix can only be extended with 3-dimensional vectors.
/// matrix.extend(
/// vec![Vector2::new(6, 7)]); // too few dimensions!
/// ```
///
/// ```should_panic
/// # use nalgebra::{DMatrix, Vector4, Matrix3x4};
/// let mut matrix =
/// DMatrix::from_vec(3, 2,
/// vec![0, 1, 2, // column 1
/// 3, 4, 5]); // column 2
///
/// // The following panics because this matrix can only be extended with 3-dimensional vectors.
/// matrix.extend(
/// vec![Vector4::new(6, 7, 8, 9)]); // too few dimensions!
/// ```
fn extend<I: IntoIterator<Item=Vector<N, RV, SV>>>(&mut self, iter: I)
{
self.data.extend(iter);
}
}

View File

@ -9,7 +9,7 @@ use base::allocator::Allocator;
use base::default_allocator::DefaultAllocator;
use base::dimension::{Dim, DimName, Dynamic, U1};
use base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut};
use base::Scalar;
use base::{Scalar, Vector};
#[cfg(feature = "abomonation-serialize")]
use abomonation::Abomonation;
@ -259,6 +259,34 @@ impl<N, R: Dim> Extend<N> for MatrixVec<N, R, Dynamic>
}
}
impl<N, R, RV, SV> Extend<Vector<N, RV, SV>> for MatrixVec<N, R, Dynamic>
where
N: Scalar,
R: Dim,
RV: Dim,
SV: Storage<N, RV>
{
/// Extends the number of columns of the `MatrixVec` with vectors
/// from the given iterator.
///
/// # Panics
/// This function panics if the number of rows of each `Vector`
/// yielded by the iterator is not equal to the number of rows
/// of this `MatrixVec`.
fn extend<I: IntoIterator<Item=Vector<N, RV, SV>>>(&mut self, iter: I)
{
let nrows = self.nrows.value();
let iter = iter.into_iter();
let (lower, _upper) = iter.size_hint();
self.data.reserve(nrows * lower);
for vector in iter {
assert_eq!(nrows, vector.shape().0);
self.data.extend(vector.iter());
}
self.ncols = Dynamic::new(self.data.len() / nrows);
}
}
impl<N> Extend<N> for MatrixVec<N, Dynamic, U1>
{
/// Extends the number of rows of the `MatrixVec` with elements