?? huf_service.h
字號:
// ==============================================================
//
// 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_service_H
#define huf_service_H
///////////////////////////////////////
static char id_huf_service_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 : huf_service.H
//
// AUTHOR : Alex Vinokur
//
// DESCRIPTION :
// Definition and implementation
// of the following auxiliary template functions :
// ----------------------------------------------
// - string to_str (...)
// - void add_to_vector (...)
// - void fill_vector (...)
// - unsigned int get_width (...)
// - string gstr_vect_ptrs (...)
// - string gstr_vector (...) // two functions
// - string gstr_path (...)
// - string gstr_map (...)
// - ostream& operator<< (...) // two operators
// ----------------------------------------------
//
// DATE VERSION
// ---- -------
// Aug-26-1999 NHTA 1.0
// Jul-05-2001 NHTA 1.1
// Sep-11-2001 NHTA 1.2
//
// ##############################################################
#include <strstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <algo.h>
#include <functional>
#include <iostream>
#include <fstream.h>
#include <typeinfo>
#include <iomanip.h>
#include <assert.h>
//#######################################################
//##### PART : DEFINES & CONSTANTS ######################
//#######################################################
#define MIN_VALUE(x,y) ((x) < (y) ? (x) : (y))
#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))
#define ASSERT(x) if (!(x)) {cerr << endl << endl << "\t=== BUG IN PROGRAM ===" << endl;}; assert (x)
#define FATAL_TITLE "FATAL ERROR : "
#define FATAL_SHIFT " : "
#define FATAL_MSG(x) cerr << endl \
<< FATAL_TITLE \
<< x \
<< endl \
<< FATAL_SHIFT \
<< "File - " \
<< __FILE__ \
<< ", Line#" \
<< __LINE__ \
<< endl; \
exit (1)
#define ERROR_TITLE "ERROR : "
#define ERROR_SHIFT " : "
#define ERROR_MSG(x) cerr << endl \
<< ERROR_TITLE \
<< x \
<< endl \
<< ERROR_SHIFT \
<< "File - " \
<< __FILE__ \
<< ", Line#" \
<< __LINE__ \
<< endl;
//#######################################################
//##### PART : typedefs #################################
//#######################################################
typedef unsigned int CODE;
//#######################################################
//##### PART : FUNCTIONS ################################
//#######################################################
//#####################################################3
template <typename T>
string to_str (T value_i, int width_i = -1, char fill_i = ' ', const string& prefix_i = string ())
{
string ret_stringValue;
strstream tmp_strstream;
//=================================
if (width_i > 0)
{
tmp_strstream.width (width_i);
tmp_strstream.fill (fill_i);
}
tmp_strstream << prefix_i;
tmp_strstream << value_i;
//=================================
tmp_strstream << ends;
ret_stringValue = tmp_strstream.str();
tmp_strstream.rdbuf()->freeze (0);
//=================================
return ret_stringValue;
} // string to_str (T value_i)
//#####################################################3
template <typename T>
void add_to_vector (vector<T>& vector_i, const basic_string<T>& string_i)
{
copy (string_i.begin (), string_i.end (), back_inserter (vector_i));
} //void add_to_vector (T value_o)
//#####################################################3
template <typename T>
void fill_vector (vector<T>& vector_i, const basic_string<T>& string_i)
{
vector_i = vector<T> ();
add_to_vector (vector_i, string_i);
} //void fill_vector (T value_o)
//#####################################################3
template <typename T>
unsigned int get_width (T value_i)
{
unsigned int ret_intValue;
strstream tmp_strstream;
tmp_strstream << value_i;
//=================================
tmp_strstream << ends;
ret_intValue = string (tmp_strstream.str()).size ();
tmp_strstream.rdbuf()->freeze (0);
//=================================
return ret_intValue;
} // unsigned int get_width (T value_i)
//#####################################################
template <typename T1>
string gstr_vect_ptrs (const vector<T1*>& vector_i, const string& delimiter_i = "")
{
strstream tmp_strstream;
string tmp_string;
unsigned int cur_index;
cout << delimiter_i << endl;
for (cur_index = 0; cur_index < vector_i.size (); cur_index++)
{
cout << "vector element ["
<< cur_index << "] : "
<< (*(vector_i [cur_index]))
<< delimiter_i
<< endl;
}
tmp_strstream << ends;
tmp_string = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
//===================
return tmp_string;
} // gstr_vect_ptrs (const vector<CODE>& vector_i)
//#####################################################
template <typename T1>
string gstr_vector (const vector<T1>& vector_i, unsigned int start_pos_i = 0, unsigned int end_pos_i = UINT_MAX, const string& delimiter_i = "")
{
if (vector_i.empty ())
{
return "Empty Vector";
}
//=====================================
if (end_pos_i == UINT_MAX)
{
end_pos_i = vector_i.size () - 1;
}
ASSERT (end_pos_i < vector_i.size ());
ASSERT (start_pos_i <= end_pos_i);
//=====================================
strstream tmp_strstream;
string tmp_string;
ostream_iterator<T1> out (tmp_strstream, delimiter_i.c_str ());
copy (vector_i.begin () + start_pos_i, vector_i.begin () + end_pos_i + 1, out);
tmp_strstream << ends;
tmp_string = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
//===================
return tmp_string;
} // gstr_vector (const vector<CODE>& vector_i)
//#####################################################
template <typename T1>
ostream& operator<< (ostream& o, const vector<T1>& vector_i)
{
return o << gstr_vector (vector_i);
}
//#####################################################
template <typename T1>
string gstr_vector (const vector<T1>& vector_i, const string& delimiter_i, unsigned int start_pos_i = 0, unsigned int end_pos_i = UINT_MAX)
{
return gstr_vector (vector_i, start_pos_i, end_pos_i, delimiter_i);
} // string gstr_vector - 2
//#####################################################
template <unsigned int ARY>
string gstr_path (const vector<CODE>& path_i)
{
const string delimiter_CNS = (ARY > 16) ? "." : "";
strstream tmp_strstream;
string tmp_string;
if (path_i.empty ())
{
tmp_strstream << "This is Huffman Tree Root";
}
else
{
for (unsigned int cur_index = 0; cur_index < path_i.size (); cur_index++)
{
if (cur_index > 0)
{
tmp_strstream << delimiter_CNS;
}
tmp_strstream << path_i [cur_index];
}
}
//=====================================
tmp_strstream << ends;
tmp_string = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
//===================
return tmp_string;
} // gstr_path (const vector<CODE>& path_i)
//#######################################
template <typename T1, typename T2>
string gstr_map (const map<T1, T2, less<T1> >& map_i, const string &shift_i = string ())
{
strstream tmp_strstream;
string tmp_string;
tmp_strstream << endl;
tmp_strstream << endl;
tmp_strstream << shift_i;
tmp_strstream << "\tmap size = "
<< map_i.size ()
<< endl;
map<T1, T2, less<T1> >::const_iterator cur_const_iter;
for (cur_const_iter = map_i.begin(); !(cur_const_iter == map_i.end()); cur_const_iter++)
{
tmp_strstream << shift_i;
tmp_strstream << "map element ["
<< (*cur_const_iter).first
<< "] = "
<< "<"
<< (*cur_const_iter).second
<< ">";
tmp_strstream << endl;
}
tmp_strstream << ends;
tmp_string = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
return tmp_string;
} // string gstr_map
//#######################################
template <typename T1, typename T2>
ostream& operator<<(ostream &str_o, const map<T1, T2, less<T1> >& map_i)
{
return str_o << gstr_map (map_i);
} // ostream& operator<<(ostream &str_o, const map<T1>& map_i)
#endif // huf_service_H
//#######################################################
//################ END OF FILE ##########################
//#######################################################
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -