|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Stateful, one-pass FGK Huffman encoder for binary byte strings. More...
#include <Adaptive_Huffman.H>
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. | |
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.
Definition at line 240 of file Adaptive_Huffman.H.
|
defaultnoexcept |
Construct an encoder with an empty model.
|
inlinenoexcept |
Return the number of different byte values seen.
Definition at line 265 of file Adaptive_Huffman.H.
References Aleph::adaptive_huffman_detail::Model::distinct, and model.
|
inline |
Encode a chunk, retaining the model for subsequent chunks.
| input | Bytes to encode; embedded NULs are preserved. | |
| [out] | output | Bit array to append to. |
| std::overflow_error | if a frequency or bit count would overflow. |
| std::bad_alloc | if output storage cannot be allocated. |
Definition at line 331 of file Adaptive_Huffman.H.
|
inline |
Append the current code for one byte, then update the tree.
| symbol | Byte to encode, including NUL and values above 127. | |
| [out] | output | Bit array to append to (its existing prefix is kept). |
| std::overflow_error | if 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_alloc | if BitArray cannot allocate output storage. |
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().
|
inlinenoexcept |
Return the current frequency of a byte.
| symbol | Byte to query. |
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().
|
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().
|
inlinenoexcept |
Return the number of bytes encoded since construction or reset.
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().
|
private |
Owned adaptive tree.
Definition at line 242 of file Adaptive_Huffman.H.
Referenced by distinct_symbols(), encode(), frequency(), reset(), and symbols_processed().