A library for working with phylogenetic and population genetic data.
v0.27.0
Bitvector Class Reference

#include <genesis/utils/math/bitvector.hpp>

Detailed Description

Definition at line 48 of file bitvector.hpp.

Public Member Functions

 Bitvector ()=default
 Default constructor. Creates an empty Bitvector of size 0. More...
 
 Bitvector (Bitvector &&)=default
 
 Bitvector (Bitvector const &)=default
 
 Bitvector (Bitvector const &other, size_t max_size)
 Create a Bitvector by copying the first max_size of another Bitvector. More...
 
 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. More...
 
 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. More...
 
 Bitvector (std::string const &values)
 Constructor that takes a std::string of 0s and 1s to build the Bitvector. More...
 
 ~Bitvector ()=default
 
size_t count () const
 Count the number of set bits in the Bitvector, that is, its Hamming weight, or population count (popcnt). More...
 
std::vector< IntType > const & data () const
 
std::string dump () const
 
std::string dump_int (IntType x) const
 
void flip (size_t index)
 Flip (negate) the value of a single bit, with boundary check. More...
 
bool get (size_t index) const
 Return the value of a single bit, with boundary check. More...
 
size_t hash () const
 Return an std::hash value for the Bitvector. More...
 
void negate ()
 Flip all bits. More...
 
void normalize ()
 Bring the Bitvector in a normalized form, where the first bit is always zero. More...
 
bool operator!= (const Bitvector &other) const
 
Bitvectoroperator&= (Bitvector const &rhs)
 
Bitvectoroperator= (Bitvector &&)=default
 
Bitvectoroperator= (Bitvector const &)=default
 
bool operator== (const Bitvector &other) const
 
bool operator[] (size_t index) const
 Return the value of a single bit, without boundary check. More...
 
Bitvectoroperator^= (Bitvector const &rhs)
 
Bitvectoroperator|= (Bitvector const &rhs)
 
Bitvector operator~ () const
 
void set (size_t index)
 Set the value of a single bit to true, with boundary check. More...
 
void set (size_t index, bool value)
 Set the value of a single bit to a given bool value, with boundary check. More...
 
void set_all (const bool value=false)
 Set all the bits to a specified value. More...
 
size_t size () const
 Return the size (number of bits) of this Bitvector. More...
 
void unset (size_t index)
 Set the value of a single bit to false, with boundary check. More...
 
IntType x_hash () const
 Return a hash value of type IntType that is quicker to calculate than hash(). More...
 

Public Types

using IntType = uint64_t
 

Static Public Attributes

static const size_t IntSize = sizeof(IntType) * 8
 

Friends

Bitvector operator& (Bitvector const &lhs, Bitvector const &rhs)
 
Bitvector operator^ (Bitvector const &lhs, Bitvector const &rhs)
 
Bitvector operator| (Bitvector const &lhs, Bitvector const &rhs)
 

Constructor & Destructor Documentation

◆ Bitvector() [1/7]

Bitvector ( )
default

Default constructor. Creates an empty Bitvector of size 0.

◆ Bitvector() [2/7]

Bitvector ( size_t  size,
bool  initial_value = false 
)
inline

Constructor that takes a size and an optional bool value to initialize the Bitvector, false by default.

Definition at line 72 of file bitvector.hpp.

◆ Bitvector() [3/7]

Bitvector ( size_t  size,
std::initializer_list< size_t >  list 
)
inline

Constructor that takes a size and a list of values (positions) to be set to true.

Definition at line 83 of file bitvector.hpp.

◆ Bitvector() [4/7]

Bitvector ( std::string const &  values)

Constructor that takes a std::string of 0s and 1s to build the Bitvector.

This is for cases where some fixed Bitvector needs to be constructed (e.g., for testing purposes). The constructor throws if any character in the string is not 0 or 1.

Definition at line 93 of file bitvector.cpp.

◆ Bitvector() [5/7]

Bitvector ( Bitvector const &  other,
size_t  max_size 
)

Create a Bitvector by copying the first max_size of another Bitvector.

If max_size > other.size(), all max_size are used.

Definition at line 112 of file bitvector.cpp.

◆ ~Bitvector()

~Bitvector ( )
default

◆ Bitvector() [6/7]

Bitvector ( Bitvector const &  )
default

◆ Bitvector() [7/7]

Bitvector ( Bitvector &&  )
default

Member Function Documentation

