@@ -1885,10 +1885,84 @@ where
18851885 }
18861886 }
18871887
1888+ /// Transform the array into `shape`; any shape with the same number of
1889+ /// elements is accepted, but the source array must be contiguous.
1890+ ///
1891+ /// If an index ordering is not specified, the default is `RowMajor`.
1892+ /// The operation will only succeed if the array's memory layout is compatible with
1893+ /// the index ordering.
1894+ ///
1895+ /// Use `.to_shape()` instead for more flexible reshaping of arrays, which
1896+ /// allows copying elements if required.
1897+ ///
1898+ /// **Errors** if the shapes don't have the same number of elements.<br>
1899+ /// **Errors** if order RowMajor is given but input is not c-contiguous.
1900+ /// **Errors** if order ColumnMajor is given but input is not f-contiguous.
1901+ ///
1902+ /// If shape is not given: use memory layout of incoming array. Row major arrays are
1903+ /// reshaped using row major index ordering, column major arrays with column major index
1904+ /// ordering.
1905+ ///
1906+ /// ```
1907+ /// use ndarray::{aview1, aview2};
1908+ /// use ndarray::Order;
1909+ ///
1910+ /// assert!(
1911+ /// aview1(&[1., 2., 3., 4.]).into_shape_with_order((2, 2)).unwrap()
1912+ /// == aview2(&[[1., 2.],
1913+ /// [3., 4.]])
1914+ /// );
1915+ ///
1916+ /// assert!(
1917+ /// aview1(&[1., 2., 3., 4.]).into_shape_with_order(((2, 2), Order::ColumnMajor)).unwrap()
1918+ /// == aview2(&[[1., 3.],
1919+ /// [2., 4.]])
1920+ /// );
1921+ /// ```
1922+ pub fn into_shape_with_order < E > ( self , shape : E ) -> Result < ArrayBase < S , E :: Dim > , ShapeError >
1923+ where
1924+ E : ShapeArg ,
1925+ {
1926+ let ( shape, order) = shape. into_shape_and_order ( ) ;
1927+ self . into_shape_with_order_impl ( shape, order. unwrap_or ( Order :: RowMajor ) )
1928+ }
1929+
1930+ fn into_shape_with_order_impl < E > ( self , shape : E , order : Order )
1931+ -> Result < ArrayBase < S , E > , ShapeError >
1932+ where
1933+ E : Dimension ,
1934+ {
1935+ let shape = shape. into_dimension ( ) ;
1936+ if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
1937+ return Err ( error:: incompatible_shapes ( & self . dim , & shape) ) ;
1938+ }
1939+
1940+ // Check if contiguous, then we can change shape
1941+ unsafe {
1942+ // safe because arrays are contiguous and len is unchanged
1943+ match order {
1944+ Order :: RowMajor if self . is_standard_layout ( ) => {
1945+ Ok ( self . with_strides_dim ( shape. default_strides ( ) , shape) )
1946+ }
1947+ Order :: ColumnMajor if self . raw_view ( ) . reversed_axes ( ) . is_standard_layout ( ) => {
1948+ Ok ( self . with_strides_dim ( shape. fortran_strides ( ) , shape) )
1949+ }
1950+ _otherwise => Err ( error:: from_kind ( error:: ErrorKind :: IncompatibleLayout ) )
1951+ }
1952+ }
1953+ }
1954+
18881955 /// Transform the array into `shape`; any shape with the same number of
18891956 /// elements is accepted, but the source array or view must be in standard
18901957 /// or column-major (Fortran) layout.
18911958 ///
1959+ /// **Note** that `.into_shape()` "moves" elements differently depending on if the input array
1960+ /// is C-contig or F-contig, it follows the index order that corresponds to the memory order.
1961+ /// Prefer to use `.to_shape()` or `.into_shape_with_order()`.
1962+ ///
1963+ /// Because of this, the method is deprecated. That reshapes depend on memory order is not
1964+ /// intuitive.
1965+ ///
18921966 /// **Errors** if the shapes don't have the same number of elements.<br>
18931967 /// **Errors** if the input array is not c- or f-contiguous.
18941968 ///
@@ -1901,6 +1975,7 @@ where
19011975 /// [3., 4.]])
19021976 /// );
19031977 /// ```
1978+ #[ deprecated = "Use `.into_shape_with_order()` or `.to_shape()`" ]
19041979 pub fn into_shape < E > ( self , shape : E ) -> Result < ArrayBase < S , E :: Dim > , ShapeError >
19051980 where
19061981 E : IntoDimension ,
@@ -1948,7 +2023,7 @@ where
19482023 self . into_shape_clone_order ( shape, order)
19492024 }
19502025
1951- pub fn into_shape_clone_order < E > ( self , shape : E , order : Order )
2026+ fn into_shape_clone_order < E > ( self , shape : E , order : Order )
19522027 -> Result < ArrayBase < S , E > , ShapeError >
19532028 where
19542029 S : DataOwned ,
@@ -2020,7 +2095,7 @@ where
20202095 A : Clone ,
20212096 E : IntoDimension ,
20222097 {
2023- return self . clone ( ) . into_shape_clone ( shape) . unwrap ( ) ;
2098+ // return self.clone().into_shape_clone(shape).unwrap();
20242099 let shape = shape. into_dimension ( ) ;
20252100 if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
20262101 panic ! (
0 commit comments