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

The code of Huffman of a string of bytes as the fast coders do it, beyond the canonical code of chapter 12, Statistical Coding, of Ferragina's book, which decodes a bit at a time on a map of the symbols. More...

Files

file  ADS/compression/HuffmanCoder.h
 The code of Huffman of a string of bytes as the fast coders do it, beyond the canonical code of chapter 12, Statistical Coding, of Ferragina's book [see CanonicalHuffman.h], which decodes a bit at a time on a map of the symbols:

Functions

std::vector< std::uint8_t > ADS::huffmanCompress (std::span< const std::uint8_t > in)
 the code of Huffman of the bytes of in, with the lengths and the size
std::vector< std::uint8_t > ADS::huffmanDecompress (std::span< const std::uint8_t > in)
 the bytes that huffmanCompress() coded in in
std::array< std::uint8_t, 256 > ADS::limitedHuffmanLengths (const std::array< std::uint64_t, 256 > &f, int L)
 the optimal lengths of at most \( L \) bits of the codes of the 256 bytes of frequencies \( f \), 0 for a byte of frequency 0, by package-merge

Detailed description

The code of Huffman of a string of bytes as the fast coders do it, beyond the canonical code of chapter 12, Statistical Coding, of Ferragina's book, which decodes a bit at a time on a map of the symbols.

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

Function documentation

◆ huffmanCompress()

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

the code of Huffman of the bytes of in, with the lengths and the size

In pseudocode (the coders by tables of Moffat and Turpin, beyond the book), \( x \) the input of \( n \) bytes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{huffmanCompress}( x ) \cr 2 & \quad f \gets \text{the counts of the bytes of } x \cr 3 & \quad \textbf{if}\ \text{at most one byte has } f > 0 \ \textbf{then return}\ ( n , 0 , \text{that byte} ) \cr 4 & \quad \ell \gets \mathrm{limitedHuffmanLengths}( f , 11 ); \ c \gets \text{the canonical codes of } \ell \text{, each turned } \text{around} \cr 5 & \quad \text{write } n \text{, } \max \ell \text{ and the lengths } \ell \text{, 4 bits each} \cr 6 & \quad \textbf{for}\ k \gets 0\ \textbf{to}\ 3\ \textbf{do}\quad \triangleright\ \text{the four quarters of } x \cr 7 & \qquad \textbf{foreach}\ \text{byte } b \text{ of the quarter } k \ \textbf{do}\ \text{append } c[ b ] \text{ to the stream } k \cr 8 & \qquad \text{pad the stream to a byte, and write its size if } k < 3 \cr 9 & \quad \textbf{return}\ \text{the header, the four streams and 8 } \text{bytes of } 0 \end{array}} \]

Postcondition
the output decompresses to the input (audit)

◆ huffmanDecompress()

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

the bytes that huffmanCompress() coded in in

In pseudocode (the decoding by tables of Moffat and Turpin, beyond the book), \( T[ v ] \) for each value \( v \) of \( W \) bits the up to 3 symbols whose codes fit whole in \( v \), their bits and their number:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{huffmanDecompress}( y ) \cr 2 & \quad \text{read } n \text{ and } \ell;\ \textbf{if}\ \max \ell = 0 \ \textbf{then return}\ n \text{ copies of the byte stored} \cr 3 & \quad W \gets 12\ \textbf{if}\ n \ge 2^{18}\ \textbf{else}\ 11 \cr 4 & \quad \textbf{foreach}\ v \in [ 0 , 2^W )\ \textbf{do} \ T[ v ] \gets \text{the symbols decoded from } v \text{, at most 3} \cr 5 & \quad \textbf{while}\ \text{each of the 4 streams has room for} \text{ 12 more symbols}\ \textbf{do} \cr 6 & \qquad \textbf{for}\ j \gets 1\ \textbf{to}\ 4\ \textbf{do} \cr 7 & \qquad\quad \textbf{for}\ k \gets 0\ \textbf{to}\ 3\ \textbf{do}\quad \triangleright\ \text{the four chains overlap} \cr 8 & \qquad\qquad e \gets T[ \text{the next } W \text{ bits of the stream } k ];\ \text{output the symbols of } e \text{ and skip its bits} \cr 9 & \qquad \text{reload the word of each stream} \cr 10 & \quad \text{decode the rest one symbol at a time;}\ \textbf{return} \ \text{ the output} \end{array}} \]

Precondition
in is an output of huffmanCompress()

◆ limitedHuffmanLengths()

std::array< std::uint8_t, 256 > ADS::limitedHuffmanLengths ( const std::array< std::uint64_t, 256 > & f,
int L )
inlinenodiscard

the optimal lengths of at most \( L \) bits of the codes of the 256 bytes of frequencies \( f \), 0 for a byte of frequency 0, by package-merge

In pseudocode (the package-merge of Larmore and Hirschberg, beyond the book), \( m \) the bytes of positive frequency: a byte has the length \( \ell \) if it is in the cheapest \( 2 m - 2 \) items at \( \ell \) of the \( L \) lists, i.e., the lengths minimize \( \sum f \ell \) subject to \( \sum 2^{-\ell} \le 1 \) and \( \ell \le L \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{limitedHuffmanLengths}( f , L ) \cr 2 & \quad S \gets \text{the bytes with } f > 0 \text{ by increasing } f;\ \textbf{if}\ |S| \le 1\ \textbf{then return}\ \ldots \cr 3 & \quad \mathit{list} \gets S\quad \triangleright\ \text{items with } \text{the weight } f \cr 4 & \quad \textbf{for}\ l \gets 1\ \textbf{to}\ L - 1\ \textbf{do} \cr 5 & \qquad \mathit{packs} \gets \text{the pairs of consecutive items of } \mathit{list} \text{, each weighing their sum} \cr 6 & \qquad \mathit{list} \gets \mathrm{merge}( S , \mathit{packs} ) \quad \triangleright\ \text{by weight} \cr 7 & \quad \ell \gets 0 \cr 8 & \quad \textbf{foreach}\ \text{byte } b \text{ inside the first } 2 m - 2 \text{ items of } \mathit{list}\ \textbf{do} \ \ell[ b ] \gets \ell[ b ] + 1 \cr 9 & \quad \textbf{return}\ \ell \end{array}} \]

Precondition
\( 1 \le L \le 64 \), and \( 2^L \) is at least the number of the bytes of positive frequency