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

The arithmetic code (chapter 12, Statistical Coding, of Ferragina's book), exact and in integers. More...

Files

file  ADS/compression/ArithmeticCoding.h
 The arithmetic code (chapter 12, Statistical Coding, of Ferragina's book), exact and in integers.

Classes

class  ADS::RangeEncoder
 the integer arithmetic encoder of Witten, Neal and Cleary on \( w \) bits More...
class  ADS::RangeDecoder
 the decoder of a RangeEncoder on \( w \) bits More...

Functions

template<class C>
std::pair< Rational, Rational > ADS::arithmeticInterval (std::span< const C > text, const std::map< C, Rational > &prob)
 the interval \( [ l , l + s ) \) of text, as the pair \( ( l , s ) \)
template<class C>
std::string ADS::arithmeticCodeExact (std::span< const C > text, const std::map< C, Rational > &prob)
 the arithmetic code of text: the first \( \lceil \log_2( 2 / s ) \rceil \) bits of \( l + s / 2 \), a string of '0' and '1'
template<class C>
std::vector< C > ADS::arithmeticDecodeExact (const std::string &bits, std::size_t n, const std::map< C, Rational > &prob)
 the text of \( n \) symbols of the code bits
template<class C>
std::vector< std::uint8_t > ADS::arithmeticEncode (std::span< const C > text, const std::map< C, std::uint64_t > &counts, int w=32)
 the arithmetic code of text with the static model of the counts
template<class C>
std::vector< C > ADS::arithmeticDecode (std::span< const std::uint8_t > code, std::size_t n, const std::map< C, std::uint64_t > &counts, int w=32)
 the \( n \) symbols of the code with the static model of the counts
std::vector< std::uint8_t > ADS::adaptiveArithmeticEncode (std::span< const std::uint32_t > x, std::uint32_t sigma)
 the adaptive arithmetic code of the symbols of \( x \) in \( [ 0 , \sigma ) \)
std::vector< std::uint32_t > ADS::adaptiveArithmeticDecode (std::span< const std::uint8_t > code, std::size_t n, std::uint32_t sigma)
 the \( n \) symbols of the adaptive arithmetic code in \( [ 0 , \sigma ) \)
std::vector< std::uint8_t > ADS::adaptiveArithmeticEncode (std::string_view s)
 the adaptive arithmetic code of the bytes of \( s \), those of adaptiveArithmeticEncode() above with \( \sigma = 256 \)
std::string ADS::adaptiveArithmeticDecode (std::span< const std::uint8_t > code, std::size_t n)
 the \( n \) bytes of the adaptive arithmetic code, by adaptiveArithmeticDecode() above with \( \sigma = 256 \)

Detailed description

The arithmetic code (chapter 12, Statistical Coding, of Ferragina's book), exact and in integers.

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

Function documentation

◆ adaptiveArithmeticDecode() [1/2]

std::string ADS::adaptiveArithmeticDecode ( std::span< const std::uint8_t > code,
std::size_t n )
inlinenodiscard

the \( n \) bytes of the adaptive arithmetic code, by adaptiveArithmeticDecode() above with \( \sigma = 256 \)

◆ adaptiveArithmeticDecode() [2/2]

std::vector< std::uint32_t > ADS::adaptiveArithmeticDecode ( std::span< const std::uint8_t > code,
std::size_t n,
std::uint32_t sigma )
inlinenodiscard

the \( n \) symbols of the adaptive arithmetic code in \( [ 0 , \sigma ) \)

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book), the model of the encoder rebuilt symbol by symbol:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{adaptiveArithmeticDecode}( y , n , \sigma ) \cr 2 & \quad D \gets \text{a } \mathrm{RangeDecoder} \text{ of } y;\ F \gets ( 1 , \ldots , 1 ) \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad c \gets F.\mathrm{find}( D.\mathrm{target}( F.\mathrm{total}() ) );\ a \gets F.\mathrm{prefix}( c ) \cr 5 & \qquad D.\mathrm{consume}( a , a + F.\mathrm{count}( c ) , F.\mathrm{total}() );\ x[ i ] \gets c \cr 6 & \qquad F.\mathrm{add}( c , 1 );\ \textbf{if}\ F.\mathrm{total}() > \max( 2^{16} , 2 \sigma )\ \textbf{then}\ \text{halve the counts} \cr 7 & \quad \textbf{return}\ x \end{array}} \]

Precondition
\( 1 \le \sigma \le 2^{20} \)

◆ adaptiveArithmeticEncode() [1/2]

std::vector< std::uint8_t > ADS::adaptiveArithmeticEncode ( std::span< const std::uint32_t > x,
std::uint32_t sigma )
inlinenodiscard

the adaptive arithmetic code of the symbols of \( x \) in \( [ 0 , \sigma ) \)

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book, with the halving of the counts), \( F \) a tree of Fenwick of the counts, all 1 at the start:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{adaptiveArithmeticEncode}( x , \sigma ) \cr 2 & \quad E \gets \text{a } \mathrm{RangeEncoder};\ F \gets ( 1 , \ldots , 1 ) \cr 3 & \quad \textbf{foreach}\ c \in x\ \textbf{do} \cr 4 & \qquad a \gets F.\mathrm{prefix}( c );\ E.\mathrm{encode}( a , a + F.\mathrm{count}( c ) , F.\mathrm{total}() ) \cr 5 & \qquad F.\mathrm{add}( c , 1 ) \cr 6 & \qquad \textbf{if}\ F.\mathrm{total}() > \max( 2^{16} , 2 \sigma ) \ \textbf{then}\ \text{halve the counts, rounding up} \cr 7 & \quad \textbf{return}\ E.\mathrm{finish}() \end{array}} \]

