95 return huffman_node->get_key();
100 huffman_node->get_key()++;
105 huffman_node->get_key() = freq;
140 const std::string &str = p->
get_key();
164 template <
class Node>
167 if (p ==
nullptr or (
LLINK(p) ==
nullptr and RLINK(p) ==
nullptr))
189 delete leaf->freq_node;
190 leaf->freq_node =
nullptr;
201 std::unique_ptr<Freq_Node> freq_node(
new Freq_Node);
203 freq_node->get_key().second = node->
get_key();
214 std::unique_ptr<Huffman_Node> huffman_node(
new Huffman_Node(bin_node.get()));
217 const size_t new_freq =
get_freq(l_huffman_node) +
get_freq(r_huffman_node);
225 std::unique_ptr<Freq_Node> freq_node(
new Freq_Node);
226 freq_node->get_key().first = std::to_string(new_freq);
227 freq_node->get_key().second = new_freq;
228 LLINK(freq_node.get()) = l_huffman_node->freq_node;
229 RLINK(freq_node.get()) = r_huffman_node->freq_node;
230 huffman_node->freq_node = freq_node.release();
241 LLINK(bin_node.get()) = l_huffman_node->bin_node;
242 RLINK(bin_node.get()) = r_huffman_node->bin_node;
243 if (not
is_leaf(l_huffman_node->bin_node))
244 delete l_huffman_node;
245 if (not
is_leaf(r_huffman_node->bin_node))
246 delete r_huffman_node;
291 huffman_node_ptr ==
nullptr)
294 huffman_node = huffman_node_ptr->second;
302 const size_t symbol_code_size = symbol_code.
size();
303 for (
size_t i = 0; i < symbol_code_size; ++i)
304 bit_stream.
push(symbol_code[i]);
325 return is_leaf(p) ? p->get_key() :
"";
334 input >> p->get_key();
386 Aleph::save_tree_in_array_of_chars<BinNode<std::string>,
Get_Key>(
root_, array_name,
output);
476 root_ = bits_to_tree<BinNode<std::string>>(
prefix);
501 void set_freq(
const std::string &str,
const size_t &freq)
508 if (huffman_node_ptr !=
nullptr)
510 std::string msg =
"Frequency for symbol " + str +
" has already set";
522 output << str.size() <<
" ";
523 for (
unsigned char c : str)
524 output <<
static_cast<unsigned int>(c) <<
" ";
536 for (
size_t i = 0; i < len; ++i)
538 unsigned int byte = 0;
542 str[i] =
static_cast<char>(
static_cast<unsigned char>(byte));
613 char *curr_stream = input;
615 curr_token[1] =
'\0';
618 while (*curr_stream !=
'\0')
620 curr_token[0] = *curr_stream++;
642 void read_input(std::istream &input,
const bool &with_freqs =
false)
646 char curr_token[2] = {
'\0',
'\0'};
649 while (input.get(ch))
671 <<
"End symbol is already present as a normal symbol";
673 std::string end = str;
700 char *curr_stream = input;
702 curr_token[1] =
'\0';
703 while (*curr_stream !=
'\0')
705 curr_token[0] = *curr_stream++;
710 return bit_stream.
size();
729 char curr_token[2] = {
'\0',
'\0'};
731 while (input.get(ch))
738 return bit_stream.
size();
783 const size_t &bit_stream_len = bit_stream.
size();
785 for (
size_t i = 0; i < bit_stream_len; ++i)
796 const std::string &symbol = p->
get_key();
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
WeightedDigraph::Node Node
Space-efficient bit array implementation.
Node for binary search tree.
Contiguous array of bits.
int read_bit(const size_t i) const
Read bit i.
void pop()
Removes the last bit of the array.
void load(std::istream &input)
Loads an array of bits saved with save().
void push(const unsigned int value)
Inserts the value at the end of the array.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
void empty()
Remove all the elements of the set.
Node * insert(Node *p) noexcept(nothrow_cmp)
Inserts a node into a heap.
bool is_empty() const noexcept
void update(Node *p) noexcept(nothrow_cmp)
Updates the priority of a node contained in the heap.
Node * getMin_ne() noexcept(nothrow_cmp)
Removes the node with the lowest priority from a heap that is not empty.
Node * top()
Returns the node with the lowest priority according to the comparison criterion specified in the decl...
Node * getMin()
Removes the node with the lowest priority from the heap.
const size_t & size() const noexcept
BinNode< std::string > * root
Huffman_Decoder_Engine(BinNode< std::string > *p, const std::string &end)
Decoder constructor.
BinNode< std::string > *& get_root()
Returns the root of the Huffman decoding tree.
void decode(const BitArray &bit_stream, std::ostream &output) const
Decode a bit stream.
void insert_end_symbol_node(const std::string &str)
Freq_Node *& get_freq_root()
Returns the root of the frequency tree.
static void save_leaf_keys_in_prefix(BinNode< std::string > *p, std::ostream &output)
void update_freq(const std::string &str)
void build_encoding_map()
void save_tree_in_array_of_chars(const std::string &array_name, std::ostream &output) const
Generate C/C++ array declarations for a Huffman tree.
void save_tree(std::ostream &output) const
Save a Huffman tree into a stream.
size_t encode(char *input, BitArray &bit_stream)
Encode the input text.
static std::string load_string_from_bytes(std::istream &input)
void load_tree(std::istream &input)
Load and build a binary tree from a stream.
static const size_t Max_Token_Size
static void make_freq_node(Huffman_Node *node)
void clear_build_state() noexcept
BinNode< std::string > *& get_root()
Returns the root of the Huffman decoding tree.
void set_freq(const std::string &str, const size_t &freq)
Define the frequency of a symbol.
void merge_two_nodes(const bool with_freqs)
BinNode< std::string > * root_
static void free_inner_nodes(Node *p) noexcept
static void save_string_as_bytes(const std::string &str, std::ostream &output)
void read_input(char *input, const bool &with_freqs=false)
Read a NUL-terminated character std::string, count frequencies and build the prefix tree.
static void load_leaf_keys_in_prefix(BinNode< std::string > *p, std::istream &input)
void read_input(std::istream &input, const bool &with_freqs=false)
Read a stream, count frequencies and build the prefix tree.
bool test_end(const std::string &str) const
void set_end_of_stream(const std::string &str)
Defines the end-of-stream symbol.
~Huffman_Encoder_Engine()
Destructor - frees all allocated memory.
void undo_generation() noexcept
Huffman_Encoder_Engine()
Encoder constructor.
static void append_code(BitArray &bit_stream, const BitArray &symbol_code)
static void build_prefix_encoding(BinNode< std::string > *p, BitArray &array, Code_Map &codes)
Huffman_Node * insert_symbol_node(const std::string &str, const size_t freq)
const std::string & get_end_of_stream() const noexcept
Return the configured end-of-stream symbol.
size_t encode(std::istream &input, BitArray &bit_stream)
Encode the input text from a stream.
BinNode< std::string > * generate_huffman_tree(const bool &with_freqs=false)
Generate the Huffman prefix tree.
void tree_to_bits(Node *root, BitArray &array)
Compute a bit code for the binary tree.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
Main namespace for Aleph-w library functions.
BinHeap< size_t > Huffman_Heap
DynMapTree< std::string, BitArray, Treap_Vtl > Code_Map
static void increase_freq(Huffman_Node *huffman_node) noexcept
static void prefix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
static const size_t & get_freq(Huffman_Node *huffman_node) noexcept
static bool is_leaf(BinNode< std::string > *p) noexcept
static void set_freq(Huffman_Node *huffman_node, const size_t &freq) noexcept
Node heap without virtual destructor.
std::string operator()(BinNode< std::string > *p) const noexcept
void operator()(BinNode< std::string > *p, std::istream &input) const noexcept
Huffman_Node(BinNode< std::string > *node)
BinNode< std::string > * bin_node
Huffman_Node * next_symbol
The node of the symbol that entered the encoder before this one, if this is the node of a symbol: the...
Binary heap implementation using tree structure.
Utility functions for binary tree operations.
Dynamic key-value map based on balanced binary search trees.
Treap: randomized BST combining tree and heap properties.