41 "Unsupported size of C++ int type" );
102 || (startIdx.pos ==
pos && startIdx.bit <
bit),
"BITBUFFER",
103 "Given buffer start index is greater than this index." )
137 "BITBUFFER",
"32bit too narrow for endcoding BitBuffer::Index." )
275 {
return reinterpret_cast<char*
>( &
data[idx.pos] ); }
323 const Index& endIndex );
369 if( capacityNeeded >
storage.capacity() )
397 :
ma( monoAllocator )
415 if( capacityNeeded >
storage.capacity() )
438template<u
integer TCapacity>
462 if( capacityNeeded > TCapacity ) {
463 ALIB_ERROR(
"BITBUFFER",
"Local bit buffer cannot expand its capacity" )
574 template<ShiftOpRHS TW
idth,
typename TIntegral,
bool TMaskValue= false>
578 template<lang::ShiftOpRHS TW
idth,
typename TValue,
bool TMaskValue= false>
579 requires ( std::unsigned_integral<TValue>
580 && !std::same_as < TValue, bool>
583 static_assert(
bitsof(TValue) >= TWidth,
"Fixed size given greater than value type.");
586 || TWidth ==
bitsof(TValue)
587 || value == (value &
lang::LowerMask<TValue>(TWidth) ),
"BITBUFFER",
588 "Upper bits dirty while TMaskValue flag not set." )
590 if constexpr ( (TWidth <
bitsof(TValue)) && TMaskValue )
591 value&=
lang::LowerMask<TWidth, TValue >();
605 template<lang::ShiftOpRHS TW
idth,
typename TValue,
bool TMaskValue= false>
606 requires ( std::unsigned_integral<TValue>
607 && !std::same_as < TValue, bool>
610 static_assert(
bitsof(TValue) >= TWidth,
"Fixed size given greater than value type.");
613 || TWidth ==
bitsof(TValue)
614 || value == (value &
lang::LowerMask<TValue>(TWidth) ),
"BITBUFFER",
615 "Upper bits dirty while TMaskValue not set." )
617 if constexpr ( (TWidth <
bitsof(TValue)) && TMaskValue )
618 value&=
lang::LowerMask<TWidth, TValue>();
622 value>>= bitsWritten;
628 if(bitsWritten >= TWidth )
642 template<lang::ShiftOpRHS TW
idth,
typename TValue,
bool TMaskValue= false>
643 requires ( std::signed_integral<TValue> && !std::same_as<TValue, bool> )
645 using TUI=
typename std::make_unsigned<TValue>::type;
649 template<
typename TValue>
650 requires ( std::same_as<TValue, bool> )
675 template<
typename TValue,
bool TMaskValue= false>
676 requires ( std::integral<TValue> && (
sizeof(TValue) <=
sizeof(
TStorage) ) )
679 template<
typename TValue,
bool TMaskValue= false>
680 requires ( std::integral<TValue> && (
sizeof(TValue) <=
sizeof(
TStorage) ) )
684 "BITBUFFER",
"BitBufferBase::Write: Width too high: ", width )
689 "BITBUFFER",
"Upper bits dirty while TMaskValue not set.")
691 if constexpr (TMaskValue)
692 if( width <
bitsof(TValue) )
706 template<
typename TValue,
bool TMaskValue= false>
707 requires ( std::integral<TValue> && (
sizeof(TValue) >
sizeof(
TStorage) ) )
711 "BitBufferBase::Write: Width too high: ", width )
714 || value == (value &
lang::LowerMask<TValue>(width) ),
715 "BITBUFFER",
"Upper bits dirty while TMaskValue not set.")
717 if constexpr (TMaskValue)
718 if( width <=
bitsof(TValue) )
719 value&=
lang::LowerMask<TValue>(width);
735 value>>= bitsWritten;
741 if(bitsWritten >= width )
777 template<
typename TUIntegral>
778 requires ( std::unsigned_integral<TUIntegral> && !std::same_as< TUIntegral, bool> )
796 template<
typename TSIntegral>
797 requires ( std::signed_integral<TSIntegral> && !std::same_as< TSIntegral, bool> )
800 using TUnsigned=
typename std::make_unsigned<TSIntegral>::type;
802 : TUnsigned( (TUnsigned(-(value+ 1)) << 1) | 1 ) );
859 "BitBufferBase overflow detected. Ensure a higher capacity" )
900 template<lang::ShiftOpRHS TW
idth,
typename TResult=
int>
903 template<lang::ShiftOpRHS TW
idth,
typename TResult=
int>
904 requires ( std::integral<TResult> && ( TWidth <=
bitsof(
TStorage)) )
911 if constexpr ( TWidth == 1 ) {
922 static_assert(
bitsof(TResult) >= TWidth,
"Fixed size to read greater than given result type.");
924 result= TResult(
word );
938 result |= TResult( (
word << bitsRead )
941 result |= TResult(
word << bitsRead );
950 template<lang::ShiftOpRHS TW
idth,
typename TResult=
int>
951 requires ( std::integral<TResult> && ( TWidth >
bitsof(
TStorage)) )
953 static_assert(
bitsof(TResult) >= TWidth,
"Fixed size to read greater than given result type.");
956 TResult result =
word;
961 result|= TResult(
word) << bitsRead;
964 while( bitsRead < TWidth);
997 template<
typename TResult=
int>
1000 template<
typename TResult=
int>
1001 requires (
sizeof(TResult) <=
sizeof(
TStorage) )
1005 "Read size given greater than value type.")
1011 result= TResult(
word );
1022 result |= TResult( (
word << bitsRead )
1030 template<
typename TResult=
int>
1031 requires (
sizeof(TResult) >
sizeof(
TStorage) )
1035 "Read size given greater than value type.")
1042 result= TResult(
word );
1053 result |= TResult( (
word << bitsRead )
1060 TResult result = TResult(
word );
1065 result|= TResult(
word) << bitsRead;
1068 while( bitsRead < width);
1097 template<
typename TUIntegral>
1100 template<
typename TUIntegral>
1101 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 8) )
1104 template<
typename TUIntegral>
1105 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 16) )
1109 template<
typename TUIntegral>
1110 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 32) )
1113 template<
typename TUIntegral>
1114 requires( std::unsigned_integral<TUIntegral> && (
bitsof(TUIntegral) == 64) )
1117 template<
typename TSIntegral>
1118 requires( std::signed_integral<TSIntegral> )
1120 using TUnsigned=
typename std::make_unsigned<TSIntegral>::type;
1122 return result & 1 ? TSIntegral( -TSIntegral( result >> 1 ) - 1 )
1123 : TSIntegral( result >> 1 );
1156template<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 #"BitRWBase::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".