Empirical
Classes | Public Member Functions | List of all members
emp::BitVector Class Reference

A drop-in replacement for std::vector<bool>, but with extra bitwise logic features. More...

#include <BitVector.h>

Public Member Functions

 BitVector (size_t in_num_bits=0, bool init_val=false)
 Build a new BitVector with specified bit count (default 0) and initialization (default 0) More...
 
 BitVector (const BitVector &in_set)
 Copy constructor of existing bit field. More...
 
 BitVector (BitVector &&in_set)
 Move constructor of existing bit field. More...
 
 ~BitVector ()
 Destructor. More...
 
BitVectoroperator= (const BitVector &in_set)
 Assignment operator. More...
 
BitVectoroperator= (BitVector &&in_set)
 Move operator. More...
 
BitVectorResize (size_t new_bits)
 Resize this BitVector to have the specified number of bits. More...
 
bool operator== (const BitVector &in_set) const
 Test if two bit vectors are identical. More...
 
bool operator< (const BitVector &in_set) const
 Compare the would-be numerical values of two bit vectors. More...
 
bool operator<= (const BitVector &in_set) const
 Compare the would-be numerical values of two bit vectors. More...
 
bool operator!= (const BitVector &in_set) const
 Determine if two bit vectors are different. More...
 
bool operator> (const BitVector &in_set) const
 Compare the would-be numerical values of two bit vectors. More...
 
bool operator>= (const BitVector &in_set) const
 Compare the would-be numerical values of two bit vectors. More...
 
size_t GetSize () const
 How many bits do we currently have? More...
 
bool Get (size_t index) const
 Retrive the bit value from the specified index. More...
 
void Set (size_t index, bool value=true)
 Update the bit value at the specified index. More...
 
std::size_t Hash () const
 A simple hash function for bit vectors. More...
 
uint8_t GetByte (size_t index) const
 Retrive the byte at the specified byte index. More...
 
void SetByte (size_t index, uint8_t value)
 Update the byte at the specified byte index. More...
 
uint32_t GetUInt (size_t index) const
 Retrive the 32-bit uint from the specifeid uint index. More...
 
void SetUInt (size_t index, uint32_t value)
 Update the 32-bit uint at the specified uint index. More...
 
uint32_t GetUIntAtBit (size_t index)
 Retrive the 32-bit uint at the specified BIT index. More...
 
template<size_t OUT_BITS>
field_t GetValueAtBit (size_t index)
 Retrieve the specified number of bits (stored in the field type) at the target bit index. More...
 
bool Any () const
 Return true if ANY bits are set to 1, otherwise return false. More...
 
bool None () const
 Return true if NO bits are set to 1, otherwise return false. More...
 
bool All () const
 Return true if ALL bits are set to 1, otherwise return false. More...
 
 operator bool () const
 Casting a bit array to bool identifies if ANY bits are set to 1. More...
 
bool operator[] (size_t index) const
 Const index operator – return the bit at the specified position. More...
 
BitProxy operator[] (size_t index)
 Index operator – return a proxy to the bit at the specified position so it can be an lvalue. More...
 
void Clear ()
 Set all bits to 0. More...
 
void SetAll ()
 Set all bits to 1. More...
 
void Print (std::ostream &out=std::cout) const
 Regular print function (from most significant bit to least) More...
 
void PrintFields (std::ostream &out=std::cout, const std::string spacer=" ") const
 Print a space between each field (or other provided spacer) More...
 
void PrintArray (std::ostream &out=std::cout) const
 Print from smallest bit position to largest. More...
 
void PrintOneIDs (std::ostream &out=std::cout, std::string spacer=" ") const
 Print the positions of all one bits, spaces are the default separator. More...
 
size_t CountOnes_Sparse () const
 Count 1's by looping through once for each bit equal to 1. More...
 
size_t CountOnes_Mixed () const
 Count 1's in semi-parallel; fastest for even 0's & 1's. More...
 
size_t CountOnes () const
 Count the number of ones in the BitVector. More...
 
int FindBit () const
 Return the position of the first one; return -1 if no ones in vector. More...
 
int PopBit ()
 Return the position of the first one and change it to a zero. Return -1 if no ones. More...
 
int FindBit (const size_t start_pos) const
 
emp::vector< size_t > GetOnes () const
 Return positions of all ones. More...
 
BitVector NOT () const
 Perform a Boolean NOT on this BitVector and return the result. More...
 
