42 "Unsupported size of C++ int type" );
104 || (startIdx.pos ==
pos && startIdx.bit <
bit),
"BITBUFFER",
105 "Given buffer start index is greater than this index." )
139 "BITBUFFER",
"32bit too narrow for endcoding BitBuffer::Index." )
277 {
return reinterpret_cast<char*
>( &
data[idx.pos] ); }
325 const Index& endIndex );
372 if( capacityNeeded >
storage.capacity() )
401 :
ma( monoAllocator )
419 if( capacityNeeded >
storage.capacity() )
442template<u
integer TCapacity>
467 if( capacityNeeded > TCapacity ) {
468 ALIB_ERROR(
"BITBUFFER",
"Local bit buffer cannot expand its capacity" )
581 template<ShiftOpRHS TW
idth,
typename TIntegral,
bool TMaskValue= false>
585 template<lang::ShiftOpRHS TW
idth,
typename TValue,
bool TMaskValue= false>
586 requires ( std::unsigned_integral<TValue>
587 && !std::same_as < TValue, bool>
589 void Write(TValue value ) {
590 static_assert(
bitsof(TValue) >= TWidth,
"Fixed size given greater than value type.");
593 || TWidth ==
bitsof(TValue)
594 || value == (value &
lang::LowerMask<TValue>(TWidth) ),
"BITBUFFER",
595 "Upper bits dirty while TMaskValue flag not set." )
597 if constexpr ( (TWidth <
bitsof(TValue)) && TMaskValue )
598 value&=
lang::LowerMask<TWidth, TValue >();
612 template<lang::ShiftOpRHS TW
idth,
typename TValue,
bool TMaskValue= false>
613 requires ( std::unsigned_integral<TValue>
614 && !std::same_as < TValue, bool>
616 void Write(TValue value ) {
617 static_assert(
bitsof(TValue) >= TWidth,
"Fixed size given greater than value type.");
620 || TWidth ==
bitsof(TValue)
621 || value == (value &
lang::LowerMask<TValue>(TWidth) ),
"BITBUFFER",
622 "Upper bits dirty while TMaskValue not set." )
624 if constexpr ( (TWidth <
bitsof(TValue)) && TMaskValue )
625 value&=
lang::LowerMask<TWidth, TValue>();
629 value>>= bitsWritten;
635 if(bitsWritten >= TWidth )
649 template<lang::ShiftOpRHS TW
idth,
typename TValue,
bool TMaskValue= false>
650 requires ( std::signed_integral<TValue> && !std::same_as<TValue, bool> )
651 void Write( TValue value)
653 using TUI=
typename std::make_unsigned<TValue>::type;
657 template<
typename TValue>
658 requires ( std::same_as<TValue, bool> )
659 void Write( TValue value)
681 template<
typename TValue,
bool TMaskValue= false>
682 requires ( std::integral<TValue> && (
sizeof(TValue) <=
sizeof(
TStorage) ) )
686 "BITBUFFER",
"BitBufferBase::Write: Width too high: ", width )
691 "BITBUFFER",
"Upper bits dirty while TMaskValue not set.")
693 if constexpr (TMaskValue)
694 if( width <
bitsof(TValue) )
720 template<
typename TValue,
bool TMaskValue= false>
721 requires ( std::integral<TValue> && (
sizeof(TValue) >
sizeof(
TStorage) ) )
725 "BitBufferBase::Write: Width too high: ", width )
729 "BITBUFFER",
"Upper bits dirty while TMaskValue not set.")
731 if constexpr (TMaskValue)
732 if( width <=
bitsof(TValue) )
749 value>>= bitsWritten;
755 if(bitsWritten >= width )
790 template<
typename TUIntegral>
791 requires ( std::unsigned_integral<TUIntegral> && !std::same_as< TUIntegral, bool> )
809 template<
typename TSIntegral>
810 requires ( std::signed_integral<TSIntegral> && !std::same_as< TSIntegral, bool> )
813 using TUnsigned=
typename std::make_unsigned<TSIntegral>::type;
815 : TUnsigned( (TUnsigned(-(value+ 1)) << 1) | 1 ) );
875 "BitBufferBase overflow detected. Ensure a higher capacity" )
912 template<lang::ShiftOpRHS TW
idth,
typename TResult=
int>
913 requires ( std::integral<TResult> && ( TWidth <=
bitsof(
TStorage)) )
920 if constexpr ( TWidth == 1 ) {
931 static_assert(
bitsof(TResult) >= TWidth,
"Fixed size to read greater than given result type.");
933 result= TResult(
word );
947 result |= TResult( (
word << bitsRead )
950 result |= TResult(
word << bitsRead );
971 template<lang::ShiftOpRHS TW
idth,
typename TResult=
int>
972 requires ( std::integral<TResult> && ( TWidth >
bitsof(
TStorage)) )
974 static_assert(
bitsof(TResult) >= TWidth,
"Fixed size to read greater than given result type.");
977 TResult result =
word;
982 result|= TResult(
word) << bitsRead;
985 while( bitsRead < TWidth);
1014 template<
typename TResult=
int>
1015 requires (
sizeof(TResult) <=
sizeof(
TStorage) )
1019 "Read size given greater than value type.")
1025 result= TResult(
word );
1036 result |= TResult( (
word << bitsRead )
1058 template<
typename TResult=
int>
1059 requires (
sizeof(TResult) >
sizeof(
TStorage) )
1063 "Read size given greater than value type.")
1070 result= TResult(
word );
1081 result |= TResult( (
word << bitsRead )
1088 TResult result = TResult(
word );
1093 result|= TResult(
word) << bitsRead;
1096 while( bitsRead < width);
1117 template<
typename TUIntegral>
1118 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 8) )
1127 template<
typename TUIntegral>
1128 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 16) )
1138 template<
typename TUIntegral>
1139 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 32) )
1149 template<
typename TUIntegral>
1150 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 64) )
1160 template<
typename TSIntegral>
1161 requires( std::signed_integral<TSIntegral> )
1163 using TUnsigned=
typename std::make_unsigned<TSIntegral>::type;
1165 return result & 1 ? TSIntegral( -TSIntegral( result >> 1 ) - 1 )
1166 : TSIntegral( result >> 1 );
1198template<u
integer TCapacity>
bool operator!=(const Index &rhs) const
bool operator<=(const Index &rhs) const
void SetFromByteOffset(uinteger byteOffset)
lang::ShiftOpRHS bit
Current bit index in the current word.
void Clear()
Sets this index to zero, hence pointing to the first bit in the buffer.
integer GetByteOffset(Index startIdx=Index(0, 0)) const
bool operator<(const Index &rhs) const
uinteger CountBits() const
static Index Decode64(uint64_t code)
bool operator>(const Index &rhs) const
uinteger pos
Index of the current word to read/write.
bool operator==(const Index &rhs) const
bool operator>=(const Index &rhs) const
Index()=default
Default constructor initializing members pos and bit to zero.
static Index Decode32(uint32_t code)
Index(uinteger pPos, lang::ShiftOpRHS pBit)
lang::ShiftOpRHS Bit() const
virtual ALIB_DLL uinteger Capacity() const =0
uinteger RemainingSize(const Index &idx) const
ALIB_DLL Index Unterminate(Index terminationIndex)
void SetWord(const Index &index, TStorage value)
virtual ~BitBufferBase()
Virtual destructor (does nothing, needed for abstract virtual class).
BitBufferBase() noexcept
Default Constructor (the only one).
ALIB_DLL Index Terminate(Index writerIndex)
ALIB_DLL void ToLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
virtual ALIB_DLL bool EnsureCapacity(uinteger bitsRequired, BitBufferBase::Index index)=0
ALIB_DLL void FromLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
TStorage GetWord(const Index &index) const
char * CharStream(Index idx=Index(0, 0))
BitBufferLocal() noexcept
Constructor.
virtual bool EnsureCapacity(uinteger bitsRequired, BitBufferBase::Index idx) override
TStorage storage[(TCapacity+bitsof(TStorage) - 1)/bitsof(TStorage)]
The array that holds the data.
virtual uinteger Capacity() const override
StdVectorMA< TStorage > storage
The vector that holds the data.
MonoAllocator & GetAllocator()
virtual uinteger Capacity() const override
BitBufferMA(MonoAllocator &monoAllocator, uinteger initialCapacity)
virtual bool EnsureCapacity(uinteger bitsRequired, BitBufferBase::Index idx) override
std::vector< TStorage > storage
The vector that holds the data.
virtual bool EnsureCapacity(uinteger bitsRequired, BitBufferBase::Index idx) override
virtual uinteger Capacity() const override
BitBuffer(uinteger initialCapacity)
BitBufferBase & bb
The bit buffer to write into. Provided on construction.
uinteger RemainingSize() const
BitBufferBase::Index idx
The current reading/writing index within bb.
BitBufferBase & GetBuffer() const
BitBufferBase::Index GetIndex() const
BitRWBase(BitBufferBase &buffer)
Reads bits from a BitBufferBase.
BitReader(BitBufferBase &buffer)
TResult Read(lang::ShiftOpRHS width)
ALIB_DLL uint64_t readUIntegral64()
BitBufferBase::TStorage TStorage
Local type alias (shortcut)
ALIB_DLL uint32_t readUIntegral32()
ALIB_DLL uint16_t readUIntegral16()
BitReader(BitBufferBase &buffer, const BitBufferBase::Index &index)
BitBufferBase::TStorage word
The current word, which is partly read and shifted to start with current bit.
void Reset(const BitBufferBase::Index &index)
TResult Read(lang::ShiftOpRHS width)
ALIB_DLL uint8_t readUIntegral8()
void Reset()
Resets this reader to the start of the bit buffer.
Writes bits into a BitBufferBase.
void Reset(const BitBufferBase::Index &index)
~BitWriter()
Destructs a bit writer. Invokes Flush().
void Write(TIntegral value)
void Write(lang::ShiftOpRHS width, TValue value)
void Write(lang::ShiftOpRHS width, TValue value)
void Reset()
Resets the internal index of this writer to the start of the bit buffer.
void Write(TSIntegral value)
BitBufferBase::TStorage word
The current word, which is partly written and not stored in buffer, yet.
BitWriter(BitBufferBase &buffer)
BitWriter(BitBufferBase &buffer, const BitBufferBase::Index &index)
ALIB_DLL void writeUIntegral(uint8_t value)
void Write(TUIntegral value)
BitBufferBase::TStorage TStorage
Local type alias (shortcut)
#define ALIB_ERROR(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
constexpr int Log2OfSize()
constexpr TIntegral LowerBits(TIntegral value)
constexpr TIntegral LowerMask()
bitbuffer::BitReader BitReader
Type alias in namespace alib.
std::vector< T, StdMA< T > > StdVectorMA
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
bitbuffer::BitBufferMA BitBufferMA
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
bitbuffer::BitBufferLocal< TCapacity > BitBufferLocal
Type alias in namespace alib.
bitbuffer::BitBuffer BitBuffer
Type alias in namespace alib.
bitbuffer::BitWriter BitWriter
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
int ShiftOpRHS
Type alias in namespace alib.