Write a highly optimized c++ function that does in-place buffer transposition. Buffer is non-square and has the size of NxM. Use the "Following the cycles" algorithm. Don’t use additional memory (like “visited” array).
Example of input (printed as 3x4):
0, 1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11
Output (printed as 4x3):
0, 3, 6, 9,
1, 4, 7, 10,
2, 5, 8, 11
Assistant
Sure! Below is a highly optimized C++ implementation of in-place transposition of a non-square NxM buffer using the "following the cycles" algorithm. It works in-place without extra memory such as a visited array.
This method relies on the fact that transpose can be viewed as a permutation on the 1D array indices, and the permutation decomposes into cycles. You follow each cycle and rotate elements accordingly.
Explanation:
The buffer is a 1D array representing an N (rows) by M (cols) matrix.
The transpose will be M by N.
Position (r, c) in input (N x M) maps to position (c, r) in output (M x N).
Linear indices map as:
pos = r * M + c (input)
pos' = c * N + r (output)
This defines a permutation on indices [0, N*M).
The algorithm:
For each index i, if it hasn't been already swapped, follow the cycle:
Move element at i to mapping(i), then the element at mapping(i) to mapping(mapping(i)) and so forth, until you return to i.
To avoid extra visited array, we can skip trivial cycles and use the fact that for each cycle, the minimal element index in cycle will be visited first.
Code:
cpp
#include <iostream>
#include <vector>
#include <algorithm>
// Returns the transposed position of linear index i in NxM matrix.
inline size_t transpose_pos(size_t i, size_t N, size_t M) {
// i = r * M + c
// transposed pos = c * N + r
size_t r = i / M;
size_t c = i % M;
return c * N + r;
}
// In-place transpose of non-square NxM matrix stored row-major in `data`.
template<typename T>
void inplace_transpose(T* data, size_t N, size_t M) {
const size_t size = N * M;
// Handle degenerate case (square matrix) with standard approach:
if (N == M) {
for (size_t r = 0; r < N; ++r) {
for (size_t c = r + 1; c < M; ++c) {
std::swap(data[r * M + c], data[c * N + r]);
}
}
return;
}
// For non-square, apply "following cycles" algorithm with no visited array.
// To detect cycles, only process positions i where i < transpose_pos(i),
// this prevents double processing of the same cycle.
for (size_t start = 0; start < size; ++start) {
size_t next = transpose_pos(start, N, M);
// If cycle is a fixed point or processed before:
if (next == start || next < start)
continue;
// Follow cycle and rotate elements:
T tmp = std::move(data[start]);
size_t current = start;
while (true) {
size_t target = transpose_pos(current, N, M);
if (target == start) {
data[current] = std::move(tmp);
break;
}
data[current] = std::move(data[target]);
current = target;
}
}
}
Example usage and test:
cpp
int main() {
size_t N = 3, M = 4;
std::vector<int> data = {
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11
};
std::cout << "Original matrix (" << N << "x" << M << "):\n";
for (size_t r = 0; r < N; ++r) {
for (size_t c = 0; c < M; ++c)
std::cout << data[r*M + c] << "\t";
std::cout << "\n";
}
std::cout << "\n";
inplace_transpose(data.data(), N, M);
std::cout << "Transposed matrix (" << M << "x" << N << "):\n";
for (size_t r = 0; r < M; ++r) {
for (size_t c = 0; c < N; ++c)
std::cout << data[r*N + c] << "\t";
std::cout << "\n";
}
return 0;
}
This code performs the transpose in-place, does not allocate extra buffers, and avoids using a visited array (instead using an index ordering trick to process each cycle exactly once). It supports generic types with move semantics if applicable.