| 1 |
//! Utility for transposing multi-dimensional data stored as a flat slice |
| 2 |
//! |
| 3 |
//! This library treats Rust slices as flattened row-major 2D arrays, and provides functions to transpose these 2D arrays, so that the row data becomes the column data, and vice versa. |
| 4 |
//! ``` |
| 5 |
//! // Create a 2D array in row-major order: the rows of our 2D array are contiguous, |
| 6 |
//! // and the columns are strided |
| 7 |
//! let input_array = vec![ 1, 2, 3, |
| 8 |
//! 4, 5, 6]; |
| 9 |
//! |
| 10 |
//! // Treat our 6-element array as a 2D 3x2 array, and transpose it to a 2x3 array |
| 11 |
//! let mut output_array = vec![0; 6]; |
| 12 |
//! transpose::transpose(&input_array, &mut output_array, 3, 2); |
| 13 |
//! |
| 14 |
//! // The rows have become the columns, and the columns have become the rows |
| 15 |
//! let expected_array = vec![ 1, 4, |
| 16 |
//! 2, 5, |
| 17 |
//! 3, 6]; |
| 18 |
//! assert_eq!(output_array, expected_array); |
| 19 |
//! |
| 20 |
//! // If we transpose our data again, we should get our original data back. |
| 21 |
//! let mut final_array = vec![0; 6]; |
| 22 |
//! transpose::transpose(&output_array, &mut final_array, 2, 3); |
| 23 |
//! assert_eq!(final_array, input_array); |
| 24 |
//! ``` |
| 25 |
//! |
| 26 |
//! This library supports both in-place and out-of-place transposes. The out-of-place |
| 27 |
//! transpose is much, much faster than the in-place transpose -- the in-place transpose should |
| 28 |
//! only be used in situations where the system doesn't have enough memory to do an out-of-place transpose. |
| 29 |
//! |
| 30 |
//! The out-of-place transpose uses one out of three different algorithms depending on the length of the input array. |
| 31 |
//! |
| 32 |
//! - Small: simple iteration over the array. |
| 33 |
//! - Medium: divide the array into tiles of fixed size, and process each tile separately. |
| 34 |
//! - Large: recursively split the array into smaller parts until each part is of a good size for the tiling algorithm, and then transpose each part. |
| 35 |
|
| 36 |
#![no_std] |
| 37 |
|
| 38 |
extern crate num_integer; |
| 39 |
extern crate strength_reduce; |
| 40 |
|
| 41 |
mod in_place; |
| 42 |
mod out_of_place; |
| 43 |
pub use in_place::transpose_inplace; |
| 44 |
pub use out_of_place::transpose; |