ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::CanonicalHuffman< C > Class Template Reference

the canonical prefix code of the given lengths of the symbols of type C More...

#include <ADS/compression/CanonicalHuffman.h>

Public types

using symbol_type = C
 the type of the symbols

Public member functions

 CanonicalHuffman (const std::map< C, int > &lengths)
 the canonical code of the lengths
const std::map< C, int > & lengths (void) const
 the lengths of the codes
int maxLength (void) const
 the longest length
std::uint64_t firstCode (int l) const
 \( \mathit{fc}[ l ] \), the value of the first code of length \( l \)
std::string code (const C &c) const
 the code of \( c \), a string of '0' and '1'
void encode (const C &c, PackedBitWriter &w) const
 appends the code of \( c \) to \( w \)
std::vector< std::uint8_t > encode (std::span< const C > text) const
 the codes of the symbols of text, packed in bytes
C decode (PackedBitReader &r) const
 the symbol whose code is next in \( r \)
std::vector< C > decode (const std::vector< std::uint8_t > &bits, std::size_t n) const
 the first \( n \) symbols encoded in bits, each by decode() above

Static public member functions

template<class F>
static CanonicalHuffman fromFrequencies (const std::map< C, F > &freq)
 the canonical code of the code of Huffman of the frequencies

Detailed description

template<class C>
class ADS::CanonicalHuffman< C >

the canonical prefix code of the given lengths of the symbols of type C

Member typedef documentation

◆ symbol_type

template<class C>
using ADS::CanonicalHuffman< C >::symbol_type = C

the type of the symbols

Constructor and destructor documentation

◆ CanonicalHuffman()

template<class C>
ADS::CanonicalHuffman< C >::CanonicalHuffman ( const std::map< C, int > & lengths)
explicit

the canonical code of the lengths

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book, rounding up), the symbols of each length in their order:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{CanonicalHuffman}( L ) \cr 2 & \quad \max \gets \max_c L[ c ];\ \mathit{symb}[ l ] \gets \text{the } \text{symbols of length } l \text{, for each } l \cr 3 & \quad \mathit{fc}[ \max ] \gets 0 \cr 4 & \quad \textbf{for}\ l \gets \max - 1\ \textbf{downto}\ 1 \ \textbf{do} \cr 5 & \qquad \mathit{fc}[ l ] \gets \lceil ( \mathit{fc}[ l + 1 ] + |\mathit{symb}[ l + 1 ]| ) / 2 \rceil \cr 6 & \quad \textbf{for}\ l \gets 1\ \textbf{to}\ \max\ \textbf{do} \cr 7 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ |\mathit{symb}[ l ]| - 1\ \textbf{do}\ \mathit{code}[ \mathit{symb}[ l ][ i ] ] \gets \mathit{fc}[ l ] + i \end{array}} \]

Precondition
lengths is not empty, each length in \( [ 1 , 63 ] \), \( \sum_c 2^{-L[ c ]} \le 1 \)

Member function documentation

◆ code()

template<class C>
std::string ADS::CanonicalHuffman< C >::code ( const C & c) const
nodiscard

the code of \( c \), a string of '0' and '1'

Precondition
\( c \) has a code

◆ decode() [1/2]

template<class C>
std::vector< C > ADS::CanonicalHuffman< C >::decode ( const std::vector< std::uint8_t > & bits,
std::size_t n ) const
nodiscard

the first \( n \) symbols encoded in bits, each by decode() above

Precondition
bits begins with \( n \) codes

◆ decode() [2/2]

template<class C>
C ADS::CanonicalHuffman< C >::decode ( PackedBitReader & r) const
nodiscard

the symbol whose code is next in \( r \)

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book): the codes of length \( l \) are the values from \( \mathit{fc}[ l ] \) on, and every proper prefix of a longer code is smaller:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decode}( r ) \cr 2 & \quad v \gets \mathrm{nextBit}( r );\ l \gets 1 \cr 3 & \quad \textbf{while}\ v < \mathit{fc}[ l ]\ \textbf{do} \ v \gets 2 v + \mathrm{nextBit}( r );\ l \gets l + 1 \cr 4 & \quad \textbf{return}\ \mathit{symb}[ l ][ v - \mathit{fc}[ l ] ] \end{array}} \]

Precondition
the next bits of \( r \) are a code

◆ encode() [1/2]

template<class C>
void ADS::CanonicalHuffman< C >::encode ( const C & c,
PackedBitWriter & w ) const

appends the code of \( c \) to \( w \)

Precondition
\( c \) has a code

◆ encode() [2/2]

template<class C>
std::vector< std::uint8_t > ADS::CanonicalHuffman< C >::encode ( std::span< const C > text) const
nodiscard

the codes of the symbols of text, packed in bytes

In pseudocode (chapter 12, Statistical Coding, of Ferragina's book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{encode}( \mathit{text} ) \cr 2 & \quad \textbf{foreach}\ c \in \mathit{text}\ \textbf{do} \ \text{append the } L[ c ] \text{ low bits of } \mathit{code}[ c ] \text{ to } w \cr 3 & \quad \textbf{return}\ w \end{array}} \]

Precondition
each symbol of text has a code
Postcondition
decode( result , text.size() ) is text (audit)

◆ firstCode()

template<class C>
std::uint64_t ADS::CanonicalHuffman< C >::firstCode ( int l) const
nodiscard

\( \mathit{fc}[ l ] \), the value of the first code of length \( l \)

Precondition
\( 1 \le l \le \) maxLength()

◆ fromFrequencies()

template<class C>
template<class F>
CanonicalHuffman ADS::CanonicalHuffman< C >::fromFrequencies ( const std::map< C, F > & freq)
staticnodiscard

the canonical code of the code of Huffman of the frequencies

Precondition
as huffmanLengths()

◆ lengths()

template<class C>
const std::map< C, int > & ADS::CanonicalHuffman< C >::lengths ( void ) const
inlinenodiscard

the lengths of the codes

◆ maxLength()

template<class C>
int ADS::CanonicalHuffman< C >::maxLength ( void ) const
inlinenodiscard

the longest length


The documentation for this class was generated from the following file: