2017-02-13 01:17:09 +08:00
|
|
|
use std::fmt::{self, Debug, Formatter};
|
2016-12-05 05:44:42 +08:00
|
|
|
use std::hash::{Hash, Hasher};
|
2018-07-20 21:25:55 +08:00
|
|
|
#[cfg(feature = "abomonation-serialize")]
|
|
|
|
use std::io::{Result as IOResult, Write};
|
|
|
|
use std::ops::{Deref, DerefMut, Mul};
|
2017-02-16 05:04:34 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-07-20 21:25:55 +08:00
|
|
|
use serde::de::{Error, SeqAccess, Visitor};
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2017-02-13 01:17:09 +08:00
|
|
|
use serde::ser::SerializeSeq;
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-07-20 21:25:55 +08:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
use std::marker::PhantomData;
|
2018-07-20 21:25:55 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
use std::mem;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-14 18:07:06 +08:00
|
|
|
#[cfg(feature = "abomonation-serialize")]
|
|
|
|
use abomonation::Abomonation;
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
use generic_array::{ArrayLength, GenericArray};
|
2018-07-20 21:25:55 +08:00
|
|
|
use typenum::Prod;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-05-19 23:15:15 +08:00
|
|
|
use base::allocator::Allocator;
|
|
|
|
use base::default_allocator::DefaultAllocator;
|
2018-07-20 21:25:55 +08:00
|
|
|
use base::dimension::{DimName, U1};
|
|
|
|
use base::storage::{ContiguousStorage, ContiguousStorageMut, Owned, Storage, StorageMut};
|
|
|
|
use base::Scalar;
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Static Storage.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/// A array-based statically sized matrix data storage.
|
|
|
|
#[repr(C)]
|
2018-12-06 05:40:03 +08:00
|
|
|
pub struct ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
|
|
|
data: GenericArray<N, Prod<R::Value, C::Value>>,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
#[deprecated(note="renamed to `ArrayStorage`")]
|
|
|
|
/// Renamed to [ArrayStorage].
|
|
|
|
pub type MatrixArray<N, R, C> = ArrayStorage<N, R, C>;
|
|
|
|
|
|
|
|
impl<N, R, C> Hash for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Hash,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.data[..].hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> Deref for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
type Target = GenericArray<N, Prod<R::Value, C::Value>>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> DerefMut for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> Debug for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Debug,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
2017-02-13 01:17:09 +08:00
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
2016-12-05 05:44:42 +08:00
|
|
|
self.data.fmt(fmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> Copy for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Copy,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
GenericArray<N, Prod<R::Value, C::Value>>: Copy,
|
2018-10-22 13:00:10 +08:00
|
|
|
{}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> Clone for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Clone,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
2018-12-06 05:40:03 +08:00
|
|
|
ArrayStorage {
|
2018-02-02 19:26:35 +08:00
|
|
|
data: self.data.clone(),
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> Eq for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Eq,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
2018-10-22 13:00:10 +08:00
|
|
|
{}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> PartialEq for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: PartialEq,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, right: &Self) -> bool {
|
2018-02-02 19:26:35 +08:00
|
|
|
self.data == right.data
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
unsafe impl<N, R, C> Storage<N, R, C> for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
DefaultAllocator: Allocator<N, R, C, Buffer = Self>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
type RStride = U1;
|
|
|
|
type CStride = R;
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn ptr(&self) -> *const N {
|
|
|
|
self[..].as_ptr()
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn shape(&self) -> (R, C) {
|
|
|
|
(R::name(), C::name())
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2017-08-03 01:37:44 +08:00
|
|
|
#[inline]
|
|
|
|
fn strides(&self) -> (Self::RStride, Self::CStride) {
|
|
|
|
(Self::RStride::name(), Self::CStride::name())
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
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, R, C>
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Allocator<N, R, C> {
|
2017-08-03 01:37:44 +08:00
|
|
|
self
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn clone_owned(&self) -> Owned<N, R, C>
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Allocator<N, R, C> {
|
2017-08-03 01:37:44 +08:00
|
|
|
let it = self.iter().cloned();
|
|
|
|
|
|
|
|
DefaultAllocator::allocate_from_iterator(self.shape().0, self.shape().1, it)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn as_slice(&self) -> &[N] {
|
|
|
|
&self[..]
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
unsafe impl<N, R, C> StorageMut<N, R, C> for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
DefaultAllocator: Allocator<N, R, C, Buffer = Self>,
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
#[inline]
|
|
|
|
fn ptr_mut(&mut self) -> *mut N {
|
|
|
|
self[..].as_mut_ptr()
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn as_mut_slice(&mut self) -> &mut [N] {
|
|
|
|
&mut self[..]
|
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
unsafe impl<N, R, C> ContiguousStorage<N, R, C> for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
DefaultAllocator: Allocator<N, R, C, Buffer = Self>,
|
2018-10-22 13:00:10 +08:00
|
|
|
{}
|
2016-12-05 05:44:42 +08:00
|
|
|
|
2018-12-06 05:40:03 +08:00
|
|
|
unsafe impl<N, R, C> ContiguousStorageMut<N, R, C> for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
DefaultAllocator: Allocator<N, R, C, Buffer = Self>,
|
2018-10-22 13:00:10 +08:00
|
|
|
{}
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Allocation-less serde impls.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
// XXX: open an issue for GenericArray so that it implements serde traits?
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> Serialize for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar + Serialize,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2017-02-13 01:17:09 +08:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2018-10-22 13:00:10 +08:00
|
|
|
where S: Serializer {
|
2018-02-02 19:26:35 +08:00
|
|
|
let mut serializer = serializer.serialize_seq(Some(R::dim() * C::dim()))?;
|
2017-02-13 01:17:09 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
for e in self.iter() {
|
|
|
|
serializer.serialize_element(e)?;
|
2017-02-13 01:17:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
serializer.end()
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 01:17:09 +08:00
|
|
|
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<'a, N, R, C> Deserialize<'a> for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar + Deserialize<'a>,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2017-02-13 01:17:09 +08:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2018-10-22 13:00:10 +08:00
|
|
|
where D: Deserializer<'a> {
|
2018-12-06 05:40:03 +08:00
|
|
|
deserializer.deserialize_seq(ArrayStorageVisitor::new())
|
2018-02-02 19:26:35 +08:00
|
|
|
}
|
2017-02-13 01:17:09 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2017-02-13 01:17:09 +08:00
|
|
|
/// A visitor that produces a matrix array.
|
2018-12-06 05:40:03 +08:00
|
|
|
struct ArrayStorageVisitor<N, R, C> {
|
2018-02-02 19:26:35 +08:00
|
|
|
marker: PhantomData<(N, R, C)>,
|
2017-02-13 01:17:09 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> ArrayStorageVisitor<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2017-02-13 01:17:09 +08:00
|
|
|
/// Construct a new sequence visitor.
|
|
|
|
pub fn new() -> Self {
|
2018-12-06 05:40:03 +08:00
|
|
|
ArrayStorageVisitor {
|
2017-02-13 01:17:09 +08:00
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 05:04:34 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<'a, N, R, C> Visitor<'a> for ArrayStorageVisitor<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
N: Scalar + Deserialize<'a>,
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
{
|
2018-12-06 05:40:03 +08:00
|
|
|
type Value = ArrayStorage<N, R, C>;
|
2017-02-13 01:17:09 +08:00
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a matrix array")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-12-06 05:40:03 +08:00
|
|
|
fn visit_seq<V>(self, mut visitor: V) -> Result<ArrayStorage<N, R, C>, V::Error>
|
2018-10-22 13:00:10 +08:00
|
|
|
where V: SeqAccess<'a> {
|
2017-02-13 01:17:09 +08:00
|
|
|
let mut out: Self::Value = unsafe { mem::uninitialized() };
|
|
|
|
let mut curr = 0;
|
|
|
|
|
2017-04-25 02:05:45 +08:00
|
|
|
while let Some(value) = try!(visitor.next_element()) {
|
2019-01-17 00:20:56 +08:00
|
|
|
*out.get_mut(curr).ok_or_else(|| V::Error::invalid_length(curr, &self))? = value;
|
2017-02-13 01:17:09 +08:00
|
|
|
curr += 1;
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:05:45 +08:00
|
|
|
if curr == R::dim() * C::dim() {
|
|
|
|
Ok(out)
|
2018-02-02 19:26:35 +08:00
|
|
|
} else {
|
2017-04-25 02:05:45 +08:00
|
|
|
Err(V::Error::invalid_length(curr, &self))
|
|
|
|
}
|
2017-02-13 01:17:09 +08:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 18:07:06 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "abomonation-serialize")]
|
2018-12-06 05:40:03 +08:00
|
|
|
impl<N, R, C> Abomonation for ArrayStorage<N, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
R: DimName,
|
|
|
|
C: DimName,
|
|
|
|
R::Value: Mul<C::Value>,
|
|
|
|
Prod<R::Value, C::Value>: ArrayLength<N>,
|
|
|
|
N: Abomonation,
|
2017-08-14 18:07:06 +08:00
|
|
|
{
|
2018-07-20 21:25:55 +08:00
|
|
|
unsafe fn entomb<W: Write>(&self, writer: &mut W) -> IOResult<()> {
|
2017-08-15 02:53:55 +08:00
|
|
|
for element in self.data.as_slice() {
|
2018-07-20 21:25:55 +08:00
|
|
|
element.entomb(writer)?;
|
2017-08-15 02:53:55 +08:00
|
|
|
}
|
2017-08-14 18:07:06 +08:00
|
|
|
|
2018-07-20 21:25:55 +08:00
|
|
|
Ok(())
|
2017-08-14 18:07:06 +08:00
|
|
|
}
|
|
|
|
|
2017-08-15 02:53:55 +08:00
|
|
|
unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> {
|
|
|
|
for element in self.data.as_mut_slice() {
|
|
|
|
let temp = bytes;
|
|
|
|
bytes = if let Some(remainder) = element.exhume(temp) {
|
|
|
|
remainder
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(bytes)
|
2017-08-14 18:07:06 +08:00
|
|
|
}
|
2018-07-20 21:25:55 +08:00
|
|
|
|
|
|
|
fn extent(&self) -> usize {
|
|
|
|
self.data
|
|
|
|
.as_slice()
|
|
|
|
.iter()
|
|
|
|
.fold(0, |acc, e| acc + e.extent())
|
|
|
|
}
|
2017-08-14 18:07:06 +08:00
|
|
|
}
|