gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
BitStream Class Reference

#include <BitStream.H>

Collaboration diagram for BitStream:
[legend]

Public Member Functions

 BitStream ()
 Constructor. More...
 
virtual ~BitStream ()
 Destructor. More...
 
std::vector< VTYPE >::size_type size () const
 
float byteSize () const
 
template<typename T >
reverseBits (T bits) const
 
template<typename T >
reverseBits (T bits, unsigned int N) const
 
template<typename T >
BitStreamoperator<< (T bits)
 
template<typename T >
BitStreampush_back (const T bits, const int N)
 
template<typename T >
pop_back (const unsigned int N)
 
template<typename T >
pop_front (const unsigned int N)
 
BitStreamrotateL (const unsigned int N)
 
BitStreamrotateR (const unsigned int N)
 
std::ostream & hexDump (std::ostream &stream)
 
void clear ()
 
int reserve (std::vector< VTYPE >::size_type N)
 
std::vector< VTYPE >::size_type capacity () const
 
void dump (void)
 
void dumpHex (void)
 
template<typename T >
getBits (std::vector< VTYPE >::size_type i, unsigned int N) const
 
template<typename T >
operator[] (std::vector< VTYPE >::size_type i) const
 
template<typename T >
std::vector< std::vector< VTYPE >::size_type > find (T toFind, const unsigned int N) const
 
std::vector< std::vector< VTYPE >::size_type > find (BitStream toFind, const unsigned int N) const
 

Protected Types

typedef unsigned int VTYPE
 Use this type for the basic stream type. More...
 

Protected Member Functions

void testMask (unsigned int mSize) const
 
void testMask (unsigned int mSize, VTYPE compareAgainst) const
 
VTYPE genMask (int M) const
 

Private Member Functions

BitStreampush_backVType (const VTYPE *tempBits, int N, const int sizeOfT)
 
unsigned int VTYPEBits () const
 
unsigned int takenBits () const
 
VTYPE maskBitsToRight (const unsigned int M, const VTYPE &bits) const
 
VTYPE maskOffsetBitsToRight (const unsigned int M, const unsigned int offset, const VTYPE &bits) const
 
VTYPE maskBitsToLeft (const unsigned int M, const VTYPE bits) const
 
void reverseBits (unsigned char *bits, const unsigned int N) const
 
VTYPE shiftLeftSubword (std::vector< VTYPE >::iterator firstWord, std::vector< VTYPE >::iterator lastWord, const unsigned int N)
 
VTYPE shiftRightSubword (std::vector< VTYPE >::iterator firstWord, std::vector< VTYPE >::iterator lastWord, const unsigned int N)
 

Private Attributes

std::vector< VTYPEdata
 The array to hold the bitstream. More...
 
int freeBits
 The number of free bits in the array. More...
 

Static Private Attributes

static const unsigned char revChars [] = {0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255}
 Characters reversed. 8 bit reversals. More...
 

Friends

std::ostream & operator<< (std::ostream &stream, const BitStream &bitStream)
 

Detailed Description

Create bit streams using this class.

This stream is packed with the oldest entries at the beginning, newest entries added to the end. I.e. push_back. The front is the beginning and the back is the end. The char 0x 80 for example, will be packed as 0b 1000 0000.

In general MSBs are regarded as being on the 'left' and LSBs are regarded as being on the 'right'.

If you push_back in more bits then the data type you provide, leading zeros will be added, for example : If you pack 9 bits from a char, 0x8 again, you get : 0100 0000 0

Here are some examples :

short s=0b1111010100001010; short s2=0b1100110011110000; int i3=0x1f2f3f4f;

bitStream.push_back(s,4); // add specific bits from a variable cout<<bitStream<<endl; cout<<(bitset<4>)s<<'
'<<endl;

bitStream.push_back(s2,16); cout<<bitStream<<endl; cout<<(bitset<4>)s<<(bitset<16>)s2<<'
'<<endl;

bitStream<<i3; // add all bits from a variable cout<<bitStream<<endl; cout<<(bitset<4>)s<<(bitset<16>)s2<<(bitset<32>)i3<<'
'<<endl;

to get leading zeros, increase the number of bits to push_back. For example, for a char c : bitsStream.push_back(c, 10); This will add two leading zeros. <>

Outputs the following; 1010 1010

10101100110011110000 10101100110011110000

