GF2::Matrix — Iterate and Potentially Change Elements
Methods to set or flip the element values in a bit-matrix based on the return value from a function call.
constexpr Matrix &set_if(std::invocable<std::size_t, std::size_t> auto f); (1)
constexpr Matrix &flip_if(std::invocable<std::size_t, std::size_t> auto f); (2)
| 1 | Sets element at (i, j) to 1 if f(i,j) != 0, otherwise sets it to 0. |
| 2 | Flips the value of element (i, j) if f(i,j) != 0, otherwise leaves it unchanged. |
f is a function that we expect to call as f(i,j) for each index pair.
Returns
Both methods return a reference to *this so can be chained with other calls.
Example
#include <GF2/GF2.h>
int main()
{
GF2::Matrix<> m(4,8);
std::cout << "m:\n" << m << '\n';
m.set_if([](std::size_t i, std::size_t j) { return (i + j) % 2; });
std::cout << "m:\n" << m << '\n';
m.flip_if([](std::size_t i, std::size_t j) { return (i + j) % 2; });
std::cout << "m:\n" << m << '\n';
}
Output
m:
00000000
00000000
00000000
00000000
m:
01010101
10101010
01010101
10101010
m:
00000000
00000000
00000000
See Also