Skip to content

Commit 3888399

Browse files
committed
Adds missing aliases to ArrayBase from RawRef and LayoutRef
1 parent f77ad96 commit 3888399

File tree

3 files changed

+185
-22
lines changed

3 files changed

+185
-22
lines changed

src/alias_asref.rs

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,86 @@ use crate::{
55
AxisDescription,
66
Dimension,
77
LayoutRef,
8+
NdIndex,
89
RawArrayView,
910
RawData,
11+
RawDataMut,
1012
RawRef,
1113
Slice,
1214
SliceArg,
1315
};
1416

17+
/// Functions coming from RawRef
18+
impl<A, S: RawData<Elem = A>, D: Dimension> ArrayBase<S, D>
19+
{
20+
/// Return a raw pointer to the element at `index`, or return `None`
21+
/// if the index is out of bounds.
22+
///
23+
/// ```
24+
/// use ndarray::arr2;
25+
///
26+
/// let a = arr2(&[[1., 2.], [3., 4.]]);
27+
///
28+
/// let v = a.raw_view();
29+
/// let p = a.get_ptr((0, 1)).unwrap();
30+
///
31+
/// assert_eq!(unsafe { *p }, 2.);
32+
/// ```
33+
pub fn get_ptr<I>(&self, index: I) -> Option<*const A>
34+
where I: NdIndex<D>
35+
{
36+
<Self as AsRef<RawRef<_, _>>>::as_ref(self).get_ptr(index)
37+
}
38+
39+
/// Return a raw pointer to the element at `index`, or return `None`
40+
/// if the index is out of bounds.
41+
///
42+
/// ```
43+
/// use ndarray::arr2;
44+
///
45+
/// let mut a = arr2(&[[1., 2.], [3., 4.]]);
46+
///
47+
/// let v = a.raw_view_mut();
48+
/// let p = a.get_mut_ptr((0, 1)).unwrap();
49+
///
50+
/// unsafe {
51+
/// *p = 5.;
52+
/// }
53+
///
54+
/// assert_eq!(a.get((0, 1)), Some(&5.));
55+
/// ```
56+
pub fn get_mut_ptr<I>(&mut self, index: I) -> Option<*mut A>
57+
where
58+
S: RawDataMut<Elem = A>,
59+
I: NdIndex<D>,
60+
{
61+
<Self as AsMut<RawRef<_, _>>>::as_mut(self).get_mut_ptr(index)
62+
}
63+
64+
/// Return a pointer to the first element in the array.
65+
///
66+
/// Raw access to array elements needs to follow the strided indexing
67+
/// scheme: an element at multi-index *I* in an array with strides *S* is
68+
/// located at offset
69+
///
70+
/// *Σ<sub>0 ≤ k < d</sub> I<sub>k</sub> × S<sub>k</sub>*
71+
///
72+
/// where *d* is `self.ndim()`.
73+
#[inline(always)]
74+
pub fn as_ptr(&self) -> *const A
75+
{
76+
<Self as AsRef<RawRef<_, _>>>::as_ref(self).as_ptr()
77+
}
78+
79+
/// Return a raw view of the array.
80+
#[inline]
81+
pub fn raw_view(&self) -> RawArrayView<S::Elem, D>
82+
{
83+
<Self as AsRef<RawRef<_, _>>>::as_ref(self).raw_view()
84+
}
85+
}
86+
87+
/// Functions coming from LayoutRef
1588
impl<S: RawData, D: Dimension> ArrayBase<S, D>
1689
{
1790
/// Slice the array in place without changing the number of dimensions.
@@ -182,28 +255,6 @@ impl<S: RawData, D: Dimension> ArrayBase<S, D>
182255
self.as_mut().merge_axes(take, into)
183256
}
184257

