@@ -1885,11 +1885,19 @@ where
1885
1885
}
1886
1886
1887
1887
/// Transform the array into `shape`; any shape with the same number of
1888
- /// elements is accepted, but the source array or view must be in standard
1889
- /// or column-major (Fortran) layout.
1888
+ /// elements is accepted, but the source array must be in contiguous row-major (C) or
1889
+ /// column-major (F) layout.
1890
+ ///
1891
+ /// If a memory ordering is specified (optional) in the shape argument, the operation
1892
+ /// will only succeed if the input has this memory order.
1890
1893
///
1891
1894
/// **Errors** if the shapes don't have the same number of elements.<br>
1892
- /// **Errors** if the input array is not c- or f-contiguous.
1895
+ /// **Errors** if the input array is not c- or f-contiguous.<br>
1896
+ /// **Errors** if a memory ordering is requested that is not compatible with the array.<br>
1897
+ ///
1898
+ /// If shape is not given: use memory layout of incoming array. Row major arrays are
1899
+ /// reshaped using row major index ordering, column major arrays with column major index
1900
+ /// ordering.
1893
1901
///
1894
1902
/// ```
1895
1903
/// use ndarray::{aview1, aview2};
@@ -1899,10 +1907,24 @@ where
1899
1907
/// == aview2(&[[1., 2.],
1900
1908
/// [3., 4.]])
1901
1909
/// );
1910
+ ///
1911
+ /// assert!(
1912
+ /// aview1(&[1., 2., 3., 4.]).into_shape(((2, 2), Order::ColumnMajor)).unwrap()
1913
+ /// == aview2(&[[1., 3.],
1914
+ /// [2., 4.]])
1915
+ /// );
1902
1916
/// ```
1903
1917
pub fn into_shape < E > ( self , shape : E ) -> Result < ArrayBase < S , E :: Dim > , ShapeError >
1904
1918
where
1905
- E : IntoDimension ,
1919
+ E : ShapeArg ,
1920
+ {
1921
+ let ( shape, order) = shape. into_shape_and_order ( ) ;
1922
+ self . into_shape_order ( shape, order)
1923
+ }
1924
+
1925
+ fn into_shape_order < E > ( self , shape : E , order : Option < Order > ) -> Result < ArrayBase < S , E > , ShapeError >
1926
+ where
1927
+ E : Dimension ,
1906
1928
{
1907
1929
let shape = shape. into_dimension ( ) ;
1908
1930
if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
@@ -1911,12 +1933,14 @@ where
1911
1933
// Check if contiguous, if not => copy all, else just adapt strides
1912
1934
unsafe {
1913
1935
// safe because arrays are contiguous and len is unchanged
1914
- if self . is_standard_layout ( ) {
1915
- Ok ( self . with_strides_dim ( shape. default_strides ( ) , shape) )
1916
- } else if self . ndim ( ) > 1 && self . raw_view ( ) . reversed_axes ( ) . is_standard_layout ( ) {
1917
- Ok ( self . with_strides_dim ( shape. fortran_strides ( ) , shape) )
1918
- } else {
1919
- Err ( error:: from_kind ( error:: ErrorKind :: IncompatibleLayout ) )
1936
+ match order {
1937
+ None | Some ( Order :: RowMajor ) if self . is_standard_layout ( ) => {
1938
+ Ok ( self . with_strides_dim ( shape. default_strides ( ) , shape) )
1939
+ }
1940
+ None | Some ( Order :: ColumnMajor ) if self . raw_view ( ) . reversed_axes ( ) . is_standard_layout ( ) => {
1941
+ Ok ( self . with_strides_dim ( shape. fortran_strides ( ) , shape) )
1942
+ }
1943
+ _otherwise => Err ( error:: from_kind ( error:: ErrorKind :: IncompatibleLayout ) )
1920
1944
}
1921
1945
}
1922
1946
}
@@ -1932,7 +1956,7 @@ where
1932
1956
self . into_shape_clone_order ( shape, order)
1933
1957
}
1934
1958
1935
- pub fn into_shape_clone_order < E > ( self , shape : E , order : Order )
1959
+ fn into_shape_clone_order < E > ( self , shape : E , order : Order )
1936
1960
-> Result < ArrayBase < S , E > , ShapeError >
1937
1961
where
1938
1962
S : DataOwned ,
@@ -2004,7 +2028,7 @@ where
2004
2028
A : Clone ,
2005
2029
E : IntoDimension ,
2006
2030
{
2007
- return self . clone ( ) . into_shape_clone ( shape) . unwrap ( ) ;
2031
+ // return self.clone().into_shape_clone(shape).unwrap();
2008
2032
let shape = shape. into_dimension ( ) ;
2009
2033
if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
2010
2034
panic ! (
0 commit comments