Skip to content

Add getter for dim in Shape and StrideShape #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions src/shape_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,35 @@ pub struct Shape<D> {
}

#[derive(Copy, Clone, Debug)]
pub(crate) enum Contiguous { }
pub(crate) enum Contiguous {}

impl<D> Shape<D> {
pub(crate) fn is_c(&self) -> bool {
matches!(self.strides, Strides::C)
}
}


/// An array shape of n dimensions in c-order, f-order or custom strides.
#[derive(Copy, Clone, Debug)]
pub struct StrideShape<D> {
pub(crate) dim: D,
pub(crate) strides: Strides<D>,
}

impl<D> StrideShape<D>
where
D: Dimension,
{
/// Return a reference to the dimension
pub fn raw_dim(&self) -> &D {
&self.dim
}
/// Return the size of the shape in number of elements
pub fn size(&self) -> usize {
self.dim.size()
}
}

/// Stride description
#[derive(Copy, Clone, Debug)]
pub(crate) enum Strides<D> {
Expand All @@ -37,21 +50,26 @@ pub(crate) enum Strides<D> {
/// Column-major ("F"-order)
F,
/// Custom strides
Custom(D)
Custom(D),
}

impl<D> Strides<D> {
/// Return strides for `dim` (computed from dimension if c/f, else return the custom stride)
pub(crate) fn strides_for_dim(self, dim: &D) -> D
where D: Dimension
where
D: Dimension,
{
match self {
Strides::C => dim.default_strides(),
Strides::F => dim.fortran_strides(),
Strides::Custom(c) => {
debug_assert_eq!(c.ndim(), dim.ndim(),
debug_assert_eq!(
c.ndim(),
dim.ndim(),
"Custom strides given with {} dimensions, expected {}",
c.ndim(), dim.ndim());
c.ndim(),
dim.ndim()
);
c
}
}
Expand Down Expand Up @@ -94,11 +112,7 @@ where
{
fn from(value: T) -> Self {
let shape = value.into_shape();
let st = if shape.is_c() {
Strides::C
} else {
Strides::F
};
let st = if shape.is_c() { Strides::C } else { Strides::F };
StrideShape {
strides: st,
dim: shape.dim,
Expand Down Expand Up @@ -161,8 +175,10 @@ impl<D> Shape<D>
where
D: Dimension,
{
// Return a reference to the dimension
//pub fn dimension(&self) -> &D { &self.dim }
/// Return a reference to the dimension
pub fn raw_dim(&self) -> &D {
&self.dim
}
/// Return the size of the shape in number of elements
pub fn size(&self) -> usize {
self.dim.size()
Expand Down