bit::matrix
— Conditional Set/Flip
We have methods to set or flip the element values in a bit-matrix based on the return value from a function call.
constexpr bit::matrix&
1(std::invocable<std::size_t, std::size_t> auto f);
set_if
constexpr bit::matrix&
2(std::invocable<std::size_t, std::size_t> auto f); flip_if
- 1
-
Sets element at
(i, j)
to 1 iff(i,j) != 0
, otherwise sets it to 0. - 2
-
Flips the value of element
(i, j)
iff(i,j) != 0
, otherwise leaves it unchanged.
f
is a function we expect to call as f(i,j)
for each index pair.
Both methods return a reference to *this
so they can be chained with other calls.
Example
#include <bit/bit.h>
int main()
{
::matrix<> m(4,8);
bitstd::cout << "m:\n" << m << '\n';
.set_if([](std::size_t i, std::size_t j) { return (i + j) % 2; });
mstd::cout << "m:\n" << m << '\n';
.flip_if([](std::size_t i, std::size_t j) { return (i + j) % 2; });
mstd::cout << "m:\n" << m << '\n';
}
Output
m:
│0 0 0 0 0 0 0 0│
│0 0 0 0 0 0 0 0│
│0 0 0 0 0 0 0 0│
│0 0 0 0 0 0 0 0│
m:
│0 1 0 1 0 1 0 1│
│1 0 1 0 1 0 1 0│
│0 1 0 1 0 1 0 1│
│1 0 1 0 1 0 1 0│
m:
│0 0 0 0 0 0 0 0│
│0 0 0 0 0 0 0 0│
│0 0 0 0 0 0 0 0│
│0 0 0 0 0 0 0 0│
See Also
matrix::set
matrix::reset
matrix::set_diagonal
matrix::reset_diagonal
matrix::flip