the canonical prefix code of the given lengths of the symbols of type C
More...
#include <ADS/compression/CanonicalHuffman.h>
|
| | 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
|
template<class C>
class ADS::CanonicalHuffman< C >
the canonical prefix code of the given lengths of the symbols of type C
◆ symbol_type
◆ CanonicalHuffman()
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 \)
◆ code()
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]
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]
appends the code of \( c \) to \( w \)
- Precondition
- \( c \) has a code
◆ encode() [2/2]
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()
\( \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>
the canonical code of the code of Huffman of the frequencies
- Precondition
- as huffmanLengths()
◆ lengths()
◆ maxLength()
The documentation for this class was generated from the following file: