8#ifndef HPP_ALIB_BITBUFFER_AC_V1_ALGOS
9#define HPP_ALIB_BITBUFFER_AC_V1_ALGOS 1
11#if !defined(HPP_ALIB_MONOMEM_DETAIL_ARRAY_COMPRESSION_ALLOW)
12# error "ALib sources with ending '.inl' must not be included from outside."
15#if !defined(HPP_ALIB_BITBUFFER_AC_V1_HUFFMAN)
19namespace alib {
namespace bitbuffer {
namespace ac_v1 {
31 using TUI=
typename std::make_unsigned<TI>::type;
38 for(
size_t i= 0; i < data.
length(); ++i)
40 TUI val= data.
get( i );
41 int bytes=
sizeof(TUI);
59 for(
size_t i= 0; i < data.
length(); ++i)
61 TUI val= data.
get( i );
66 he.
Write(uint8_t (val >> bits ) );
83 using TUI=
typename std::make_unsigned<TI>::type;
88 for(
size_t i= 0; i < data.
length(); ++i)
91 int bytes=
sizeof(TUI);
116 for(
size_t i= 0; i < data.
length(); ++i)
132 using TUI=
typename std::make_unsigned<TI>::type;
133 for(
size_t i= 0; i < data.
length(); ++i)
146 using TUI=
typename std::make_unsigned<TI>::type;
155 bw.
Write<lang::Log2OfSize<TUI>() + 1>( bitCnt );
160 for(
size_t i= 0; i < data.
length(); ++i )
173 using TUI=
typename std::make_unsigned<TI>::type;
174 auto bitCnt= br.
Read<lang::Log2OfSize<TUI>() + 1>();
175 TUI min = br.
Read<TUI>();
179 for(
size_t i= 0; i < data.
length(); ++i)
183 for(
size_t i= 0; i < data.
length(); ++i)
184 data.
set(i, br.
Read<TUI>( bitCnt ) + min );
198 using TUI=
typename std::make_unsigned<TI>::type;
199 TUI first= data.
get(0);
201 for(
size_t i= 1; i < data.
length(); ++i)
203 TUI second= data.
get(i);
226 using TUI=
typename std::make_unsigned<TI>::type;
227 auto prevVal= br.
Read<TUI>();
228 data.
set(0, prevVal);
229 for(
size_t i= 1; i < data.
length(); ++i)
230 data.
set(i, prevVal= br.
Read<1>() ? prevVal
243 using TUI=
typename std::make_unsigned<TI>::type;
253 auto maxSegLen = (std::numeric_limits<size_t>::min)();
256 for(
int pass= 0 ; pass < 2 ; ++ pass )
259 TUI val = data.
get(0);
265 size_t segEnd= segStart + 1;
269 if( segEnd == data.
length() )
275 val= data.
get(segEnd);
277 sparse= (prevVal == val);
280 while( ( (prevVal == val) == sparse )
281 && ++segEnd < data.
length())
284 val = data.
get(segEnd);
292 maxSegLen= (std::max)( maxSegLen, segEnd - segStart );
299 bw.
Write( bitCntRep +1, (segEnd - segStart) << 1 );
300 bw.
Write( bitCntVal , TUI(prevVal - data.
min ) );
304 bw.
Write( bitCntRep +1, ((segEnd - segStart) << 1) | 1 );
305 for(
size_t j= segStart; j < segEnd; ++j)
306 bw.
Write( bitCntVal, TUI(data.
get(j) - data.
min ) );
313 while( segStart < data.
length() );
321 bw.
Write<lang::Log2OfSize<uint32_t>()+1 + lang::Log2OfSize<TUI>()+1>(
323 | bitCntVal << (lang::Log2OfSize<uint32_t>()+1) );
338 using TUI=
typename std::make_unsigned<TI>::type;
339 if( !data.
length() )
return;
341 auto bitCntRep = br.
Read<lang::Log2OfSize<uint32_t>()+1 + lang::Log2OfSize<TUI>()+1>();
342 auto bitCntVal = bitCntRep >> (lang::Log2OfSize<uint32_t>() + 1);
343 bitCntRep&= lang::LowerMask<lang::Log2OfSize<uint32_t>() + 1,
int>();
345 TUI minVal = br.
Read<TUI>();
348 while( segStart < data.
length() )
350 size_t cntRep= br.
Read<
size_t>( bitCntRep + 1 );
351 size_t segEnd= segStart + (cntRep >> 1);
352 if( (cntRep & 1) == 0 )
354 TUI val = br.
Read<TUI>( bitCntVal ) + minVal;
355 for(
size_t i= segStart ; i < segEnd ; ++i )
360 for(
size_t i= segStart ; i < segEnd ; ++i )
361 data.
set(i, br.
Read<TUI>( bitCntVal ) + minVal);
379 using TUI=
typename std::make_unsigned<TI>::type;
393 bw.
Write<2 * (lang::Log2OfSize<TUI>() + 1)>( bitCntPos
394 | bitCntNeg << (lang::Log2OfSize<TUI>() + 1) );
398 TUI first= data.
get(0);
400 for(
size_t i= 1; i < data.
length(); ++i)
404 TUI second= data.
get(i);
410 bool posNeg= second >= first;
412 bw.
Write(posNeg ? bitCntPos : bitCntNeg,
413 posNeg ? TUI( second - first - data.
minInc )
414 : TUI( first - second - data.
minDec ) );
432 using TUI=
typename std::make_unsigned<TI>::type;
439 auto bitCntPos = br.
Read<2 * (lang::Log2OfSize<TUI>()+1)>();
440 auto bitCntNeg = bitCntPos >> (lang::Log2OfSize<TUI>()+1);
441 bitCntPos&= lang::LowerMask<(lang::Log2OfSize<TUI>()+1),
int>();
442 auto minDiffPos= br.
Read<TUI>();
443 auto minDiffNeg= br.
Read<TUI>();
444 auto prevVal = br.
Read<TUI>();
445 data.
set(0, prevVal);
446 for(
size_t i= 1; i < data.
length(); ++i)
453 bool posNeg= br.
Read<1>();
454 TUI diff = br.
Read<TUI>( posNeg ? bitCntPos : bitCntNeg );
455 val = posNeg ? TUI( prevVal + minDiffPos + diff )
456 : TUI( prevVal - minDiffNeg - diff );
458 data.
set(i, prevVal= val);
void Write(TIntegral value)
TUI minDec
Minimum decrease between two adjacent values.
TUI minInc
Minimum increase between two adjacent values.
TUI maxInc
Maximum increase between two adjacent values.
TUI maxDec
Maximum decrease between two adjacent values.
ALIB_FORCE_INLINE size_t length() const
ALIB_FORCE_INLINE TUI get(size_t idx) const
TUI min
Maximum value (when zig-zag encoded)
TUI max
Maximum value (when zig-zag encoded)
ALIB_FORCE_INLINE void set(size_t idx, TUI value)
void Write(uint8_t symbol)
ALIB_FORCE_INLINE void CountSymbol(uint8_t symbol)
#define ALIB_WARNINGS_RESTORE
#define ALIB_WARNINGS_ALLOW_SHIFT_COUNT_OVERFLOW
void writeHuffman(BitWriter &bw, ArrayCompressor::Array< TI > &data)
void readSparse(BitReader &br, ArrayCompressor::Array< TI > &data)
void writeSparse(BitWriter &bw, ArrayCompressor::Array< TI > &data)
void readUncompressed(BitReader &br, ArrayCompressor::Array< TI > &data)
void readIncremental(BitReader &br, ArrayCompressor::Array< TI > &data)
void writeIncremental(BitWriter &bw, ArrayCompressor::Array< TI > &data)
void readVerySparse(BitReader &br, ArrayCompressor::Array< TI > &data)
void readMinMax(BitReader &br, ArrayCompressor::Array< TI > &data)
void writeUncompressed(BitWriter &bw, ArrayCompressor::Array< TI > &data)
void writeMinMax(BitWriter &bw, ArrayCompressor::Array< TI > &data)
void writeVerySparse(BitWriter &bw, ArrayCompressor::Array< TI > &data)
void readHuffman(BitReader &br, ArrayCompressor::Array< TI > &data)
constexpr int MSB0(TIntegral value)
constexpr int MSB(TIntegral value)
int ShiftOpRHS
Type alias in namespace alib.