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

The canonical code of Huffman (chapter 12, Statistical Coding, of Ferragina's book). More...

Files

file  ADS/compression/CanonicalHuffman.h
 The canonical code of Huffman (chapter 12, Statistical Coding, of Ferragina's book): the lengths \( L( \sigma ) \) of the codes of Huffman [see Huffman.h, whose huffman() gives them], and a new code with the same lengths, hence as short, chosen so that neither the encoder nor the decoder needs the tree.

Classes

class  ADS::CanonicalHuffman< C >
 the canonical prefix code of the given lengths of the symbols of type C More...

Functions

template<class C, class F>
std::map< C, int > ADS::huffmanLengths (const std::map< C, F > &freq)
 the lengths of the codes of Huffman [see Huffman.h] of the symbols with the frequencies freq
template<class C, class F>
std::map< C, int > ADS::limitedLengths (const std::map< C, F > &freq, int L)
 the lengths of a code of Huffman of no length above \( L \), by halving the frequencies (rounding up) until the code of Huffman of the halved ones has no length above \( L \)

Detailed description

The canonical code of Huffman (chapter 12, Statistical Coding, of Ferragina's book).

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

Function documentation

◆ huffmanLengths()

template<class C, class F>
std::map< C, int > ADS::huffmanLengths ( const std::map< C, F > & freq)
nodiscard

the lengths of the codes of Huffman [see Huffman.h] of the symbols with the frequencies freq

It reads the lengths off the codes of huffman():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{huffmanLengths}( f ) \cr 2 & \quad \textbf{foreach}\ ( c , s ) \in \mathrm{huffman}( f ) \ \textbf{do}\ L[ c ] \gets |s| \cr 3 & \quad \textbf{return}\ L \end{array}} \]

Precondition
freq is not empty, no frequency is negative

◆ limitedLengths()

template<class C, class F>
std::map< C, int > ADS::limitedLengths ( const std::map< C, F > & freq,
int L )
nodiscard

the lengths of a code of Huffman of no length above \( L \), by halving the frequencies (rounding up) until the code of Huffman of the halved ones has no length above \( L \)

In pseudocode (the iterative scaling of chapter 12, Statistical Coding, of Ferragina's book), which ends since the frequencies become all 1 and the code then has lengths at most \( \lceil \log_2 |\Sigma| \rceil \le L \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{limitedLengths}( f , L ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad \ell \gets \mathrm{huffmanLengths}( f ) \cr 4 & \qquad \textbf{if}\ \max_c \ell[ c ] \le L\ \textbf{then return} \ \ell \cr 5 & \qquad \textbf{foreach}\ c\ \textbf{do}\ f[ c ] \gets \lceil f[ c ] / 2 \rceil \end{array}} \]

Precondition
freq is not empty, the frequencies are positive integers, \( 1 \le L \le 63 \) and \( 2^L \) is at least the number of symbols
Postcondition
no length is above \( L \)