use imp_prelude::*;
use dimension::{self, stride_offset};
use error::ShapeError;
use StrideShape;
impl<'a, A, D> ArrayBase<ViewRepr<&'a A>, D>
where D: Dimension,
{
pub fn from_shape<Sh>(shape: Sh, xs: &'a [A])
-> Result<Self, ShapeError>
where Sh: Into<StrideShape<D>>,
{
let shape = shape.into();
let dim = shape.dim;
let strides = shape.strides;
dimension::can_index_slice(xs, &dim, &strides).map(|_| {
unsafe {
Self::new_(xs.as_ptr(), dim, strides)
}
})
}
#[cfg_attr(has_deprecated, deprecated(note="Use from_shape instead."))]
pub fn from_slice_dim_stride(dim: D, strides: D, xs: &'a [A])
-> Result<Self, ShapeError>
{
dimension::can_index_slice(xs, &dim, &strides).map(|_| {
unsafe {
Self::new_(xs.as_ptr(), dim, strides)
}
})
}
pub fn split_at(self, axis: Axis, index: Ix)
-> (Self, Self)
{
assert!(index <= self.shape().axis(axis));
let left_ptr = self.ptr;
let right_ptr = if index == self.shape().axis(axis) {
self.ptr
} else {
let offset = stride_offset(index, self.strides.axis(axis));
unsafe {
self.ptr.offset(offset)
}
};
let mut dim_left = self.dim.clone();
dim_left.set_axis(axis, index);
let left = unsafe {
Self::new_(left_ptr, dim_left, self.strides.clone())
};
let mut dim_right = self.dim;
let right_len = dim_right.axis(axis) - index;
dim_right.set_axis(axis, right_len);
let right = unsafe {
Self::new_(right_ptr, dim_right, self.strides)
};
(left, right)
}
}
impl<'a, A, D> ArrayBase<ViewRepr<&'a mut A>, D>
where D: Dimension,
{
pub fn from_shape<Sh>(shape: Sh, xs: &'a mut [A])
-> Result<Self, ShapeError>
where Sh: Into<StrideShape<D>>,
{
let shape = shape.into();
let dim = shape.dim;
let strides = shape.strides;
dimension::can_index_slice(xs, &dim, &strides).map(|_| {
unsafe {
Self::new_(xs.as_mut_ptr(), dim, strides)
}
})
}
#[cfg_attr(has_deprecated, deprecated(note="Use from_shape instead."))]
pub fn from_slice_dim_stride(dim: D, strides: D, xs: &'a mut [A])
-> Result<Self, ShapeError>
{
dimension::can_index_slice(xs, &dim, &strides).map(|_| {
unsafe {
Self::new_(xs.as_mut_ptr(), dim, strides)
}
})
}
pub fn split_at(self, axis: Axis, index: Ix)
-> (Self, Self)
{
assert!(index <= self.shape().axis(axis));
let left_ptr = self.ptr;
let right_ptr = if index == self.shape().axis(axis) {
self.ptr
} else {
let offset = stride_offset(index, self.strides.axis(axis));
unsafe {
self.ptr.offset(offset)
}
};
let mut dim_left = self.dim.clone();
dim_left.set_axis(axis, index);
let left = unsafe {
Self::new_(left_ptr, dim_left, self.strides.clone())
};
let mut dim_right = self.dim;
let right_len = dim_right.axis(axis) - index;
dim_right.set_axis(axis, right_len);
let right = unsafe {
Self::new_(right_ptr, dim_right, self.strides)
};
(left, right)
}
}