|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Huffman encoder. More...
#include <Huffman.H>
Classes | |
| struct | Get_Key |
| struct | Load_Key |
Public Member Functions | |
| Huffman_Encoder_Engine () | |
| Encoder constructor. | |
| ~Huffman_Encoder_Engine () | |
| Destructor - frees all allocated memory. | |
| void | save_tree (std::ostream &output) const |
| Save a Huffman tree into a stream. | |
| 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. | |
| BinNode< std::string > *& | get_root () |
| Returns the root of the Huffman decoding tree. | |
| BinNode< std::string > * | generate_huffman_tree (const bool &with_freqs=false) |
| Generate the Huffman prefix tree. | |
| void | load_tree (std::istream &input) |
| Load and build a binary tree from a stream. | |
| Freq_Node *& | get_freq_root () |
| Returns the root of the frequency tree. | |
| void | set_freq (const std::string &str, const size_t &freq) |
| Define the frequency of a symbol. | |
| void | read_input (char *input, const bool &with_freqs=false) |
| Read a NUL-terminated character std::string, count frequencies and build the prefix tree. | |
| void | read_input (std::istream &input, const bool &with_freqs=false) |
| Read a stream, count frequencies and build the prefix tree. | |
| void | set_end_of_stream (const std::string &str) |
| Defines the end-of-stream symbol. | |
| const std::string & | get_end_of_stream () const noexcept |
| Return the configured end-of-stream symbol. | |
| size_t | encode (char *input, BitArray &bit_stream) |
| Encode the input text. | |
| size_t | encode (std::istream &input, BitArray &bit_stream) |
| Encode the input text from a stream. | |
Private Member Functions | |
| void | build_encoding_map () |
| void | undo_generation () noexcept |
| void | merge_two_nodes (const bool with_freqs) |
| bool | test_end (const std::string &str) const |
| Huffman_Node * | insert_symbol_node (const std::string &str, const size_t freq) |
| void | update_freq (const std::string &str) |
| void | insert_end_symbol_node (const std::string &str) |
| void | clear_build_state () noexcept |
Static Private Member Functions | |
| static void | build_prefix_encoding (BinNode< std::string > *p, BitArray &array, Code_Map &codes) |
| template<class Node > | |
| static void | free_inner_nodes (Node *p) noexcept |
| static void | make_freq_node (Huffman_Node *node) |
| static void | append_code (BitArray &bit_stream, const BitArray &symbol_code) |
| static void | save_string_as_bytes (const std::string &str, std::ostream &output) |
| static std::string | load_string_from_bytes (std::istream &input) |
| static void | save_leaf_keys_in_prefix (BinNode< std::string > *p, std::ostream &output) |
| static void | load_leaf_keys_in_prefix (BinNode< std::string > *p, std::istream &input) |
Private Attributes | |
| BinNode< std::string > * | root_ |
| Huffman_Heap | heap_ |
| Symbol_Map | symbol_map_ |
| Code_Map | code_map_ |
| Huffman_Node * | symbols_ = nullptr |
| Freq_Node * | freq_root_ |
| std::string | end_symbol_ |
| size_t | text_len_ |
Static Private Attributes | |
| static const size_t | Max_Token_Size = 256 |
|
inline |
|
inline |
Destructor - frees all allocated memory.
Definition at line 315 of file Huffman.H.
References clear_build_state().
|
inlinestaticprivate |
Definition at line 300 of file Huffman.H.
References Aleph::BitArray::push(), and Aleph::BitArray::size().
|
inlineprivate |
Definition at line 152 of file Huffman.H.
References ah_domain_error_if, build_prefix_encoding(), code_map_, Aleph::DynSetTree< Key, Tree, Compare >::empty(), root_, and symbol_map_.
Referenced by load_tree().
|
inlinestaticprivate |
Definition at line 136 of file Huffman.H.
References build_prefix_encoding(), Aleph::BinNode< Key >::get_key(), Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::is_leaf(), LLINK, Aleph::BitArray::pop(), Aleph::BitArray::push(), and RLINK.
Referenced by build_encoding_map(), build_prefix_encoding(), and generate_huffman_tree().
|
inlineprivatenoexcept |
Definition at line 583 of file Huffman.H.
References Aleph::Huffman_Node::bin_node, code_map_, Aleph::DynSetTree< Key, Tree, Compare >::empty(), end_symbol_, Aleph::GenBinHeap< NodeType, Key, Compare >::getMin_ne(), heap_, Aleph::GenBinHeap< NodeType, Key, Compare >::is_empty(), symbol_map_, symbols_, and text_len_.
Referenced by ~Huffman_Encoder_Engine(), and load_tree().
|
inline |
Encode the input text.
encode(input, bit_stream) reads input, encodes it and appends the result to bit_stream.
| [in] | input | NUL-terminated std::string containing the text to encode. |
| [out] | bit_stream | Bit array where the encoded stream is appended. |
bit_stream after encoding. | std::domain_error | if the prefix tree has not been generated. |
Definition at line 694 of file Huffman.H.
References ah_domain_error_if, append_code(), code_map_, end_symbol_, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Max_Token_Size, root_, and Aleph::BitArray::size().
Referenced by main(), and read_and_encode().
|
inline |
Encode the input text from a stream.
encode(input, bit_stream) reads from input until EOF, encodes the symbols and appends the result to bit_stream.
| [in] | input | Stream containing the text to encode. |
| [out] | bit_stream | Bit array where the encoded stream is appended. |
bit_stream after encoding. | std::domain_error | if the prefix tree has not been generated. |
Definition at line 723 of file Huffman.H.
References ah_domain_error_if, append_code(), code_map_, end_symbol_, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), root_, and Aleph::BitArray::size().
|
inlinestaticprivatenoexcept |
Definition at line 165 of file Huffman.H.
References Aleph::and, free_inner_nodes(), LLINK, and RLINK.
Referenced by free_inner_nodes(), and undo_generation().
|
inline |
Generate the Huffman prefix tree.
generate_huffman_tree(with_freqs) runs Huffman's algorithm and builds the prefix tree from the collected symbol frequencies. If with_freqs is true, an additional tree containing frequencies is built as well.
| [in] | with_freqs | If true, also build the frequency tree. |
destroyRec(encoder.get_root()), which also clears the root that the encoder keeps. load_tree() frees the tree that the encoder still has. With with_freqs, the same holds for get_freq_root(). | std::bad_alloc | if there is not enough memory. The encoder is then as it was, with its symbols and their frequencies. |
Definition at line 414 of file Huffman.H.
References ah_domain_error_if, Aleph::Huffman_Node::bin_node, build_prefix_encoding(), code_map_, Aleph::DynSetTree< Key, Tree, Compare >::empty(), Aleph::Huffman_Node::freq_node, freq_root_, Aleph::GenBinHeap< NodeType, Key, Compare >::getMin_ne(), heap_, Aleph::GenBinHeap< NodeType, Key, Compare >::is_empty(), Aleph::is_leaf(), make_freq_node(), merge_two_nodes(), Aleph::Huffman_Node::next_symbol, root_, Aleph::GenBinHeap< NodeType, Key, Compare >::size(), Aleph::DynSetTree< Key, Tree, Compare >::swap(), symbol_map_, symbols_, Aleph::GenBinHeap< NodeType, Key, Compare >::top(), and undo_generation().
Referenced by read_input(), read_input(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Return the configured end-of-stream symbol.
Definition at line 679 of file Huffman.H.
References end_symbol_.
Referenced by TEST_F().
|
inline |
|
inline |
|
inlineprivate |
Definition at line 578 of file Huffman.H.
References insert_symbol_node().
Referenced by read_input(), read_input(), and set_end_of_stream().
|
inlineprivate |
Definition at line 261 of file Huffman.H.
References heap_, Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), Aleph::Huffman_Node::next_symbol, Aleph::set_freq(), symbol_map_, and symbols_.
Referenced by insert_end_symbol_node(), set_freq(), and update_freq().
|
inlinestaticprivate |
Definition at line 563 of file Huffman.H.
References Aleph::BinNode< Key >::get_key(), Aleph::is_leaf(), LLINK, load_leaf_keys_in_prefix(), load_string_from_bytes(), and RLINK.
Referenced by load_leaf_keys_in_prefix(), and load_tree().
|
inlinestaticprivate |
Definition at line 527 of file Huffman.H.
References ah_domain_error_if, and Max_Token_Size.
Referenced by load_leaf_keys_in_prefix(), and load_tree().
|
inline |
Load and build a binary tree from a stream.
load_tree(input) reads a Huffman tree previously saved with save_tree() and restores it into memory.
| [in] | input | Input stream containing the serialized tree. |
| std::bad_alloc | if there is not enough memory. |
| std::domain_error | if the stream is malformed. |
Definition at line 467 of file Huffman.H.
References build_encoding_map(), clear_build_state(), Aleph::destroyRec(), end_symbol_, freq_root_, Aleph::BitArray::load(), load_leaf_keys_in_prefix(), load_string_from_bytes(), Aleph::prefix(), and root_.
|
inlinestaticprivate |
Definition at line 197 of file Huffman.H.
References Aleph::Huffman_Node::bin_node, Aleph::Huffman_Node::freq_node, Aleph::BinHeapNode< Key >::get_key(), and Aleph::BinNode< Key >::get_key().
Referenced by generate_huffman_tree(), and merge_two_nodes().
|
inlineprivate |
Definition at line 210 of file Huffman.H.
References Aleph::get_freq(), Aleph::GenBinHeap< NodeType, Key, Compare >::getMin(), heap_, Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), Aleph::is_leaf(), LLINK, make_freq_node(), RLINK, and Aleph::set_freq().
Referenced by generate_huffman_tree().
|
inline |
Read a NUL-terminated character std::string, count frequencies and build the prefix tree.
read_input(input, with_freqs) reads the NUL-terminated std::string input, counts the frequency of each symbol and builds the Huffman prefix tree.
| [in] | input | NUL-terminated std::string to encode. |
| [in] | with_freqs | If true, also build the frequency tree. |
| std::bad_alloc | if there is not enough memory. |
Definition at line 609 of file Huffman.H.
References ah_domain_error_if, end_symbol_, generate_huffman_tree(), insert_end_symbol_node(), Max_Token_Size, root_, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), set_end_of_stream(), symbol_map_, text_len_, and update_freq().
Referenced by HuffmanBtreepicTest::create_encoder(), main(), read_and_encode(), TEST_F(), and TEST_F().
|
inline |
Read a stream, count frequencies and build the prefix tree.
read_input(input, with_freqs) reads characters from input until EOF, counts symbol frequencies and builds the Huffman prefix tree.
| [in] | input | Stream containing the text to encode. |
| [in] | with_freqs | If true, also build the frequency tree. |
| std::bad_alloc | if there is not enough memory. |
Definition at line 642 of file Huffman.H.
References ah_domain_error_if, end_symbol_, generate_huffman_tree(), insert_end_symbol_node(), root_, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), set_end_of_stream(), symbol_map_, text_len_, and update_freq().
|
inlinestaticprivate |
Definition at line 547 of file Huffman.H.
References Aleph::BinNode< Key >::get_key(), Aleph::is_leaf(), LLINK, output, RLINK, save_leaf_keys_in_prefix(), and save_string_as_bytes().
Referenced by save_leaf_keys_in_prefix(), and save_tree().
|
inlinestaticprivate |
Definition at line 520 of file Huffman.H.
References output.
Referenced by save_leaf_keys_in_prefix(), and save_tree().
|
inline |
Save a Huffman tree into a stream.
The serialization format is:
| [out] | output | An output stream where the tree will be written. |
| std::domain_error | if the tree has not been generated. |
Definition at line 350 of file Huffman.H.
References ah_domain_error_if, end_symbol_, output, Aleph::prefix(), root_, save_leaf_keys_in_prefix(), save_string_as_bytes(), and Aleph::tree_to_bits().
|
inline |
Generate C/C++ array declarations for a Huffman tree.
save_tree_in_array_of_chars(array_name, output) writes two array declarations that can be used to reconstruct the binary tree:
const unsigned char array_name_cdp[n] = { ... };const char * array_name_k[] = { ... };The first array stores the topology as a prefix bit code (a Lukasiewicz word). The second array stores node contents in preorder. The Get_Key functor is used to stringify the node contents; internal nodes typically return an empty std::string.
| [in] | array_name | Prefix name for the generated arrays. |
| [out] | output | Output stream where declarations are written. |
| std::domain_error | if the tree has not been generated. |
Definition at line 382 of file Huffman.H.
References ah_domain_error_if, output, and root_.
Referenced by main().
|
inline |
Defines the end-of-stream symbol.
Definition at line 664 of file Huffman.H.
References ah_domain_error_if, end_symbol_, insert_end_symbol_node(), root_, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), and symbol_map_.
Referenced by read_input(), read_input(), and TEST_F().
|
inline |
Define the frequency of a symbol.
set_freq(str, freq) tells the encoder that symbol str has frequency freq.
| [in] | str | Symbol. |
| [in] | freq | Symbol frequency. |
| std::bad_alloc | if there is not enough memory. |
Definition at line 501 of file Huffman.H.
References ah_domain_error, ah_domain_error_if, insert_symbol_node(), root_, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), symbol_map_, and test_end().
|
inlineprivate |
Definition at line 251 of file Huffman.H.
References end_symbol_.
Referenced by set_freq(), and update_freq().
|
inlineprivatenoexcept |
Definition at line 177 of file Huffman.H.
References free_inner_nodes(), Aleph::GenBinHeap< NodeType, Key, Compare >::getMin_ne(), heap_, Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::is_empty(), Aleph::is_leaf(), Aleph::Huffman_Node::next_symbol, and symbols_.
Referenced by generate_huffman_tree().
|
inlineprivate |
Definition at line 283 of file Huffman.H.
References ah_domain_error_if, heap_, Aleph::increase_freq(), insert_symbol_node(), root_, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), symbol_map_, test_end(), and Aleph::GenBinHeap< NodeType, Key, Compare >::update().
Referenced by read_input(), and read_input().
|
private |
Definition at line 124 of file Huffman.H.
Referenced by build_encoding_map(), clear_build_state(), encode(), encode(), and generate_huffman_tree().
|
private |
Definition at line 133 of file Huffman.H.
Referenced by clear_build_state(), encode(), encode(), get_end_of_stream(), load_tree(), read_input(), read_input(), save_tree(), set_end_of_stream(), and test_end().
|
private |
Definition at line 131 of file Huffman.H.
Referenced by generate_huffman_tree(), get_freq_root(), and load_tree().
|
private |
Definition at line 122 of file Huffman.H.
Referenced by clear_build_state(), generate_huffman_tree(), insert_symbol_node(), merge_two_nodes(), undo_generation(), and update_freq().
|
staticprivate |
Definition at line 518 of file Huffman.H.
Referenced by encode(), load_string_from_bytes(), and read_input().
|
private |
Definition at line 121 of file Huffman.H.
Referenced by build_encoding_map(), encode(), encode(), generate_huffman_tree(), get_root(), load_tree(), read_input(), read_input(), save_tree(), save_tree_in_array_of_chars(), set_end_of_stream(), set_freq(), and update_freq().
|
private |
Definition at line 123 of file Huffman.H.
Referenced by build_encoding_map(), clear_build_state(), generate_huffman_tree(), insert_symbol_node(), read_input(), read_input(), set_end_of_stream(), set_freq(), and update_freq().
|
private |
Definition at line 129 of file Huffman.H.
Referenced by clear_build_state(), generate_huffman_tree(), insert_symbol_node(), and undo_generation().
|
private |
Definition at line 134 of file Huffman.H.
Referenced by clear_build_state(), read_input(), and read_input().