⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huf_methods.h

📁 huffman_template_algorithm.zip
💻 H
📖 第 1 页 / 共 5 页
字号:
                          cur_index < vectorHuffmanCodes_.size ();
                          cur_index++
                          )
        {
                ret_boolValue = decodeOneSymbol (vectorHuffmanCodes_ [cur_index], cur_decoded_symbol_value);
                if (show_i)
                {
                        cout <<"TESTED : Code <"
                             << gstr_path<ARY> (vectorHuffmanCodes_ [cur_index])
                             << "> == Symbol <"
                             << cur_decoded_symbol_value
                             << ">"
                             << endl;
                }
                ASSERT (ret_boolValue);
        }

        return ret_boolValue;
} // bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::testAllCodes ()



//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::decodeMsg (
                                        const vector<CODE>&     encoded_msg_i,
                                        vector<SYMBOL>&         decoded_msg_o
                                        ) const

{
bool    ret_boolValue = false;
        if (encoded_msg_i.empty ())
        {
                FATAL_MSG ("Empty message to be decoded");
        }

        //===================
        decoded_msg_o = vector<SYMBOL> ();
        //===================
unsigned int    cur_start_position = 0;
unsigned int    cur_symbol_number = 0;
SYMBOL          cur_symbol_value;
vector<CODE>    cur_symbol_code;
vector<vector<CODE> >   all_symbols_codes;

        while ((ret_boolValue = decodeOneSymbol (
                                        encoded_msg_i,
                                        cur_start_position,
                                        cur_symbol_number,
                                        cur_symbol_code,
                                        cur_symbol_value
                                        )))
        {
                decoded_msg_o.push_back (cur_symbol_value);
                all_symbols_codes.push_back (cur_symbol_code);
                if (!(cur_start_position < encoded_msg_i.size ()))
                {
                        break;
                }
        } // while

        //=================
        if (!ret_boolValue)
        {
                FATAL_MSG ("Cannot decode all message -> "
                                 << gstr_path<ARY> (encoded_msg_i)
                                 << endl
                                 << FATAL_SHIFT
                                 << "Only "
                                 << cur_symbol_number
                                 << " symbols decoded"
                                 << endl
                                 << FATAL_SHIFT
                                 << "Decoded codes -> "
                                 << gstr_vector(all_symbols_codes, string (" "))
                                 << endl
                                 << FATAL_SHIFT
                                 << "Decoded symbols ->  "
                                 << gstr_vector(decoded_msg_o)
                                 );
        }

        //===================
        return ret_boolValue;


} // bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::decodeMsg


//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::encodeMsg (
                                        const vector<SYMBOL>&   source_msg_i,
                                        vector<CODE>&           encoded_msg_o
                                        ) const

{
bool    ret_boolValue = false;

        ASSERT (!source_msg_i.empty ());
        encoded_msg_o = vector<CODE> ();

vector<CODE>    cur_symbol_code;

        for (unsigned int cur_index = 0;
                          cur_index < source_msg_i.size ();
                          cur_index++
                          )
        {
                if (!(ret_boolValue = encodeOneSymbol (source_msg_i [cur_index], cur_symbol_code)))
                {
                        break;
                }

                copy (cur_symbol_code.begin (), cur_symbol_code.end (), back_inserter (encoded_msg_o));

        }

        // ================
        if (!ret_boolValue)
        {
                FATAL_MSG ("Cannot encode message"
                                 << endl
                                 << FATAL_SHIFT
                                 << gstr_vector (source_msg_i)
                                 );
        }

        return ret_boolValue;


} // bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::encodeMsg


//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::encodeMsg (
                                        const basic_string<SYMBOL>&     string_source_msg_i,
                                        string&                         string_encoded_msg_o
                                        ) const

{
        //=================================
        if (!(ARY <= 16))
        {
                FATAL_MSG ("Illegal ARY for THIS FUNCTION : Real = "
                            << ARY
                            << "; must be <= 16"
                            );
        }
        ASSERT (ARY <= 16);     // In this function !!!
        //=================================

bool    ret_value = false;


vector <SYMBOL> vector_source_msg (string_source_msg_i.size ());
        for (unsigned int cur_index = 0; cur_index < vector_source_msg.size (); cur_index++)
        {
                vector_source_msg [cur_index] = string_source_msg_i [cur_index];
        }

vector <CODE>   encoded_msg;
        ret_value = encodeMsg (vector_source_msg, encoded_msg);

        //=====================
        string_encoded_msg_o = string ();
        string_encoded_msg_o.resize (encoded_msg.size ());

strstream       tmp_strstream;
        for (unsigned int cur_index = 0; cur_index < string_encoded_msg_o.size (); cur_index++)
        {
                tmp_strstream << encoded_msg [cur_index];
        }
        tmp_strstream << ends;
        string_encoded_msg_o = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);

        //=====================
        return ret_value;
} // bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::encodeMsg (