1010110011001111000000011111001011110011111101001111 1010110011001111000000011111001011110011111101001111 <>

Examples:
BitStreamTest.C, BitStreamTest3.C, and BitStreamTest4.C.

Definition at line 83 of file BitStream.H.

Member Typedef Documentation

◆ VTYPE

typedef unsigned int BitStream::VTYPE
protected

Use this type for the basic stream type.

Definition at line 85 of file BitStream.H.

Constructor & Destructor Documentation

◆ BitStream()

BitStream::BitStream ( )

Constructor.

Definition at line 29 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~BitStream()

BitStream::~BitStream ( )
virtual

Destructor.

Definition at line 35 of file BitStream.C.

Here is the caller graph for this function:

Member Function Documentation

◆ byteSize()

float BitStream::byteSize ( ) const

How many bytes in the stream.

Returns
How many bytes in the stream, can be fractional when some bits are free.

Definition at line 43 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ capacity()

std::vector< BitStream::VTYPE >::size_type BitStream::capacity ( ) const

Return the capacity (in bits) of the bitstream

Returns
the capacity in bit

Definition at line 199 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear()

void BitStream::clear ( )

Clear the BitStream to an empty state.

Definition at line 182 of file BitStream.C.

Here is the caller graph for this function:

◆ dump()

void BitStream::dump ( void  )

print out with spaces between vector words ... good for debugging.

Definition at line 218 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dumpHex()

void BitStream::dumpHex ( void  )

print out with spaces between vector words ... good for debugging. The last word is printed including the freeBits

Definition at line 232 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find() [1/2]

template<typename T >
std::vector<std::vector<VTYPE>::size_type> BitStream::find ( toFind,
const unsigned int  N 
) const
inline

Search through the bits of the contained data.

Parameters
toFindThe bits to find in this BitStream
Nthe number of LSBs to use from the variable toFind.
Returns
A vector of indexes where toFind exists in the stream.

Definition at line 435 of file BitStream.H.

Here is the call graph for this function:

◆ find() [2/2]

std::vector< std::vector< BitStream::VTYPE >::size_type > BitStream::find ( BitStream  toFind,
const unsigned int  N 
) const

Search through the bits of the contained data.

Parameters
toFindThe bitStream to find in this BitStream
Nthe number of bits to use from the variable toFind.
Returns
A vector of indexes where toFind exists in the stream.

Definition at line 244 of file BitStream.C.

Here is the call graph for this function:

◆ genMask()

VTYPE BitStream::genMask ( int  M) const
inlineprotected

Generate an M bit mask.

Returns
VTYPE with the first M bits set.

Definition at line 215 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getBits()

template<typename T >
T BitStream::getBits ( std::vector< VTYPE >::size_type  i,
unsigned int  N 
) const
inline

Get bits from the stream. Location starting i, the number of bits is sizeof(T)*CHAR_BIT.

Parameters
iThe location to retrieve from.
NThe number of bits to retrieve.
Returns
The bits from the stream starting at location i, of length N located in the LSB of the return variable
Template Parameters
TThe type to return the bits in.

Definition at line 402 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hexDump()

std::ostream & BitStream::hexDump ( std::ostream &  stream)

Output the bit sequence in hexadecimal format.

Parameters
streamThe ostream to output to.
Returns
The ostream which was output to.

Definition at line 174 of file BitStream.C.

Here is the caller graph for this function:

◆ maskBitsToLeft()

VTYPE BitStream::maskBitsToLeft ( const unsigned int  M,
const VTYPE  bits 
) const
inlineprivate

Get the last M bits from a word. Returns these bits left shifted so to the MSB location.

Parameters
MThe number of bits to get.
bitsThe bits to get the M bits from.
Returns
The M bits from the LSB location in the MSB location

Definition at line 173 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ maskBitsToRight()

VTYPE BitStream::maskBitsToRight ( const unsigned int  M,
const VTYPE bits 
) const
inlineprivate

Get the first M bits from a word. Returns these bits right shifted so to the LSB location.

Parameters
MThe number of bits to get.
bitsThe bits to get the M bits from.
Returns
The M bits from the MSB location in the LSB location

Definition at line 150 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ maskOffsetBitsToRight()

VTYPE BitStream::maskOffsetBitsToRight ( const unsigned int  M,
const unsigned int  offset,
const VTYPE bits 
) const
inlineprivate

Get M bits from a word, with an offset from the MSB. Returns these bits right shifted so to the LSB location.

Parameters
MThe number of bits to get.
offsetThe number of bits from the MSB in to get the bits from.
bitsThe bits to get the M bits from.
Returns
The M bits from the MSB location in the LSB location

Definition at line 162 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator<<()

template<typename T >
BitStream& BitStream::operator<< ( bits)
inline

Add a simple type to the bit stream.

Parameters
bitsThe data to add to the stream.
Template Parameters
TThe type of the data.
Returns
A reference to this class.

Definition at line 279 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator[]()

template<typename T >
T BitStream::operator[] ( std::vector< VTYPE >::size_type  i) const
inline

Get bits from the stream. Location starting i, the number of bits is sizeof(T)*CHAR_BIT.

Parameters
iThe location to retrieve from.
Returns
The bits from the stream starting at location i, of size sizeof(T)*CHAR_BIT
Template Parameters
TThe type to return the bits in.

Definition at line 425 of file BitStream.H.

◆ pop_back()

template<typename T >
T BitStream::pop_back ( const unsigned int  N)
inline

Pop N bits from the end of the stream.

If the requested bit count (N) is larger then the provided return type, then only sizeof(T)*8 bits are poped from the back.

Parameters
NThe number of bits to pop.
Template Parameters
TThe type of the data to return.
Returns
N bits from the back of the stream if sizeof(T)*8 >= N. If sizeof(T)*8 < N, return bits from the end, where the returned variable (type T) contains the sizeof(T)*8 bits from the end - the other bits (N-sizeof(T)*8) are lost.

Definition at line 308 of file BitStream.H.

Here is the call graph for this function:

◆ pop_front()

template<typename T >
T BitStream::pop_front ( const unsigned int  N)
inline

Pop N bits from the front of the stream.

If the requested bit count (N) is larger then the provided return type, then only sizeof(T)*8 bits are poped from the front.

Parameters
NThe number of bits to pop.
Template Parameters
TThe type of the data to return.
Returns
N bits from the front of the stream if sizeof(T)*8 >= N. If sizeof(T)*8 < N, return bits from the front, where the returned variable (type T) contains the sizeof(T)*8 bits from the front - the other bits (N-sizeof(T)*8) are lost.

Definition at line 340 of file BitStream.H.

Here is the call graph for this function:

◆ push_back()

template<typename T >
BitStream& BitStream::push_back ( const T  bits,
const int  N 
)
inline

Pack some bits at the end of the stream. The input variables N least significant bits are packed into the stream.

Parameters
bitsthe variable to get bits from in order to fill the bit stream
NThe number of bits >0 to store.
Template Parameters
TThe type of input bit variable
Returns
A reference to this BitStream.

Definition at line 292 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ push_backVType()

BitStream & BitStream::push_backVType ( const VTYPE tempBits,
int  N,
const int  sizeOfT 
)
private

Pack some bits at the end of the stream. The input variables N least significant bits are packed into the stream.

Parameters
tempBitsA pointer to the vector of bits to store.
NThe number of bits to store >0
sizeOfTThe size of the type of the original variable from the templated method push_back_back
Returns
A reference to this object.

Definition at line 47 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reserve()

int BitStream::reserve ( std::vector< VTYPE >::size_type  N)

Resize the underlying data structure to have enough memory to store N bits.

Parameters
NThe number of bits to store.
Returns
NO_ERROR on success, or the resulting error number on failure.

Definition at line 187 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reverseBits() [1/3]

void BitStream::reverseBits ( unsigned char *  bits,
const unsigned int  N 
) const
inlineprivate

Reverse the provided bits in the character array.

This bit reverse method uses an 8 bit hash lookup table to reverse the bits in a very fast manner.

Parameters
bitsthe variable to bit reverse.
NThe number of char to reverse.

Definition at line 185 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reverseBits() [2/3]

template<typename T >
T BitStream::reverseBits ( bits) const
inline

Reverse all of the provided bits.

This bit reverse method uses an 8 bit hash lookup table to reverse the bits in a very fast manner.

Parameters
bitsthe variable to bit reverse. All of the bits are reversed.
Template Parameters
TThe type of input bit variable

Definition at line 252 of file BitStream.H.

Here is the call graph for this function:

◆ reverseBits() [3/3]

template<typename T >
T BitStream::reverseBits ( bits,
unsigned int  N 
) const
inline

