@@ -1694,6 +1694,34 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1694
1694
}
1695
1695
}
1696
1696
1697
+ /// Call `f` on a mutable reference of each element and create a new array
1698
+ /// with the new values.
1699
+ ///
1700
+ /// Elements are visited in arbitrary order.
1701
+ ///
1702
+ /// Return an array with the same shape as `self`.
1703
+ pub fn map_mut < ' a , B , F > ( & ' a mut self , f : F ) -> Array < B , D >
1704
+ where F : FnMut ( & ' a mut A ) -> B ,
1705
+ A : ' a ,
1706
+ S : DataMut
1707
+ {
1708
+ let dim = self . dim . clone ( ) ;
1709
+ if self . is_contiguous ( ) {
1710
+ let strides = self . strides . clone ( ) ;
1711
+ let slc = self . as_slice_memory_order_mut ( ) . unwrap ( ) ;
1712
+ let v = :: iterators:: to_vec_mapped ( slc. iter_mut ( ) , f) ;
1713
+ unsafe {
1714
+ ArrayBase :: from_shape_vec_unchecked (
1715
+ dim. strides ( strides) , v)
1716
+ }
1717
+ } else {
1718
+ let v = :: iterators:: to_vec_mapped ( self . iter_mut ( ) , f) ;
1719
+ unsafe {
1720
+ ArrayBase :: from_shape_vec_unchecked ( dim, v)
1721
+ }
1722
+ }
1723
+ }
1724
+
1697
1725
/// Call `f` by **v**alue on each element and create a new array
1698
1726
/// with the new values.
1699
1727
///
@@ -1819,4 +1847,32 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1819
1847
}
1820
1848
} )
1821
1849
}
1850
+
1851
+ /// Reduce the values along an axis into just one value, producing a new
1852
+ /// array with one less dimension.
1853
+ /// 1-dimensional lanes are passed as mutable references to the reducer,
1854
+ /// allowing for side-effects.
1855
+ ///
1856
+ /// Elements are visited in arbitrary order.
1857
+ ///
1858
+ /// Return the result as an `Array`.
1859
+ ///
1860
+ /// **Panics** if `axis` is out of bounds.
1861
+ pub fn map_axis_mut < ' a , B , F > ( & ' a mut self , axis : Axis , mut mapping : F )
1862
+ -> Array < B , D :: Smaller >
1863
+ where D : RemoveAxis ,
1864
+ F : FnMut ( ArrayViewMut1 < ' a , A > ) -> B ,
1865
+ A : ' a ,
1866
+ S : DataMut ,
1867
+ {
1868
+ let view_len = self . len_of ( axis) ;
1869
+ let view_stride = self . strides . axis ( axis) ;
1870
+ // use the 0th subview as a map to each 1d array view extended from
1871
+ // the 0th element.
1872
+ self . subview_mut ( axis, 0 ) . map_mut ( |first_elt : & mut A | {
1873
+ unsafe {
1874
+ mapping ( ArrayViewMut :: new_ ( first_elt, Ix1 ( view_len) , Ix1 ( view_stride) ) )
1875
+ }
1876
+ } )
1877
+ }
1822
1878
}
0 commit comments