9
9
use std:: slice;
10
10
11
11
use imp_prelude:: * ;
12
- use dimension:: { self , stride_offset } ;
12
+ use dimension;
13
13
use error:: ShapeError ;
14
14
use arraytraits:: array_out_of_bounds;
15
15
use { is_aligned, NdIndex , StrideShape } ;
@@ -111,15 +111,7 @@ impl<'a, A, D> ArrayView<'a, A, D>
111
111
pub unsafe fn from_shape_ptr < Sh > ( shape : Sh , ptr : * const A ) -> Self
112
112
where Sh : Into < StrideShape < D > >
113
113
{
114
- let shape = shape. into ( ) ;
115
- let dim = shape. dim ;
116
- let strides = shape. strides ;
117
- if cfg ! ( debug_assertions) {
118
- assert ! ( !ptr. is_null( ) , "The pointer must be non-null." ) ;
119
- assert ! ( is_aligned( ptr) , "The pointer must be aligned." ) ;
120
- dimension:: max_abs_offset_check_overflow :: < A , _ > ( & dim, & strides) . unwrap ( ) ;
121
- }
122
- ArrayView :: new_ ( ptr, dim, strides)
114
+ RawArrayView :: from_shape_ptr ( shape, ptr) . deref_into_view ( )
123
115
}
124
116
125
117
/// Convert the view into an `ArrayView<'b, A, D>` where `'b` is a lifetime
@@ -141,35 +133,11 @@ impl<'a, A, D> ArrayView<'a, A, D>
141
133
/// an array with shape 3 × 5 × 5.
142
134
///
143
135
/// <img src="https://rust-ndarray.github.io/ndarray/images/split_at.svg" width="300px" height="271px">
144
- pub fn split_at ( self , axis : Axis , index : Ix )
145
- -> ( Self , Self )
146
- {
147
- // NOTE: Keep this in sync with the ArrayViewMut version
148
- assert ! ( index <= self . len_of( axis) ) ;
149
- let left_ptr = self . ptr ;
150
- let right_ptr = if index == self . len_of ( axis) {
151
- self . ptr
152
- } else {
153
- let offset = stride_offset ( index, self . strides . axis ( axis) ) ;
154
- unsafe {
155
- self . ptr . offset ( offset)
156
- }
157
- } ;
158
-
159
- let mut dim_left = self . dim . clone ( ) ;
160
- dim_left. set_axis ( axis, index) ;
161
- let left = unsafe {
162
- Self :: new_ ( left_ptr, dim_left, self . strides . clone ( ) )
163
- } ;
164
-
165
- let mut dim_right = self . dim ;
166
- let right_len = dim_right. axis ( axis) - index;
167
- dim_right. set_axis ( axis, right_len) ;
168
- let right = unsafe {
169
- Self :: new_ ( right_ptr, dim_right, self . strides )
170
- } ;
171
-
172
- ( left, right)
136
+ pub fn split_at ( self , axis : Axis , index : Ix ) -> ( Self , Self ) {
137
+ unsafe {
138
+ let ( left, right) = self . into_raw_view ( ) . split_at ( axis, index) ;
139
+ ( left. deref_into_view ( ) , right. deref_into_view ( ) )
140
+ }
173
141
}
174
142
175
143
/// Return the array’s data as a slice, if it is contiguous and in standard order.
@@ -183,6 +151,11 @@ impl<'a, A, D> ArrayView<'a, A, D>
183
151
None
184
152
}
185
153
}
154
+
155
+ /// Converts to a raw array view.
156
+ pub ( crate ) fn into_raw_view ( self ) -> RawArrayView < A , D > {
157
+ unsafe { RawArrayView :: new_ ( self . ptr , self . dim , self . strides ) }
158
+ }
186
159
}
187
160
188
161
@@ -408,15 +381,7 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
408
381
pub unsafe fn from_shape_ptr < Sh > ( shape : Sh , ptr : * mut A ) -> Self
409
382
where Sh : Into < StrideShape < D > >
410
383
{
411
- let shape = shape. into ( ) ;
412
- let dim = shape. dim ;
413
- let strides = shape. strides ;
414
- if cfg ! ( debug_assertions) {
415
- assert ! ( !ptr. is_null( ) , "The pointer must be non-null." ) ;
416
- assert ! ( is_aligned( ptr) , "The pointer must be aligned." ) ;
417
- dimension:: max_abs_offset_check_overflow :: < A , _ > ( & dim, & strides) . unwrap ( ) ;
418
- }
419
- ArrayViewMut :: new_ ( ptr, dim, strides)
384
+ RawArrayViewMut :: from_shape_ptr ( shape, ptr) . deref_into_view_mut ( )
420
385
}
421
386
422
387
/// Convert the view into an `ArrayViewMut<'b, A, D>` where `'b` is a lifetime
@@ -433,35 +398,11 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
433
398
/// before the split and one mutable view after the split.
434
399
///
435
400
/// **Panics** if `axis` or `index` is out of bounds.
436
- pub fn split_at ( self , axis : Axis , index : Ix )
437
- -> ( Self , Self )
438
- {
439
- // NOTE: Keep this in sync with the ArrayView version
440
- assert ! ( index <= self . len_of( axis) ) ;
441
- let left_ptr = self . ptr ;
442
- let right_ptr = if index == self . len_of ( axis) {
443
- self . ptr
444
- } else {
445
- let offset = stride_offset ( index, self . strides . axis ( axis) ) ;
446
- unsafe {
447
- self . ptr . offset ( offset)
448
- }
449
- } ;
450
-
451
- let mut dim_left = self . dim . clone ( ) ;
452
- dim_left. set_axis ( axis, index) ;
453
- let left = unsafe {
454
- Self :: new_ ( left_ptr, dim_left, self . strides . clone ( ) )
455
- } ;
456
-
457
- let mut dim_right = self . dim ;
458
- let right_len = dim_right. axis ( axis) - index;
459
- dim_right. set_axis ( axis, right_len) ;
460
- let right = unsafe {
461
- Self :: new_ ( right_ptr, dim_right, self . strides )
462
- } ;
463
-
464
- ( left, right)
401
+ pub fn split_at ( self , axis : Axis , index : Ix ) -> ( Self , Self ) {
402
+ unsafe {
403
+ let ( left, right) = self . into_raw_view_mut ( ) . split_at ( axis, index) ;
404
+ ( left. deref_into_view_mut ( ) , right. deref_into_view_mut ( ) )
405
+ }
465
406
}
466
407
467
408
/// Return the array’s data as a slice, if it is contiguous and in standard order.
@@ -605,6 +546,11 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
605
546
}
606
547
}
607
548
549
+ /// Converts to a mutable raw array view.
550
+ pub ( crate ) fn into_raw_view_mut ( self ) -> RawArrayViewMut < A , D > {
551
+ unsafe { RawArrayViewMut :: new_ ( self . ptr , self . dim , self . strides ) }
552
+ }
553
+
608
554
#[ inline]
609
555
pub ( crate ) fn into_base_iter ( self ) -> Baseiter < A , D > {
610
556
unsafe {
0 commit comments