Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Adaptive_Huffman_Decoder Class Reference

Incremental FGK decoder accepting arbitrarily fragmented bits. More...

#include <Adaptive_Huffman.H>

Collaboration diagram for Aleph::Adaptive_Huffman_Decoder:
[legend]

Public Member Functions

 Adaptive_Huffman_Decoder () noexcept=default
 Construct a decoder with an empty model and no pending bits.
 
void reset () noexcept
 Clear the tree, pending bits and error state in O(256) time.
 
std::uint64_t symbols_processed () const noexcept
 Return the number of complete bytes decoded.
 
size_t distinct_symbols () const noexcept
 Return the number of different decoded byte values.
 
std::uint64_t frequency (const unsigned char symbol) const noexcept
 Return the current frequency of a decoded byte.
 
bool has_pending_symbol () const noexcept
 Test whether a tree path or NYT literal is incomplete.
 
bool decode_bit (const unsigned int bit, unsigned char &symbol)
 Consume one bit and emit a byte if its codeword is complete.
 
size_t decode (const BitArray &input, std::string &output)
 Decode a bit chunk and append all complete bytes to a string.
 
void finish () const
 Verify that an externally framed message ended at a byte boundary.
 

Private Attributes

adaptive_huffman_detail::Model model
 Owned adaptive tree.
 
int current = model.root
 Node reached by the partial codeword.
 
unsigned int literal = 0
 Partial, most-significant-bit-first byte.
 
unsigned int literal_bits = 0
 Number of literal bits collected.
 
bool failed = false
 Malformed input or a lost output byte requires reset.
 

Detailed Description

Incremental FGK decoder accepting arbitrarily fragmented bits.

A byte is emitted as soon as its last bit arrives. Partial tree paths and partial NYT literals survive across calls, so chunks may end at any bit position. Call finish() at the externally known end of the message to check that no partial codeword remains.

Memory, update complexity, copying, counter limits and thread-safety are the same as for Adaptive_Huffman_Encoder. Each bit costs O(1), plus an O((h + 1) log 256) tree update when a byte is completed. A copy includes any partially decoded codeword.

std::string text;
decoder.decode(bits, text);
decoder.finish();
Incremental FGK decoder accepting arbitrarily fragmented bits.
void finish() const
Verify that an externally framed message ended at a byte boundary.
size_t decode(const BitArray &input, std::string &output)
Decode a bit chunk and append all complete bytes to a string.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
See also
Adaptive_Huffman_Encoder

Definition at line 361 of file Adaptive_Huffman.H.

Constructor & Destructor Documentation

◆ Adaptive_Huffman_Decoder()

Aleph::Adaptive_Huffman_Decoder::Adaptive_Huffman_Decoder ( )
defaultnoexcept

Construct a decoder with an empty model and no pending bits.

Member Function Documentation

◆ decode()

size_t Aleph::Adaptive_Huffman_Decoder::decode ( const BitArray &  input,
std::string &  output 
)
inline

Decode a bit chunk and append all complete bytes to a string.

Parameters
inputNext encoded bits; no byte alignment is required.
[out]outputString to append to, preserving embedded NULs.
Returns
Number of bytes appended; incomplete codewords remain buffered.
Exceptions
std::invalid_argument,std::domain_erroror std::overflow_error under the same conditions as decode_bit(), for a chunk that is not empty: an empty one consumes nothing and returns zero.
std::bad_allocor std::length_error if output cannot grow. The decoder has then counted a byte that is not in output, so it is left failed: every later call throws std::domain_error until reset(). Use decode_bit() to control the allocation of the output, or to go on after it fails.
Note
On an exception the bytes decoded before it, in the same chunk, stay in output, and the return value is lost: compare output.size(), or symbols_processed(), with their values before the call. Input and output are caller-owned.
O(b + m (h + 1) log 256) for b input bits that complete m bytes, apart from output allocation: each bit is a step down the tree, and each completed byte a tree update, where h is the tree height.

Definition at line 494 of file Adaptive_Huffman.H.

References decode_bit(), failed, output, and Aleph::BitArray::size().

Referenced by TEST_F().

◆ decode_bit()

bool Aleph::Adaptive_Huffman_Decoder::decode_bit ( const unsigned int  bit,
unsigned char &  symbol 
)
inline

Consume one bit and emit a byte if its codeword is complete.

Parameters
bitNext encoded bit, either 0 or 1.
[out]symbolDecoded byte; left unchanged when returning false.
Returns
True exactly when one byte has been decoded.
Exceptions
std::out_of_rangeif bit is not binary; no state is changed.
std::invalid_argumentif NYT introduces a byte already seen, or is used after all 256 bytes have appeared. Call reset() on both peers before starting a new message after this error.
std::domain_errorif called, without reset(), after malformed input or after decode() could not append a decoded byte.
std::overflow_errorif the frequency limit has been reached.
Note
O(1) for a bit that does not complete a byte, and O((h + 1) log 256) for one that does, which updates the tree, where h is its height.

Definition at line 430 of file Adaptive_Huffman.H.

References ah_domain_error_if, ah_invalid_argument, ah_out_of_range_error_if, Aleph::adaptive_huffman_detail::Model::alphabet_size, Aleph::adaptive_huffman_detail::Model::check_capacity(), current, Aleph::adaptive_huffman_detail::Model::distinct, failed, Aleph::adaptive_huffman_detail::Model::Node::is_leaf(), Aleph::adaptive_huffman_detail::Model::leaves, Aleph::adaptive_huffman_detail::Model::Node::left, literal, literal_bits, model, Aleph::adaptive_huffman_detail::Model::nodes, Aleph::adaptive_huffman_detail::Model::nyt, Aleph::adaptive_huffman_detail::Model::Node::right, Aleph::adaptive_huffman_detail::Model::root, Aleph::adaptive_huffman_detail::Model::Node::symbol, Aleph::adaptive_huffman_detail::Model::update(), and value.

Referenced by decode(), TEST_F(), TEST_F(), and adaptive_huffman_demo::trace().

◆ distinct_symbols()

size_t Aleph::Adaptive_Huffman_Decoder::distinct_symbols ( ) const
inlinenoexcept

Return the number of different decoded byte values.

Returns
A value in [0, 256], in O(1).

Definition at line 393 of file Adaptive_Huffman.H.

References Aleph::adaptive_huffman_detail::Model::distinct, and model.

◆ finish()

void Aleph::Adaptive_Huffman_Decoder::finish ( ) const
inline

Verify that an externally framed message ended at a byte boundary.

Exceptions
std::invalid_argumentif a codeword or NYT literal is incomplete.
std::domain_errorafter malformed input, or after decode() could not append a decoded byte, until reset().
Note
O(1). Does not reset or seal the decoder; after a truncation error more bits may be supplied. Empty messages are valid. Byte boundary here means a decoded symbol boundary, not a multiple of eight bits.

Definition at line 522 of file Adaptive_Huffman.H.

References ah_domain_error_if, ah_invalid_argument_if, failed, and has_pending_symbol().

Referenced by TEST_F(), TEST_F(), and adaptive_huffman_demo::trace().

◆ frequency()

std::uint64_t Aleph::Adaptive_Huffman_Decoder::frequency ( const unsigned char  symbol) const
inlinenoexcept

Return the current frequency of a decoded byte.

Parameters
symbolByte to query.
Returns
Occurrence count, or zero if unseen, in O(1).

Definition at line 402 of file Adaptive_Huffman.H.

References Aleph::adaptive_huffman_detail::Model::leaves, model, Aleph::adaptive_huffman_detail::Model::nodes, and Aleph::adaptive_huffman_detail::Model::Node::weight.

◆ has_pending_symbol()

bool Aleph::Adaptive_Huffman_Decoder::has_pending_symbol ( ) const
inlinenoexcept

Test whether a tree path or NYT literal is incomplete.

Returns
True if bits for the next byte have been consumed, in O(1).

Definition at line 411 of file Adaptive_Huffman.H.

References current, literal_bits, model, and Aleph::adaptive_huffman_detail::Model::root.

Referenced by finish().

◆ reset()

void Aleph::Adaptive_Huffman_Decoder::reset ( )
inlinenoexcept

Clear the tree, pending bits and error state in O(256) time.

Definition at line 374 of file Adaptive_Huffman.H.

References current, failed, literal, literal_bits, model, Aleph::adaptive_huffman_detail::Model::reset(), and Aleph::adaptive_huffman_detail::Model::root.

Referenced by TEST_F().

◆ symbols_processed()

std::uint64_t Aleph::Adaptive_Huffman_Decoder::symbols_processed ( ) const
inlinenoexcept

Return the number of complete bytes decoded.

Returns
Total frequency at the root, in O(1).

Definition at line 385 of file Adaptive_Huffman.H.

References model, Aleph::adaptive_huffman_detail::Model::nodes, Aleph::adaptive_huffman_detail::Model::root, and Aleph::adaptive_huffman_detail::Model::Node::weight.

Referenced by TEST_F().

Member Data Documentation

◆ current

int Aleph::Adaptive_Huffman_Decoder::current = model.root
private

Node reached by the partial codeword.

Definition at line 364 of file Adaptive_Huffman.H.

Referenced by decode_bit(), has_pending_symbol(), and reset().

◆ failed

bool Aleph::Adaptive_Huffman_Decoder::failed = false
private

Malformed input or a lost output byte requires reset.

Definition at line 367 of file Adaptive_Huffman.H.

Referenced by decode(), decode_bit(), finish(), and reset().

◆ literal

unsigned int Aleph::Adaptive_Huffman_Decoder::literal = 0
private

Partial, most-significant-bit-first byte.

Definition at line 365 of file Adaptive_Huffman.H.

Referenced by decode_bit(), and reset().

◆ literal_bits

unsigned int Aleph::Adaptive_Huffman_Decoder::literal_bits = 0
private

Number of literal bits collected.

Definition at line 366 of file Adaptive_Huffman.H.

Referenced by decode_bit(), has_pending_symbol(), and reset().

◆ model

adaptive_huffman_detail::Model Aleph::Adaptive_Huffman_Decoder::model
private

Owned adaptive tree.

Definition at line 363 of file Adaptive_Huffman.H.

Referenced by decode_bit(), distinct_symbols(), frequency(), has_pending_symbol(), reset(), and symbols_processed().


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