2018-07-20 21:25:55 +08:00
|
|
|
|
#[cfg(feature = "abomonation-serialize")]
|
|
|
|
|
use std::io::{Result as IOResult, Write};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2018-05-24 23:17:34 +08:00
|
|
|
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
2018-07-20 22:10:12 +08:00
|
|
|
|
use alloc::vec::Vec;
|
2018-05-24 23:17:34 +08:00
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::base::allocator::Allocator;
|
2020-04-06 00:49:48 +08:00
|
|
|
|
use crate::base::constraint::{SameNumberOfRows, ShapeConstraint};
|
2019-03-23 21:29:07 +08:00
|
|
|
|
use crate::base::default_allocator::DefaultAllocator;
|
|
|
|
|
use crate::base::dimension::{Dim, DimName, Dynamic, U1};
|
|
|
|
|
use crate::base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut};
|
|
|
|
|
use crate::base::{Scalar, Vector};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2017-08-14 18:30:50 +08:00
|
|
|
|
#[cfg(feature = "abomonation-serialize")]
|
|
|
|
|
use abomonation::Abomonation;
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* Storage.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
/// A Vec-based matrix data storage. It may be dynamically-sized.
|
|
|
|
|
#[repr(C)]
|
2017-02-16 05:04:34 +08:00
|
|
|
|
#[derive(Eq, Debug, Clone, PartialEq)]
|
|
|
|
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
2018-12-06 05:46:17 +08:00
|
|
|
|
pub struct VecStorage<N, R: Dim, C: Dim> {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
data: Vec<N>,
|
|
|
|
|
nrows: R,
|
|
|
|
|
ncols: C,
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
#[deprecated(note = "renamed to `VecStorage`")]
|
2018-12-06 05:46:17 +08:00
|
|
|
|
/// Renamed to [VecStorage].
|
|
|
|
|
pub type MatrixVec<N, R, C> = VecStorage<N, R, C>;
|
|
|
|
|
|
|
|
|
|
impl<N, R: Dim, C: Dim> VecStorage<N, R, C> {
|
2017-02-13 01:17:09 +08:00
|
|
|
|
/// Creates a new dynamic matrix data storage from the given vector and shape.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2019-02-17 05:29:41 +08:00
|
|
|
|
pub fn new(nrows: R, ncols: C, data: Vec<N>) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
assert!(
|
|
|
|
|
nrows.value() * ncols.value() == data.len(),
|
|
|
|
|
"Data storage buffer dimension mismatch."
|
|
|
|
|
);
|
2019-02-17 05:29:41 +08:00
|
|
|
|
Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
data: data,
|
|
|
|
|
nrows: nrows,
|
|
|
|
|
ncols: ncols,
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The underlying data storage.
|
|
|
|
|
#[inline]
|
2019-02-03 18:47:23 +08:00
|
|
|
|
pub fn as_vec(&self) -> &Vec<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
&self.data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The underlying mutable data storage.
|
|
|
|
|
///
|
2019-02-03 18:47:23 +08:00
|
|
|
|
/// This is unsafe because this may cause UB if the size of the vector is changed
|
|
|
|
|
/// by the user.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2019-02-03 18:47:23 +08:00
|
|
|
|
pub unsafe fn as_vec_mut(&mut self) -> &mut Vec<N> {
|
2016-12-05 05:44:42 +08:00
|
|
|
|
&mut self.data
|
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
|
/// Resizes the underlying mutable data storage and unwraps it.
|
2017-08-03 01:37:44 +08:00
|
|
|
|
///
|
|
|
|
|
/// If `sz` is larger than the current size, additional elements are uninitialized.
|
2018-09-24 12:48:42 +08:00
|
|
|
|
/// If `sz` is smaller than the current size, additional elements are truncated.
|
2017-08-03 01:37:44 +08:00
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
|
pub unsafe fn resize(mut self, sz: usize) -> Vec<N> {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
let len = self.len();
|
|
|
|
|
|
|
|
|
|
if sz < len {
|
|
|
|
|
self.data.set_len(sz);
|
|
|
|
|
self.data.shrink_to_fit();
|
2018-02-02 19:26:35 +08:00
|
|
|
|
} else {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self.data.reserve_exact(sz - len);
|
|
|
|
|
self.data.set_len(sz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.data
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2018-09-13 11:45:14 +08:00
|
|
|
|
/// The number of elements on the underlying vector.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2018-09-13 11:45:14 +08:00
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.data.len()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
impl<N, R: Dim, C: Dim> Into<Vec<N>> for VecStorage<N, R, C> {
|
2018-11-21 06:44:29 +08:00
|
|
|
|
fn into(self) -> Vec<N> {
|
|
|
|
|
self.data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* Dynamic − Static
|
|
|
|
|
* Dynamic − Dynamic
|
|
|
|
|
*
|
|
|
|
|
*/
|
2019-12-17 07:09:14 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> Storage<N, Dynamic, C> for VecStorage<N, Dynamic, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
type RStride = U1;
|
|
|
|
|
type CStride = Dynamic;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ptr(&self) -> *const N {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self.data.as_ptr()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn shape(&self) -> (Dynamic, C) {
|
|
|
|
|
(self.nrows, self.ncols)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn strides(&self) -> (Self::RStride, Self::CStride) {
|
|
|
|
|
(Self::RStride::name(), self.nrows)
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn is_contiguous(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
fn into_owned(self) -> Owned<N, Dynamic, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
fn clone_owned(&self) -> Owned<N, Dynamic, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
self.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn as_slice(&self) -> &[N] {
|
2018-09-13 11:45:14 +08:00
|
|
|
|
&self.data
|
2017-08-03 01:37:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
|
unsafe impl<N: Scalar, R: DimName> Storage<N, R, Dynamic> for VecStorage<N, R, Dynamic>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
type RStride = U1;
|
|
|
|
|
type CStride = R;
|
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn ptr(&self) -> *const N {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self.data.as_ptr()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn shape(&self) -> (R, Dynamic) {
|
|
|
|
|
(self.nrows, self.ncols)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn strides(&self) -> (Self::RStride, Self::CStride) {
|
|
|
|
|
(Self::RStride::name(), self.nrows)
|
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn is_contiguous(&self) -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn into_owned(self) -> Owned<N, R, Dynamic>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn clone_owned(&self) -> Owned<N, R, Dynamic>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn as_slice(&self) -> &[N] {
|
2018-09-13 11:45:14 +08:00
|
|
|
|
&self.data
|
2017-08-03 01:37:44 +08:00
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
2017-08-03 01:37:44 +08:00
|
|
|
|
* StorageMut, ContiguousStorage.
|
2016-12-05 05:44:42 +08:00
|
|
|
|
*
|
|
|
|
|
*/
|
2019-12-17 07:09:14 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> StorageMut<N, Dynamic, C> for VecStorage<N, Dynamic, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
fn ptr_mut(&mut self) -> *mut N {
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self.data.as_mut_ptr()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn as_mut_slice(&mut self) -> &mut [N] {
|
|
|
|
|
&mut self.data[..]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> ContiguousStorage<N, Dynamic, C> for VecStorage<N, Dynamic, C> where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>
|
|
|
|
|
{
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> ContiguousStorageMut<N, Dynamic, C> for VecStorage<N, Dynamic, C> where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>
|
|
|
|
|
{
|
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
|
unsafe impl<N: Scalar, R: DimName> StorageMut<N, R, Dynamic> for VecStorage<N, R, Dynamic>
|
2020-04-06 00:49:48 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>,
|
2018-02-02 19:26:35 +08:00
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
|
fn ptr_mut(&mut self) -> *mut N {
|
|
|
|
|
self.data.as_mut_ptr()
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn as_mut_slice(&mut self) -> &mut [N] {
|
|
|
|
|
&mut self.data[..]
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-14 18:30:50 +08:00
|
|
|
|
|
|
|
|
|
#[cfg(feature = "abomonation-serialize")]
|
2018-12-06 05:46:17 +08:00
|
|
|
|
impl<N: Abomonation, R: Dim, C: Dim> Abomonation for VecStorage<N, R, C> {
|
2018-07-20 21:25:55 +08:00
|
|
|
|
unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
|
2017-08-14 18:30:50 +08:00
|
|
|
|
self.data.entomb(writer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
|
|
|
|
|
self.data.exhume(bytes)
|
|
|
|
|
}
|
2018-07-20 21:25:55 +08:00
|
|
|
|
|
|
|
|
|
fn extent(&self) -> usize {
|
|
|
|
|
self.data.extent()
|
|
|
|
|
}
|
2017-08-14 18:30:50 +08:00
|
|
|
|
}
|
2017-08-16 01:18:39 +08:00
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
unsafe impl<N: Scalar, R: DimName> ContiguousStorage<N, R, Dynamic> for VecStorage<N, R, Dynamic> where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>
|
|
|
|
|
{
|
|
|
|
|
}
|
2018-11-11 07:02:52 +08:00
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
unsafe impl<N: Scalar, R: DimName> ContiguousStorageMut<N, R, Dynamic> for VecStorage<N, R, Dynamic> where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>
|
2018-11-11 07:02:52 +08:00
|
|
|
|
{
|
2020-04-06 00:49:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<N, R: Dim> Extend<N> for VecStorage<N, R, Dynamic> {
|
2018-12-06 05:46:17 +08:00
|
|
|
|
/// Extends the number of columns of the `VecStorage` with elements
|
2018-11-11 07:02:52 +08:00
|
|
|
|
/// 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
|
2018-12-06 05:46:17 +08:00
|
|
|
|
/// `VecStorage`.
|
2020-04-06 00:49:48 +08:00
|
|
|
|
fn extend<I: IntoIterator<Item = N>>(&mut self, iter: I) {
|
2018-11-11 07:02:52 +08:00
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-13 02:53:30 +08:00
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
impl<'a, N: 'a + Copy, R: Dim> Extend<&'a N> for VecStorage<N, R, Dynamic> {
|
2019-11-29 22:31:52 +08:00
|
|
|
|
/// Extends the number of columns of the `VecStorage` 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
|
|
|
|
|
/// `VecStorage`.
|
2020-04-06 00:49:48 +08:00
|
|
|
|
fn extend<I: IntoIterator<Item = &'a N>>(&mut self, iter: I) {
|
2019-11-29 22:31:52 +08:00
|
|
|
|
self.extend(iter.into_iter().copied())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 05:46:17 +08:00
|
|
|
|
impl<N, R, RV, SV> Extend<Vector<N, RV, SV>> for VecStorage<N, R, Dynamic>
|
2018-11-14 05:17:00 +08:00
|
|
|
|
where
|
2019-12-17 07:09:14 +08:00
|
|
|
|
N: Scalar,
|
2018-11-14 05:17:00 +08:00
|
|
|
|
R: Dim,
|
|
|
|
|
RV: Dim,
|
2018-11-18 01:13:03 +08:00
|
|
|
|
SV: Storage<N, RV>,
|
|
|
|
|
ShapeConstraint: SameNumberOfRows<R, RV>,
|
2018-11-14 05:17:00 +08:00
|
|
|
|
{
|
2018-12-06 05:46:17 +08:00
|
|
|
|
/// Extends the number of columns of the `VecStorage` with vectors
|
2018-11-14 05:17:00 +08:00
|
|
|
|
/// 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
|
2018-12-06 05:46:17 +08:00
|
|
|
|
/// of this `VecStorage`.
|
2020-04-06 00:49:48 +08:00
|
|
|
|
fn extend<I: IntoIterator<Item = Vector<N, RV, SV>>>(&mut self, iter: I) {
|
2018-11-14 05:17:00 +08:00
|
|
|
|
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);
|
2019-12-06 06:54:17 +08:00
|
|
|
|
self.data.extend(vector.iter().cloned());
|
2018-11-14 05:17:00 +08:00
|
|
|
|
}
|
|
|
|
|
self.ncols = Dynamic::new(self.data.len() / nrows);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
|
impl<N> Extend<N> for VecStorage<N, Dynamic, U1> {
|
2018-12-06 05:46:17 +08:00
|
|
|
|
/// Extends the number of rows of the `VecStorage` with elements
|
2018-11-13 02:53:30 +08:00
|
|
|
|
/// from the given iterator.
|
2020-04-06 00:49:48 +08:00
|
|
|
|
fn extend<I: IntoIterator<Item = N>>(&mut self, iter: I) {
|
2018-11-13 02:53:30 +08:00
|
|
|
|
self.data.extend(iter);
|
|
|
|
|
self.nrows = Dynamic::new(self.data.len());
|
|
|
|
|
}
|
|
|
|
|
}
|