GF2::Matrix — Swap Two Rows/Columns

Swap any two rows or any two columns in a bit-matrix. This is a common operation in some matrix transformation algorithms.

constexpr Matrix &swap_rows(std::size_t i0, std::size_t i1);    (1)
constexpr Matrix &swap_cols(std::size_t j0, std::size_t j1);    (2)
1 Swap rows i0 and i1.
2 Swap columns j0 and j1.

These methods return a reference to *this so can be chained with other calls.

In general, these methods do not check whether the indices are in bounds and if they aren’t the behavior is undefined (but bound to be bad!) All of them will perform range checking if the GF2_DEBUG flag is set at compile time. See the discussion of the gf2_debug_assert macro.
Example
#include <GF2/GF2.h>
int main()
{
    GF2::Matrix<> m(4, 8, [](std::size_t i, std::size_t j) { return (i + j)%2; });     (1)
    std::cout << "Original:\n"              << m << '\n';
    std::cout << "Swapped first 2 rows:\n"  << m.swap_rows(0,1) << '\n';
    std::cout << "And back:\n"              << m.swap_rows(0,1) << '\n';
    std::cout << "Swapped first 2 cols:\n"  << m.swap_cols(0,1) << '\n';
    std::cout << "And back:\n"              << m.swap_cols(0,1) << '\n';
}
1 Set up a bit-matrix with a checkerboard pattern of zeros and ones.
Output
Original:
01010101
10101010
01010101
10101010
Swapped first 2 rows:
10101010
01010101
01010101
10101010
And back:
01010101
10101010
01010101
10101010
Swapped first 2 cols:
10010101
01101010
10010101
01101010
And back:
01010101
10101010
01010101
10101010
See Also

replace