@@ -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
1588impl < 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 {
0 commit comments