tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
transpose.hpp
Go to the documentation of this file.
1#ifndef TATAMI_TRANSPOSE_HPP
2#define TATAMI_TRANSPOSE_HPP
3
4#include <algorithm>
5
11namespace tatami {
12
27template<typename Input_, typename Output_>
28void transpose(const Input_* input, size_t nrow, size_t ncol, size_t input_stride, Output_* output, size_t output_stride) {
29 if ((nrow == 1 && output_stride == 1) || (ncol == 1 && input_stride == 1)) {
30 std::copy_n(input, nrow * ncol, output);
31 return;
32 }
33
34 // Using a blockwise strategy to perform the transposition,
35 // in order to be more input-friendly.
36 constexpr size_t block = 16;
37 size_t col_start = 0;
38 while (col_start < ncol) {
39 size_t col_end = col_start + std::min(block, ncol - col_start);
40
41 size_t row_start = 0;
42 while (row_start < nrow) {
43 size_t row_end = row_start + std::min(block, nrow - row_start);
44
45 // We use offsets instead of directly performing pointer
46 // arithmetic, to avoid creating an invalid pointer address (which
47 // is UB, even if unused) after the last inner loop iteration.
50
51 for (size_t c = col_start; c < col_end; ++c, ++input_offset, output_offset += output_stride) {
54
57 }
58 }
59
61 }
63 }
64}
65
79template<typename Input_, typename Output_>
80void transpose(const Input_* input, size_t nrow, size_t ncol, Output_* output) {
81 transpose(input, nrow, ncol, ncol, output, nrow);
82}
83
84}
85
86#endif
Flexible representations for matrix data.
Definition Extractor.hpp:15
void transpose(const Input_ *input, size_t nrow, size_t ncol, size_t input_stride, Output_ *output, size_t output_stride)
Definition transpose.hpp:28
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35