MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Static Public Attributes
MFM::BitVector< B > Class Template Reference

#include <BitVector.h>

Public Types

enum  { BITS = B }
 
typedef u32 BitUnitType
 

Public Member Functions

u32 GetLength () const
 
void WriteBit (u32 idx, bool bit)
 
bool ReadBit (u32 idx)
 
 BitVector ()
 
 BitVector (const BitVector &other)
 
 BitVector (const u32 *const values)
 
u32 Read (const u32 startIdx, const u32 length) const
 
void Write (const u32 startIdx, const u32 length, const u32 value)
 
u64 ReadLong (const u32 startIdx, const u32 length) const
 
void WriteLong (const u32 startIdx, const u32 length, const u64 value)
 
void SetBit (const u32 idx)
 
void ClearBit (const u32 idx)
 
bool ReadBit (const u32 idx) const
 
bool ToggleBit (const u32 idx)
 
void SetBits (const u32 startIdx, const u32 length)
 
void ClearBits (const u32 startIdx, const u32 length)
 
void StoreBits (const u32 bits, const u32 startIdx, const u32 length)
 
void Clear ()
 
void Print (ByteSink &ostream) const
 
void PrintBinary (ByteSink &ostream) const
 
bool Read (ByteSource &bs)
 
bool ReadBinary (ByteSource &bs)
 
bool operator== (const BitVector &rhs) const
 

Static Public Attributes

static const u32 BITS_PER_UNIT = sizeof(BitUnitType) * CHAR_BIT
 
static const u32 ARRAY_LENGTH = (BITS + BITS_PER_UNIT - 1) / BITS_PER_UNIT
 

Detailed Description

template<u32 B>
class MFM::BitVector< B >

A bit vector with reasonably fast operations

BITS should be a multiple of BITS_PER_UNIT (currently 32). Otherwise there will, at least, be some wasted space – and may be other issues.

Constructor & Destructor Documentation

template<u32 BITS>
MFM::BitVector< BITS >::BitVector ( )

Constructs a new BitVector. Set parameters of this BitVector using the template arguments. All bits are initialized to 0 .

template<u32 BITS>
MFM::BitVector< BITS >::BitVector ( const BitVector< B > &  other)

Copy-constructor for a BitVector. Creates an identical copy of the specified BitVector.

Parameters
otherThe BitVector to copy properties of.
template<u32 BITS>
MFM::BitVector< BITS >::BitVector ( const u32 *const  values)

Constructs a BitVector with specified inital value.

Parameters
valuesA pointer to a big-endian array of values to initialize this BitVector to. This array must contain at least as many bits as this BitVector can hold (specified as a template parameter).

Member Function Documentation

template<u32 BITS>
void MFM::BitVector< BITS >::Clear ( )

Sets all bits of this BitVector to 0 .

template<u32 B>
void MFM::BitVector< B >::ClearBit ( const u32  idx)
inline

Clears the bit at a specified index in this BitVector.

Parameters
idxThe index of the bit to clear, where the MSB is indexed at 0 .
template<u32 B>
void MFM::BitVector< B >::ClearBits ( const u32  startIdx,
const u32  length 
)
inline

Clear a contiguous range of bits, so they all have value 0.

Parameters
startIdxThe index of the first bit to clear inside this BitVector, where the MSB is indexed at 0 .
lengthThe number of bits to clear in this BitVector. If this length would exceed the number of bits in the BitVector, the excess length is ignored.
template<u32 B>
u32 MFM::BitVector< B >::GetLength ( ) const
inline

Gets the number of bits that may be indexed inside this BitVector.

Returns
The number of bits that may be indexed inside this BitVector.
Remarks
The actual number of bits used in this BitVector is a multiple of sizeof(BitUnitType) .
template<u32 BITS>
void MFM::BitVector< BITS >::Print ( ByteSink ostream) const

Prints the bits held in this BitVector to a specified ByteSink in hex format.

Parameters
ostreamThe ByteSink to print bits to.
template<u32 BITS>
void MFM::BitVector< BITS >::PrintBinary ( ByteSink ostream) const

Prints the bits held in this BitVector to a specified ByteSink in binary format.

Parameters
ostreamThe ByteSink to print bits to.
template<u32 BITS>
u32 MFM::BitVector< BITS >::Read ( const u32  startIdx,
const u32  length 
) const
inline

Reads up to 32 bits of a particular section of this BitVector.

Parameters
startIdxThe index of the first bit to read inside this BitVector, where the MSB is indexed at 0 .
lengthThe number of bits to read from this BitVector. This should be in the range [1,32] .
Returns
The bits read from the particular section of this BitVector.
template<u32 BITS>
bool MFM::BitVector< BITS >::Read ( ByteSource bs)

Load this BitVector from a specified ByteSource by reading BITS / 4 bytes in hex format. Skips leading whitespace. Returns true if all went well; returns false if insufficient consecutive hex bytes were available to fully initialize the BitVector . When false is returned the ByteSource will be positioned just before the first unacceptable byte encountered, and the BitVector will be unchanged.

Parameters
istreamThe ByteSource to read bytes from.
Returns
true if all went well, else false .
template<u32 BITS>
bool MFM::BitVector< BITS >::ReadBinary ( ByteSource bs)

Load this BitVector from a specified ByteSource by reading BITS bytes in binary format. Skips leading whitespace. Returns true if all went well; returns false if insufficient consecutive binary bytes were available to fully initialize the BitVector . When false is returned, the ByteSource will be positioned just before the first unacceptable byte encountered, and the BitVector will be unchanged.

Parameters
istreamThe ByteSource to read bytes from.
Returns
true if all went well, else false .
template<u32 BITS>
bool MFM::BitVector< BITS >::ReadBit ( u32  idx)

Reads a specified value from a particular bit in this BitVector.

Parameters
idxThe bit to read, where the MSB is index 0.
Returns
The value of the bit at idx index.
template<u32 B>
bool MFM::BitVector< B >::ReadBit ( const u32  idx) const
inline

Reads a single bit at a specified index from this BitVector.

Parameters
idxThe index of the bit to read.
Returns
1 if this bit is set, else 0 .
template<u32 BITS>
u64 MFM::BitVector< BITS >::ReadLong ( const u32  startIdx,
const u32  length 
) const
inline

Reads up to 64 bits of a particular section of this BitVector.

Parameters
startIdxThe index of the first bit to read inside this BitVector, where the MSB is indexed at 0 .
lengthThe number of bits to read from this BitVector. This should be in the range [0,64] .
Returns
The bits read from the particular section of this BitVector.
template<u32 B>
void MFM::BitVector< B >::SetBit ( const u32  idx)
inline

Sets the bit at a specified index in this BitVector.

Parameters
idxThe index of the bit to set, where the MSB is indexed at 0 .
template<u32 B>
void MFM::BitVector< B >::SetBits ( const u32  startIdx,
const u32  length 
)
inline

Set a contiguous range of bits, so they all have value 1.

Parameters
startIdxThe index of the first bit to set inside this BitVector, where the MSB is indexed at 0 .
lengthThe number of bits to set in this BitVector. If this length would exceed the number of bits in the BitVector, the excess length is ignored.
template<u32 BITS>
void MFM::BitVector< BITS >::StoreBits ( const u32  bits,
const u32  startIdx,
const u32  length 
)

Store a bit value (or pattern) into a contiguous range of bits, so they all have that value.

Parameters
bitsThe bits to store. The supplied bits are tiled onto the contiguous range of bits as many times as needed to cover the given length, with the u32 bits aligned to the underlying u32 storage units. To avoid confusion, pass only 0x0 or 0xffffffff as bits.
startIdxThe index of the first bit to store inside this BitVector, where the MSB is indexed at 0 .
lengthThe number of bits to store in this BitVector. If this length would exceed the number of bits in the BitVector, the excess length is ignored.
template<u32 BITS>
bool MFM::BitVector< BITS >::ToggleBit ( const u32  idx)

Flips the bit at a specified index in this BitVector. (i.e. bv[idx] = !bv[idx])

Parameters
idxThe index of the bit to toggle, where the MSB is indexed at 0 .
template<u32 BITS>
void MFM::BitVector< BITS >::Write ( const u32  startIdx,
const u32  length,
const u32  value 
)

Writes up to 32 bits of a specified u32 to a section of this BitVector.

Parameters
startIdxThe index of the first bit to write inside this BitVector, where the MSB is indexed at 0 .
lengthThe number of bits to write to this BitVector. This should be in the range [1,32] .
valueThe bits to write to the specified section of this BitVector.
template<u32 BITS>
void MFM::BitVector< BITS >::WriteBit ( u32  idx,
bool  bit 
)

Writes a specified value to a particular bit in this BitVector.

Parameters
idxThe bit to set, where the MSB is index 0.
bitThe value to set the bit at idx to.
template<u32 BITS>
void MFM::BitVector< BITS >::WriteLong ( const u32  startIdx,
const u32  length,
const u64  value 
)

Writes up to 64 bits of a specified u64 to a section of this BitVector.

Parameters
startIdxThe index of the first bit to write inside this BitVector, where the MSB is indexed at 0 .
lengthThe number of bits to write to this BitVector. This should be in the range [0,64] .
valueThe bits to write to the specified section of this BitVector.

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