Precondition
\( 1 \le \sigma \le 2^{20} \), each symbol of \( x \) below \( \sigma \)
Postcondition
adaptiveArithmeticDecode( result , x.size() , sigma ) is \( x \) (audit)

◆ adaptiveArithmeticEncode() [2/2]

std::vector< std::uint8_t > ADS::adaptiveArithmeticEncode ( std::string_view s)
inlinenodiscard

the adaptive arithmetic code of the bytes of \( s \), those of adaptiveArithmeticEncode() above with \( \sigma = 256 \)

Postcondition
adaptiveArithmeticDecode( result , s.size() ) is \( s \) (audit)

◆ arithmeticCodeExact()

template<class C>
std::string ADS::arithmeticCodeExact ( std::span< const C > text,
const std::map< C, Rational > & prob )
nodiscard

the arithmetic code of text: the first \( \lceil \log_2( 2 / s ) \rceil \) bits of \( l + s / 2 \), a string of '0' and '1'

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book), the bits of the fraction by doubling: the point \( x \) truncated to \( d \) bits loses less than \( 2^{-d} \le s / 2 \), so it stays in the interval:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{arithmeticCodeExact}( S , P ) \cr 2 & \quad ( l , s ) \gets \mathrm{arithmeticInterval}( S , P ); \ d \gets \lceil \log_2( 2 / s ) \rceil;\ x \gets l + s / 2 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ d\ \textbf{do} \cr 4 & \qquad x \gets 2 x \cr 5 & \qquad \textbf{if}\ x < 1\ \textbf{then}\ b_i \gets 0 \ \textbf{else}\ b_i \gets 1;\ x \gets x - 1 \cr 6 & \quad \textbf{return}\ b_1 \cdots b_d \end{array}} \]

Precondition
as arithmeticInterval()
Postcondition
the code, as a fraction, is in \( [ l , l + s ) \)

◆ arithmeticDecode()

template<class C>
std::vector< C > ADS::arithmeticDecode ( std::span< const std::uint8_t > code,
std::size_t n,
const std::map< C, std::uint64_t > & counts,
int w = 32 )
nodiscard

the \( n \) symbols of the code with the static model of the counts

In pseudocode (Witten, Neal and Cleary, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{arithmeticDecode}( y , n , \mathit{counts} , w ) \cr 2 & \quad D \gets \text{a } \mathrm{RangeDecoder} \text{ of } y \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad t \gets D.\mathrm{target}( T );\ \sigma \gets \text{the symbol} \text{ with } c_\sigma \le t < c_\sigma + \mathit{counts}[ \sigma ] \cr 5 & \qquad D.\mathrm{consume}( c_\sigma , c_\sigma + \mathit{counts}[ \sigma ] , T );\ S[ i ] \gets \sigma \cr 6 & \quad \textbf{return}\ S \end{array}} \]

Precondition
as arithmeticEncode()

◆ arithmeticDecodeExact()

template<class C>
std::vector< C > ADS::arithmeticDecodeExact ( const std::string & bits,
std::size_t n,
const std::map< C, Rational > & prob )
nodiscard

the text of \( n \) symbols of the code bits

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book), \( x \) the code as a fraction:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{arithmeticDecodeExact}( x , n , P ) \cr 2 & \quad l \gets 0;\ s \gets 1 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad c \gets \text{the symbol with } l + s f_c \le x < l + s ( f_c + P[ c ] ) \cr 5 & \qquad S[ i ] \gets c;\ l \gets l + s f_c;\ s \gets s P[ c ] \cr 6 & \quad \textbf{return}\ S \end{array}} \]

Precondition
the probabilities are positive with sum 1, bits is a string of '0' and '1'

◆ arithmeticEncode()

template<class C>
std::vector< std::uint8_t > ADS::arithmeticEncode ( std::span< const C > text,
const std::map< C, std::uint64_t > & counts,
int w = 32 )
nodiscard

the arithmetic code of text with the static model of the counts

In pseudocode (Witten, Neal and Cleary, beyond the book), \( c_\sigma \) the cumulative counts of the symbols before \( \sigma \) and \( T \) their total:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{arithmeticEncode}( S , \mathit{counts} , w ) \cr 2 & \quad E \gets \text{a } \mathrm{RangeEncoder} \text{ on } w \text{ bits} \cr 3 & \quad \textbf{foreach}\ \sigma \in S\ \textbf{do} \ E.\mathrm{encode}( c_\sigma , c_\sigma + \mathit{counts}[ \sigma ] , T ) \cr 4 & \quad \textbf{return}\ E.\mathrm{finish}() \end{array}} \]

Precondition
the counts are positive with total at most \( 2^{w - 2} \), each symbol of text is in counts, \( 2 \le w \le 32 \)
Postcondition
arithmeticDecode( result , text.size() , counts , w ) is text (audit)

◆ arithmeticInterval()

template<class C>
std::pair< Rational, Rational > ADS::arithmeticInterval ( std::span< const C > text,
const std::map< C, Rational > & prob )
nodiscard

the interval \( [ l , l + s ) \) of text, as the pair \( ( l , s ) \)

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book), \( f_c \) the cumulative probability of the symbols before \( c \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{arithmeticInterval}( S , P ) \cr 2 & \quad l \gets 0;\ s \gets 1 \cr 3 & \quad \textbf{foreach}\ c \in S\ \textbf{do}\ l \gets l + s f_c; \ s \gets s P[ c ] \cr 4 & \quad \textbf{return}\ ( l , s ) \end{array}} \]

Precondition
the probabilities are positive with sum 1, and each symbol of text is in prob