Reverse a subset of the provided bits (possibly all of the bits).

This bit reverse method uses an 8 bit hash lookup table to reverse the bits in a very fast manner.

Parameters
bitsthe variable to bit reverse. Some or all of the bits are reversed, depending on N, the number of bits to reverse requested.
NThe number of bits >0 to store.
Template Parameters
TThe type of input bit variable

Definition at line 265 of file BitStream.H.

Here is the call graph for this function:

◆ rotateL()

BitStream & BitStream::rotateL ( const unsigned int  N)

Rotate the stream to the left, left most bits are rotated to the right as required.

Parameters
NThe number of bits to rotate left by.
Returns
A reference to this class.

Definition at line 97 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rotateR()

BitStream & BitStream::rotateR ( const unsigned int  N)

Rotate the stream to the right, right most bits are rotated to the right as required.

Parameters
NThe number of bits to rotate left by.
Returns
A reference to this class.

Definition at line 145 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shiftLeftSubword()

BitStream::VTYPE BitStream::shiftLeftSubword ( std::vector< VTYPE >::iterator  firstWord,
std::vector< VTYPE >::iterator  lastWord,
const unsigned int  N 
)
private

Shift left by N sub-word length bits, returning the left most N bits.

Parameters
firstWordThe first word to be shifted left, its left most bits are returned in the LSB location
lastWordThe last word to be shifted left, its right most bits are zero upon return.
Returns
firstWord's left most N bits (MSBs) shifted to the right (LSB) location. The lastWord's N LSBs are zero.

Definition at line 80 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shiftRightSubword()

BitStream::VTYPE BitStream::shiftRightSubword ( std::vector< VTYPE >::iterator  firstWord,
std::vector< VTYPE >::iterator  lastWord,
const unsigned int  N 
)
private

Shift right by N sub-word length bits, returning the right most (lest significant) N bits shifted to the MSB location.

Parameters
firstWordThe first word to be shifted right, its left most bits are zeroed on return.
lastWordThe last word to be shifted left, its right most bits are returned in the MSB location
Returns
lastWord's right most N bits (LSBs). The firstWord's N MSBs are zero.

Definition at line 126 of file BitStream.C.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

std::vector< BitStream::VTYPE >::size_type BitStream::size ( ) const

How many bits in the stream.

Returns
How many bits in the stream.

Definition at line 39 of file BitStream.C.

Here is the caller graph for this function:

◆ takenBits()

unsigned int BitStream::takenBits ( ) const
inlineprivate

The number of bits used (taken) in the last word.

Returns
The number of bits used in the last word.

Definition at line 141 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testMask() [1/2]

void BitStream::testMask ( unsigned int  mSize) const
inlineprotected

When debugging this method can be executed to test that the mask is being generated correctly.

Parameters
mSizeThe mask size in bits

Definition at line 90 of file BitStream.H.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testMask() [2/2]

void BitStream::testMask ( unsigned int  mSize,
VTYPE  compareAgainst 
) const
inlineprotected

When debugging this method can be executed to test that the mask is being generated correctly.

Parameters
mSizeThe mask size in bits
compareAgainstThe pre-generated mask to compare against.

Definition at line 100 of file BitStream.H.

◆ VTYPEBits()

unsigned int BitStream::VTYPEBits ( ) const
inlineprivate

The number of bits in the base data type.

Returns
The number of bits in the base data type.

Definition at line 134 of file BitStream.H.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  stream,
const BitStream bitStream 
)
friend

Overload the ostream operator for printing. This method outputs the string representation of the bit stream.

Parameters
streamThe ostream to output the bitstream to.
iobThe iobitstream to output.
Returns
The ostream with the string rep. of the bitstream output.

Definition at line 204 of file BitStream.C.

Member Data Documentation

◆ data

std::vector<VTYPE> BitStream::data
private

The array to hold the bitstream.

Definition at line 116 of file BitStream.H.

◆ freeBits

int BitStream::freeBits
private

The number of free bits in the array.

Definition at line 117 of file BitStream.H.

◆ revChars

const unsigned char BitStream::revChars = {0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255}
staticprivate

Characters reversed. 8 bit reversals.

Definition at line 120 of file BitStream.H.


The documentation for this class was generated from the following files:
gtkIOStream: BitStream Class Reference
GTK+ IOStream  Beta