panga
Classes | Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Friends | List of all members
panga::BitVector Class Reference
Inheritance diagram for panga::BitVector:
Inheritance graph
[legend]
Collaboration diagram for panga::BitVector:
Collaboration graph
[legend]

Classes

struct  HexFormat_t
 
struct  HexFormatWrapper
 

Public Member Functions

 BitVector (size_t bit_count=0)
 
 BitVector (const BitVector &source)
 
BitVectoroperator= (const BitVector &rhs)
 
bool Equals (const BitVector &rhs, size_t bits_to_compare) const
 
bool Equals (const BitVector &rhs) const
 
void SetBitCount (size_t bit_count)
 
size_t GetBitCount () const
 
void Clip (size_t bit_count)
 
void Clear ()
 
void Set (size_t index)
 
void Unset (size_t index)
 
bool Get (size_t index) const
 
void Flip (size_t index)
 
template<typename IntegerType = uint64_t>
IntegerType GetInt (size_t bit_index, size_t bit_width) const
 
template<typename IntegerType = uint64_t>
void SetInt (IntegerType value, size_t bit_index, size_t bit_width)
 
void SubVector (BitVector *destination, size_t destination_start_bit_offset, size_t source_start_bit_offset, size_t bits_to_copy) const
 
size_t HammingDistance (const BitVector &rhs) const
 
size_t ToString (char *buffer, size_t buffer_length) const
 
void FromString (const char *buffer, size_t buffer_length)
 
size_t ToStringHex (char *buffer, size_t buffer_length) const
 
void FromStringHex (const char *buffer, size_t buffer_length)
 

Static Public Attributes

static HexFormat_t HexFormat
 

Protected Member Functions

const std::vector< std::byte > & GetBytes () const
 
std::vector< std::byte > & GetBytesWritable ()
 
void Resize (size_t bit_count)
 
void WriteToStream (std::ostream &out) const
 
void WriteToStreamHex (std::ostream &out) const
 

Static Protected Member Functions

template<typename IntegerType = uint64_t>
static void WriteInt (std::byte *bytes, size_t start_bit_index, size_t bits_to_copy, IntegerType value)
 
template<typename IntegerType = uint64_t>
static IntegerType ReadInt (const std::byte *bytes, size_t start_bit_index, size_t bits_to_copy)
 
static void WriteBytes (const std::byte *source, size_t source_start_bit_offset, std::byte *destination, size_t destination_start_bit_offset, size_t bits_to_copy)
 
static bool Compare (const std::byte *left, const std::byte *right, size_t bits_to_compare)
 

Friends

std::ostream & operator<< (const HexFormatWrapper &wrapper, const BitVector &bv)
 
std::ostream & operator<< (std::ostream &out, const BitVector &bv)
 

Member Function Documentation

◆ Clear()

void panga::BitVector::Clear ( )

Unset all bits in the BitVector.

◆ Clip()

void panga::BitVector::Clip ( size_t  bit_count)

Clip the length of the BitVector.
Does not reduce the size of the backing storage.
Note: Cannot grow the BitVector (bitCount must be less than current count).

◆ Compare()

bool panga::BitVector::Compare ( const std::byte *  left,
const std::byte *  right,
size_t  bits_to_compare 
)
staticprotected

Compare |bits_to_compare| bits between |left| and |right|.
Note: Does not support non-byte-aligned compare, begins comparing at the beginning of each byte array.

◆ Equals() [1/2]

bool panga::BitVector::Equals ( const BitVector rhs) const

Calculate equality between this BitVector and |rhs|.
Note: Compares up to the number of bits in |this| BitVector.

◆ Equals() [2/2]

bool panga::BitVector::Equals ( const BitVector rhs,
size_t  bits_to_compare 
) const

Calculate equality between this BitVector and |rhs|.

Parameters
bits_to_compareLimit comparision to only the first bits_to_compare bits.

◆ Flip()

void panga::BitVector::Flip ( size_t  index)

Flip one bit in the BitVector.

Parameters
indexThe index of the bit to flip.

◆ FromString()

void panga::BitVector::FromString ( const char *  buffer,
size_t  buffer_length 
)

Read a BitVector from a null-terminated string of '0' and '1' characters.

Parameters
bufferString which we will read from.
buffer_lengthLength of buffer in bytes.

◆ FromStringHex()

void panga::BitVector::FromStringHex ( const char *  buffer,
size_t  buffer_length 
)

Read a BitVector from a null-terminated string containing a hex representation.

Parameters
bufferString which we will read from.
buffer_lengthLength of buffer in bytes.

◆ Get()

bool panga::BitVector::Get ( size_t  index) const

Get a single bit from the BitVector.

Parameters
indexThe index of the bit to get.
Returns
true if bit is set.

◆ GetBitCount()

size_t panga::BitVector::GetBitCount ( ) const

Get the count of bits in the BitVector.

◆ GetBytes()

const std::vector< std::byte > & panga::BitVector::GetBytes ( ) const
protected

Get a read-only view of the byte buffer underlying this BitVector.

◆ GetBytesWritable()

std::vector< std::byte > & panga::BitVector::GetBytesWritable ( )
protected

