ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches

The tabled variant of the asymmetric numeral systems of Duda (tANS), the entropy coder of order 0 of Zstandard (RFC 8878), beside the range variant. More...

Files

file  ADS/compression/Tans.h
 The tabled variant of the asymmetric numeral systems of Duda (tANS), the entropy coder of order 0 of Zstandard (RFC 8878), beside the range variant [see Rans.h]: the transitions of the state are read from tables, so that a symbol costs a lookup, a shift and an addition, and no multiplication.

Functions

std::vector< std::uint8_t > ADS::tansCompress (std::span< const std::uint8_t > in)
 the tANS code of order 0 of the bytes of in, with the frequencies and the size
std::vector< std::uint8_t > ADS::tansDecompress (std::span< const std::uint8_t > in)
 the bytes that tansCompress() coded in in

Detailed description

The tabled variant of the asymmetric numeral systems of Duda (tANS), the entropy coder of order 0 of Zstandard (RFC 8878), beside the range variant.

The definitions, the invariants and the costs of the area are in Theory of compression.

Function documentation

◆ tansCompress()

std::vector< std::uint8_t > ADS::tansCompress ( std::span< const std::uint8_t > in)
inlinenodiscard

the tANS code of order 0 of the bytes of in, with the frequencies and the size

In pseudocode (the tANS of Duda, beyond the book), for one half of the string, \( \mathit{next}[ c_s + j ] \) being \( L \) plus the \( j \)-th state dealt to \( s \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tansCompress}( s_0 \cdots s_{n-1} ) \cr 2 & \quad f \gets \mathrm{scaleFrequencies}( \text{the counts of the } \text{bytes} , R );\ \text{deal the states by the step} \cr 3 & \quad X_0 , \ldots , X_3 \gets L \cr 4 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 0\ \textbf{do} \quad \triangleright\ \text{from the last symbol} \cr 5 & \qquad X \gets X_{i \bmod 4};\ s \gets s_i \cr 6 & \qquad k \gets \text{the number with } \lfloor X / 2^k \rfloor \in [ f_s , 2 f_s ) \cr 7 & \qquad \text{write the low } k \text{ bits of } X \cr 8 & \qquad X_{i \bmod 4} \gets \mathit{next}[ c_s + \lfloor X / 2^k \rfloor - f_s ] \cr 9 & \quad \text{write } X_0 - L , \ldots , X_3 - L \text{ on } R \text{ bits each};\ \textbf{return}\ \text{the stream} \end{array}} \]

Postcondition
the output decompresses to the input (audit)

◆ tansDecompress()

std::vector< std::uint8_t > ADS::tansDecompress ( std::span< const std::uint8_t > in)
inlinenodiscard

the bytes that tansCompress() coded in in

In pseudocode (the tANS of Duda, beyond the book), for one half of the string, the stream read backward:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tansDecompress}( y ) \cr 2 & \quad \text{read } n \text{ and } f;\ \text{deal the states by the } \text{step} \cr 3 & \quad \textbf{foreach}\ x \in [ 0 , L )\ \textbf{do} \cr 4 & \qquad s \gets \text{the symbol of } x;\ i \gets \text{its rank} \text{ among the states of } s;\ b \gets R - \lfloor \log_2( f_s + i ) \rfloor \cr 5 & \qquad D[ x ] \gets ( s , b , ( f_s + i ) 2^b - L ) \cr 6 & \quad x_3 , \ldots , x_0 \gets \text{the last } 4 R \text{ bits of } \text{the stream} \cr 7 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 8 & \qquad ( s , b , \beta ) \gets D[ x_{i \bmod 4} ];\ s_i \gets s \cr 9 & \qquad x_{i \bmod 4} \gets \beta + \text{the next } b \text{ bits,} \text{ going backward} \cr 10 & \quad \textbf{return}\ s_0 \cdots s_{n-1} \end{array}} \]

Precondition
in is an output of tansCompress()