31#ifndef ALEPH_ADAPTIVE_HUFFMAN_DEMO_SUPPORT_H
32#define ALEPH_ADAPTIVE_HUFFMAN_DEMO_SUPPORT_H
56 case '\n':
return "LF";
57 case '\r':
return "CR";
58 case '\t':
return "TAB";
59 case ' ':
return "SPACE";
61 if (
byte >= 33
and byte <= 126)
62 return std::string(1,
static_cast<char>(
byte));
63 constexpr char hex[] =
"0123456789ABCDEF";
64 std::string label =
"0x";
65 label += hex[
byte >> 4];
66 label += hex[
byte & 15];
77 const unsigned char byte)
82 return bits.get_bit_str();
111 const int symbol,
const std::uint64_t weight)
114 tree.
nodes[node].weight += weight;
115 for (
const char digit :
code)
117 const int bit = digit -
'0';
118 if (tree.
nodes[node].children[bit] == -1)
120 const int child =
static_cast<int>(tree.
nodes.size());
122 added.
code = tree.
nodes[node].code + digit;
123 tree.
nodes.append(added);
125 tree.
nodes[node].children[bit] = child;
127 node = tree.
nodes[node].children[bit];
128 tree.
nodes[node].weight += weight;
130 tree.
nodes[node].symbol = symbol;
142 auto &entry = tree.
nodes[node];
143 entry.y = 48 + 86 * depth;
144 if (depth > tree.
depth)
146 if (entry.symbol != -2)
147 entry.x = 60 + 108 * next_leaf++;
152 entry.x = (tree.
nodes[entry.children[0]].x +
153 tree.
nodes[entry.children[1]].x) / 2;
167 for (
int byte = 0;
byte < 256; ++byte)
169 const auto value =
static_cast<unsigned char>(byte);
173 else if (unseen == -1)
178 auto code =
next_code(encoder,
static_cast<unsigned char>(unseen));
187 for (
size_t i = 0; i <
count and not found; ++i)
188 if (result.
nodes[i].symbol == -2)
189 for (
int bit = 0; bit < 2; ++bit)
190 if (result.
nodes[i].children[bit] == -1)
192 const auto code = result.
nodes[i].code +
static_cast<char>(
'0' + bit);
200 for (
const auto &node : result.
nodes)
201 if (node.symbol == -2)
204 <<
"Display tree contains an incomplete internal node";
206 result.
nodes[node.children[1]].weight)
207 <<
"Display tree contains inconsistent frequencies";
210 <<
"Display tree does not represent the encoder's current state";
218 unsigned char byte = 0;
238 frames.
append(std::move(initial));
240 for (
const unsigned char byte : input)
251 frame.
path.resize(frame.
path.size() - 8);
253 unsigned char decoded = 0;
254 for (
size_t i = 0; i <
bits.size(); ++i)
258 <<
"A byte was emitted at the wrong bit boundary";
264 frames.
append(std::move(frame));
277 const std::string &indent =
" ",
278 const std::string &edge =
"")
280 const auto &entry = tree.
nodes[node];
281 out << indent << edge;
282 if (entry.symbol == -2)
283 out <<
'[' << entry.weight <<
']';
285 out << (entry.symbol == -1 ?
"NYT" :
byte_label(
static_cast<unsigned char>(entry.symbol)))
286 <<
" (" << entry.weight <<
")";
288 if (entry.symbol == -2)
290 const std::string next_indent = indent + (edge.empty() ?
"" :
291 edge[0] ==
'+' ?
"| " :
" ");
292 print_tree(
out, tree, entry.children[0], next_indent,
"+-0- ");
293 print_tree(
out, tree, entry.children[1], next_indent,
"`-1- ");
One-pass adaptive Huffman coding of bytes using FGK updates.
Exception handling system with formatted messages for Aleph-w.
#define ah_logic_error_unless(C)
Throws std::logic_error if condition does NOT hold.
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
size_t size_t int32_t value
size_t size_t int32_t * out
Incremental FGK decoder accepting arbitrarily fragmented bits.
void finish() const
Verify that an externally framed message ended at a byte boundary.
bool decode_bit(const unsigned int bit, unsigned char &symbol)
Consume one bit and emit a byte if its codeword is complete.
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.
std::uint64_t frequency(const unsigned char symbol) const noexcept
Return the current frequency of a byte.
std::uint64_t symbols_processed() const noexcept
Return the number of bytes encoded since construction or reset.
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Contiguous array of bits.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
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.
void position_nodes(Snapshot &tree, const int node, const size_t depth, size_t &next_leaf)
Assign inorder leaf positions and center parents over children.
Aleph::Array< Frame > trace(const std::string_view input)
Record an actual online round trip, starting with the empty model.
std::string byte_label(const unsigned char byte)
Return an unambiguous, terminal-safe label for a byte.
Snapshot snapshot(const Aleph::Adaptive_Huffman_Encoder &encoder)
Reconstruct and validate the tree using public encoder operations.
void insert_code(Snapshot &tree, const std::string &code, const int symbol, const std::uint64_t weight)
Insert a code into the display trie and accumulate its frequency.
std::string next_code(const Aleph::Adaptive_Huffman_Encoder &encoder, const unsigned char byte)
Probe a byte's code without changing the source encoder.
void print_tree(std::ostream &out, const Snapshot &tree, const int node=0, const std::string &indent=" ", const std::string &edge="")
Print a byte-labelled, edge-labelled tree using portable ASCII.
One node of a reconstructed display tree.
int children[2]
Child indices; -1 denotes absence.
int symbol
Byte value, -1 for NYT, -2 for an internal node.
double y
Vertical drawing coordinate.
std::uint64_t weight
Sum of the descendant symbol frequencies.
double x
Horizontal drawing coordinate.
std::string code
Root-to-node path.
One verified encoding event, plus the tree after the event.
std::string literal
Eight raw bits on a first appearance.
unsigned char byte
Byte consumed by this event.
std::string path
Tree path before the update.
bool first_appearance
Whether the byte was previously unseen.
Snapshot tree
Encoder state after the update.
size_t total_bits
Cumulative encoded length.
A complete tree for one externally observable encoder state.
size_t leaf_count
Includes the retained NYT leaf.
size_t depth
Maximum leaf depth.
Aleph::Array< Display_Node > nodes
Root at index zero.
Dynamic array container with automatic resizing.