Get a writable view of the byte buffer underlying this BitVector.

◆ GetInt()

template<typename IntegerType = uint64_t>
IntegerType panga::BitVector::GetInt ( size_t  bit_index,
size_t  bit_width 
) const
inline

Construct an integer from bits in the BitVector.

Parameters
bit_indexStarting bit index in the BitVector at which to fetch the integer.
bit_widthNumber of bits to use in constructing the integer.

◆ HammingDistance()

size_t panga::BitVector::HammingDistance ( const BitVector rhs) const

Calculate the Hamming Distance between this and |rhs|.
The Hamming Distance is the number of bits which differ between two BitVectors.

Returns
The number of bits which differ between this and |rhs|.

◆ operator=()

BitVector & panga::BitVector::operator= ( const BitVector rhs)

Deep copy a BitVector.
Resizes this to be the same size as rhs.

◆ ReadInt()

template<typename IntegerType = uint64_t>
static IntegerType panga::BitVector::ReadInt ( const std::byte *  bytes,
size_t  start_bit_index,
size_t  bits_to_copy 
)
inlinestaticprotected

Read |bits_to_copy| bits from |bytes| starting at |start_bit_index| and interpret them as an integer type value.

◆ Resize()

void panga::BitVector::Resize ( size_t  bit_count)
protected

Resize the BitVector such that it contains |bit_count| bits. Note: Does not change the value of any existing bits currently in the vector.

◆ Set()

void panga::BitVector::Set ( size_t  index)

Set a single bit the BitVector.

Parameters
indexThe index of the bit to set.

◆ SetBitCount()

void panga::BitVector::SetBitCount ( size_t  bit_count)

Set the number of bits in the BitVector.
Note: Unsets all bits in the BitVector.

◆ SetInt()

template<typename IntegerType = uint64_t>
void panga::BitVector::SetInt ( IntegerType  value,
size_t  bit_index,
size_t  bit_width 
)
inline

Store an integer value in the BitVector.

Parameters
valueValue of the integer to store.
bit_indexStarting bit index in the BitVector at which to store the integer.
bit_widthNumber of bits to store in this BitVector for the integer.

◆ SubVector()

void panga::BitVector::SubVector ( BitVector destination,
size_t  destination_start_bit_offset,
size_t  source_start_bit_offset,
size_t  bits_to_copy 
) const

Copy a range of bits from this vector.

Parameters
destinationBitVector into which we'll write bits from this.
destination_start_bit_offsetBit index into destination where we will start writing.
source_start_bit_offsetBit index into this where we will start copying.
bits_to_copyCount of bits we should copy.

◆ ToString()

size_t panga::BitVector::ToString ( char *  buffer,
size_t  buffer_length 
) const

Write a binary representation of this BitVector into a buffer.
The buffer will be null-terminated.
Each character in the resulting buffer will be a '0' or '1'.
The most-significant bit is written at the beginning of the buffer. Note: buffer_length must be > bitCount.

Parameters
bufferBuffer which we will write into.
buffer_lengthLength of buffer in bytes.
Returns
If buffer is nullptr, returns the number of bytes needed to store the binary representation of this BitVector. Otherwise, returns the number of bytes written to buffer.

◆ ToStringHex()

size_t panga::BitVector::ToStringHex ( char *  buffer,
size_t  buffer_length 
) const

Write a hex representation of this BitVector into a buffer.
The buffer will be null-terminated.
Note: buffer_length must be > bitCount / 8 * 2.

See also
BitVector::HexFormat
Parameters
bufferBuffer which we will write into.
buffer_lengthLength of buffer in bytes.
Returns
If buffer is nullptr, returns the number of bytes needed to store the hex representation of this BitVector. Otherwise, returns the number of bytes written to buffer.

◆ Unset()

void panga::BitVector::Unset ( size_t  index)

Unset a single bit in the BitVector.

Parameters
indexThe index of the bit to unset.

◆ WriteBytes()

void panga::BitVector::WriteBytes ( const std::byte *  source,
size_t  source_start_bit_offset,
std::byte *  destination,
size_t  destination_start_bit_offset,
size_t  bits_to_copy 
)
staticprotected

Copy |bits_to_copy| bits from a byte buffer |source| into |destination|.
|source_start_bit_offset| and |destination_start_bit_offset| are bit offsets into |source| and |destination|.
Supports non-byte-aligned copy, though this is slower.

◆ WriteInt()

template<typename IntegerType = uint64_t>
static void panga::BitVector::WriteInt ( std::byte *  bytes,
size_t  start_bit_index,
size_t  bits_to_copy,
IntegerType  value 
)
inlinestaticprotected

Write |bits_to_copy| bits from an integer type |value| as binary data into |bytes| beginning at |start_bit_index|.

Member Data Documentation

◆ HexFormat

BitVector::HexFormat_t panga::BitVector::HexFormat
static

An output manipulator which modifies subsequent stream operations such that the BitVector will be printed in hex characters instead of binary.
BitVector bv;
cout << BitVector::HexFormat << bv;
Note: Only the BitVector following BitVector::HexFormat will be formatted in hex.
cout << BitVector::HexFormat << bv1 << BitVector::HexFormat << bv2;


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