GF2::Vector — Change All Elements
Methods to set elements in a bit-vector to 1, reset them to 0, or flip them from 0 to 1 and vice versa. These methods can work on the entire bit-vector, on individual elements, or on blocks of contiguous elements.
Individual elements in the bit-vector
constexpr Vector &set(std::size_t i);
constexpr Vector &reset(std::size_t i);
constexpr Vector &flip(std::size_t i);
Sets, resets, or flips the value of the single element at index i.
Blocks of contiguous elements in the bit-vector
constexpr Vector &set(std::size_t first, std::size_t len);
constexpr Vector &reset(std::size_t first, std::size_t len);
constexpr Vector &flip(std::size_t first, std::size_t len);
Sets, resets, or flips the value of len elements starting at index first.
The entire bit-vector
constexpr Vector &set();
constexpr Vector &reset();
constexpr Vector &flip();
Sets, resets, or flips the values of all the elements.
The len elements starting at first must fit in the valid range for the bit-vector.
If the GF2_DEBUG flag is set at compile time this is checked and any violation will cause the program to abort with a helpful message.
|
Returns
All these methods return a reference to *this so can be chained with other calls.
Example
#include <GF2/GF2.h>
int main()
{
std::size_t n = 4;
std::size_t i = 0;
GF2::Vector<> v(n);
std::cout << "Setting ranges of elements to 1:\n";
v.reset();
std::cout << "Starting with vector of size " << v.size() << ": " << v << '\n';
for (i = 0; i < v.size(); ++i) {
std::size_t len, maxLen = v.size() - i + 1;
for (len = 1; len < maxLen; ++len) {
v.reset();
std::cout << "Setting " << len << " element(s) starting at position: " << i << ": " << v.set(i, len) << '\n';
}
}
std::cout << std::endl;
std::cout << "Setting ranges of elements to 0:\n";
v.set();
std::cout << "Starting with vector of size " << v.size() << ": " << v << '\n';
for (i = 0; i < v.size(); ++i) {
std::size_t len, maxLen = v.size() - i + 1;
for (len = 1; len < maxLen; ++len) {
v.set();
std::cout << "Resetting " << len << " element(s) starting at position: " << i << ": " << v.reset(i, len) << '\n';
}
}
std::cout << std::endl;
std::cout << "Flipping ranges of elements from 1 to 0:\n";
v.set();
std::cout << "Starting with vector of size " << v.size() << ": " << v << '\n';
for (i = 0; i < v.size(); ++i) {
// v.set();
std::size_t len, maxLen = v.size() - i + 1;
for (len = 1; len < maxLen; ++len) {
v.set();
std::cout << "Flipping " << len << " element(s) starting at position: " << i << ": " << v.flip(i, len) << '\n';
}
}
std::cout << std::endl;
return 0;
}
Output
Setting ranges of elements to 1:
Starting with vector of size 4: 0000
Setting 1 element(s) starting at position: 0: 1000
Setting 2 element(s) starting at position: 0: 1100
Setting 3 element(s) starting at position: 0: 1110
Setting 4 element(s) starting at position: 0: 1111
Setting 1 element(s) starting at position: 1: 0100
Setting 2 element(s) starting at position: 1: 0110
Setting 3 element(s) starting at position: 1: 0111
Setting 1 element(s) starting at position: 2: 0010
Setting 2 element(s) starting at position: 2: 0011
Setting 1 element(s) starting at position: 3: 0001
Setting ranges of elements to 0:
Starting with vector of size 4: 1111
Resetting 1 element(s) starting at position: 0: 0111
Resetting 2 element(s) starting at position: 0: 0011
Resetting 3 element(s) starting at position: 0: 0001
Resetting 4 element(s) starting at position: 0: 0000
Resetting 1 element(s) starting at position: 1: 1011
Resetting 2 element(s) starting at position: 1: 1001
Resetting 3 element(s) starting at position: 1: 1000
Resetting 1 element(s) starting at position: 2: 1101
Resetting 2 element(s) starting at position: 2: 1100
Resetting 1 element(s) starting at position: 3: 1110
Flipping ranges of elements from 1 to 0:
Starting with vector of size 4: 1111
Flipping 1 element(s) starting at position: 0: 0111
Flipping 2 element(s) starting at position: 0: 0011
Flipping 3 element(s) starting at position: 0: 0001
Flipping 4 element(s) starting at position: 0: 0000
Flipping 1 element(s) starting at position: 1: 1011
Flipping 2 element(s) starting at position: 1: 1001
Flipping 3 element(s) starting at position: 1: 1000
Flipping 1 element(s) starting at position: 2: 1101
Flipping 2 element(s) starting at position: 2: 1100
Flipping 1 element(s) starting at position: 3: 1110