BitVector AND (const BitVector &set2) const
 Perform a Boolean AND on this BitVector and return the result. More...
 
BitVector OR (const BitVector &set2) const
 Perform a Boolean OR on this BitVector and return the result. More...
 
BitVector NAND (const BitVector &set2) const
 Perform a Boolean NAND on this BitVector and return the result. More...
 
BitVector NOR (const BitVector &set2) const
 Perform a Boolean NOR on this BitVector and return the result. More...
 
BitVector XOR (const BitVector &set2) const
 Perform a Boolean XOR on this BitVector and return the result. More...
 
BitVector EQU (const BitVector &set2) const
 Perform a Boolean EQU on this BitVector and return the result. More...
 
BitVectorNOT_SELF ()
 Perform a Boolean NOT with this BitVector, store result here, and return this object. More...
 
BitVectorAND_SELF (const BitVector &set2)
 Perform a Boolean AND with this BitVector, store result here, and return this object. More...
 
BitVectorOR_SELF (const BitVector &set2)
 Perform a Boolean OR with this BitVector, store result here, and return this object. More...
 
BitVectorNAND_SELF (const BitVector &set2)
 Perform a Boolean NAND with this BitVector, store result here, and return this object. More...
 
BitVectorNOR_SELF (const BitVector &set2)
 Perform a Boolean NOR with this BitVector, store result here, and return this object. More...
 
BitVectorXOR_SELF (const BitVector &set2)
 Perform a Boolean XOR with this BitVector, store result here, and return this object. More...
 
BitVectorEQU_SELF (const BitVector &set2)
 Perform a Boolean EQU with this BitVector, store result here, and return this object. More...
 
BitVector SHIFT (const int shift_size) const
 Positive shifts go left and negative go right (0 does nothing); return result. More...
 
BitVectorSHIFT_SELF (const int shift_size)
 Positive shifts go left and negative go right; store result here, and return this object. More...
 
BitVector operator~ () const
 Operator bitwise NOT... More...
 
BitVector operator& (const BitVector &ar2) const
 Operator bitwise AND... More...
 
BitVector operator| (const BitVector &ar2) const
 Operator bitwise OR... More...
 
BitVector operator^ (const BitVector &ar2) const
 Operator bitwise XOR... More...
 
BitVector operator<< (const size_t shift_size) const
 Operator shift left... More...
 
BitVector operator>> (const size_t shift_size) const
 Operator shift right... More...
 
const BitVectoroperator&= (const BitVector &ar2)
 Compound operator bitwise AND... More...
 
const BitVectoroperator|= (const BitVector &ar2)
 Compound operator bitwise OR... More...
 
const BitVectoroperator^= (const BitVector &ar2)
 Compound operator bitwise XOR... More...
 
const BitVectoroperator<<= (const size_t shift_size)
 Compound operator for shift left... More...
 
const BitVectoroperator>>= (const size_t shift_size)
 Compound operator for shift right... More...
 
size_t size () const
 Function to allow drop-in replacement with std::vector<bool>. More...
 
void resize (std::size_t new_size)
 Function to allow drop-in replacement with std::vector<bool>. More...
 
bool all () const
 Function to allow drop-in replacement with std::vector<bool>. More...
 
bool any () const
 Function to allow drop-in replacement with std::vector<bool>. More...
 
bool none () const
 Function to allow drop-in replacement with std::vector<bool>. More...
 
size_t count () const
 Function to allow drop-in replacement with std::vector<bool>. More...
 

Detailed Description

A drop-in replacement for std::vector<bool>, but with extra bitwise logic features.

This class stores an arbirary number of bits in a set of "fields" (either 32-bit or 64-bit, depending on which should be faster.) Individual bits can be extracted, -or- bitwise logic (or bit magic) can be used on the groups of bits,

Constructor & Destructor Documentation

emp::BitVector::BitVector ( size_t  in_num_bits = 0,
bool  init_val = false 
)
inline

Build a new BitVector with specified bit count (default 0) and initialization (default 0)

emp::BitVector::BitVector ( const BitVector in_set)
inline

Copy constructor of existing bit field.

emp::BitVector::BitVector ( BitVector &&  in_set)
inline

Move constructor of existing bit field.

emp::BitVector::~BitVector ( )
inline

Destructor.

Member Function Documentation

bool emp::BitVector::All ( ) const
inline

Return true if ALL bits are set to 1, otherwise return false.

