@@ -18,8 +18,8 @@ use crate::arraytraits;
18
18
use crate :: dimension;
19
19
use crate :: dimension:: IntoDimension ;
20
20
use crate :: dimension:: {
21
- abs_index, axes_of, do_slice, merge_axes, offset_from_ptr_to_memory , size_of_shape_checked ,
22
- stride_offset, Axes ,
21
+ abs_index, axes_of, do_slice, merge_axes, move_min_stride_axis_to_last ,
22
+ offset_from_ptr_to_memory , size_of_shape_checked , stride_offset, Axes ,
23
23
} ;
24
24
use crate :: error:: { self , ErrorKind , ShapeError } ;
25
25
use crate :: math_cell:: MathCell ;
@@ -1456,20 +1456,29 @@ where
1456
1456
/// Return the array’s data as a slice if it is contiguous,
1457
1457
/// return `None` otherwise.
1458
1458
pub fn as_slice_memory_order_mut ( & mut self ) -> Option < & mut [ A ] >
1459
+ where
1460
+ S : DataMut ,
1461
+ {
1462
+ self . try_as_slice_memory_order_mut ( ) . ok ( )
1463
+ }
1464
+
1465
+ /// Return the array’s data as a slice if it is contiguous, otherwise
1466
+ /// return `self` in the `Err` variant.
1467
+ pub ( crate ) fn try_as_slice_memory_order_mut ( & mut self ) -> Result < & mut [ A ] , & mut Self >
1459
1468
where
1460
1469
S : DataMut ,
1461
1470
{
1462
1471
if self . is_contiguous ( ) {
1463
1472
self . ensure_unique ( ) ;
1464
1473
let offset = offset_from_ptr_to_memory ( & self . dim , & self . strides ) ;
1465
1474
unsafe {
1466
- Some ( slice:: from_raw_parts_mut (
1475
+ Ok ( slice:: from_raw_parts_mut (
1467
1476
self . ptr . offset ( offset) . as_ptr ( ) ,
1468
1477
self . len ( ) ,
1469
1478
) )
1470
1479
}
1471
1480
} else {
1472
- None
1481
+ Err ( self )
1473
1482
}
1474
1483
}
1475
1484
@@ -2070,27 +2079,7 @@ where
2070
2079
slc. iter ( ) . fold ( init, f)
2071
2080
} else {
2072
2081
let mut v = self . view ( ) ;
2073
- // put the narrowest axis at the last position
2074
- match v. ndim ( ) {
2075
- 0 | 1 => { }
2076
- 2 => {
2077
- if self . len_of ( Axis ( 1 ) ) <= 1
2078
- || self . len_of ( Axis ( 0 ) ) > 1
2079
- && self . stride_of ( Axis ( 0 ) ) . abs ( ) < self . stride_of ( Axis ( 1 ) ) . abs ( )
2080
- {
2081
- v. swap_axes ( 0 , 1 ) ;
2082
- }
2083
- }
2084
- n => {
2085
- let last = n - 1 ;
2086
- let narrow_axis = v
2087
- . axes ( )
2088
- . filter ( |ax| ax. len ( ) > 1 )
2089
- . min_by_key ( |ax| ax. stride ( ) . abs ( ) )
2090
- . map_or ( last, |ax| ax. axis ( ) . index ( ) ) ;
2091
- v. swap_axes ( last, narrow_axis) ;
2092
- }
2093
- }
2082
+ move_min_stride_axis_to_last ( & mut v. dim , & mut v. strides ) ;
2094
2083
v. into_elements_base ( ) . fold ( init, f)
2095
2084
}
2096
2085
}
@@ -2200,12 +2189,20 @@ where
2200
2189
/// Modify the array in place by calling `f` by mutable reference on each element.
2201
2190
///
2202
2191
/// Elements are visited in arbitrary order.
2203
- pub fn map_inplace < F > ( & mut self , f : F )
2192
+ pub fn map_inplace < ' a , F > ( & ' a mut self , f : F )
2204
2193
where
2205
2194
S : DataMut ,
2206
- F : FnMut ( & mut A ) ,
2207
- {
2208
- self . unordered_foreach_mut ( f) ;
2195
+ A : ' a ,
2196
+ F : FnMut ( & ' a mut A ) ,
2197
+ {
2198
+ match self . try_as_slice_memory_order_mut ( ) {
2199
+ Ok ( slc) => slc. iter_mut ( ) . for_each ( f) ,
2200
+ Err ( arr) => {
2201
+ let mut v = arr. view_mut ( ) ;
2202
+ move_min_stride_axis_to_last ( & mut v. dim , & mut v. strides ) ;
2203
+ v. into_elements_base ( ) . for_each ( f) ;
2204
+ }
2205
+ }
2209
2206
}
2210
2207
2211
2208
/// Modify the array in place by calling `f` by **v**alue on each element.
0 commit comments