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

📄 huf_methods.h

📁 huffman_template_algorithm.zip
💻 H
📖 第 1 页 / 共 5 页
字号:
                             )
                        {
                                cout << endl;
                                cout << "\t";
                                cout << "\t";
                                cout << "\t";
                                cout << ""
                                     << setw (setw_weight_CNS)
                                     << (*const_iterSymbols).second
                                     << ", "
                                     << setw (getLongestSymbolSize ())
                                     << (*const_iterSymbols).first
                                     << ", ";
                                if (vectorHuffmanProcess_i.size () > 1)
                                {
                                        cout << "...";
                                }
                                cout <<(*(mapAlphabet_.find ((*const_iterSymbols).first))).second.symbol_path_
                                     << "  ";
                        } // for (const_iterSymbols = ...
                } // if (!(vectorHuffmanProcess_i [cur_index]->is_TerminalNode_))
                else
                {
                        ASSERT (vectorHuffmanProcess_i [cur_index]->mapSymbols_.size () == 1);
                        const_iterSymbols = vectorHuffmanProcess_i [cur_index]->mapSymbols_.begin();
                        ASSERT ((*const_iterSymbols).second == vectorHuffmanProcess_i [cur_index]->weight_);
                        cout << ", " << ((*const_iterSymbols).first);
                }

                cout << endl;
        }
        cout << endl;

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

} // void showHuffmanProcessStatus



//#######################################
// static
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
void BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::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
                                        )
{
string  tmp_string;

strstream       tmp_strstream_weight;
        tmp_strstream_weight << weight_i;
        tmp_strstream_weight << ends;
        tmp_string = tmp_strstream_weight.str();
        tmp_strstream_weight.rdbuf()->freeze (0);
        cout.setf (ios::right, ios::adjustfield);
        cout << setw (setw_weight_i)
             << tmp_string.c_str ();


strstream       tmp_strstream_symbol;
        tmp_strstream_symbol << symbol_i;
        tmp_strstream_symbol << ends;
        tmp_string = tmp_strstream_symbol.str();
        tmp_strstream_symbol.rdbuf()->freeze (0);
        cout.setf (ios::left, ios::adjustfield);
        cout << spaces_offset_i
             << setw (setw_symbol_i)
             << tmp_string.c_str ();

        cout.setf (ios::left, ios::adjustfield);
        cout << spaces_offset_i
             << gstr_path<ARY> (path_i);

        cout << endl;
} // void BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::print_show_line_S



//#######################################
// static
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
void BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::print_show_title_S (
                                        const string&   spaces_offset_i,
                                        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
                                        )
{
        cout.setf (ios::right, ios::adjustfield);
        cout << setw (setw_weight_i)
             << weight_title_i.c_str ();

        cout.setf (ios::left, ios::adjustfield);
        cout << spaces_offset_i
             << setw (setw_symbol_i)
             << symbol_title_i.c_str ();

        cout.setf (ios::left, ios::adjustfield);
        cout << spaces_offset_i
             << code_title_i.c_str ();

        cout << endl;

const char      the_char = '-';
unsigned int    cur_index;
        cout.setf (ios::right, ios::adjustfield);
        cout << setw (setw_weight_i - weight_title_i.size ()) << string ().c_str ();
        for (cur_index = 0; cur_index < weight_title_i.size (); cur_index++)
        {
                cout << the_char;
        }

        cout.setf (ios::left, ios::adjustfield);
        cout << spaces_offset_i;
        for (cur_index = 0; cur_index < symbol_title_i.size (); cur_index++)
        {
                cout << the_char;
        }

        cout.setf (ios::left, ios::adjustfield);
        cout << spaces_offset_i;
        for (cur_index = 0; cur_index < code_title_i.size (); cur_index++)
        {
                cout << the_char;
        }

        cout << endl;

} // void BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::print_show_title_S



//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::decodeOneSymbol (
                                        const vector<CODE>&     encoded_msg_i,
                                        unsigned int&           cur_start_position_io,
                                        unsigned int&           cur_symbol_number_io,
                                        vector<CODE>&           cur_symbol_code_o,
                                        SYMBOL&                 cur_symbol_value_o
                                        ) const

{
bool    ret_boolValue = false;
unsigned int cur_index;

const InternalNode<SYMBOL, WEIGHT>*     curPtrInternalNode = &rootNode_;
        ASSERT (!(curPtrInternalNode == NULL));
        ASSERT (!(rootNode_.is_TerminalNode_));
        ASSERT (cur_start_position_io < encoded_msg_i.size ());

CODE            cur_digit;
        //===================
        cur_symbol_code_o = vector<CODE> ();
        for (cur_index = cur_start_position_io;
             cur_index < encoded_msg_i.size ();
             cur_index++
             )
        {
                cur_digit = encoded_msg_i [cur_index];
                if (!((cur_digit >= 0) & (cur_digit < ARY)))
                {
                        FATAL_MSG ("Illegal digit in encoded message"
                                    << endl
                                    << FATAL_SHIFT
                                    << "Digit ["
                                    << cur_index
                                    << "] = "
                                    << encoded_msg_i [cur_index]
                                    << "; Valid range is ["
                                    << 0
                                    << " - "
                                    << (ARY - 1)
                                    << "]"
                                    << endl
                                    << FATAL_SHIFT
                                    << "Note! First digit is 0-th digit"
                                    << endl
                                    << FATAL_SHIFT
                                    << "The encoded message is <"
                                    << gstr_path<ARY> (encoded_msg_i)
                                    << ">"
                                    );
                }
                //======================

                ASSERT (!(curPtrInternalNode->arc_ [cur_digit] == NULL));
                if (curPtrInternalNode->arc_ [cur_digit]->is_TerminalNode_)
                {
                        ret_boolValue = true;
                        break;
                }

                //======================
                ASSERT (!(curPtrInternalNode->arc_ [cur_digit] == NULL));
                curPtrInternalNode = dynamic_cast<InternalNode<SYMBOL, WEIGHT>*> (curPtrInternalNode->arc_ [cur_digit]);
                ASSERT (!(curPtrInternalNode == NULL));
                //======================
        } // for (cur_index =

        //===================
        if (!ret_boolValue)
        {
                cur_index--;
                ERROR_MSG ("Illegal last code in encoded message"
                            << endl
                            << ERROR_SHIFT
                            << "Digits ["
                            << cur_start_position_io
                            << ", "
                            << cur_index
                            << "] = "
                            << gstr_vector (encoded_msg_i, cur_start_position_io, cur_index)
                            << endl
                            << ERROR_SHIFT
                            << "Note! First digit is 0-th digit"
                            << endl
                            << ERROR_SHIFT
                            << "The encoded message is <"
                            << gstr_path<ARY> (encoded_msg_i)
                            << ">"
                            );
                return ret_boolValue;
        }

        //===================
        copy (encoded_msg_i.begin () + cur_start_position_io, encoded_msg_i.begin () + cur_index + 1, back_inserter (cur_symbol_code_o));

        //===================
        ASSERT (mapHuffmanCodes_.count (cur_symbol_code_o));
        cur_symbol_value_o = (*(mapHuffmanCodes_.find (cur_symbol_code_o))).second;
        cur_start_position_io = cur_index + 1;
        cur_symbol_number_io++;

        //===================

        return ret_boolValue;


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



//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::decodeOneSymbol (
                                        const vector<CODE>&     encoded_symbol_code_i,
                                        SYMBOL&                 decoded_symbol_value_o
                                        ) const

{
bool    ret_boolValue = false;
        ASSERT (!(encoded_symbol_code_i.empty ()));

const InternalNode<SYMBOL, WEIGHT>*     curPtrInternalNode = &rootNode_;
        ASSERT (!(curPtrInternalNode == NULL));
        ASSERT (!(rootNode_.is_TerminalNode_));

unsigned int    cur_index;
CODE            cur_digit;
        //===================
        for (cur_index = 0; cur_index < (encoded_symbol_code_i.size () - 1); cur_index++)
        {
                cur_digit = encoded_symbol_code_i [cur_index];
                ASSERT (cur_digit >= 0);
                ASSERT (cur_digit < ARY);

                ASSERT (!(curPtrInternalNode->arc_ [cur_digit] == NULL));
                ASSERT (!(curPtrInternalNode->arc_ [cur_digit]->is_TerminalNode_));


                if (curPtrInternalNode->arc_ [cur_digit]->is_TerminalNode_)
                {
                        return ret_boolValue;   // false
                }

                curPtrInternalNode = dynamic_cast<InternalNode<SYMBOL, WEIGHT>*> (curPtrInternalNode->arc_ [cur_digit]);
                ASSERT (!(curPtrInternalNode == NULL));
        } // for (cur_index =

        //================================
        cur_index = encoded_symbol_code_i.size () - 1;
        //===================
        cur_digit = encoded_symbol_code_i [cur_index];
        ASSERT (!(curPtrInternalNode->arc_ [cur_digit] == NULL));
        ASSERT (curPtrInternalNode->arc_ [cur_digit]->is_TerminalNode_);
        if ((!curPtrInternalNode->arc_ [cur_digit]->is_TerminalNode_))
        {
                return ret_boolValue;   // false
        }
        ret_boolValue = true;

        //===================
        ASSERT (mapHuffmanCodes_.count (encoded_symbol_code_i));
        decoded_symbol_value_o = (*(mapHuffmanCodes_.find (encoded_symbol_code_i))).second;
        //===================

        return ret_boolValue;


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



//#######################################
template <typename SYMBOL, typename WEIGHT, unsigned int ARY>
bool BasicHuffmanTree<SYMBOL, WEIGHT, ARY>::testAllCodes (bool show_i) const
{
                //======================
                if (show_i)
                {
                        showAll ("testAllCodes");
                }
                //======================

bool    ret_boolValue = false;
SYMBOL  cur_decoded_symbol_value;
        ASSERT (!(vectorHuffmanCodes_.empty ()));
        for (unsigned int cur_index = 0;

⌨️ 快捷键说明

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