//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::decodeMsg (
                                        const string&           string_encoded_msg_i,
                                        basic_string<SYMBOL>&   string_decoded_msg_o
                                        ) const

{
        //=================================
        if (!(ARY <= 16))
        {
                FATAL_MSG ("Illegal ARY for THIS FUNCTION : Real = "
                            << ARY
                            << "; must be <= 16"
                            );
        }
        ASSERT (ARY <= 16);     // In this function !!!
        //=================================

bool    ret_value = false;
vector <CODE>   vector_encoded_msg (string_encoded_msg_i.size ());
unsigned int    eof_int_test;
string          cur_string;
        for (unsigned int cur_index = 0; cur_index < vector_encoded_msg.size (); cur_index++)
        {
                ASSERT (isxdigit (string_encoded_msg_i [cur_index]));

                cur_string = string_encoded_msg_i [cur_index];
                istrstream      int_stream (cur_string.c_str ());

                //int_stream.clear ();
                int_stream >> hex >> vector_encoded_msg [cur_index];
                ASSERT (!(int_stream.eof ()));
                ASSERT (!(int_stream.fail ()));
                ASSERT ((int_stream >> hex >> eof_int_test).eof ());

                int_stream.rdbuf()->freeze (0);
        }

vector <SYMBOL> vector_decoded_msg;
        ret_value = decodeMsg (vector_encoded_msg, vector_decoded_msg);
        //=====================
        string_decoded_msg_o = basic_string<SYMBOL> ();
        string_decoded_msg_o.resize (vector_decoded_msg.size ());
        for (unsigned int cur_index = 0; cur_index < string_decoded_msg_o.size (); cur_index++)
        {
                string_decoded_msg_o [cur_index] = vector_decoded_msg [cur_index];
        }

        //=====================
        return ret_value;
} // bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::decodeMsg (





//#######################################################
//##### PART : template class DriedHuffmanTree ##########
//############ Methods ##################################
//#######################################################

//-----------------------
// Constructor-1
template <typename WEIGHT, unsigned int ARY>
DriedHuffmanTree<WEIGHT, ARY>::DriedHuffmanTree (
                        const vector<WEIGHT>& weight_vector_i
                        )
{
        doDriedHuffmanTree (weight_vector_i);
} // DriedHuffmanTree<WEIGHT, ARY>::DriedHuffmanTree ()


//-----------------------
// Constructor-2
template <typename WEIGHT, unsigned int ARY>
DriedHuffmanTree<WEIGHT, ARY>::DriedHuffmanTree (const string& weights_file_name_i)
{
vector<WEIGHT>  weight_vector;

ifstream fin (weights_file_name_i.c_str ());

        if (!fin)
        {
                FATAL_MSG ("Cannot open file <"
                            << weights_file_name_i
                            << "> for reading"
                            << endl
                            << FATAL_SHIFT
                            << "The file must contain weights to be Huffman-coded"
                            );
        }

        copy(istream_iterator<WEIGHT>(fin),
             istream_iterator<WEIGHT>(),
             back_inserter(weight_vector));

        doDriedHuffmanTree (weight_vector);
} // DriedHuffmanTree<WEIGHT, ARY>::DriedHuffmanTree ()


//##########################################################################

//-----------------------
template <typename WEIGHT, unsigned int ARY>
void DriedHuffmanTree<WEIGHT, ARY>::doDriedHuffmanTree (
                        const vector<WEIGHT>& weight_vector_i
                        )
{
vector<Cell<string, WEIGHT> >        data_vector;
        for (unsigned int cur_index = 0;
                          cur_index < weight_vector_i.size ();
                          cur_index++
                          )
        {
                data_vector.push_back (Cell<string, WEIGHT> (
                                        to_str (
                                                cur_index + 1,
                                                get_width (weight_vector_i.size ()),
                                                '0',
                                                "Symbol#"
                                                ),
                                        weight_vector_i [cur_index]
                                        ));
        }


        doBasicHuffmanTree (data_vector);

} // DriedHuffmanTree<WEIGHT, ARY>::DriedHuffmanTree ()


#endif	// huf_methods_H

//#######################################################
//################ END OF FILE ##########################
//#######################################################

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -