Skip to content

Commit b94faf7

Browse files
barakugavbluss
authored andcommitted
Add flatten methods
1 parent 7538644 commit b94faf7

File tree

1 file changed

+92
-2
lines changed

1 file changed

+92
-2
lines changed

src/impl_methods.rs

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,32 @@ unsafe fn unlimited_transmute<A, B>(data: A) -> B
30653126
}
30663127

30673128
type DimMaxOf<A, B> = <A as DimMax<B>>::Output;
3129+
3130+
#[cfg(test)]
3131+
mod tests {
3132+
use super::*;
3133+
use crate::arr3;
3134+
3135+
#[test]
3136+
fn test_flatten() {
3137+
let array = arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
3138+
let flattened = array.flatten();
3139+
assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
3140+
}
3141+
3142+
#[test]
3143+
fn test_flatten_with_order() {
3144+
let array = arr2(&[[1, 2], [3, 4], [5, 6], [7, 8]]);
3145+
let flattened = array.flatten_with_order(Order::RowMajor);
3146+
assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
3147+
let flattened = array.flatten_with_order(Order::ColumnMajor);
3148+
assert_eq!(flattened, arr1(&[1, 3, 5, 7, 2, 4, 6, 8]));
3149+
}
3150+
3151+
#[test]
3152+
fn test_into_flat() {
3153+
let array = arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
3154+
let flattened = array.into_flat();
3155+
assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
3156+
}
3157+
}

0 commit comments

Comments
 (0)