|
Aleph-w 3.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) |
| Save a Huffman tree into a stream. | |
| void | save_tree_in_array_of_chars (const std::string &array_name, std::ostream &output) |
| 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_prefix_encoding (BinNode< std::string > *p, BitArray &array) |
| void | build_encoding_map () |
| bool | test_end (const std::string &str) const |
| 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 | 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 |
| 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 202 of file Huffman.H.
References clear_build_state().
|
inlinestaticprivate |
Definition at line 185 of file Huffman.H.
References Aleph::maps(), Aleph::DynList< T >::push(), and Aleph::HTList::size().
|
inlineprivate |
Definition at line 145 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 generate_huffman_tree(), and load_tree().
|
inlineprivate |
Definition at line 133 of file Huffman.H.
References build_prefix_encoding(), code_map, Aleph::BinNode< Key >::get_key(), Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::is_leaf(), Aleph::LLINK(), Aleph::BitArray::pop(), Aleph::BitArray::push(), and Aleph::RLINK().
Referenced by build_encoding_map(), and build_prefix_encoding().
|
inlineprivatenoexcept |
Definition at line 502 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(), Aleph::maps(), symbol_map, and text_len.
Referenced by ~Huffman_Encoder_Engine(), and load_tree().
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 612 of file Huffman.H.
References ah_domain_error_if, append_code(), code_map, end_symbol, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Aleph::maps(), Max_Token_Size, root, and Aleph::HTList::size().
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 643 of file Huffman.H.
References ah_domain_error_if, append_code(), code_map, end_symbol, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Aleph::DynList< T >::get(), Aleph::maps(), root, and Aleph::HTList::size().
|
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. |
| std::bad_alloc | if there is not enough memory. |
Definition at line 299 of file Huffman.H.
References ah_domain_error_if, build_encoding_map(), freq_root, Aleph::get_freq(), Aleph::BinNode< Key >::get_key(), Aleph::GenBinHeap< NodeType, Key, Compare >::getMin(), heap, Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::is_empty(), Aleph::LLINK(), Aleph::maps(), Aleph::RLINK(), root, Aleph::set_freq(), and Aleph::GenBinHeap< NodeType, Key, Compare >::size().
Referenced by read_input(), and read_input().
|
inlinenoexcept |
Return the configured end-of-stream symbol.
Definition at line 597 of file Huffman.H.
References end_symbol.
|
inline |
Returns the root of the frequency tree.
Definition at line 392 of file Huffman.H.
References ah_domain_error_if, and freq_root.
|
inline |
Returns the root of the Huffman decoding tree.
Definition at line 279 of file Huffman.H.
References ah_domain_error_if, and root.
Definition at line 492 of file Huffman.H.
References Aleph::DynList< T >::get(), heap, Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), Aleph::maps(), and symbol_map.
Referenced by read_input(), read_input(), and set_end_of_stream().
|
inlinestaticprivate |
Definition at line 477 of file Huffman.H.
References Aleph::BinNode< Key >::get_key(), Aleph::is_leaf(), Aleph::LLINK(), load_leaf_keys_in_prefix(), load_string_from_bytes(), Aleph::maps(), and Aleph::RLINK().
Referenced by load_leaf_keys_in_prefix(), and load_tree().
|
inlinestaticprivate |
Definition at line 441 of file Huffman.H.
References ah_domain_error_if, Aleph::maps(), 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 374 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::maps(), Aleph::prefix(), and root.
|
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 528 of file Huffman.H.
References ah_domain_error_if, end_symbol, generate_huffman_tree(), insert_end_symbol_node(), Aleph::maps(), Max_Token_Size, root, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), set_end_of_stream(), symbol_map, text_len, and update_freq().
|
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 561 of file Huffman.H.
References ah_domain_error_if, end_symbol, generate_huffman_tree(), Aleph::DynList< T >::get(), insert_end_symbol_node(), Aleph::maps(), root, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), set_end_of_stream(), symbol_map, text_len, and update_freq().
|
inlinestaticprivate |
Definition at line 461 of file Huffman.H.
References Aleph::BinNode< Key >::get_key(), Aleph::is_leaf(), Aleph::LLINK(), output, Aleph::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 434 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 236 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 269 of file Huffman.H.
References ah_domain_error_if, Aleph::maps(), output, and root.
Defines the end-of-stream symbol.
Definition at line 583 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(), and read_input().
|
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 409 of file Huffman.H.
References ah_domain_error, ah_domain_error_if, Aleph::DynList< T >::get(), heap, Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), Aleph::maps(), root, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), Aleph::set_freq(), symbol_map, and test_end().
Definition at line 155 of file Huffman.H.
References end_symbol.
Referenced by set_freq(), and update_freq().
Definition at line 162 of file Huffman.H.
References ah_domain_error_if, Aleph::DynList< T >::get(), heap, Aleph::increase_freq(), Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), Aleph::maps(), 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 126 of file Huffman.H.
Referenced by build_encoding_map(), build_prefix_encoding(), clear_build_state(), encode(), and encode().
|
private |
Definition at line 130 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 128 of file Huffman.H.
Referenced by generate_huffman_tree(), get_freq_root(), and load_tree().
|
private |
Definition at line 124 of file Huffman.H.
Referenced by clear_build_state(), generate_huffman_tree(), insert_end_symbol_node(), set_freq(), and update_freq().
Definition at line 432 of file Huffman.H.
Referenced by encode(), load_string_from_bytes(), and read_input().
|
private |
Definition at line 123 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 125 of file Huffman.H.
Referenced by build_encoding_map(), clear_build_state(), insert_end_symbol_node(), read_input(), read_input(), set_end_of_stream(), set_freq(), and update_freq().
|
private |
Definition at line 131 of file Huffman.H.
Referenced by clear_build_state(), read_input(), and read_input().