Constructs and manages a Huffman tree for encoding. More...
#include <Huffman.hpp>
Public Member Functions | |
void | encode_vec (vector< uint8_t > &vec) |
Encodes a vector of bytes in-place using the Huffman codes. | |
void | encodeFile (const string &filename) |
Reads a file and encodes its contents using the Huffman tree. | |
Public Attributes | |
unique_ptr< HuffmanNode > | root |
Pointer to the root node of the Huffman tree. | |
unordered_map< uint8_t, bitset< 32 > > | code8b |
Map of 8-bit codes for each character. | |
unordered_map< uint8_t, bitset< 32 > > | code16b |
Map of 16-bit codes for each character. | |
unordered_map< uint8_t, bitset< 32 > > | code32b |
Map of 32-bit codes for each character. | |
Private Member Functions | |
unordered_map< uint8_t, uint64_t > | count_characters (const vector< uint8_t > &str) |
Counts the frequency of each byte in the input string. | |
unique_ptr< HuffmanNode > | create_tree (priority_queue< unique_ptr< HuffmanNode >, vector< unique_ptr< HuffmanNode > >, HuffmanNode::CompareNodes > &pq) |
Creates a Huffman tree from a given min-heap (priority queue). | |
priority_queue< unique_ptr< HuffmanNode >, vector< unique_ptr< HuffmanNode > >, HuffmanNode::CompareNodes > | create_min_heap () |
Creates a min-heap of Huffman nodes from the frequency map. | |
void | create_code (const unique_ptr< HuffmanNode > &p, bitset< 32 > unique_code) |
Recursively generates the code for each character from the tree. | |
void | create_codes () |
Initializes the code tables for all 8, 16, and 32-bit representations. | |
void | print_codes () |
Prints all the generated codes to standard output. | |
Private Attributes | |
unordered_map< uint8_t, uint64_t > | frecuencies |
Stores the frequency of each byte (character) in the input. | |
Constructs and manages a Huffman tree for encoding.
This class builds a Huffman tree from a byte vector, generates codes for each byte, and can encode input data using 8, 16, or 32-bit representations. It is optimized for educational demonstration purposes and supports visualizing and printing the generated codes.
|
private |
Counts the frequency of each byte in the input string.
str | Vector of bytes to analyze. |
|
private |
Recursively generates the code for each character from the tree.
p | Pointer to current node. |
unique_code | Current prefix code. |
|
private |
Initializes the code tables for all 8, 16, and 32-bit representations.
|
private |
Creates a min-heap of Huffman nodes from the frequency map.
|
private |
Creates a Huffman tree from a given min-heap (priority queue).
pq | A priority queue of Huffman nodes sorted by frequency. |
void HuffmanTree::encode_vec | ( | vector< uint8_t > & | vec | ) |
Encodes a vector of bytes in-place using the Huffman codes.
vec | Vector of bytes to encode. |
void HuffmanTree::encodeFile | ( | const string & | filename | ) |
Reads a file and encodes its contents using the Huffman tree.
filename | Path to the input file. |
|
private |
Prints all the generated codes to standard output.
unordered_map<uint8_t, bitset<32> > HuffmanTree::code16b |
Map of 16-bit codes for each character.
unordered_map<uint8_t, bitset<32> > HuffmanTree::code32b |
Map of 32-bit codes for each character.
unordered_map<uint8_t, bitset<32> > HuffmanTree::code8b |
Map of 8-bit codes for each character.
|
private |
Stores the frequency of each byte (character) in the input.
Used to build the Huffman tree.
unique_ptr<HuffmanNode> HuffmanTree::root |
Pointer to the root node of the Huffman tree.