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

Stateful, one-pass FGK Huffman encoder for binary byte strings. More...

#include <Adaptive_Huffman.H>

Collaboration diagram for Aleph::Adaptive_Huffman_Encoder:
[legend]

Public Member Functions

 Adaptive_Huffman_Encoder () noexcept=default
 Construct an encoder with an empty model.
 
void reset () noexcept
 Reset the model in O(256) time; caller-owned output is untouched.
 
std::uint64_t symbols_processed () const noexcept
 Return the number of bytes encoded since construction or reset.
 
size_t distinct_symbols () const noexcept
 Return the number of different byte values seen.
 
std::uint64_t frequency (const unsigned char symbol) const noexcept
 Return the current frequency of a byte.
 
size_t encode (const unsigned char symbol, BitArray &output)
 Append the current code for one byte, then update the tree.
 
size_t encode (const std::string_view input, BitArray &output)
 Encode a chunk, retaining the model for subsequent chunks.
 

Private Attributes

adaptive_huffman_detail::Model model
 Owned adaptive tree.
 

Detailed Description

Stateful, one-pass FGK Huffman encoder for binary byte strings.

encode() appends to a caller-owned BitArray and retains its tree between calls. Feeding chunks produces exactly the same bits as feeding their concatenation. No input storage or frequency prepass is needed.

Each instance owns a fixed, allocation-free model of at most 513 nodes. Encoding costs O((h + 1) log 256) per byte, apart from output allocation, where h is the tree height (at most 256). Copies own independent models. Different instances may be used concurrently; a shared instance requires external synchronization. Frequencies are not rescaled: after UINT64_MAX bytes, reset both peers at the same externally framed boundary.

encoder.encode("abracadabra", bits);
Stateful, one-pass FGK Huffman encoder for binary byte strings.
size_t encode(const unsigned char symbol, BitArray &output)
Append the current code for one byte, then update the tree.
Contiguous array of bits.
Definition bitArray.H:201
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
See also
Adaptive_Huffman_Decoder

Definition at line 240 of file Adaptive_Huffman.H.

Constructor & Destructor Documentation

◆ Adaptive_Huffman_Encoder()

Aleph::Adaptive_Huffman_Encoder::Adaptive_Huffman_Encoder ( )
defaultnoexcept

Construct an encoder with an empty model.

Member Function Documentation

◆ distinct_symbols()

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

Return the number of different byte values seen.

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

Definition at line 265 of file Adaptive_Huffman.H.

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

◆ encode() [1/2]

size_t Aleph::Adaptive_Huffman_Encoder::encode ( const std::string_view  input,
BitArray &  output 
)
inline

Encode a chunk, retaining the model for subsequent chunks.

Parameters
inputBytes to encode; embedded NULs are preserved.
[out]outputBit array to append to.
Returns
Number of bits appended by this call (zero for empty input).
Exceptions
std::overflow_errorif a frequency or bit count would overflow.
std::bad_allocif output storage cannot be allocated.
Note
Successfully encoded bytes remain committed on an exception; the failing byte has the exception guarantee of encode(unsigned char, BitArray &).
O(n (h + 1) log 256) for n input bytes, apart from output allocation, where h is the tree height (at most 256).

Definition at line 331 of file Adaptive_Huffman.H.

References encode(), and output.

◆ encode() [2/2]

size_t Aleph::Adaptive_Huffman_Encoder::encode ( const unsigned char  symbol,
BitArray &  output 
)
inline

Append the current code for one byte, then update the tree.

Parameters
symbolByte to encode, including NUL and values above 127.
[out]outputBit array to append to (its existing prefix is kept).
Returns
Number of appended bits, between 1 and 263: a known byte takes at most 256, and an unseen one at most 255 and its eight bits.
Exceptions
std::overflow_errorif the frequency or output size would overflow. The output size can only overflow where size_t has 32 bits: with 64 bits a BitArray cannot hold that many bits.
std::bad_allocif BitArray cannot allocate output storage.
Note
On an allocation failure the model is unchanged, but output may contain a partial codeword. Restore its previous size before retrying.
O((h + 1) log 256), apart from output allocation, where h is the tree height: the code is read in O(h) and the update takes the rest.

Definition at line 294 of file Adaptive_Huffman.H.

References ah_overflow_error_if, Aleph::adaptive_huffman_detail::Model::alphabet_size, Aleph::adaptive_huffman_detail::Model::check_capacity(), Aleph::count(), Aleph::adaptive_huffman_detail::Model::leaves, model, Aleph::adaptive_huffman_detail::Model::nodes, Aleph::adaptive_huffman_detail::Model::nyt, output, Aleph::adaptive_huffman_detail::Model::Node::parent, Aleph::adaptive_huffman_detail::Model::Node::right, and Aleph::adaptive_huffman_detail::Model::update().

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

◆ frequency()

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

Return the current frequency of a byte.

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

Definition at line 274 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.

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

◆ reset()

void Aleph::Adaptive_Huffman_Encoder::reset ( )
inlinenoexcept

Reset the model in O(256) time; caller-owned output is untouched.

Definition at line 249 of file Adaptive_Huffman.H.

References model, and Aleph::adaptive_huffman_detail::Model::reset().

◆ symbols_processed()

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

Return the number of bytes encoded since construction or reset.

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

Definition at line 257 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 adaptive_huffman_demo::snapshot().

Member Data Documentation

◆ model

adaptive_huffman_detail::Model Aleph::Adaptive_Huffman_Encoder::model
private

Owned adaptive tree.

Definition at line 242 of file Adaptive_Huffman.H.

Referenced by distinct_symbols(), encode(), frequency(), reset(), and symbols_processed().


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