Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Huffman_Encoder_Engine Class Reference

Huffman encoder. More...

#include <Huffman.H>

Collaboration diagram for Aleph::Huffman_Encoder_Engine:
[legend]

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_Nodefreq_root
 
std::string end_symbol
 
size_t text_len
 

Static Private Attributes

static const size_t Max_Token_Size = 256
 

Detailed Description

Huffman encoder.

See also
Huffman_Decoder_Engine

Definition at line 121 of file Huffman.H.

Constructor & Destructor Documentation

◆ Huffman_Encoder_Engine()

Aleph::Huffman_Encoder_Engine::Huffman_Encoder_Engine ( )
inline

Encoder constructor.

Definition at line 195 of file Huffman.H.

◆ ~Huffman_Encoder_Engine()

Aleph::Huffman_Encoder_Engine::~Huffman_Encoder_Engine ( )
inline

Destructor - frees all allocated memory.

Definition at line 202 of file Huffman.H.

References clear_build_state().

Member Function Documentation

◆ append_code()

static void Aleph::Huffman_Encoder_Engine::append_code ( BitArray bit_stream,
const BitArray symbol_code 
)
inlinestaticprivate

Definition at line 185 of file Huffman.H.

References Aleph::maps(), Aleph::DynList< T >::push(), and Aleph::HTList::size().

Referenced by encode(), and encode().

◆ build_encoding_map()

void Aleph::Huffman_Encoder_Engine::build_encoding_map ( )
inlineprivate

◆ build_prefix_encoding()

void Aleph::Huffman_Encoder_Engine::build_prefix_encoding ( BinNode< std::string > *  p,
BitArray array 
)
inlineprivate

◆ clear_build_state()

◆ encode() [1/2]

size_t Aleph::Huffman_Encoder_Engine::encode ( char input,
BitArray bit_stream 
)
inline

Encode the input text.

encode(input, bit_stream) reads input, encodes it and appends the result to bit_stream.

Parameters
[in]inputNUL-terminated std::string containing the text to encode.
[out]bit_streamBit array where the encoded stream is appended.
Returns
Total bit length of bit_stream after encoding.
Exceptions
std::domain_errorif 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() [2/2]

size_t Aleph::Huffman_Encoder_Engine::encode ( std::istream &  input,
BitArray bit_stream 
)
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.

Parameters
[in]inputStream containing the text to encode.
[out]bit_streamBit array where the encoded stream is appended.
Returns
Total bit length of bit_stream after encoding.
Exceptions
std::domain_errorif 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().

◆ generate_huffman_tree()

BinNode< std::string > * Aleph::Huffman_Encoder_Engine::generate_huffman_tree ( const bool with_freqs = false)
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.

Parameters
[in]with_freqsIf true, also build the frequency tree.
Returns
The root of the Huffman decoding (prefix) tree.
Exceptions
std::bad_allocif there is not enough memory.
See also
build_prefix_encoding Huffman_Decoder_Engine

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().

◆ get_end_of_stream()

const std::string & Aleph::Huffman_Encoder_Engine::get_end_of_stream ( ) const
inlinenoexcept

Return the configured end-of-stream symbol.

Definition at line 597 of file Huffman.H.

References end_symbol.

◆ get_freq_root()

Freq_Node *& Aleph::Huffman_Encoder_Engine::get_freq_root ( )
inline

Returns the root of the frequency tree.

Definition at line 392 of file Huffman.H.

References ah_domain_error_if, and freq_root.

◆ get_root()

BinNode< std::string > *& Aleph::Huffman_Encoder_Engine::get_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.

◆ insert_end_symbol_node()

void Aleph::Huffman_Encoder_Engine::insert_end_symbol_node ( const std::string &  str)
inlineprivate

◆ load_leaf_keys_in_prefix()

static void Aleph::Huffman_Encoder_Engine::load_leaf_keys_in_prefix ( BinNode< std::string > *  p,
std::istream &  input 
)
inlinestaticprivate

◆ load_string_from_bytes()

static std::string Aleph::Huffman_Encoder_Engine::load_string_from_bytes ( std::istream &  input)
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().

◆ load_tree()

void Aleph::Huffman_Encoder_Engine::load_tree ( std::istream &  input)
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.

