2016-12-05 05:44:42 +08:00
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
|
|
use core::Scalar;
|
|
|
|
|
use core::dimension::{Dim, DimName, Dynamic, U1};
|
2018-02-02 19:26:35 +08:00
|
|
|
|
use core::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
use core::allocator::Allocator;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
use core::default_allocator::DefaultAllocator;
|
|
|
|
|
|
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))]
|
2016-12-05 05:44:42 +08:00
|
|
|
|
pub struct MatrixVec<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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<N, R: Dim, C: Dim> MatrixVec<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]
|
|
|
|
|
pub fn new(nrows: R, ncols: C, data: Vec<N>) -> MatrixVec<N, R, C> {
|
2018-02-02 19:26:35 +08:00
|
|
|
|
assert!(
|
|
|
|
|
nrows.value() * ncols.value() == data.len(),
|
|
|
|
|
"Data storage buffer dimension mismatch."
|
|
|
|
|
);
|
2016-12-05 05:44:42 +08:00
|
|
|
|
MatrixVec {
|
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]
|
|
|
|
|
pub fn data(&self) -> &Vec<N> {
|
|
|
|
|
&self.data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The underlying mutable data storage.
|
|
|
|
|
///
|
|
|
|
|
/// This is unsafe because this may cause UB if the vector is modified by the user.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn data_mut(&mut self) -> &mut Vec<N> {
|
|
|
|
|
&mut self.data
|
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
|
|
/// Resizes the undelying mutable data storage and unrwaps it.
|
|
|
|
|
///
|
|
|
|
|
/// If `sz` is larger than the current size, additional elements are uninitialized.
|
|
|
|
|
/// If `sz` is smaller than the current size, additional elements are trucated.
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<N, R: Dim, C: Dim> Deref for MatrixVec<N, R, C> {
|
|
|
|
|
type Target = Vec<N>;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* Dynamic − Static
|
|
|
|
|
* Dynamic − Dynamic
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-08-03 01:37:44 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> Storage<N, Dynamic, C> for MatrixVec<N, Dynamic, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>,
|
|
|
|
|
{
|
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>
|
2018-02-02 19:26:35 +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>
|
2018-02-02 19:26:35 +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] {
|
|
|
|
|
&self[..]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<N: Scalar, R: DimName> Storage<N, R, Dynamic> for MatrixVec<N, R, Dynamic>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>,
|
|
|
|
|
{
|
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>
|
2018-02-02 19:26:35 +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>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
self.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn as_slice(&self) -> &[N] {
|
|
|
|
|
&self[..]
|
|
|
|
|
}
|
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
|
|
|
|
*
|
|
|
|
|
*/
|
2017-08-03 01:37:44 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> StorageMut<N, Dynamic, C> for MatrixVec<N, Dynamic, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>,
|
|
|
|
|
{
|
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[..]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> ContiguousStorage<N, Dynamic, C> for MatrixVec<N, Dynamic, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
unsafe impl<N: Scalar, C: Dim> ContiguousStorageMut<N, Dynamic, C> for MatrixVec<N, Dynamic, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, Dynamic, C, Buffer = Self>,
|
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
unsafe impl<N: Scalar, R: DimName> StorageMut<N, R, Dynamic> for MatrixVec<N, R, Dynamic>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>,
|
|
|
|
|
{
|
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")]
|
|
|
|
|
impl<N: Abomonation, R: Dim, C: Dim> Abomonation for MatrixVec<N, R, C> {
|
|
|
|
|
unsafe fn entomb(&self, writer: &mut Vec<u8>) {
|
|
|
|
|
self.data.entomb(writer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn embalm(&mut self) {
|
|
|
|
|
self.data.embalm()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
|
|
|
|
|
self.data.exhume(bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-16 01:18:39 +08:00
|
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
|
unsafe impl<N: Scalar, R: DimName> ContiguousStorage<N, R, Dynamic> for MatrixVec<N, R, Dynamic>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<N: Scalar, R: DimName> ContiguousStorageMut<N, R, Dynamic> for MatrixVec<N, R, Dynamic>
|
2018-02-02 19:26:35 +08:00
|
|
|
|
where
|
|
|
|
|
DefaultAllocator: Allocator<N, R, Dynamic, Buffer = Self>,
|
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
|
}
|