Skip to content

Commit 692fae5

Browse files
committed
Adds 'flatten()' method to array
1 parent 7538644 commit 692fae5

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/impl_methods.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,73 @@ 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+
self.flatten_with_order(Order::RowMajor)
2151+
}
2152+
2153+
/// Flatten the array to a one-dimensional array.
2154+
///
2155+
/// `order` specifies the *logical* order in which the array is to be read and reshaped.
2156+
/// The array is returned as a `CowArray`; a view if possible, otherwise an owned array.
2157+
///
2158+
/// ```
2159+
/// use ndarray::{arr1, arr2};
2160+
/// use ndarray::order::Order;
2161+
///
2162+
/// let array = arr2(&[[1, 2], [3, 4], [5, 6], [7, 8]]);
2163+
/// let flattened = array.flatten_with_order(Order::RowMajor);
2164+
/// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2165+
/// let flattened = array.flatten_with_order(Order::ColumnMajor);
2166+
/// assert_eq!(flattened, arr1(&[1, 3, 5, 7, 2, 4, 6, 8]));
2167+
/// ```
2168+
pub fn flatten_with_order(&self, order: Order) -> CowArray<'_, A, Ix1>
2169+
where
2170+
A: Clone,
2171+
S: Data,
2172+
{
2173+
self.to_shape((self.len(), order)).unwrap()
2174+
}
2175+
2176+
/// Flatten the array to a one-dimensional array, consuming the array.
2177+
///
2178+
/// If possible, no copy is made, and the new array use the same memory as the original array.
2179+
/// Otherwise, a new array is allocated and the elements are copied.
2180+
///
2181+
/// ```
2182+
/// use ndarray::{arr1, arr3};
2183+
///
2184+
/// let array = arr3(&[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);
2185+
/// let flattened = array.into_flat();
2186+
/// assert_eq!(flattened, arr1(&[1, 2, 3, 4, 5, 6, 7, 8]));
2187+
/// ```
2188+
pub fn into_flat(self) -> ArrayBase<S, Ix1>
2189+
where
2190+
A: Clone,
2191+
S: DataOwned,
2192+
{
2193+
let len = self.len();
2194+
if self.is_standard_layout() {
2195+
self.into_shape_with_order(len).unwrap()
2196+
} else {
2197+
ArrayBase::from_shape_vec(len, self.iter().cloned().collect()).unwrap()
2198+
}
2199+
}
2200+
21342201
/// Convert any array or array view to a dynamic dimensional array or
21352202
/// array view (respectively).
21362203
///
@@ -3065,3 +3132,32 @@ unsafe fn unlimited_transmute<A, B>(data: A) -> B
30653132
}
30663133

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

0 commit comments

Comments
 (0)