|
A library for working with phylogenetic and population genetic data.
v0.27.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_MATH_BITVECTOR_H_
2 #define GENESIS_UTILS_MATH_BITVECTOR_H_
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;
276 std::vector<IntType>
const&
data()
const
294 void unset_padding_();
302 static const std::array<IntType, IntSize> bit_mask_;
321 static const std::array<IntType, IntSize> ones_mask_;
326 static const std::array<IntType, 4> count_mask_;
333 std::vector<IntType> data_;
380 return static_cast<std::size_t
>( value.
x_hash() );
411 #endif // include guard
Bitvector operator~() const
IntType x_hash() const
Return a hash value of type IntType that is quicker to calculate than hash().
Bitvector(size_t size, bool initial_value=false)
Constructor that takes a size and an optional bool value to initialize the Bitvector,...
void set(size_t index)
Set the value of a single bit to true, with boundary check.
Helper structure that yields the hash of a given Bitvector.
bool get(size_t index) const
Return the value of a single bit, with boundary check.
friend Bitvector operator|(Bitvector const &lhs, Bitvector const &rhs)
bool operator[](size_t index) const
Return the value of a single bit, without boundary check.
std::vector< IntType > const & data() const
void flip(size_t index)
Flip (negate) the value of a single bit, with boundary check.
bool operator!=(const Bitvector &other) const
void set(size_t index, bool value)
Set the value of a single bit to a given bool value, with boundary check.
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.
Bitvector & operator&=(Bitvector const &rhs)
std::string to_string(GenomeLocus const &locus)
Bitvector()=default
Default constructor. Creates an empty Bitvector of size 0.
Bitvector & operator|=(Bitvector const &rhs)
void negate()
Flip all bits.
size_t hash() const
Return an std::hash value for the Bitvector.
friend Bitvector operator&(Bitvector const &lhs, Bitvector const &rhs)
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
static const size_t IntSize
bool operator==(const Bitvector &other) const
std::string dump_int(IntType x) const
std::size_t operator()(genesis::utils::Bitvector const &value) const
Helper structer that yields the x_hash of a given Bitvector.
std::size_t operator()(genesis::utils::Bitvector const &value) const
void normalize()
Bring the Bitvector in a normalized form, where the first bit is always zero.
void set_all(const bool value=false)
Set all the bits to a specified value.
size_t count() const
Count the number of set bits in the Bitvector, that is, its Hamming weight, or population count (popc...
Bitvector & operator=(Bitvector const &)=default
size_t size() const
Return the size (number of bits) of this Bitvector.
Bitvector & operator^=(Bitvector const &rhs)
friend Bitvector operator^(Bitvector const &lhs, Bitvector const &rhs)
void unset(size_t index)
Set the value of a single bit to false, with boundary check.