◆ count()

size_t count ( ) const

Count the number of set bits in the Bitvector, that is, its Hamming weight, or population count (popcnt).

Definition at line 223 of file bitvector.cpp.

◆ data()

std::vector<IntType> const& data ( ) const
inline

Definition at line 276 of file bitvector.hpp.

◆ dump()

std::string dump ( ) const

Definition at line 327 of file bitvector.cpp.

◆ dump_int()

std::string dump_int ( IntType  x) const

Definition at line 341 of file bitvector.cpp.

◆ flip()

void flip ( size_t  index)
inline

Flip (negate) the value of a single bit, with boundary check.

Definition at line 193 of file bitvector.hpp.

◆ get()

bool get ( size_t  index) const
inline

Return the value of a single bit, with boundary check.

Definition at line 130 of file bitvector.hpp.

◆ hash()

size_t hash ( ) const

Return an std::hash value for the Bitvector.

Definition at line 252 of file bitvector.cpp.

◆ negate()

void negate ( )

Flip all bits.

Definition at line 270 of file bitvector.cpp.

◆ normalize()

void normalize ( )

Bring the Bitvector in a normalized form, where the first bit is always zero.

If the first bit is zero, nothing happens. However, if is is one, the whole Bitvector is flipped using negate().

Definition at line 281 of file bitvector.cpp.

◆ operator!=()

bool operator!= ( const Bitvector other) const

Definition at line 214 of file bitvector.cpp.

◆ operator&=()

Bitvector & operator&= ( Bitvector const &  rhs)

Definition at line 128 of file bitvector.cpp.

◆ operator=() [1/2]

Bitvector& operator= ( Bitvector &&  )
default

◆ operator=() [2/2]

Bitvector& operator= ( Bitvector const &  )
default

◆ operator==()

bool operator== ( const Bitvector other) const

Definition at line 201 of file bitvector.cpp.

◆ operator[]()

bool operator[] ( size_t  index) const
inline

Return the value of a single bit, without boundary check.

Definition at line 121 of file bitvector.hpp.

◆ operator^=()

Bitvector & operator^= ( Bitvector const &  rhs)

Definition at line 158 of file bitvector.cpp.

◆ operator|=()

Bitvector & operator|= ( Bitvector const &  rhs)

Definition at line 143 of file bitvector.cpp.

◆ operator~()

Bitvector operator~ ( ) const

Definition at line 173 of file bitvector.cpp.

◆ set() [1/2]

void set ( size_t  index)
inline

Set the value of a single bit to true, with boundary check.

Definition at line 147 of file bitvector.hpp.

◆ set() [2/2]

void set ( size_t  index,
bool  value 
)
inline

Set the value of a single bit to a given bool value, with boundary check.

Definition at line 181 of file bitvector.hpp.

◆ set_all()

void set_all ( const bool  value = false)

Set all the bits to a specified value.

Definition at line 288 of file bitvector.cpp.

◆ size()

size_t size ( ) const
inline

Return the size (number of bits) of this Bitvector.

Definition at line 230 of file bitvector.hpp.

◆ unset()

void unset ( size_t  index)
inline

Set the value of a single bit to false, with boundary check.

Definition at line 164 of file bitvector.hpp.

◆ x_hash()

Bitvector::IntType x_hash ( ) const

Return a hash value of type IntType that is quicker to calculate than hash().

This can be used for obtaining a simple hash using xor of the words. The avalanche effect is of course not present, but for many applications, this hash is good enough and quite useful.

Definition at line 261 of file bitvector.cpp.

Friends And Related Function Documentation

◆ operator&

Bitvector operator& ( Bitvector const &  lhs,
Bitvector const &  rhs 
)
friend

Definition at line 180 of file bitvector.cpp.

◆ operator^

Bitvector operator^ ( Bitvector const &  lhs,
Bitvector const &  rhs 
)
friend

Definition at line 194 of file bitvector.cpp.

◆ operator|

Bitvector operator| ( Bitvector const &  lhs,
Bitvector const &  rhs 
)
friend

Definition at line 187 of file bitvector.cpp.

Member Typedef Documentation

◆ IntType

using IntType = uint64_t

Definition at line 56 of file bitvector.hpp.

Member Data Documentation

◆ IntSize

const size_t IntSize = sizeof(IntType) * 8
static

Definition at line 57 of file bitvector.hpp.


The documentation for this class was generated from the following files: