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

📄 huf_methods.h

📁 huffman_template_algorithm.zip
💻 H
📖 第 1 页 / 共 5 页
字号:
                ERROR_MSG ("Code <"
                           << gstr_path<ARY> (path_i)
                           << "> is out of Huffman Codes for this Alphabet"
                           );
                showAll ();
                return false;

        }

        symbol_o = (*(mapHuffmanCodes_.find (path_i))).second;

        //=====================================
        return true;

} // WEIGHT BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::knowCodeSymbol () const




//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
void BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::showAll (const string& msg_i) const
{

        cout << endl;
        cout << "########### showAll (BEGIN) ###########" << endl;
        if (!msg_i.empty ())
        {
                cout << "\t" << "===== " << msg_i << " =====" << endl;
                cout << endl;
        }

        cout << endl;
        cout << "\t" << "-> This is ";
        switch (ARY)
        {
                case 1 :
                        ASSERT (ARY > 1);
                        break;

                case 2 :
                        cout << "Binary";
                        break;

                case 3 :
                        cout << "Ternary";
                        break;

                default :
                        cout << ARY << "-ary";
                        break;
        }
        cout << " Huffman Coding <-" << endl;

        cout << "\t" << "Alphabet size \t\t= " << getAlphabetSize () << endl;
        cout << "\t" << "Shortest code size \t= " << getShortestCodeSize () << endl;
        cout << "\t" << "Longest code size \t= " << getLongestCodeSize () << endl;
        cout << endl;
        cout << "\t" << "Weights sum \t\t= " << getWeightsSum () << endl;
        cout << "\t" << "Average weight \t\t= " << getAverageWeight () << endl;
        cout << endl;
        cout << "\t" << "Code-sizes sum \t\t= " << getCodeSizesSum () << endl;
        cout << "\t" << "Average code-size \t= " << getAverageCodeSize () << endl;
        cout << endl;
        cout << "\t" << "Weighted code-sizes sum = " << getWeightedCodeSizesSum () << endl;
        cout << "\t" << "Ave. weighted code-size = " << getAverageWeightedCodeSize () << endl;
        cout << endl;
        cout << endl;



        //==================================
Tree_MAP_SYMBOLS::const_iterator                const_iterAlphabet;
Tree_MAP_HUFFMAN_DECODING::const_iterator       const_iterHuffmanCodes;
vector<CODE>                            cur_path;
WEIGHT                                  cur_weight;
SYMBOL                                  cur_symbol;
const string                            spaces_offset_CNS = "   ";
const string                            weight_title_CNS = "Weight";
const string                            symbol_title_CNS = "Symbol";
const string                            code_title_CNS = "Code";
const int                               setw_weight_CNS = 12;
const int                               setw_symbol_CNS = MAX_VALUE (symbol_title_CNS.size (), getLongestSymbolSize ());

        cout << endl;
        cout << endl;
        cout << "\t======================= " << endl;
        cout << "\tSymbols and their codes" << endl;
        cout << "\t -> Sorted by Symbol" << endl;
        cout << "\t======================= " << endl;
        print_show_title_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        symbol_title_CNS,
                        setw_weight_CNS,
                        weight_title_CNS,
                        code_title_CNS
                        );

        //=====================================
        for (const_iterAlphabet = mapAlphabet_.begin();
             !(const_iterAlphabet == mapAlphabet_.end());
             const_iterAlphabet++
             )
        {
                if (!encodeOneSymbol ((*const_iterAlphabet).first, cur_path))
                {
                        ASSERT (0);
                }
                if (!knowSymbolWeight ((*const_iterAlphabet).first, cur_weight))
                {
                        ASSERT (0);
                }

                print_show_line_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        (*const_iterAlphabet).first,
                        setw_weight_CNS,
                        cur_weight,
                        cur_path
                        );

        } // for (const_iterAlphabet = mapAlphabet_.begin() ...
        //=====================================

        cout << endl;
        cout << endl;
        cout << "\t=========================" << endl;
        cout << "\tCodes and their symbols" << endl;
        cout << "\t -> Lexico-Sorted by Code" << endl;
        cout << "\t=========================" << endl;
        print_show_title_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        symbol_title_CNS,
                        setw_weight_CNS,
                        weight_title_CNS,
                        code_title_CNS
                        );

        //=====================================
        for (const_iterHuffmanCodes = mapHuffmanCodes_.begin();
             !(const_iterHuffmanCodes == mapHuffmanCodes_.end());
             const_iterHuffmanCodes++
             )
        {
                if (!knowCodeSymbol ((*const_iterHuffmanCodes).first, cur_symbol))
                {
                        ASSERT (0);
                }
                if (!knowSymbolWeight (cur_symbol, cur_weight))
                {
                        ASSERT (0);
                }


                print_show_line_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        cur_symbol,
                        setw_weight_CNS,
                        cur_weight,
                        (*const_iterHuffmanCodes).first
                        );

        } // for (const_iterHuffmanCodes = mapHuffmanCodes_.begin() ...
        //=====================================


        cout << endl;
        cout << endl;
        cout << "\t=======================" << endl;
        cout << "\tCodes and their symbols" << endl;
        cout << "\t -> Sorted by Code Size" << endl;
        cout << "\t=======================" << endl;
        print_show_title_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        symbol_title_CNS,
                        setw_weight_CNS,
                        weight_title_CNS,
                        code_title_CNS
                        );

        //=====================================
unsigned int    the_index;
        for (the_index = 0; the_index < vectorHuffmanCodes_.size (); the_index++)
        {
                if (!knowCodeSymbol (vectorHuffmanCodes_ [the_index], cur_symbol))
                {
                        ASSERT (0);
                }
                if (!knowSymbolWeight (cur_symbol, cur_weight))
                {
                        ASSERT (0);
                }

                print_show_line_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        cur_symbol,
                        setw_weight_CNS,
                        cur_weight,
                        vectorHuffmanCodes_ [the_index]
                        );
        } // for (const_iterHuffmanCodes = mapHuffmanCodes_.begin() ...
        //=====================================



        cout << endl;
        cout << endl;
        cout << "\t=======================" << endl;
        cout << "\tCodes and their symbols" << endl;
        cout << "\t -> Sorted by Weight" << endl;
        cout << "\t=======================" << endl;
        print_show_title_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        symbol_title_CNS,
                        setw_weight_CNS,
                        weight_title_CNS,
                        code_title_CNS
                        );

vector <Cell<SYMBOL, WEIGHT> >  tmp_vectorCell;
        for (const_iterAlphabet = mapAlphabet_.begin();
             !(const_iterAlphabet == mapAlphabet_.end());
             const_iterAlphabet++
             )
        {
                tmp_vectorCell.push_back ((*const_iterAlphabet).second);
        }
        stable_sort (tmp_vectorCell.begin (), tmp_vectorCell.end (), lessCellsCompare<SYMBOL, WEIGHT> ());


        for (the_index = 0; the_index < tmp_vectorCell.size (); the_index++)
        {
                cur_symbol = tmp_vectorCell [the_index].data_symbol_;
                if (!knowSymbolWeight (cur_symbol, cur_weight))
                {
                        ASSERT (0);
                }
                if (!encodeOneSymbol (cur_symbol, cur_path))
                {
                        ASSERT (0);
                }

                print_show_line_S (
                        spaces_offset_CNS,
                        setw_symbol_CNS,
                        cur_symbol,
                        setw_weight_CNS,
                        cur_weight,
                        cur_path
                        );
        } // for (const_iterHuffmanCodes = mapHuffmanCodes_.begin() ...
        //=====================================


        cout << endl;
        cout << "########### showAll (END) #############" << endl;
        cout << endl;

} // void showAll


//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
void BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::showHuffmanProcessStatus (
                        vector<Node<SYMBOL, WEIGHT>*>&  vectorHuffmanProcess_i,
                        int                             cur_stage_i,
                        const string&                   msg_i
                        ) const
{
unsigned int    cur_index;

        cout << endl;
        cout << "########### showHuffmanProcessStatus (BEGIN) ###########" << endl;
        if (!msg_i.empty ())
        {
                cout << "\t" << "===== " << msg_i << " =====" << endl;
                cout << endl;
        }
        cout << endl;
        cout << "Process StageNo    = " << cur_stage_i << endl;
        cout << "ProcessVector Size = " << vectorHuffmanProcess_i.size () << endl;
        cout << endl;

string                  tmp_string;
const unsigned int      setw_weight_CNS = 12;
Node<SYMBOL, WEIGHT>::Node_MAP_SYMBOLS::const_iterator  const_iterSymbols;
const string            linedel_CNS = "--------------------------------------------------";
        for (cur_index = 0; cur_index < vectorHuffmanProcess_i.size (); cur_index++)
        {
                if ((cur_index == 0) || (cur_index == ARY))
                {
                        cout << linedel_CNS << endl;
                }
                //---------------------------------------


                if (vectorHuffmanProcess_i [cur_index]->is_TerminalNode_)
                {
                        tmp_string = " " + to_str (vectorHuffmanProcess_i [cur_index]->weight_) + " ";
                }
                else
                {
                        tmp_string = "[" + to_str (vectorHuffmanProcess_i [cur_index]->weight_) + "]";
                }

                cout.setf (ios::right, ios::adjustfield);
                cout << ""
                     << setw (5)
                     << (cur_index + 1)
                     << ((cur_stage_i == vectorHuffmanProcess_i [cur_index]->creation_stage_) ? "*" : " ")
                     << " -> "
                     << setw (setw_weight_CNS + 2)
                     << tmp_string.c_str ();

                if (!(vectorHuffmanProcess_i [cur_index]->is_TerminalNode_))
                {
                        for (const_iterSymbols = vectorHuffmanProcess_i [cur_index]->mapSymbols_.begin();
                             !(const_iterSymbols == vectorHuffmanProcess_i [cur_index]->mapSymbols_.end());
                             const_iterSymbols++

⌨️ 快捷键说明

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