bit::vector — Conditional Change
Define methods to set or flip the element values in a bit-vector based on the return value from a function call.
1constexpr bit::vector &set_if(std::invocable<std::size_t, std::size_t> auto f);
2constexpr bit::vector &flip_if(std::invocable<std::size_t, std::size_t> auto f);- 1
-
Sets element
ito 1 iff(i) != 0, otherwise sets it to 0. - 2
-
Flips the value of element
iif f(i) != 0; otherwise, leaves it unchanged.
f is a function, and we expect to call f(i) for each set index.
These return a reference to *this, so can be chained with other calls.
Example
#include <bit/bit.h>
int main()
{
1 bit::vector<> v(16);
std::cout << "v: " << v << '\n';
2 v.set_if([](std::size_t i) { return (i + 1) % 2; });
std::cout << "v: " << v << '\n';
3 v.flip_if([](std::size_t i) { return (i + 1) % 2; });
std::cout << "v: " << v << '\n';
}- 1
- Start with a bit-vector whose elements are all 0 by default.
- 2
-
Using the
set_ifmethod with a lambda to set the even indices 0,2,4,… - 3
-
Using the
flip_ifmethod with a lambda to flip the even indices 0,2,4,…
Output
v: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
v: [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0]
v: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]