Parameters
[in]inputInput stream containing the serialized tree.
Exceptions
std::bad_allocif there is not enough memory.
std::domain_errorif the stream is malformed.
See also
save_tree()

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.

◆ read_input() [1/2]

void Aleph::Huffman_Encoder_Engine::read_input ( char input,
const bool with_freqs = false 
)
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.

Parameters
[in]inputNUL-terminated std::string to encode.
[in]with_freqsIf true, also build the frequency tree.
Exceptions
std::bad_allocif 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().

◆ read_input() [2/2]

void Aleph::Huffman_Encoder_Engine::read_input ( std::istream &  input,
const bool with_freqs = false 
)
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.

Parameters
[in]inputStream containing the text to encode.
[in]with_freqsIf true, also build the frequency tree.
Exceptions
std::bad_allocif 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().

◆ save_leaf_keys_in_prefix()

static void Aleph::Huffman_Encoder_Engine::save_leaf_keys_in_prefix ( BinNode< std::string > *  p,
std::ostream &  output 
)
inlinestaticprivate

◆ save_string_as_bytes()

static void Aleph::Huffman_Encoder_Engine::save_string_as_bytes ( const std::string &  str,
std::ostream &  output 
)
inlinestaticprivate

Definition at line 434 of file Huffman.H.

References output.

Referenced by save_leaf_keys_in_prefix(), and save_tree().

◆ save_tree()

void Aleph::Huffman_Encoder_Engine::save_tree ( std::ostream &  output)
inline

Save a Huffman tree into a stream.

The serialization format is:

  • a prefix bit sequence describing the tree topology
  • the end-of-stream symbol (length + bytes)
  • the leaf keys in preorder (length + bytes)
Parameters
[out]outputAn output stream where the tree will be written.
Exceptions
std::domain_errorif the tree has not been generated.
See also
load_tree()

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().

◆ save_tree_in_array_of_chars()

void Aleph::Huffman_Encoder_Engine::save_tree_in_array_of_chars ( const std::string &  array_name,
std::ostream &  output 
)
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.

Parameters
[in]array_namePrefix name for the generated arrays.
[out]outputOutput stream where declarations are written.
Exceptions
std::domain_errorif the tree has not been generated.
See also
Aleph::load_tree_from_array()

Definition at line 269 of file Huffman.H.

References ah_domain_error_if, Aleph::maps(), output, and root.

◆ set_end_of_stream()

void Aleph::Huffman_Encoder_Engine::set_end_of_stream ( const std::string &  str)
inline

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().

◆ set_freq()

void Aleph::Huffman_Encoder_Engine::set_freq ( const std::string &  str,
const size_t freq 
)
inline

Define the frequency of a symbol.

set_freq(str, freq) tells the encoder that symbol str has frequency freq.

Parameters
[in]strSymbol.
[in]freqSymbol frequency.
Exceptions
std::bad_allocif 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().

◆ test_end()

bool Aleph::Huffman_Encoder_Engine::test_end ( const std::string &  str) const
inlineprivate

Definition at line 155 of file Huffman.H.

References end_symbol.

Referenced by set_freq(), and update_freq().

◆ update_freq()

Member Data Documentation

◆ code_map

Code_Map Aleph::Huffman_Encoder_Engine::code_map
private

◆ end_symbol

std::string Aleph::Huffman_Encoder_Engine::end_symbol
private

◆ freq_root

Freq_Node* Aleph::Huffman_Encoder_Engine::freq_root
private

Definition at line 128 of file Huffman.H.

Referenced by generate_huffman_tree(), get_freq_root(), and load_tree().

◆ heap

Huffman_Heap Aleph::Huffman_Encoder_Engine::heap
private

◆ Max_Token_Size

const size_t Aleph::Huffman_Encoder_Engine::Max_Token_Size = 256
staticprivate

Definition at line 432 of file Huffman.H.

Referenced by encode(), load_string_from_bytes(), and read_input().

◆ root

◆ symbol_map

Symbol_Map Aleph::Huffman_Encoder_Engine::symbol_map
private

◆ text_len

size_t Aleph::Huffman_Encoder_Engine::text_len
private

Definition at line 131 of file Huffman.H.

Referenced by clear_build_state(), read_input(), and read_input().


The documentation for this class was generated from the following file: