@@ -18,8 +18,8 @@ use crate::arraytraits;
1818use crate :: dimension;
1919use crate :: dimension:: IntoDimension ;
2020use 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 ,
2323} ;
2424use crate :: error:: { self , ErrorKind , ShapeError } ;
2525use crate :: math_cell:: MathCell ;
@@ -1448,20 +1448,29 @@ where
14481448 /// Return the array’s data as a slice if it is contiguous,
14491449 /// return `None` otherwise.
14501450 pub fn as_slice_memory_order_mut ( & mut self ) -> Option < & mut [ A ] >
1451+ where
1452+ S : DataMut ,
1453+ {
1454+ self . try_as_slice_memory_order_mut ( ) . ok ( )
1455+ }
1456+
1457+ /// Return the array’s data as a slice if it is contiguous, otherwise
1458+ /// return `self` in the `Err` variant.
1459+ pub ( crate ) fn try_as_slice_memory_order_mut ( & mut self ) -> Result < & mut [ A ] , & mut Self >
14511460 where
14521461 S : DataMut ,
14531462 {
14541463 if self . is_contiguous ( ) {
14551464 self . ensure_unique ( ) ;
14561465 let offset = offset_from_ptr_to_memory ( & self . dim , & self . strides ) ;
14571466 unsafe {
1458- Some ( slice:: from_raw_parts_mut (
1467+ Ok ( slice:: from_raw_parts_mut (
14591468 self . ptr . offset ( offset) . as_ptr ( ) ,
14601469 self . len ( ) ,
14611470 ) )
14621471 }
14631472 } else {
1464- None
1473+ Err ( self )
14651474 }
14661475 }
14671476
@@ -1943,7 +1952,7 @@ where
19431952 S : DataMut ,
19441953 A : Clone ,
19451954 {
1946- self . unordered_foreach_mut ( move |elt| * elt = x. clone ( ) ) ;
1955+ self . map_inplace ( move |elt| * elt = x. clone ( ) ) ;
19471956 }
19481957
19491958 fn zip_mut_with_same_shape < B , S2 , E , F > ( & mut self , rhs : & ArrayBase < S2 , E > , mut f : F )
@@ -1995,7 +2004,7 @@ where
19952004 S : DataMut ,
19962005 F : FnMut ( & mut A , & B ) ,
19972006 {
1998- self . unordered_foreach_mut ( move |elt| f ( elt, rhs_elem) ) ;
2007+ self . map_inplace ( move |elt| f ( elt, rhs_elem) ) ;
19992008 }
20002009
20012010 /// Traverse two arrays in unspecified order, in lock step,
@@ -2037,27 +2046,7 @@ where
20372046 slc. iter ( ) . fold ( init, f)
20382047 } else {
20392048 let mut v = self . view ( ) ;
2040- // put the narrowest axis at the last position
2041- match v. ndim ( ) {
2042- 0 | 1 => { }
2043- 2 => {
2044- if self . len_of ( Axis ( 1 ) ) <= 1
2045- || self . len_of ( Axis ( 0 ) ) > 1
2046- && self . stride_of ( Axis ( 0 ) ) . abs ( ) < self . stride_of ( Axis ( 1 ) ) . abs ( )
2047- {
2048- v. swap_axes ( 0 , 1 ) ;
2049- }
2050- }
2051- n => {
2052- let last = n - 1 ;
2053- let narrow_axis = v
2054- . axes ( )
2055- . filter ( |ax| ax. len ( ) > 1 )
2056- . min_by_key ( |ax| ax. stride ( ) . abs ( ) )
2057- . map_or ( last, |ax| ax. axis ( ) . index ( ) ) ;
2058- v. swap_axes ( last, narrow_axis) ;
2059- }
2060- }
2049+ move_min_stride_axis_to_last ( & mut v. dim , & mut v. strides ) ;
20612050 v. into_elements_base ( ) . fold ( init, f)
20622051 }
20632052 }
@@ -2167,12 +2156,20 @@ where
21672156 /// Modify the array in place by calling `f` by mutable reference on each element.
21682157 ///
21692158 /// Elements are visited in arbitrary order.
2170- pub fn map_inplace < F > ( & mut self , f : F )
2159+ pub fn map_inplace < ' a , F > ( & ' a mut self , f : F )
21712160 where
21722161 S : DataMut ,
2173- F : FnMut ( & mut A ) ,
2174- {
2175- self . unordered_foreach_mut ( f) ;
2162+ A : ' a ,
2163+ F : FnMut ( & ' a mut A ) ,
2164+ {
2165+ match self . try_as_slice_memory_order_mut ( ) {
2166+ Ok ( slc) => slc. iter_mut ( ) . for_each ( f) ,
2167+ Err ( arr) => {
2168+ let mut v = arr. view_mut ( ) ;
2169+ move_min_stride_axis_to_last ( & mut v. dim , & mut v. strides ) ;
2170+ v. into_elements_base ( ) . for_each ( f) ;
2171+ }
2172+ }
21762173 }
21772174
21782175 /// Modify the array in place by calling `f` by **v**alue on each element.
@@ -2202,7 +2199,7 @@ where
22022199 F : FnMut ( A ) -> A ,
22032200 A : Clone ,
22042201 {
2205- self . unordered_foreach_mut ( move |x| * x = f ( x. clone ( ) ) ) ;
2202+ self . map_inplace ( move |x| * x = f ( x. clone ( ) ) ) ;
22062203 }
22072204
22082205 /// Call `f` for each element in the array.
0 commit comments