@@ -2131,6 +2131,77 @@ where
21312131 }
21322132 }
21332133
2134+ /// Flatten the array to a one-dimensional array.
2135+ ///
2136+ /// The array is returned as a `CowArray`; a view if possible, otherwise an owned array.
2137+ ///
2138+ /// ```
2139+ /// use ndarray::{arr1, arr3};
2140+ ///
2141+ /// let array = arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
2142+ /// let flattened = array.flatten();
2143+ /// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2144+ /// ```
2145+ pub fn flatten ( & self ) -> CowArray < ' _ , A , Ix1 >
2146+ where
2147+ A : Clone ,
2148+ S : Data ,
2149+ {
2150+ // let array = crate::arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
2151+ // let flattened = array.flatten();
2152+ // assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2153+
2154+ self . flatten_with_order ( Order :: RowMajor )
2155+ }
2156+
2157+ /// Flatten the array to a one-dimensional array.
2158+ ///
2159+ /// `order` specifies the *logical* order in which the array is to be read and reshaped.
2160+ /// The array is returned as a `CowArray`; a view if possible, otherwise an owned array.
2161+ ///
2162+ /// ```
2163+ /// use ndarray::{arr1, arr2};
2164+ /// use ndarray::order::Order;
2165+ ///
2166+ /// let array = arr2(&[[1, 2], [3, 4], [5, 6], [7, 8]]);
2167+ /// let flattened = array.flatten_with_order(Order::RowMajor);
2168+ /// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2169+ /// let flattened = array.flatten_with_order(Order::ColumnMajor);
2170+ /// assert_eq!(flattened, arr1(&[1, 3, 5, 7, 2, 4, 6, 8]));
2171+ /// ```
2172+ pub fn flatten_with_order ( & self , order : Order ) -> CowArray < ' _ , A , Ix1 >
2173+ where
2174+ A : Clone ,
2175+ S : Data ,
2176+ {
2177+ self . to_shape ( ( self . len ( ) , order) ) . unwrap ( )
2178+ }
2179+
2180+ /// Flatten the array to a one-dimensional array, consuming the array.
2181+ ///
2182+ /// If possible, no copy is made, and the new array use the same memory as the original array.
2183+ /// Otherwise, a new array is allocated and the elements are copied.
2184+ ///
2185+ /// ```
2186+ /// use ndarray::{arr1, arr3};
2187+ ///
2188+ /// let array = arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
2189+ /// let flattened = array.into_flat();
2190+ /// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2191+ /// ```
2192+ pub fn into_flat ( self ) -> ArrayBase < S , Ix1 >
2193+ where
2194+ A : Clone ,
2195+ S : DataOwned ,
2196+ {
2197+ let len = self . len ( ) ;
2198+ if self . is_standard_layout ( ) {
2199+ self . into_shape_with_order ( len) . unwrap ( )
2200+ } else {
2201+ ArrayBase :: from_shape_vec ( len, self . iter ( ) . cloned ( ) . collect ( ) ) . unwrap ( )
2202+ }
2203+ }
2204+
21342205 /// Convert any array or array view to a dynamic dimensional array or
21352206 /// array view (respectively).
21362207 ///
@@ -3065,3 +3136,32 @@ unsafe fn unlimited_transmute<A, B>(data: A) -> B
30653136}
30663137
30673138type DimMaxOf < A , B > = <A as DimMax < B > >:: Output ;
3139+
3140+ #[ cfg( test) ]
3141+ mod tests {
3142+ use super :: * ;
3143+ use crate :: arr3;
3144+
3145+ #[ test]
3146+ fn test_flatten ( ) {
3147+ let array = arr3 ( & [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] ) ;
3148+ let flattened = array. flatten ( ) ;
3149+ assert_eq ! ( flattened, arr1( & [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ) ;
3150+ }
3151+
3152+ #[ test]
3153+ fn test_flatten_with_order ( ) {
3154+ let array = arr2 ( & [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] , [ 7 , 8 ] ] ) ;
3155+ let flattened = array. flatten_with_order ( Order :: RowMajor ) ;
3156+ assert_eq ! ( flattened, arr1( & [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ) ;
3157+ let flattened = array. flatten_with_order ( Order :: ColumnMajor ) ;
3158+ assert_eq ! ( flattened, arr1( & [ 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 ] ) ) ;
3159+ }
3160+
3161+ #[ test]
3162+ fn test_into_flat ( ) {
3163+ let array = arr3 ( & [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 5 , 6 ] , [ 7 , 8 ] ] ] ) ;
3164+ let flattened = array. into_flat ( ) ;
3165+ assert_eq ! ( flattened, arr1( & [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) ) ;
3166+ }
3167+ }
0 commit comments