pub fn transpose_inplace<T: Copy>(
buffer: &mut [T],
scratch: &mut [T],
width: usize,
height: usize,
)
Expand description
Transpose the input array in-place.
Given an input array of size input_width * input_height, representing flattened 2D data stored in row-major order, transpose the rows and columns of that input array, in-place.
Despite being in-place, this algorithm requires max(width, height) in scratch space.
// row-major order: the rows of our 2D array are contiguous,
// and the columns are strided
let original_array = vec![ 1, 2, 3,
4, 5, 6];
let mut input_array = original_array.clone();
// Treat our 6-element array as a 2D 3x2 array, and transpose it to a 2x3 array
// transpose_inplace requires max(width, height) scratch space, which is in this case 3
let mut scratch = vec![0; 3];
transpose::transpose_inplace(&mut input_array, &mut scratch, 3, 2);
// The rows have become the columns, and the columns have become the rows
let expected_array = vec![ 1, 4,
2, 5,
3, 6];
assert_eq!(input_array, expected_array);
// If we transpose it again, we should get our original data back.
transpose::transpose_inplace(&mut input_array, &mut scratch, 2, 3);
assert_eq!(original_array, input_array);
ยงPanics
Panics if input.len() != input_width * input_height
or if scratch.len() != max(width, height)