libfly
6.2.2
C++20 utility library for Linux, macOS, and Windows
|
#include <huffman_decoder.hpp>
Public Member Functions | |
HuffmanDecoder () noexcept | |
code_type | compute_kraft_mcmillan_constant () const |
![]() | |
virtual | ~Decoder ()=default |
bool | decode_string (const std::string &encoded, std::string &decoded) |
bool | decode_file (const std::filesystem::path &encoded, const std::filesystem::path &decoded) |
Protected Member Functions | |
bool | decode_binary (fly::BitStreamReader &encoded, std::ostream &decoded) override |
![]() | |
bool | decode_internal (std::istream &encoded, std::ostream &decoded) final |
Implementation of the Decoder interface for Huffman coding.
|
noexcept |
Constructor.
code_type fly::coders::HuffmanDecoder::compute_kraft_mcmillan_constant | ( | ) | const |
Compute the Kraft-McMillan constant of the decoded Huffman codes. Primarily meant for unit testing.
|
overrideprotectedvirtual |
Huffman decode a stream.
Because large input streams are encoded in chunks, they must also be decoded in chunks. The input stream is decoded until either the end of the stream or the chunk size is reached. The decoding sequence is then repeated for each chunk.
The sequence to decode a stream is:
1. Decode the canonical Huffman codes from the stream. 2. Convert the canonical codes to a prefix table. 3. Decode the input stream using the table.
Prefix tables (step 2) function via the property that no Huffman code is a prefix of any other code. Thus, a table can be formed as an array, whose indices are integers where the most-significant bits are Huffman codes.
Decoding a symbol from the input stream (step 3) consists of peeking N bits from the input stream, where N is maximum length of the decoded Huffman codes. These bits are the index into the prefix table; a single lookup is performed to find the corresponding Huffman code. The actual length of the code is then discarded from the input stream.
encoded | Stream holding the contents to decode. |
decoded | Stream to store the decoded contents. |
Implements fly::coders::BinaryDecoder.