185-
/// Return a raw view of the array.
186-
#[inline]
187-
pub fn raw_view(&self) -> RawArrayView<S::Elem, D>
188-
{
189-
<Self as AsRef<RawRef<_, _>>>::as_ref(self).raw_view()
190-
}
191-
192-
/// Return a pointer to the first element in the array.
193-
///
194-
/// Raw access to array elements needs to follow the strided indexing
195-
/// scheme: an element at multi-index *I* in an array with strides *S* is
196-
/// located at offset
197-
///
198-
/// *Σ<sub>0 ≤ k < d</sub> I<sub>k</sub> × S<sub>k</sub>*
199-
///
200-
/// where *d* is `self.ndim()`.
201-
#[inline(always)]
202-
pub fn as_ptr(&self) -> *const S::Elem
203-
{
204-
<Self as AsRef<RawRef<_, _>>>::as_ref(self).as_ptr()
205-
}
206-
207258
/// Return the total number of elements in the array.
208259
pub fn len(&self) -> usize
209260
{

src/impl_2d.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,70 @@ impl<A> LayoutRef<A, Ix2>
148148
m == n
149149
}
150150
}
151+
152+
impl<S: RawData> ArrayBase<S, Ix2>
153+
{
154+
/// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
155+
///
156+
/// ```
157+
/// use ndarray::{array, Axis};
158+
///
159+
/// let array = array![[1., 2.],
160+
/// [3., 4.],
161+
/// [5., 6.]];
162+
/// assert_eq!(array.nrows(), 3);
163+
///
164+
/// // equivalent ways of getting the dimensions
165+
/// // get nrows, ncols by using dim:
166+
/// let (m, n) = array.dim();
167+
/// assert_eq!(m, array.nrows());
168+
/// // get length of any particular axis with .len_of()
169+
/// assert_eq!(m, array.len_of(Axis(0)));
170+
/// ```
171+
pub fn nrows(&self) -> usize
172+
{
173+
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).nrows()
174+
}
175+
176+
/// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
177+
///
178+
/// ```
179+
/// use ndarray::{array, Axis};
180+
///
181+
/// let array = array![[1., 2.],
182+
/// [3., 4.],
183+
/// [5., 6.]];
184+
/// assert_eq!(array.ncols(), 2);
185+
///
186+
/// // equivalent ways of getting the dimensions
187+
/// // get nrows, ncols by using dim:
188+
/// let (m, n) = array.dim();
189+
/// assert_eq!(n, array.ncols());
190+
/// // get length of any particular axis with .len_of()
191+
/// assert_eq!(n, array.len_of(Axis(1)));
192+
/// ```
193+
pub fn ncols(&self) -> usize
194+
{
195+
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).ncols()
196+
}
197+
198+
/// Return true if the array is square, false otherwise.
199+
///
200+
/// # Examples
201+
/// Square:
202+
/// ```
203+
/// use ndarray::array;
204+
/// let array = array![[1., 2.], [3., 4.]];
205+
/// assert!(array.is_square());
206+
/// ```
207+
/// Not square:
208+
/// ```
209+
/// use ndarray::array;
210+
/// let array = array![[1., 2., 5.], [3., 4., 6.]];
211+
/// assert!(!array.is_square());
212+
/// ```
213+
pub fn is_square(&self) -> bool
214+
{
215+
<Self as AsRef<LayoutRef<_, _>>>::as_ref(self).is_square()
216+
}
217+
}

src/impl_dyn.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,51 @@ impl<A> LayoutRef<A, IxDyn>
5959
}
6060
}
6161

62+
impl<S: RawData> ArrayBase<S, IxDyn>
63+
{
64+
/// Insert new array axis of length 1 at `axis`, modifying the shape and
65+
/// strides in-place.
66+
///
67+
/// **Panics** if the axis is out of bounds.
68+
///
69+
/// ```
70+
/// use ndarray::{Axis, arr2, arr3};
71+
///
72+
/// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
73+
/// assert_eq!(a.shape(), &[2, 3]);
74+
///
75+
/// a.insert_axis_inplace(Axis(1));
76+
/// assert_eq!(a, arr3(&[[[1, 2, 3]], [[4, 5, 6]]]).into_dyn());
77+
/// assert_eq!(a.shape(), &[2, 1, 3]);
78+
/// ```
79+
#[track_caller]
80+
pub fn insert_axis_inplace(&mut self, axis: Axis)
81+
{
82+
self.as_mut().insert_axis_inplace(axis)
83+
}
84+
85+
/// Collapses the array to `index` along the axis and removes the axis,
86+
/// modifying the shape and strides in-place.
87+
///
88+
/// **Panics** if `axis` or `index` is out of bounds.
89+
///
90+
/// ```
91+
/// use ndarray::{Axis, arr1, arr2};
92+
///
93+
/// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
94+
/// assert_eq!(a.shape(), &[2, 3]);
95+
///
96+
/// a.index_axis_inplace(Axis(1), 1);
97+
/// assert_eq!(a, arr1(&[2, 5]).into_dyn());
98+
/// assert_eq!(a.shape(), &[2]);
99+
/// ```
100+
#[track_caller]
101+
pub fn index_axis_inplace(&mut self, axis: Axis, index: usize)
102+
{
103+
self.as_mut().index_axis_inplace(axis, index)
104+
}
105+
}
106+
62107
impl<A, S> ArrayBase<S, IxDyn>
63108
where S: Data<Elem = A>
64109
{

0 commit comments

Comments
 (0)