bool emp::BitVector::all ( ) const
inline

Function to allow drop-in replacement with std::vector<bool>.

BitVector emp::BitVector::AND ( const BitVector set2) const
inline

Perform a Boolean AND on this BitVector and return the result.

BitVector& emp::BitVector::AND_SELF ( const BitVector set2)
inline

Perform a Boolean AND with this BitVector, store result here, and return this object.

bool emp::BitVector::Any ( ) const
inline

Return true if ANY bits are set to 1, otherwise return false.

bool emp::BitVector::any ( ) const
inline

Function to allow drop-in replacement with std::vector<bool>.

void emp::BitVector::Clear ( )
inline

Set all bits to 0.

size_t emp::BitVector::count ( ) const
inline

Function to allow drop-in replacement with std::vector<bool>.

size_t emp::BitVector::CountOnes ( ) const
inline

Count the number of ones in the BitVector.

size_t emp::BitVector::CountOnes_Mixed ( ) const
inline

Count 1's in semi-parallel; fastest for even 0's & 1's.

size_t emp::BitVector::CountOnes_Sparse ( ) const
inline

Count 1's by looping through once for each bit equal to 1.

BitVector emp::BitVector::EQU ( const BitVector set2) const
inline

Perform a Boolean EQU on this BitVector and return the result.

BitVector& emp::BitVector::EQU_SELF ( const BitVector set2)
inline

Perform a Boolean EQU with this BitVector, store result here, and return this object.

int emp::BitVector::FindBit ( ) const
inline

Return the position of the first one; return -1 if no ones in vector.

int emp::BitVector::FindBit ( const size_t  start_pos) const
inline

Return the position of the first one after start_pos; return -1 if no ones in vector. You can loop through all 1-bit positions of a BitVector "bv" with:

for (int pos = bv.FindBit(); pos >= 0; pos = bv.FindBit(pos+1)) { ... }

bool emp::BitVector::Get ( size_t  index) const
inline

Retrive the bit value from the specified index.

uint8_t emp::BitVector::GetByte ( size_t  index) const
inline

Retrive the byte at the specified byte index.

emp::vector<size_t> emp::BitVector::GetOnes ( ) const
inline

Return positions of all ones.

size_t emp::BitVector::GetSize ( ) const
inline

How many bits do we currently have?

uint32_t emp::BitVector::GetUInt ( size_t  index) const
inline

Retrive the 32-bit uint from the specifeid uint index.

uint32_t emp::BitVector::GetUIntAtBit ( size_t  index)
inline

Retrive the 32-bit uint at the specified BIT index.

template<size_t OUT_BITS>
field_t emp::BitVector::GetValueAtBit ( size_t  index)
inline

Retrieve the specified number of bits (stored in the field type) at the target bit index.

std::size_t emp::BitVector::Hash ( ) const
inline

A simple hash function for bit vectors.

BitVector emp::BitVector::NAND ( const BitVector set2) const
inline

Perform a Boolean NAND on this BitVector and return the result.

BitVector& emp::BitVector::NAND_SELF ( const BitVector set2)
inline

Perform a Boolean NAND with this BitVector, store result here, and return this object.

bool emp::BitVector::None ( ) const
inline

Return true if NO bits are set to 1, otherwise return false.

bool emp::BitVector::none ( ) const
inline

Function to allow drop-in replacement with std::vector<bool>.

BitVector emp::BitVector::NOR ( const BitVector set2) const
inline

Perform a Boolean NOR on this BitVector and return the result.

BitVector& emp::BitVector::NOR_SELF ( const BitVector set2)
inline

Perform a Boolean NOR with this BitVector, store result here, and return this object.

BitVector emp::BitVector::NOT ( ) const
inline

Perform a Boolean NOT on this BitVector and return the result.

BitVector& emp::BitVector::NOT_SELF ( )
inline

Perform a Boolean NOT with this BitVector, store result here, and return this object.

emp::BitVector::operator bool ( ) const
inlineexplicit

Casting a bit array to bool identifies if ANY bits are set to 1.

bool emp::BitVector::operator!= ( const BitVector in_set) const
inline

Determine if two bit vectors are different.

BitVector emp::BitVector::operator& ( const BitVector ar2) const
inline

Operator bitwise AND...

const BitVector& emp::BitVector::operator&= ( const BitVector ar2)
inline

Compound operator bitwise AND...

bool emp::BitVector::operator< ( const BitVector in_set) const
inline

