📄 huffman_template_algorithm.html
字号:
unsigned int setw_symbol_i,
const string& symbol_title_i,
unsigned int setw_weight_i,
const string& weight_title_i,
const string& code_title_i
);
static void print_show_line_S (
const string& spaces_offset_i,
unsigned int setw_symbol_i,
const SYMBOL& symbol_i,
unsigned int setw_weight_i,
const WEIGHT& weight_i,
const vector<CODE>& path_i
);
bool knowSymbolWeight (
const SYMBOL& symbol_i,
WEIGHT& weight_o
) const;
bool knowCodeSymbol (
const vector<CODE>& path_i,
SYMBOL& symbol_o
) const;
WEIGHT getWeightsSum () const;
WEIGHT getAverageWeight () const
{
return (getWeightsSum ()/getAlphabetSize ());
}
unsigned int getCodeAry () const {return ARY;}
unsigned int getLongestSymbolSize () const;
unsigned int getAlphabetSize () const
{
return vectorHuffmanCodes_.size ();
}
unsigned int getShortestCodeSize () const
{
return vectorHuffmanCodes_[0].size ();
}
unsigned int getLongestCodeSize () const
{
return vectorHuffmanCodes_[vectorHuffmanCodes_.size () - 1].size ();
}
unsigned int getCodeSizesSum () const;
float getAverageCodeSize () const
{
return (static_cast<float>(getCodeSizesSum ())/static_cast<float>(getAlphabetSize ()));
}
WEIGHT getAverageWeightedCodeSize () const
{
return (getWeightedCodeSizesSum ())/(getAlphabetSize ());
}
WEIGHT getWeightedCodeSizesSum () const;
protected :
void doBasicHuffmanTree (
const vector<Cell<SYMBOL, WEIGHT> >& data_vector_i
);
BasicHuffmanTree () {}
BasicHuffmanTree (
const vector<Cell<SYMBOL, WEIGHT> >& data_vector_i
);
BasicHuffmanTree (const string& data_file_name_i);
virtual ~BasicHuffmanTree () {}
public :
bool encodeMsg (
const vector<SYMBOL>& source_msg_i,
vector<CODE>& encoded_msg_o)
const;
bool encodeMsg (
const basic_string<SYMBOL>& source_msg_i,
string& encoded_msg_o)
const;
bool decodeMsg (
const vector<CODE>& encoded_msg_i,
vector<SYMBOL>& decoded_msg_o
) const;
bool decodeMsg (
const string& encoded_msg_i,
basic_string<SYMBOL>& decoded_msg_o
) const;
void showAll (const string& msg_i = string ()) const;
};
<a NAME="label_LoadedHuffmanTree_class"></a>
//#######################################################
//##### PART : template class <b><font color="blue">LoadedHuffmanTree</font></b> #########
//############ <font color="FF00FF"><b>Definition</b></font> ###############################
//#######################################################
//----------- template class LoadedHuffmanTree -----------
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
class LoadedHuffmanTree : public BasicHuffmanTree<SYMBOL, WEIGHT, ARY>
{
public :
LoadedHuffmanTree () : BasicHuffmanTree<SYMBOL, WEIGHT, ARY> () {}
LoadedHuffmanTree (
const vector<Cell<SYMBOL, WEIGHT> >& data_vector_i
)
:
BasicHuffmanTree<SYMBOL, WEIGHT, ARY> (data_vector_i) {}
LoadedHuffmanTree (const string& data_file_name_i)
: BasicHuffmanTree<SYMBOL, WEIGHT, ARY> (data_file_name_i) {}
~LoadedHuffmanTree () {}
};
<a NAME="label_DriedHuffmanTree_class"></a>
//#######################################################
//##### PART : template class <b><a href="#label_DriedHuffmanTree_method">DriedHuffmanTree</a></b> ##########
//############ <font color="FF00FF"><b>Definition</b></font> ###############################
//#######################################################
//----------- template class DriedHuffmanTree -----------
template <typename WEIGHT, unsigned int ARY>
class DriedHuffmanTree : public BasicHuffmanTree<string, WEIGHT, ARY>
{
private :
void doDriedHuffmanTree (
const vector<WEIGHT>& weight_vector_i
);
protected :
public :
DriedHuffmanTree (
const vector<WEIGHT>& weight_vector_i
);
DriedHuffmanTree (const string& weights_file_name_i);
~DriedHuffmanTree () {}
};
#endif // huf_class_H
//#######################################################
//################ END OF FILE ##########################
//#######################################################
</PRE></TD></TR></TABLE>
------------------- C++ code : END ----------------------
=== File #2 of 4 : huf_class.H ==========================
<a NAME="label_huf_methods"></a>
#########################################################
=== File <font color="blue"><b>#3</b></font> <a href="#label_huf_main">of 4</a> : <font color="blue"><b>huf_methods.H</b></font> ========================
------------------- C++ code : BEGIN --------------------
<TABLE><TR><TD bgcolor="#DEEEDD"><PRE>
// ==============================================================
//
// Copyright (c) 1999-2001 by Alex Vinokur. This work and all works
// derived from it may be copied and modified without any
// restrictions other than that a copy of this copyright notice
// must be included in any copy of this work or any derived work.
//
// ==============================================================
///////////////////////////////////////
#ifndef huf_methods_H
#define huf_methods_H
///////////////////////////////////////
static char id_huf_methods_H[] = "@(#)## n-ary Huffman Template Algorithm ## Author : Alex Vinokur ## "__FILE__;
// ##############################################################
// =============================
// n-ary Huffman Template Algorithm
// The algorithm (program) contains the following files :
// - huf_service.H
// - huf_class.H
// - huf_methods.H
// - huf_main.C
// =============================
//
// FILE : <font color="blue"><b>huf_methods.H</b></font>
//
// AUTHOR : Alex Vinokur
//
// DESCRIPTION :
// <font color="#FF00FF"><b>Implementation of methods of the following template classes :</b></font>
// ----------------------------------------------
// - Cell <SYMBOL, WEIGHT>
// - Node <SYMBOL, WEIGHT>
// - InternalNode <SYMBOL, WEIGHT>
// - TerminalNode <SYMBOL, WEIGHT>
// - BasicHuffmanTree <SYMBOL, WEIGHT, ARY>
// - DriedHuffmanTree <WEIGHT, ARY>
// ----------------------------------------------
// Note. The following class has no its own methods :
// - LoadedHuffmanTree <SYMBOL, WEIGHT, ARY>
// ----------------------------------------------
//
// DATE VERSION
// ---- -------
// Aug-26-1999 NHTA 1.0
// Jul-05-2001 NHTA 1.1
// Sep-11-2001 NHTA 1.2
//
// ##############################################################
//=====================
#include "huf_class.H"
//=====================
<a NAME="label_Cell_method"></a>
//#######################################################
//##### PART : template class <b><a href="#label_Cell_class">Cell</a></b> ######################
//############ <font color="FF7733"><b>Methods</b></font> ##################################
//#######################################################
//-----------------------
// Constructor
template <typename SYMBOL, typename WEIGHT>
Cell<SYMBOL, WEIGHT>::Cell (
const SYMBOL& data_symbol_i,
const WEIGHT& data_weight_i,
unsigned int symbol_original_index_i
)
{
data_symbol_ = data_symbol_i;
data_weight_ = data_weight_i;
symbol_original_index_ = symbol_original_index_i;
} // Cell<SYMBOL, WEIGHT>::Cell (
//-----------------------
template <typename SYMBOL, typename WEIGHT>
istream& operator>>(istream &stream_o, Cell<SYMBOL, WEIGHT>& instance_i)
{
stream_o >> instance_i.data_symbol_ >> instance_i.data_weight_;
return stream_o;
}
<a NAME="label_Node_method"></a>
//#######################################################
//##### PART : template class <b><a href="#label_Node_class">Node</a></b> ######################
//############ <font color="FF7733"><b>Methods</b></font> ##################################
//#######################################################
//-----------------------
template <typename SYMBOL, typename WEIGHT>
ostream& operator<<(ostream &str_o, const Node<SYMBOL, WEIGHT>& instance_i)
{
const string shift_CNS = "\t---> ";
return str_o << endl
<< shift_CNS
<< "weight_ = "
<< instance_i.weight_
<< gstr_map (instance_i.mapSymbols_, shift_CNS);
}
<a NAME="label_InternalNode_method"></a>
//#######################################################
//##### PART : template class <b><a href="#label_InternalNode_class">InternalNode</a></b> ##############
//############ <font color="FF7733"><b>Methods</b></font> ##################################
//#######################################################
//-----------------------
template <typename SYMBOL, typename WEIGHT>
void InternalNode<SYMBOL, WEIGHT>::addNode(Node<SYMBOL, WEIGHT> const * const ptr2_i)
{
SYMBOL cur_symbol;
WEIGHT cur_weight;
Node_MAP_SYMBOLS::const_iterator const_iterSymbols;
ASSERT (!(ptr2_i == NULL));
//=============== ptr2 =====================
weight_ += ptr2_i->weight_;
for (const_iterSymbols = ptr2_i->mapSymbols_.begin();
!(const_iterSymbols == ptr2_i->mapSymbols_.end());
const_iterSymbols++
)
{
cur_symbol = (*const_iterSymbols).first;
cur_weight = (*const_iterSymbols).second;
//==========================================
ASSERT (!mapSymbols_.count (cur_symbol));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -