GF2::Vector --Location & Iteration Over Set Bits
Methods to iterate over the set elements/bits in a bit-vector.
constexpr std::size_t first() const; (1)
constexpr std::size_t last() const; (2)
constexpr std::size_t next(std::size_t pos) const; (3)
constexpr std::size_t prev(std::size_t pos) const; (4)
| 1 | Returns the index of the first set element or npos if there are none set. |
| 2 | Returns the index of the last set element or npos if there are none set. |
| 3 | Returns the index of the next set element after the argument or npos if there are no more set elements. |
| 4 | Returns the index of the previous set element before the argument or npos if there are no more set elements. |
Parameters |
|
|---|---|
|
The index to start a search from. Doesn’t have to be the index of a set element. |
Return Value |
|
A return value of |
|
Example
#include <GF2/GF2.h>
int main()
{
GF2::Vector<> v(11, [&](size_t k) { return (k + 1) % 2; }); (1)
std::cout << "The set indices in " << v << " are ";
auto pos = v.first(); (2)
while (pos != GF2::Vector<>::npos) { (3)
std::cout << pos << ' ';
pos = v.next(pos); (4)
}
std::cout << std::endl;
}
| 1 | Creates a vector of size 11 by calling a lambda that sets all the even indices. |
| 2 | Find the index of the first set element (should be 0). |
| 3 | Keep going until the search fails. |
| 4 | Try to find the index of a set bit after the current pos, |
Output
The set indices in 10101010101 are 0 2 4 6 8 10
See Also