ALib C++ Library
Library Version: 2312 R0
Documentation generated by doxygen
Public Static Methods | Public Methods | Private Static Fields | Private Fields | List of all members
BitBufferBase::Index Class Reference

#include <bitbuffer.hpp>

Class Description

Defines a bit position within outer class BitBufferBase. A bit position is determined by the index in the storage array along with the number of the currently written (or read) bit. Types BitWriter and BitReader use this type to define their current write (read) position.

Methods Encode32 and Encode64 shorten the information, by storing the bit position in the upper bits of a 32, respectively 64 bit value. This is useful whenever a broader number of bit buffer indices are to be stored. The use case to mention here is "lazy decoding of data", where only the index to the bit buffer is kept in memory.) positions

Definition at line 85 of file bitbuffer.hpp.

Public Static Methods

static Index Decode32 (uint32_t code)
 
static Index Decode64 (uint64_t code)
 

Public Methods

 Index ()=default
 
 Index (uinteger pPos, ShiftOpRHS pBit)
 
ShiftOpRHS Bit () const
 
void Clear ()
 
uinteger CountBits () const
 
uint32_t Encode32 ()
 
uint64_t Encode64 ()
 
integer GetByteOffset (Index startIdx=Index(0, 0)) const
 
bool IsAligned () const
 
bool operator!= (const Index &rhs) const
 
bool operator< (const Index &rhs) const
 
bool operator<= (const Index &rhs) const
 
bool operator== (const Index &rhs) const
 
bool operator> (const Index &rhs) const
 
bool operator>= (const Index &rhs) const
 
uinteger Pos () const
 
void SetFromByteOffset (uinteger size)
 

Private Static Fields

static constexpr ShiftOpRHS BIT_COUNT_SIZE = sizeof(TStorage) == 8 ? 6 : 5
 

Private Fields

ShiftOpRHS bit = 0
 Current bit index in the current word.
 
uinteger pos = 0
 Index of the current word to read/write.
 

Constructor & Destructor Documentation

◆ Index() [1/2]

Index ( )
default

Default constructor initializing members pos and bit to zero.

◆ Index() [2/2]

Index ( uinteger  pPos,
ShiftOpRHS  pBit 
)
inline

Constructor

Parameters
pPosThe initial value for member pos.
pBitThe initial value for member bit.

Definition at line 112 of file bitbuffer.hpp.

Member Function Documentation

◆ Bit()

ShiftOpRHS Bit ( ) const
inline

Returns the number of the actual bit in the actual word of the buffer buffer.

Returns
The number of the next bit to read/write.

Definition at line 127 of file bitbuffer.hpp.

◆ Clear()

void Clear ( )
inline

Sets this index to zero, hence pointing to the first bit in the buffer.

Definition at line 143 of file bitbuffer.hpp.

◆ CountBits()

uinteger CountBits ( ) const
inline

Returns the number of bits used in respect to this index.

Returns
The number of bits written or read.

Definition at line 179 of file bitbuffer.hpp.

◆ Decode32()

static Index Decode32 ( uint32_t  code)
inlinestatic

Static method that decodes an index information, encoded with Encode32, to an instance of this class.

Parameters
codeThe encoded information.
Returns
The decoded index.

Definition at line 229 of file bitbuffer.hpp.

Here is the call graph for this function:

◆ Decode64()

static Index Decode64 ( uint64_t  code)
inlinestatic

Static method that decodes an index information, encoded with Encode64, to an instance of this class.

Parameters
codeThe encoded information.
Returns
The decoded index.

Definition at line 249 of file bitbuffer.hpp.

◆ Encode32()

uint32_t Encode32 ( )
inline

Encodes this index information into a 32-bit variable by using the upper 5 (or 6) bits for the bit index. As a result, the possible value range of index data is reduced. The reduction depends on the platform's size of type int. In case of 32-bit, five bits are needed to store the bit position. In the case of 64-bit, six bits are needed.
As the underlying TStorage type changes as well, in both cases, the resulting addressable storage bytes is limited to the same value:

  • TStorage 64-bit: 2^(32-6) * 8 bytes = 512 megabytes
  • TStorage 32-bit: 2^(32-5) * 4 bytes = 512 megabytes

