Belle II Software development
BitStream Class Reference

Bit stream struct. More...

#include <BitStream.h>

Public Member Functions

 BitStream ()
 Default constructor for ROOT.
 
 BitStream (int n)
 Constructor with the reserved and cleared storage prepared for incoming bits.
 
void putNBits (unsigned int value, unsigned int n)
 Push n least significant bits of "value" to the stream.
 
unsigned int getNBits (unsigned int n)
 Fetch n bits.
 
size_t getPos () const
 Get current position in the stream.
 
void setPos (size_t pos)
 Set position in the stream.
 
std::vector< unsigned int > & getStore ()
 Get data vector.
 
void resize ()
 Resize data vector to the current position.
 

Protected Attributes

size_t m_pos
 Current position in the storage.
 
std::vector< unsigned int > m_store
 The bit storage.
 

Detailed Description

Bit stream struct.

Class contains vector of unsigned ints as a storage and current bit position in the storage. One can put and fetch up to 32 bits at once.

Definition at line 19 of file BitStream.h.

Constructor & Destructor Documentation

◆ BitStream() [1/2]

BitStream ( )
inline

Default constructor for ROOT.

Current position is at begining of the storage.

Definition at line 24 of file BitStream.h.

24: m_pos(0), m_store(1, 0) {}
std::vector< unsigned int > m_store
The bit storage.
Definition: BitStream.h:79
size_t m_pos
Current position in the storage.
Definition: BitStream.h:78

◆ BitStream() [2/2]

BitStream ( int  n)
inlineexplicit

Constructor with the reserved and cleared storage prepared for incoming bits.

Be sure the size is enough for incoming data since the class does not check bounds. Current position is at begining of the storage.

Definition at line 31 of file BitStream.h.

31: m_pos(0), m_store(n, 0) {}

Member Function Documentation

◆ getNBits()

unsigned int getNBits ( unsigned int  n)
inline

Fetch n bits.

Update current position accordingly

Parameters
n– how many bits fetch from the current position in the stream
Returns
n fetched bits

Definition at line 52 of file BitStream.h.

53 {
54 unsigned int bpos = m_pos % 32, wpos = m_pos / 32;
55// make sure we don't access memory we don't own
56 if (branch_unlikely(m_pos + n > m_store.size() * 32)) throw std::range_error("Not enough bits in stream");
57 unsigned int res = m_store[wpos] >> bpos;
58 if (bpos + n > 32) res |= m_store[wpos + 1] << (32 - bpos);
59 m_pos += n;
60 return res & (0xffffffffu >> (32 - n));
61 }
#define branch_unlikely(x)
A macro to tell the compiler that the argument x will be very likely be false.
Definition: Utils.h:134

◆ getPos()

size_t getPos ( ) const
inline

Get current position in the stream.

Definition at line 64 of file BitStream.h.

64{return m_pos;}

◆ getStore()

std::vector< unsigned int > & getStore ( )
inline

Get data vector.

Definition at line 70 of file BitStream.h.

70{return m_store;}

◆ putNBits()

void putNBits ( unsigned int  value,
unsigned int  n 
)
inline

Push n least significant bits of "value" to the stream.

Update current position accordingly.

Parameters
value– value to put in the stream
n– how many least signfificant bits of "value" put in the stream (n<=32)

Definition at line 37 of file BitStream.h.

38 {
39 unsigned int bpos = m_pos % 32, wpos = m_pos / 32;
40 value &= 0xffffffffu >> (32 - n);
41// check if we have enough space and double in size if necessary.
42 if (branch_unlikely(m_pos + n > m_store.size() * 32)) m_store.resize(2 * m_store.size(), 0);
43 m_store[wpos] |= value << bpos;
44 if (bpos + n > 32) m_store[wpos + 1] = value >> (32 - bpos);
45 m_pos += n;
46 }

◆ resize()

void resize ( )
inline

Resize data vector to the current position.

Definition at line 73 of file BitStream.h.

74 {
75 m_store.resize((m_pos + 31) / 32);
76 }

◆ setPos()

void setPos ( size_t  pos)
inline

Set position in the stream.

Definition at line 67 of file BitStream.h.

67{m_pos = pos;}

Member Data Documentation

◆ m_pos

size_t m_pos
protected

Current position in the storage.

Definition at line 78 of file BitStream.h.

◆ m_store

std::vector<unsigned int> m_store
protected

The bit storage.

Definition at line 79 of file BitStream.h.


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