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>
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>
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> )
652 using TUI=
typename std::make_unsigned<TValue>::type;
656 template<
typename TValue>
657 requires ( std::same_as<TValue, bool> )
682 template<
typename TValue,
bool TMaskValue= false>
683 requires ( std::integral<TValue> && (
sizeof(TValue) <=
sizeof(
TStorage) ) )
686 template<
typename TValue,
bool TMaskValue= false>
687 requires ( std::integral<TValue> && (
sizeof(TValue) <=
sizeof(
TStorage) ) )
691 "BITBUFFER",
"BitBufferBase::Write: Width too high: ", width )
696 "BITBUFFER",
"Upper bits dirty while TMaskValue not set.")
698 if constexpr (TMaskValue)
699 if( width <
bitsof(TValue) )
713 template<
typename TValue,
bool TMaskValue= false>
714 requires ( std::integral<TValue> && (
sizeof(TValue) >
sizeof(
TStorage) ) )
718 "BitBufferBase::Write: Width too high: ", width )
721 || value == (value &
lang::LowerMask<TValue>(width) ),
722 "BITBUFFER",
"Upper bits dirty while TMaskValue not set.")
724 if constexpr (TMaskValue)
725 if( width <=
bitsof(TValue) )
726 value&=
lang::LowerMask<TValue>(width);
742 value>>= bitsWritten;
748 if(bitsWritten >= width )
784 template<
typename TUIntegral>
785 requires ( std::unsigned_integral<TUIntegral> && !std::same_as< TUIntegral, bool> )
803 template<
typename TSIntegral>
804 requires ( std::signed_integral<TSIntegral> && !std::same_as< TSIntegral, bool> )
807 using TUnsigned=
typename std::make_unsigned<TSIntegral>::type;
809 : TUnsigned( (TUnsigned(-(value+ 1)) << 1) | 1 ) );
868 "BitBufferBase overflow detected. Ensure a higher capacity" )
909 template<lang::ShiftOpRHS TW
idth,
typename TResult=
int>
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 );
959 template<lang::ShiftOpRHS TW
idth,
typename TResult=
int>
960 requires ( std::integral<TResult> && ( TWidth >
bitsof(
TStorage)) )
962 static_assert(
bitsof(TResult) >= TWidth,
"Fixed size to read greater than given result type.");
965 TResult result =
word;
970 result|= TResult(
word) << bitsRead;
973 while( bitsRead < TWidth);
1006 template<
typename TResult=
int>
1009 template<
typename TResult=
int>
1010 requires (
sizeof(TResult) <=
sizeof(
TStorage) )
1014 "Read size given greater than value type.")
1020 result= TResult(
word );
1031 result |= TResult( (
word << bitsRead )
1039 template<
typename TResult=
int>
1040 requires (
sizeof(TResult) >
sizeof(
TStorage) )
1044 "Read size given greater than value type.")
1051 result= TResult(
word );
1062 result |= TResult( (
word << bitsRead )
1069 TResult result = TResult(
word );
1074 result|= TResult(
word) << bitsRead;
1077 while( bitsRead < width);
1106 template<
typename TUIntegral>
1109 template<
typename TUIntegral>
1110 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 8) )
1113 template<
typename TUIntegral>
1114 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 16) )
1118 template<
typename TUIntegral>
1119 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 32) )
1122 template<
typename TUIntegral>
1123 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 64) )
1126 template<
typename TSIntegral>
1127 requires( std::signed_integral<TSIntegral> )
1129 using TUnsigned=
typename std::make_unsigned<TSIntegral>::type;
1131 return result & 1 ? TSIntegral( -TSIntegral( result >> 1 ) - 1 )
1132 : TSIntegral( result >> 1 );
1165template<u
integer TCapacity>
#define ALIB_ERROR(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
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
uinteger RemainingSize(const Index &idx) const
Index Unterminate(Index terminationIndex)
virtual uinteger Capacity() const =0
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).
Index Terminate(Index writerIndex)
void ToLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
void FromLittleEndianEncoding(const Index &startIndex, const Index &endIndex)
TStorage GetWord(const Index &index) const
virtual bool EnsureCapacity(uinteger bitsRequired, BitBufferBase::Index index)=0
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)
uint64_t readUIntegral64()
BitBufferBase::TStorage TStorage
Local type alias (shortcut).
uint32_t readUIntegral32()
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 ReadBits(lang::ShiftOpRHS width)
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 WriteInt(TUIntegral value)
void Reset()
Resets the internal index of this writer to the start of the bit buffer.
void WriteBits(lang::ShiftOpRHS width, TValue 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)
void writeUIntegral(uint8_t value)
void WriteInt(TSIntegral value)
BitBufferBase::TStorage TStorage
Local type alias (shortcut).
void WriteBits(TIntegral value)
constexpr TIntegral LowerMask()
constexpr int Log2OfSize()
constexpr TIntegral LowerBits(TIntegral value)
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
bitbuffer::BitReader BitReader
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
bitbuffer::BitBufferMA BitBufferMA
Type alias in namespace alib.
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.
std::vector< T, StdMA< T > > StdVectorMA
Type alias in namespace alib.
int ShiftOpRHS
Type alias in namespace alib.