Compare the would-be numerical values of two bit vectors.

BitVector emp::BitVector::operator<< ( const size_t  shift_size) const
inline

Operator shift left...

const BitVector& emp::BitVector::operator<<= ( const size_t  shift_size)
inline

Compound operator for shift left...

bool emp::BitVector::operator<= ( const BitVector in_set) const
inline

Compare the would-be numerical values of two bit vectors.

BitVector& emp::BitVector::operator= ( const BitVector in_set)
inline

Assignment operator.

BitVector& emp::BitVector::operator= ( BitVector &&  in_set)
inline

Move operator.

bool emp::BitVector::operator== ( const BitVector in_set) const
inline

Test if two bit vectors are identical.

bool emp::BitVector::operator> ( const BitVector in_set) const
inline

Compare the would-be numerical values of two bit vectors.

bool emp::BitVector::operator>= ( const BitVector in_set) const
inline

Compare the would-be numerical values of two bit vectors.

BitVector emp::BitVector::operator>> ( const size_t  shift_size) const
inline

Operator shift right...

const BitVector& emp::BitVector::operator>>= ( const size_t  shift_size)
inline

Compound operator for shift right...

bool emp::BitVector::operator[] ( size_t  index) const
inline

Const index operator – return the bit at the specified position.

BitProxy emp::BitVector::operator[] ( size_t  index)
inline

Index operator – return a proxy to the bit at the specified position so it can be an lvalue.

BitVector emp::BitVector::operator^ ( const BitVector ar2) const
inline

Operator bitwise XOR...

const BitVector& emp::BitVector::operator^= ( const BitVector ar2)
inline

Compound operator bitwise XOR...

BitVector emp::BitVector::operator| ( const BitVector ar2) const
inline

Operator bitwise OR...

const BitVector& emp::BitVector::operator|= ( const BitVector ar2)
inline

Compound operator bitwise OR...

BitVector emp::BitVector::operator~ ( ) const
inline

Operator bitwise NOT...

BitVector emp::BitVector::OR ( const BitVector set2) const
inline

Perform a Boolean OR on this BitVector and return the result.

BitVector& emp::BitVector::OR_SELF ( const BitVector set2)
inline

Perform a Boolean OR with this BitVector, store result here, and return this object.

int emp::BitVector::PopBit ( )
inline

Return the position of the first one and change it to a zero. Return -1 if no ones.

void emp::BitVector::Print ( std::ostream &  out = std::cout) const
inline

Regular print function (from most significant bit to least)

void emp::BitVector::PrintArray ( std::ostream &  out = std::cout) const
inline

Print from smallest bit position to largest.

void emp::BitVector::PrintFields ( std::ostream &  out = std::cout,
const std::string  spacer = " " 
) const
inline

Print a space between each field (or other provided spacer)

void emp::BitVector::PrintOneIDs ( std::ostream &  out = std::cout,
std::string  spacer = " " 
) const
inline

Print the positions of all one bits, spaces are the default separator.

BitVector& emp::BitVector::Resize ( size_t  new_bits)
inline

Resize this BitVector to have the specified number of bits.

void emp::BitVector::resize ( std::size_t  new_size)
inline

Function to allow drop-in replacement with std::vector<bool>.

void emp::BitVector::Set ( size_t  index,
bool  value = true 
)
inline

Update the bit value at the specified index.

void emp::BitVector::SetAll ( )
inline

Set all bits to 1.

void emp::BitVector::SetByte ( size_t  index,
uint8_t  value 
)
inline

Update the byte at the specified byte index.

void emp::BitVector::SetUInt ( size_t  index,
uint32_t  value 
)
inline

Update the 32-bit uint at the specified uint index.

BitVector emp::BitVector::SHIFT ( const int  shift_size) const
inline

Positive shifts go left and negative go right (0 does nothing); return result.

BitVector& emp::BitVector::SHIFT_SELF ( const int  shift_size)
inline

Positive shifts go left and negative go right; store result here, and return this object.

size_t emp::BitVector::size ( ) const
inline

Function to allow drop-in replacement with std::vector<bool>.

BitVector emp::BitVector::XOR ( const BitVector set2) const
inline

Perform a Boolean XOR on this BitVector and return the result.

BitVector& emp::BitVector::XOR_SELF ( const BitVector set2)
inline

Perform a Boolean XOR with this BitVector, store result here, and return this object.


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