1 #ifndef GENESIS_UTILS_MATH_BITVECTOR_H_ 2 #define GENESIS_UTILS_MATH_BITVECTOR_H_ 76 data_.resize( (size / IntSize) + (size % IntSize == 0 ? 0 : 1) );
86 for (
size_t e : list) {
122 assert( index / IntSize < data_.size() );
123 assert( index % IntSize < bit_mask_.size() );
124 return static_cast<bool> (data_[index /
IntSize] & bit_mask_[index %
IntSize]);
130 inline bool get (
size_t index)
const 132 if (index >= size_) {
133 throw std::out_of_range(
134 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
139 assert( index / IntSize < data_.size() );
140 assert( index % IntSize < bit_mask_.size() );
141 return static_cast<bool> (data_[index /
IntSize] & bit_mask_[index %
IntSize]);
147 inline void set (
size_t index)
149 if (index >= size_) {
150 throw std::out_of_range(
151 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
156 assert( index / IntSize < data_.size() );
157 assert( index % IntSize < bit_mask_.size() );
166 if (index >= size_) {
167 throw std::out_of_range(
168 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
173 assert( index / IntSize < data_.size() );
174 assert( index % IntSize < bit_mask_.size() );
181 inline void set (
size_t index,
bool value)
193 inline void flip (
size_t index)
195 if (index >= size_) {
196 throw std::out_of_range(
197 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
202 assert( index / IntSize < data_.size() );
203 assert( index % IntSize < bit_mask_.size() );
239 size_t count()
const;
271 void set_all(
const bool value =
false);
273 std::string
dump()
const;
289 void unset_padding_();
297 static const std::array<IntType, IntSize> bit_mask_;
316 static const std::array<IntType, IntSize> ones_mask_;
321 static const std::array<IntType, 4> count_mask_;
328 std::vector<IntType> data_;
375 return static_cast<std::size_t
>( value.
x_hash() );
406 #endif // include guard void unset(size_t index)
Set the value of a single bit to false, with boundary check.
Bitvector & operator^=(Bitvector const &rhs)
Bitvector & operator&=(Bitvector const &rhs)
Bitvector(size_t size, bool initial_value=false)
Constructor that takes a size and an optional bool value to initialize the Bitvector, false by default.
size_t count() const
Count the number of set bits in the Bitvector, that is, its Hamming weight, or population count (popc...
bool operator[](size_t index) const
Return the value of a single bit, without boundary check.
Helper structer that yields the x_hash of a given Bitvector.
IntType x_hash() const
Return a hash value of type IntType that is quicker to calculate than hash().
static const size_t IntSize
Bitvector()=default
Default constructor. Creates an empty Bitvector of size 0.
Bitvector operator~() const
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
void negate()
Flip all bits.
friend Bitvector operator&(Bitvector const &lhs, Bitvector const &rhs)
Bitvector(size_t size, std::initializer_list< size_t > list)
Constructor that takes a size and a list of values (positions) to be set to true. ...
void flip(size_t index)
Flip (negate) the value of a single bit, with boundary check.
bool operator==(const Bitvector &other) const
bool operator!=(const Bitvector &other) const
Helper structure that yields the hash of a given Bitvector.
friend Bitvector operator^(Bitvector const &lhs, Bitvector const &rhs)
void normalize()
Bring the Bitvector in a normalized form, where the first bit is always zero.
Bitvector & operator|=(Bitvector const &rhs)
std::string dump_int(IntType x) const
std::shared_ptr< BaseOutputTarget > to_string(std::string &target_string)
Obtain an output target for writing to a string.
Bitvector & operator=(Bitvector const &)=default
void set_all(const bool value=false)
Set all the bits to a specified value.
size_t hash() const
Return an std::hash value for the Bitvector.
size_t size() const
Return the size (number of bits) of this Bitvector.
friend Bitvector operator|(Bitvector const &lhs, Bitvector const &rhs)