48#ifndef ALEPH_ADAPTIVE_HUFFMAN_H
49#define ALEPH_ADAPTIVE_HUFFMAN_H
63namespace adaptive_huffman_detail {
64static_assert(CHAR_BIT == 8,
"Adaptive Huffman requires eight-bit bytes");
71 static constexpr int root = 0;
86 [[nodiscard]]
bool is_leaf() const noexcept
130 <<
"Adaptive Huffman frequency limit reached; reset both codecs";
145 while (
low + 1 < high)
181 void update(
const unsigned char symbol)
noexcept
183 int node =
leaves[symbol];
186 const int old_nyt =
nyt;
210 if (leader != node
and leader !=
nodes[node].parent)
274 [[nodiscard]] std::uint64_t
frequency(
const unsigned char symbol)
const noexcept
307 const size_t count = length + (unseen ? 8 : 0);
309 <<
"Adaptive Huffman output bit count overflow";
311 output.push(path[--length]);
313 for (
int shift = 7; shift >= 0; --shift)
314 output.push((symbol >> shift) & 1u);
333 const size_t initial_size =
output.size();
334 for (
const unsigned char symbol : input)
336 return output.size() - initial_size;
402 [[nodiscard]] std::uint64_t
frequency(
const unsigned char symbol)
const noexcept
430 bool decode_bit(
const unsigned int bit,
unsigned char &symbol)
434 <<
"Reset the adaptive Huffman decoder after malformed input or a lost byte";
496 const size_t initial_size =
output.size();
497 for (
size_t i = 0; i < input.
size(); ++i)
499 unsigned char symbol;
500 if (
decode_bit(
static_cast<unsigned int>(input[i]), symbol))
503 output.push_back(
static_cast<char>(symbol));
511 return output.size() - initial_size;
525 <<
"Reset the adaptive Huffman decoder after malformed input or a lost byte";
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_invalid_argument()
Throws std::invalid_argument unconditionally.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Space-efficient bit array implementation.
size_t size_t int32_t value
Incremental FGK decoder accepting arbitrarily fragmented bits.
void reset() noexcept
Clear the tree, pending bits and error state in O(256) time.
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.
unsigned int literal_bits
Number of literal bits collected.
adaptive_huffman_detail::Model model
Owned adaptive tree.
std::uint64_t symbols_processed() const noexcept
Return the number of complete bytes decoded.
void finish() const
Verify that an externally framed message ended at a byte boundary.
Adaptive_Huffman_Decoder() noexcept=default
Construct a decoder with an empty model and no pending bits.
size_t decode(const BitArray &input, std::string &output)
Decode a bit chunk and append all complete bytes to a string.
size_t distinct_symbols() const noexcept
Return the number of different decoded byte values.
int current
Node reached by the partial codeword.
bool decode_bit(const unsigned int bit, unsigned char &symbol)
Consume one bit and emit a byte if its codeword is complete.
unsigned int literal
Partial, most-significant-bit-first byte.
bool failed
Malformed input or a lost output byte requires reset.
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.
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.
adaptive_huffman_detail::Model model
Owned adaptive tree.
std::uint64_t symbols_processed() const noexcept
Return the number of bytes encoded since construction or reset.
void reset() noexcept
Reset the model in O(256) time; caller-owned output is untouched.
Adaptive_Huffman_Encoder() noexcept=default
Construct an encoder with an empty model.
size_t encode(const std::string_view input, BitArray &output)
Encode a chunk, retaining the model for subsequent chunks.
Contiguous array of bits.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Main namespace for Aleph-w library functions.
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
bool is_leaf() const noexcept
Test whether this node is a leaf.
std::uint64_t weight
Subtree frequency.
int parent
Parent index, or -1 at the root.
int order
Position in the sibling-property numbering.
int right
Right child index, or -1 at a leaf.
unsigned char symbol
Byte value, meaningful only at data leaves.
int left
Left child index, or -1 at a leaf.
int used
Number of active nodes.
static constexpr int root
Stable root node index.
void reset() noexcept
Restore the initial tree in O(alphabet_size) time.
void swap_nodes(const int a, const int b) noexcept
Exchange two non-ancestor subtrees and their order numbers.
int block_leader(const int node) const noexcept
Find the highest-order node of a node's weight block.
int leaves[alphabet_size]
Byte-to-leaf index, or -1 if unseen.
Node nodes[max_nodes]
Stable node storage.
int by_order[max_nodes]
Order-to-node index.
void update(const unsigned char symbol) noexcept
Insert an unseen byte or increment an existing byte's path.
int nyt
Current NYT node index.
static constexpr int max_nodes
Including NYT.
void check_capacity() const
Reject an update that would overflow the frequency counters.
Model() noexcept
Construct the initial tree containing only NYT.
static constexpr int alphabet_size
Number of possible bytes.
size_t distinct
Number of bytes seen so far.