|
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) 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_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 197 of file Huffman.H.
References clear_build_state().
|
inlinestaticprivate |
Definition at line 182 of file Huffman.H.
References Aleph::blossom_maximum_cardinality_matching().
|
inlineprivate |
Definition at line 142 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 126 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(), LLINK, Aleph::BitArray::pop(), Aleph::BitArray::push(), and RLINK.
Referenced by build_encoding_map(), and build_prefix_encoding().
|
inlineprivatenoexcept |
Definition at line 489 of file Huffman.H.
References Aleph::Huffman_Node::bin_node, Aleph::blossom_maximum_cardinality_matching(), 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_, 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 598 of file Huffman.H.
References ah_domain_error_if, append_code(), Aleph::blossom_maximum_cardinality_matching(), code_map_, end_symbol_, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Max_Token_Size, and root_.
|
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 627 of file Huffman.H.
References ah_domain_error_if, append_code(), Aleph::blossom_maximum_cardinality_matching(), code_map_, end_symbol_, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), and root_.
|
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 291 of file Huffman.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), 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(), LLINK, 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 583 of file Huffman.H.
References end_symbol_.
|
inline |
Returns the root of the frequency tree.
Definition at line 381 of file Huffman.H.
References ah_domain_error_if, and freq_root_.
|
inline |
Returns the root of the Huffman decoding tree.
Definition at line 272 of file Huffman.H.
References ah_domain_error_if, and root_.
Definition at line 479 of file Huffman.H.
References Aleph::blossom_maximum_cardinality_matching(), heap_, Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), and symbol_map_.
Referenced by read_input(), read_input(), and set_end_of_stream().
|
inlinestaticprivate |
Definition at line 464 of file Huffman.H.
References Aleph::blossom_maximum_cardinality_matching(), 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 428 of file Huffman.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), 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 363 of file Huffman.H.
References Aleph::blossom_maximum_cardinality_matching(), 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_.
|
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 514 of file Huffman.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), 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(), 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 547 of file Huffman.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), 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 448 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 421 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 232 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 264 of file Huffman.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), output, and root_.
Defines the end-of-stream symbol.
Definition at line 569 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 397 of file Huffman.H.
References ah_domain_error, ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), heap_, Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), root_, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), Aleph::set_freq(), symbol_map_, and test_end().
Definition at line 152 of file Huffman.H.
References end_symbol_.
Referenced by set_freq(), and update_freq().
Definition at line 159 of file Huffman.H.
References ah_domain_error_if, Aleph::blossom_maximum_cardinality_matching(), heap_, Aleph::increase_freq(), Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), 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 119 of file Huffman.H.
Referenced by build_encoding_map(), build_prefix_encoding(), clear_build_state(), encode(), and encode().
|
private |
Definition at line 123 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 121 of file Huffman.H.
Referenced by generate_huffman_tree(), get_freq_root(), and load_tree().
|
private |
Definition at line 117 of file Huffman.H.
Referenced by clear_build_state(), generate_huffman_tree(), insert_end_symbol_node(), set_freq(), and update_freq().
|
staticprivate |
Definition at line 419 of file Huffman.H.
Referenced by encode(), load_string_from_bytes(), and read_input().
|
private |
Definition at line 116 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 118 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 124 of file Huffman.H.
Referenced by clear_build_state(), read_input(), and read_input().