@@ -2017,8 +2017,6 @@ where
20172017 /// possible, otherwise they are copied to create a new array.
20182018 ///
20192019 /// If an index ordering is not specified, the default is `RowMajor`.
2020- /// The operation will only succeed if the array's memory layout is compatible with
2021- /// the index ordering, so that the array elements can be rearranged in place.
20222020 ///
20232021 /// # `.to_shape` vs `.into_shape_clone`
20242022 ///
@@ -2131,6 +2129,69 @@ where
21312129 }
21322130 }
21332131
2132+ /// Flatten the array to a one-dimensional array.
2133+ ///
2134+ /// The array is returned as a `CowArray`; a view if possible, otherwise an owned array.
2135+ ///
2136+ /// ```
2137+ /// use ndarray::{arr1, arr3};
2138+ ///
2139+ /// let array = arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
2140+ /// let flattened = array.flatten();
2141+ /// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2142+ /// ```
2143+ pub fn flatten ( & self ) -> CowArray < ' _ , A , Ix1 >
2144+ where
2145+ A : Clone ,
2146+ S : Data ,
2147+ {
2148+ self . flatten_with_order ( Order :: RowMajor )
2149+ }
2150+
2151+ /// Flatten the array to a one-dimensional array.
2152+ ///
2153+ /// `order` specifies the *logical* order in which the array is to be read and reshaped.
2154+ /// The array is returned as a `CowArray`; a view if possible, otherwise an owned array.
2155+ ///
2156+ /// ```
2157+ /// use ndarray::{arr1, arr2};
2158+ /// use ndarray::Order;
2159+ ///
2160+ /// let array = arr2(&[[1, 2], [3, 4], [5, 6], [7, 8]]);
2161+ /// let flattened = array.flatten_with_order(Order::RowMajor);
2162+ /// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2163+ /// let flattened = array.flatten_with_order(Order::ColumnMajor);
2164+ /// assert_eq!(flattened, arr1(&[1, 3, 5, 7, 2, 4, 6, 8]));
2165+ /// ```
2166+ pub fn flatten_with_order ( & self , order : Order ) -> CowArray < ' _ , A , Ix1 >
2167+ where
2168+ A : Clone ,
2169+ S : Data ,
2170+ {
2171+ self . to_shape ( ( self . len ( ) , order) ) . unwrap ( )
2172+ }
2173+
2174+ /// Flatten the array to a one-dimensional array, consuming the array.
2175+ ///
2176+ /// If possible, no copy is made, and the new array use the same memory as the original array.
2177+ /// Otherwise, a new array is allocated and the elements are copied.
2178+ ///
2179+ /// ```
2180+ /// use ndarray::{arr1, arr3};
2181+ ///
2182+ /// let array = arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
2183+ /// let flattened = array.into_flat();
2184+ /// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2185+ /// ```
2186+ pub fn into_flat ( self ) -> ArrayBase < S , Ix1 >
2187+ where
2188+ A : Clone ,
2189+ S : DataOwned ,
2190+ {
2191+ let len = self . len ( ) ;
2192+ self . into_shape_clone ( Ix1 ( len) ) . unwrap ( )
2193+ }
2194+
21342195 /// Convert any array or array view to a dynamic dimensional array or
21352196 /// array view (respectively).
21362197 ///
@@ -3065,3 +3126,36 @@ unsafe fn unlimited_transmute<A, B>(data: A) -> B
30653126}
30663127
30673128type DimMaxOf < A , B > = <A as DimMax < B > >:: Output ;
3129+
3130+ #[ cfg( test) ]
3131+ mod tests
3132+ {
3133+ use super :: * ;
3134+ use crate :: arr3;
3135+
3136+ #[ test]
3137+ fn test_flatten ( )
3138+ {
3139+ let array = arr3 ( & [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] ) ;
3140+ let flattened = array. flatten ( ) ;
3141+ assert_eq ! ( flattened, arr1( & [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ) ;
3142+ }
3143+
3144+ #[ test]
3145+ fn test_flatten_with_order ( )
3146+ {
3147+ let array = arr2 ( & [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] , [ 7 , 8 ] ] ) ;
3148+ let flattened = array. flatten_with_order ( Order :: RowMajor ) ;
3149+ assert_eq ! ( flattened, arr1( & [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ) ;
3150+ let flattened = array. flatten_with_order ( Order :: ColumnMajor ) ;
3151+ assert_eq ! ( flattened, arr1( & [ 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 ] ) ) ;
3152+ }
3153+
3154+ #[ test]
3155+ fn test_into_flat ( )
3156+ {
3157+ let array = arr3 ( & [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] ) ;
3158+ let flattened = array. into_flat ( ) ;
3159+ assert_eq ! ( flattened, arr1( & [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ) ;
3160+ }
3161+ }
0 commit comments