|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_MATH_BITVECTOR_H_
2 #define GENESIS_UTILS_MATH_BITVECTOR_H_
64 static const size_t npos = std::numeric_limits<size_t>::max();
98 for(
size_t e : list ) {
111 :
Bitvector( std::distance( first,last ), false )
114 for(
auto it = first; it != last; ++it ) {
159 assert( index /
IntSize < data_.size() );
160 assert( index %
IntSize < bit_mask_.size() );
161 return static_cast<bool> (data_[index /
IntSize] & bit_mask_[index %
IntSize]);
167 inline bool get(
size_t index )
const
169 if( index >= size_ ) {
170 throw std::out_of_range(
171 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
176 assert( index /
IntSize < data_.size() );
177 assert( index %
IntSize < bit_mask_.size() );
178 return static_cast<bool> (data_[index /
IntSize] & bit_mask_[index %
IntSize]);
184 inline void set(
size_t index )
186 if (index >= size_) {
187 throw std::out_of_range(
188 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
193 assert( index /
IntSize < data_.size() );
194 assert( index %
IntSize < bit_mask_.size() );
203 if( index >= size_ ) {
204 throw std::out_of_range(
205 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
210 assert( index /
IntSize < data_.size() );
211 assert( index %
IntSize < bit_mask_.size() );
218 inline void set(
size_t index,
bool value )
242 void set(
size_t first,
size_t last,
bool value =
true );
247 inline void flip(
size_t index )
249 if( index >= size_ ) {
250 throw std::out_of_range(
251 "Cannot access element " +
std::to_string(index) +
" in Bitvector of size " +
256 assert( index /
IntSize < data_.size() );
257 assert( index %
IntSize < bit_mask_.size() );
266 return flip( index );
313 size_t count()
const;
331 size_t count(
size_t first,
size_t last )
const;
377 void set_all(
const bool value =
false);
379 std::string
dump()
const;
382 std::vector<IntType>
const&
data()
const
400 void unset_padding_();
405 static size_t count_(
IntType x );
413 static const std::array<IntType, IntSize> bit_mask_;
429 static const std::array<IntType, IntSize> ones_mask_;
437 static const std::array<IntType, 4> count_mask_;
444 std::vector<IntType> data_;
491 return static_cast<std::size_t
>( value.
x_hash() );
522 #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)
size_t find_next_set(size_t start) const
Return the index of the next position in the Bitvector that is set.
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)
Bitvector(It first, It last)
Construct a Bitvector using a range of bools.
void negate()
Flip all bits.
void toggle(size_t index)
Alias for flip(), see there for details.
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
bool empty() const
Return whether the Bitvector is empty, that is, has size() == 0.
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.
static const size_t npos
Value to indicate that find_next_set() did not find any set bits.
bool any_set() const
Return if any bits are set at all.
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.