In case bit buffers grow to over half a gigabyte, 64-bit encoding should be performed by using alternative method Encode64.

Returns
The encoded index.

Definition at line 202 of file bitbuffer.hpp.

◆ Encode64()

uint64_t Encode64 ( )
inline

Encodes this index information into a 64-bit value by using the upper 5 5 (or 6) bits for the bit index.

See also
For a shorter encoding, limited to bit buffer sizes of 512 megabytes, see method Encode32.
Returns
The encoded index.

Definition at line 218 of file bitbuffer.hpp.

◆ GetByteOffset()

integer GetByteOffset ( Index  startIdx = Index(0, 0)) const
inline

Returns the size of the memory from given startIdx to this index occupied by the internal storage words of the buffer.

Parameters
startIdxThe starting index. Defaults to {0,0}.
Returns
The size of the buffer (part) in bytes.

Definition at line 156 of file bitbuffer.hpp.

◆ IsAligned()

bool IsAligned ( ) const
inline

Returns true, if the next bit to read/write is the first of the current storage word in the buffer. Alignment of buffers may become important when buffers are serialized (e.g. to mass storage devices). Method BitBufferBase::Terminate may be used to receive an aligned index.

Returns
The result of Bit() == 0.

Definition at line 138 of file bitbuffer.hpp.

◆ operator!=()

bool operator!= ( const Index rhs) const
inline

Comparison operator.

Parameters
rhsThe right hand side argument of the comparison.
Returns
true if this object does not equal rhs, false otherwise.

Definition at line 271 of file bitbuffer.hpp.

◆ operator<()

bool operator< ( const Index rhs) const
inline

Comparison operator.

Parameters
rhsThe right hand side argument of the comparison.
Returns
true if this object is smaller than rhs, false otherwise.

Definition at line 282 of file bitbuffer.hpp.

◆ operator<=()

bool operator<= ( const Index rhs) const
inline

Comparison operator.

Parameters
rhsThe right hand side argument of the comparison.
Returns
true if this object is smaller or equal than rhs, false otherwise.

Definition at line 294 of file bitbuffer.hpp.

◆ operator==()

bool operator== ( const Index rhs) const
inline

Comparison operator.

Parameters
rhsThe right hand side argument of the comparison.
Returns
true if this object equals rhs, false otherwise.

Definition at line 259 of file bitbuffer.hpp.

◆ operator>()

bool operator> ( const Index rhs) const
inline

Comparison operator.

Parameters
rhsThe right hand side argument of the comparison.
Returns
true if this object is greater than rhs, false otherwise.

Definition at line 317 of file bitbuffer.hpp.

◆ operator>=()

bool operator>= ( const Index rhs) const
inline

Comparison operator.

Parameters
rhsThe right hand side argument of the comparison.
Returns
true if this object is greater or equal than rhs, false otherwise.

Definition at line 306 of file bitbuffer.hpp.

◆ Pos()

uinteger Pos ( ) const
inline

Returns the index of the actual storage word in the buffer.

Returns
The index of the current word containing the next bit to read/write.

Definition at line 121 of file bitbuffer.hpp.

◆ SetFromByteOffset()

void SetFromByteOffset ( uinteger  size)
inline

Sets this index to point to the word and bit given by a byte offset.
This method is useful when bit buffers are deserialized from character streams.

Parameters
sizeThe position within the buffer in bytes.

Definition at line 169 of file bitbuffer.hpp.

Member Data Documentation

◆ BIT_COUNT_SIZE

constexpr ShiftOpRHS BIT_COUNT_SIZE = sizeof(TStorage) == 8 ? 6 : 5
staticconstexprprivate

The number of bits needed to encode the bit number.

Definition at line 94 of file bitbuffer.hpp.


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