Skip to content

Commit b123a5e

Browse files
committed
Add from_shape and deprecate from_dim_slice for ArrayView, ArrayViewMut
1 parent abb0afc commit b123a5e

File tree

4 files changed

+148
-106
lines changed

4 files changed

+148
-106
lines changed

src/impl_constructors.rs

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -15,106 +15,11 @@
1515
use libnum::{Zero, One, Float};
1616

1717
use imp_prelude::*;
18+
use {Shape, StrideShape};
1819
use dimension;
1920
use linspace;
2021
use error::{self, ShapeError, ErrorKind};
2122

22-
/// A contiguous array shape of n dimensions.
23-
///
24-
/// Either c- or f- memory ordered (*c* a.k.a *row major* is the default).
25-
#[derive(Copy, Clone, Debug)]
26-
pub struct Shape<D> {
27-
dim: D,
28-
is_c: bool,
29-
}
30-
31-
/// An array shape of n dimensions c-order, f-order or custom strides.
32-
#[derive(Copy, Clone, Debug)]
33-
pub struct StrideShape<D> {
34-
dim: D,
35-
strides: D,
36-
custom: bool,
37-
}
38-
39-
/// A trait for `Shape` and `D where D: Dimension` that allows
40-
/// customizing the memory layout (strides) of an array shape.
41-
pub trait ShapeBuilder {
42-
type Dim: Dimension;
43-
44-
fn f(self) -> Shape<Self::Dim>;
45-
fn set_f(self, is_f: bool) -> Shape<Self::Dim>;
46-
fn strides(self, strides: Self::Dim) -> StrideShape<Self::Dim>;
47-
}
48-
49-
impl<D> From<D> for Shape<D>
50-
where D: Dimension
51-
{
52-
fn from(d: D) -> Self {
53-
Shape {
54-
dim: d,
55-
is_c: true,
56-
}
57-
}
58-
}
59-
60-
impl<D> From<D> for StrideShape<D>
61-
where D: Dimension
62-
{
63-
fn from(d: D) -> Self {
64-
StrideShape {
65-
strides: d.default_strides(),
66-
dim: d,
67-
custom: false,
68-
}
69-
}
70-
}
71-
72-
impl<D> From<Shape<D>> for StrideShape<D>
73-
where D: Dimension
74-
{
75-
fn from(shape: Shape<D>) -> Self {
76-
let d = shape.dim;
77-
let st = if shape.is_c { d.default_strides() } else { d.fortran_strides() };
78-
StrideShape {
79-
strides: st,
80-
dim: d,
81-
custom: false,
82-
}
83-
}
84-
}
85-
86-
impl<D> ShapeBuilder for D
87-
where D: Dimension
88-
{
89-
type Dim = D;
90-
fn f(self) -> Shape<D> { self.set_f(true) }
91-
fn set_f(self, is_f: bool) -> Shape<D> {
92-
Shape::from(self).set_f(is_f)
93-
}
94-
fn strides(self, st: D) -> StrideShape<D> {
95-
Shape::from(self).strides(st)
96-
}
97-
}
98-
99-
impl<D> ShapeBuilder for Shape<D>
100-
where D: Dimension
101-
{
102-
type Dim = D;
103-
fn f(self) -> Self { self.set_f(true) }
104-
fn set_f(mut self, is_f: bool) -> Self {
105-
self.is_c = !is_f;
106-
self
107-
}
108-
fn strides(self, st: D) -> StrideShape<D> {
109-
StrideShape {
110-
dim: self.dim,
111-
strides: st,
112-
custom: true,
113-
}
114-
}
115-
}
116-
117-
11823
/// Constructor methods for one-dimensional arrays.
11924
///
12025
/// Note that the constructor methods apply to `OwnedArray` and `RcArray`,

src/impl_views.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use imp_prelude::*;
1010
use dimension::{self, stride_offset};
1111
use error::ShapeError;
1212

