9#include <unordered_map>
37 unique_ptr<HuffmanNode>
left;
60 HuffmanNode(uint8_t character, uint64_t frecuency, unique_ptr<HuffmanNode> lft, unique_ptr<HuffmanNode> rght);
77 bool operator()(
const unique_ptr<HuffmanNode> &a,
const unique_ptr<HuffmanNode> &b)
const
108 unordered_map<uint8_t, uint64_t>
count_characters(
const vector<uint8_t> &str);
131 void create_code(
const unique_ptr<HuffmanNode> &p, bitset<32> unique_code);
152 unordered_map<uint8_t, bitset<32>>
code8b;
Constructs and manages a Huffman tree for encoding.
Definition Huffman.hpp:94
unordered_map< uint8_t, bitset< 32 > > code16b
Map of 16-bit codes for each character.
Definition Huffman.hpp:157
unordered_map< uint8_t, bitset< 32 > > code32b
Map of 32-bit codes for each character.
Definition Huffman.hpp:162
void create_codes()
Initializes the code tables for all 8, 16, and 32-bit representations.
Definition Huffman.cpp:80
void encodeFile(const string &filename)
Reads a file and encodes its contents using the Huffman tree.
Definition Huffman.cpp:94
unique_ptr< HuffmanNode > root
Pointer to the root node of the Huffman tree.
Definition Huffman.hpp:147
void encode_vec(vector< uint8_t > &vec)
Encodes a vector of bytes in-place using the Huffman codes.
Definition Huffman.cpp:86
void create_code(const unique_ptr< HuffmanNode > &p, bitset< 32 > unique_code)
Recursively generates the code for each character from the tree.
Definition Huffman.cpp:53
unordered_map< uint8_t, bitset< 32 > > code8b
Map of 8-bit codes for each character.
Definition Huffman.hpp:152
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.
Definition Huffman.cpp:43
unordered_map< uint8_t, uint64_t > count_characters(const vector< uint8_t > &str)
Counts the frequency of each byte in the input string.
Definition Huffman.cpp:19
unordered_map< uint8_t, uint64_t > frecuencies
Stores the frequency of each byte (character) in the input.
Definition Huffman.hpp:100
void print_codes()
Prints all the generated codes to standard output.
Definition Huffman.cpp:108
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).
Definition Huffman.cpp:28
Comparator for use in priority queues (min-heap).
Definition Huffman.hpp:69
bool operator()(const unique_ptr< HuffmanNode > &a, const unique_ptr< HuffmanNode > &b) const
Comparator function used to build a min-heap.
Definition Huffman.hpp:77
uint64_t fr
The frequency of the character or combined frequency of children.
Definition Huffman.hpp:32
HuffmanNode(uint8_t character, uint64_t frecuency)
Constructor for a leaf node.
Definition Huffman.cpp:3
unique_ptr< HuffmanNode > left
Pointer to the left child node.
Definition Huffman.hpp:37
unique_ptr< HuffmanNode > right
Pointer to the right child node.
Definition Huffman.hpp:42
uint8_t ch
The character stored in this node (only valid for leaf nodes).
Definition Huffman.hpp:27