13+
use StrideShape;
14+
1315
/// # Methods for Array Views
1416
///
1517
/// Methods for read-only array views `ArrayView<'a, A, D>`
@@ -21,17 +23,17 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a A>, D>
2123
{
2224
/// Create a read-only array view borrowing its data from a slice.
2325
///
24-
/// Checks whether `dim` and `strides` are compatible with the slice's
26+
/// Checks whether `shape` are compatible with the slice's
2527
/// length, returning an `Err` if not compatible.
2628
///
2729
/// ```
2830
/// use ndarray::ArrayView;
2931
/// use ndarray::arr3;
32+
/// use ndarray::ShapeBuilder;
3033
///
3134
/// let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
32-
/// let a = ArrayView::from_slice_dim_stride((2, 3, 2),
33-
/// (1, 4, 2),
34-
/// &s).unwrap();
35+
/// let a = ArrayView::from_shape((2, 3, 2).strides((1, 4, 2)),
36+
/// &s).unwrap();
3537
///
3638
/// assert!(
3739
/// a == arr3(&[[[0, 2],
@@ -43,6 +45,22 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a A>, D>
4345
/// );
4446
/// assert!(a.strides() == &[1, 4, 2]);
4547
/// ```
48+
pub fn from_shape<Sh>(shape: Sh, xs: &'a [A])
49+
-> Result<Self, ShapeError>
50+
where Sh: Into<StrideShape<D>>,
51+
{
52+
let shape = shape.into();
53+
let dim = shape.dim;
54+
let strides = shape.strides;
55+
dimension::can_index_slice(xs, &dim, &strides).map(|_| {
56+
unsafe {
57+
Self::new_(xs.as_ptr(), dim, strides)
58+
}
59+
})
60+
}
61+
62+
#[cfg_attr(has_deprecated, deprecated(note="Use from_shape instead."))]
63+
/// ***Deprecated: Use from_shape instead***
4664
pub fn from_slice_dim_stride(dim: D, strides: D, xs: &'a [A])
4765
-> Result<Self, ShapeError>
4866
{
@@ -111,11 +129,11 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a mut A>, D>
111129
/// ```
112130
/// use ndarray::ArrayViewMut;
113131
/// use ndarray::arr3;
132+
/// use ndarray::ShapeBuilder;
114133
///
115134
/// let mut s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
116-
/// let mut a = ArrayViewMut::from_slice_dim_stride((2, 3, 2),
117-
/// (1, 4, 2),
118-
/// &mut s).unwrap();
135+
/// let mut a = ArrayViewMut::from_shape((2, 3, 2).strides((1, 4, 2)),
136+
/// &mut s).unwrap();
119137
///
120138
/// a[[0, 0, 0]] = 1;
121139
/// assert!(
@@ -128,6 +146,22 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a mut A>, D>
128146
/// );
129147
/// assert!(a.strides() == &[1, 4, 2]);
130148
/// ```
149+
pub fn from_shape<Sh>(shape: Sh, xs: &'a mut [A])
150+
-> Result<Self, ShapeError>
151+
where Sh: Into<StrideShape<D>>,
152+
{
153+
let shape = shape.into();
154+
let dim = shape.dim;
155+
let strides = shape.strides;
156+
dimension::can_index_slice(xs, &dim, &strides).map(|_| {
157+
unsafe {
158+
Self::new_(xs.as_mut_ptr(), dim, strides)
159+
}
160+
})
161+
}
162+
163+
#[cfg_attr(has_deprecated, deprecated(note="Use from_shape instead."))]
164+
/// ***Deprecated: Use from_shape instead***
131165
pub fn from_slice_dim_stride(dim: D, strides: D, xs: &'a mut [A])
132166
-> Result<Self, ShapeError>
133167
{

src/lib.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ pub use arraytraits::AsArray;
108108
pub use linalg_traits::{LinalgScalar, NdFloat};
109109
pub use stacking::stack;
110110

111-
pub use impl_constructors::{
112-
ShapeBuilder, Shape, StrideShape,
113-
};
111+
pub use shape_builder::{ ShapeBuilder };
114112

115113
mod arraytraits;
116114
#[cfg(feature = "serde")]
@@ -138,6 +136,7 @@ mod linspace;
138136
mod numeric_util;
139137
mod si;
140138
mod error;
139+
mod shape_builder;
141140
mod stacking;
142141

143142
/// Implementation's prelude. Common types used everywhere.
@@ -701,3 +700,21 @@ enum ElementsRepr<S, C> {
701700
Slice(S),
702701
Counted(C),
703702
}
703+
704+
705+
/// A contiguous array shape of n dimensions.
706+
///
707+
/// Either c- or f- memory ordered (*c* a.k.a *row major* is the default).
708+
#[derive(Copy, Clone, Debug)]
709+
pub struct Shape<D> {
710+
dim: D,
711+
is_c: bool,
712+
}
713+
714+
/// An array shape of n dimensions c-order, f-order or custom strides.
715+
#[derive(Copy, Clone, Debug)]
716+
pub struct StrideShape<D> {
717+
dim: D,
718+
strides: D,
719+
custom: bool,
720+
}

src/shape_builder.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
use Dimension;
3+
use {Shape, StrideShape};
4+
5+
/// A trait for `Shape` and `D where D: Dimension` that allows
6+
/// customizing the memory layout (strides) of an array shape.
7+
///
8+
/// This trait is used together with array constructor methods like
9+
/// `OwnedArray::from_shape_vec`.
10+
pub trait ShapeBuilder {
11+
type Dim: Dimension;
12+
13+
fn f(self) -> Shape<Self::Dim>;
14+
fn set_f(self, is_f: bool) -> Shape<Self::Dim>;
15+
fn strides(self, strides: Self::Dim) -> StrideShape<Self::Dim>;
16+
}
17+
18+
impl<D> From<D> for Shape<D>
19+
where D: Dimension
20+
{
21+
fn from(d: D) -> Self {
22+
Shape {
23+
dim: d,
24+
is_c: true,
25+
}
26+
}
27+
}
28+
29+
impl<D> From<D> for StrideShape<D>
30+
where D: Dimension
31+
{
32+
fn from(d: D) -> Self {
33+
StrideShape {
34+
strides: d.default_strides(),
35+
dim: d,
36+
custom: false,
37+
}
38+
}
39+
}
40+
41+
impl<D> From<Shape<D>> for StrideShape<D>
42+
where D: Dimension
43+
{
44+
fn from(shape: Shape<D>) -> Self {
45+
let d = shape.dim;
46+
let st = if shape.is_c { d.default_strides() } else { d.fortran_strides() };
47+
StrideShape {
48+
strides: st,
49+
dim: d,
50+
custom: false,
51+
}
52+
}
53+
}
54+
55+
impl<D> ShapeBuilder for D
56+
where D: Dimension
57+
{
58+
type Dim = D;
59+
fn f(self) -> Shape<D> { self.set_f(true) }
60+
fn set_f(self, is_f: bool) -> Shape<D> {
61+
Shape::from(self).set_f(is_f)
62+
}
63+
fn strides(self, st: D) -> StrideShape<D> {
64+
Shape::from(self).strides(st)
65+
}
66+
}
67+
68+
impl<D> ShapeBuilder for Shape<D>
69+
where D: Dimension
70+
{
71+
type Dim = D;
72+
fn f(self) -> Self { self.set_f(true) }
73+
fn set_f(mut self, is_f: bool) -> Self {
74+
self.is_c = !is_f;
75+
self
76+
}
77+
fn strides(self, st: D) -> StrideShape<D> {
78+
StrideShape {
79+
dim: self.dim,
80+
strides: st,
81+
custom: true,
82+
}
83+
}
84+
}
85+
86+

0 commit comments

Comments
 (0)