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

📄 fx_vect2.h

📁 可重用的向量/矩阵类代码,这是一个灵活的向量/矩阵类代码
💻 H
📖 第 1 页 / 共 2 页
字号:

// #########################################################
// === File #5 of 15 : fx_vect2.h ==========================
// ------------------- C++ code : BEGIN --------------------

// ==============================================================
//
//  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.
//
// ==============================================================
// #########################################################
// ## mailto:alexvn@bigfoot.com, mailto:alexv@hitechclub.com
// ## http://up.to/alexv, http://go.to/alexv_math
// #########################################################
// ==============================================================


///////////////////////////////////////

#ifndef FX_VECT2_H
#define FX_VECT2_H

///////////////////////////////////////


// ##############################################################
//
//  SOFTWARE : Vector and Matrix with arbitrary bounds
//  FILE     : fx_vect2.h
//
//  DESCRIPTION :
//         Implementation of flexible vector template classes
//	   --------------------------------------------------
//         - ClassFlexibleVector
//         - ClassVeryFlexibleVector
//	   --------------------------------------------------
//
//  ----           -------
//  Oct-30-2001    FVM 1.0
//
// ##############################################################


//===============================================
#include "fx_vect.h"
//===============================================


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

#define	BUG_Vector_MESSAGE(x)	BUG_MESSAGE (x)


//########################################################
//################### ClassFlexibleVector ################
//######################## Methods #######################
//########################################################

//==================
template <class	T>
inline ostream &
operator<< (ostream &os, const ClassFlexibleVector<T>& value_i)
//==================
{
  return ( value_i.osprint(os) );
}


//==================
// Constructor-0
template <class	T>
inline ClassFlexibleVector<T> :: ClassFlexibleVector() : 
					ObjectLocation (), 
					ClassBasicVector (), 
					minIndex_ (0)
//==================
{
  thePtrAboveVector_ = NULL;
  theAboveOrdinalNumber_ = INT_MIN;
} // ClassFlexibleVector :: ClassFlexibleVector()


//==================
// Constructor-1
template <class	T>
inline ClassFlexibleVector<T> :: ClassFlexibleVector(
					int 		minIndex_i, 
					const string 	fileName_i, 
					const int	lineNo_i, 
					const string	compilationDate_i, 
					const string	compilationTime_i
					) :
					  ObjectLocation (
						fileName_i, 
						lineNo_i, 
						compilationDate_i, 
						compilationTime_i), 
					  ClassBasicVector (), 
					  minIndex_ (minIndex_i)
//==================
{
  thePtrAboveVector_ = NULL;
  theAboveOrdinalNumber_ = INT_MIN;

  basicUsualVector_ = vector<T> ();
} // ClassFlexibleVector :: ClassFlexibleVector()



//==================
// Constructor-2
template <class	T>
inline ClassFlexibleVector<T> :: ClassFlexibleVector(
					int 		minIndex_i, 
					int		maxIndex_i, 
					const T&	value_i, 
					const string 	fileName_i, 
					const int	lineNo_i, 
					const string	compilationDate_i, 
					const string	compilationTime_i
					) :
					  ObjectLocation (
						fileName_i, 
						lineNo_i, 
						compilationDate_i, 
						compilationTime_i), 
					  ClassBasicVector (), 
					  minIndex_ (minIndex_i)
//==================
{
  thePtrAboveVector_ = NULL;
  theAboveOrdinalNumber_ = INT_MIN;

  if (!(minIndex_i <= maxIndex_i))
  {
    cerr << "Invalid vector range : [" 
	 << minIndex_i 
	 << "," 
	 << maxIndex_i << "]" 
	 << endl; 
    cerr << eqline1_CNS << endl;
    cerr << gstrObjectLocationShow () << endl;

    return;
    ASSERT (0);	// Currently unused
  }

  basicUsualVector_ = vector<T> (maxIndex_i - minIndex_i + 1, value_i); 
} // ClassFlexibleVector :: ClassFlexibleVector()


//==================
// Copy Constructor
template <class	T>
ClassFlexibleVector<T> :: ClassFlexibleVector(const ClassFlexibleVector& theCopyInstance_i) : 
				ClassBasicVector ()
//==================
{
  (*this).hardAssign (theCopyInstance_i);
} // ClassFlexibleVector :: ClassFlexibleVector()



//==================
// Destructor
template <class	T>
ClassFlexibleVector<T> :: ~ClassFlexibleVector() 
//==================
{
} // ClassFlexibleVector :: ~ClassFlexibleVector()


//==================
template <class	T>
bool ClassFlexibleVector<T> 
	:: indexBelongsToVectorRange (int externalIndex_i) const 
//==================
{
  return ((externalIndex_i >= Get_minIndex ()) && (externalIndex_i <= Get_maxIndex()));
} // bool ClassFlexibleVector<T> :: indexBelongsToVectorRange (int externalIndex_i) const 

//==================
template <class	T>
int ClassFlexibleVector<T> 
	:: getInternalIndex (int externalIndex_i) const 
//==================
{
  return (externalIndex_i - minIndex_);
} // int ClassFlexibleVector<T> :: getInternalIndex (int externalIndex_i) const 



//==================
template <class	T>
void ClassFlexibleVector<T> :: push_back (const T& element_i) 
//==================
{
  basicUsualVector_.push_back (element_i);
} // void ClassFlexibleVector<T> :: push_back (const T& element_i) 



//==================
template <class	T>
bool ClassFlexibleVector<T> 
	:: pop_first () 
//==================
{
  if (empty ()) return false;

  ASSERT (size () > 0);
  basicUsualVector_.erase (basicUsualVector_.begin());

  return true;
} // bool ClassFlexibleVector<T> :: pop_first (const T& element_i) 



//==================
template <class	T>
bool ClassFlexibleVector<T> 
	:: pop_back () 
//==================
{
  if (empty ()) return false;

  ASSERT (size () > 0);
  basicUsualVector_.pop_back ();

  return true;
} // bool ClassFlexibleVector<T> :: pop_back (const T& element_i) 


//==================
template <class	T>
bool ClassFlexibleVector<T> 
	:: pop_element (int externalIndex_i) 
//==================
{
int	theIndex;

  ASSERT (indexBelongsToVectorRange (externalIndex_i));

  if (empty ()) return false;

  ASSERT (size () > 0);
  basicUsualVector_.erase (basicUsualVector_.begin() + getInternalIndex (externalIndex_i));

  return true;
} // bool ClassFlexibleVector<T> :: pop_element (const T& element_i) 


//==================
template <class	T>
ClassFlexibleVector<T>& ClassFlexibleVector<T>::softAssign (const ClassFlexibleVector<T>& instance_i) 
//==================
{
  *this = instance_i;
  return *this;
} // ClassFlexibleVector<T>& ClassFlexibleVector<T> :: softAssign ... 


//==================
template <class	T>
ClassFlexibleVector<T>& ClassFlexibleVector<T> :: hardAssign (const ClassFlexibleVector<T>& instance_i) 
//==================
{
  *this = instance_i;
  minIndex_ = instance_i.minIndex_;
  return *this;
} // ClassFlexibleVector<T>& ClassFlexibleVector<T> :: hardAssign ... 



//==================
template <class	T>
inline ClassFlexibleVector<T>& ClassFlexibleVector<T> 
	:: operator= (const ClassFlexibleVector& instance_i) 
//==================
{
  if (!empty ())
  {
    if (size () != ((ClassFlexibleVector&)instance_i).size ()) 
    {
      cerr << endl << eqline1_CNS  << endl;
      cerr << "Operator= : Vectors' Sizes are not idendical : " 
           << endl 
           << "\tFirst vector holds " 
           << size () 
           << " elements; " 
           << " range is [" 
           << Get_minIndex () 
           << " : " 
           << Get_maxIndex () 
           << "]," 
           << endl 
           << "\tSecond vector holds " 
           << ((ClassFlexibleVector&)instance_i).size () 
           << " elements; " 
           << "range is [" 
           << ((ClassFlexibleVector&)instance_i).Get_minIndex () 
           << " : " 
           << ((ClassFlexibleVector&)instance_i).Get_maxIndex () 
           << "]" 
           << endl;
      cerr << eqline1_CNS << endl;

      cerr << endl 
           << "First vector : " 
           << gstrObjectLocationShow () 
           << endl;

      cerr << endl
           << "Second vector : " 
           << ((ClassFlexibleVector&)instance_i).gstrObjectLocationShow () 
           << endl;
	
       return *this;
       ASSERT (0);	// Currently unused

    } // if (size () != instance_i.size ())
  } // if (!empty ())

  //---------------------------------------
  // Not must be !!! minIndex_ = instance_i.minIndex_ ;
  basicUsualVector_ = instance_i.basicUsualVector_ ;
  //---------------------------------------

  (*this).addAllSecondaryIdentifications (instance_i);

  return *this;
} // ClassFlexibleVector<T>& ClassFlexibleVector<T>::operator= (const ClassFlexibleVector& instance_i)



//==================
template <class	T>
string ClassFlexibleVector<T> 
	::getErrorMsgAboutVectorIndexOutOfRange (
		int		externalIndex_i, 
		const string&	theFuncName_i,
		const string 	fileName_i = "", 
		const int	lineNo_i = 0, 
		const string	compilationDate_i = "", 
		const string	compilationTime_i = ""
		) const
//==================
{
string          ret_stringValue;
strstream       tmp_strstream;

  tmp_strstream << ""
                << endl 
		<< eqline1_CNS 
		<< endl
		<< "### "
		<< theFuncName_i
		<< endl;

const string theVectorOrMatrix = (getAboveOrdinalNumber () == INT_MIN) ? "Vector Element" : "Matrix Column"; 
  tmp_strstream << ""
                //<< "Vector Element (or Matrix Column) index " 
		<< theVectorOrMatrix 
		<< " index " 
		<< externalIndex_i 
                << " out of range [" 
		<< Get_minIndex () 
		<< ", " 
		<< Get_maxIndex ()
		<< "]"; 

  if (empty ())
  {
    tmp_strstream << "  (Range is Empty)"; 
  }

  tmp_strstream << endl;

  if (!(getAboveOrdinalNumber () == INT_MIN))
  {
    tmp_strstream << ""
		  << "CONCLUSION : "		
		  << "MatrixElement " 
		  << "[Row#"
		  << getAboveOrdinalNumber ()
		  << "]"
		  << " "
		  << "[Column#"
		  << externalIndex_i 
		  << "]"
		  << "  Not Existing !!!"
		  << endl;
  }
	

  tmp_strstream << eqline1_CNS 
		<< endl;


  tmp_strstream << gstrObjectLocationShow () << endl;


  if (!fileName_i.empty ())
  {
    tmp_strstream << endl;
    tmp_strstream << "\t#################################################" <<  endl;
    tmp_strstream << "\t###=========== Index Out Of Range ============###" <<  endl;
    tmp_strstream << "\t#################################################" <<  endl;
    tmp_strstream << "\t### See : " << fileName_i << ", line#" << lineNo_i << endl;
    tmp_strstream << "\t###     : (Compilation - " << compilationDate_i << ",   " << compilationTime_i << ")" << endl;
    tmp_strstream << "\t#################################################" <<  endl;
    tmp_strstream << endl;
  }

  tmp_strstream << eqline1_CNS 
	        << endl
	        << endl
	        << endl
	        << endl;


  tmp_strstream << ends;
  ret_stringValue = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);

  return ret_stringValue;
} // string ClassFlexibleVector<T>::getErrorMsgAboutVectorIndexOutOfRange



//==================
template <class	T>
T& ClassFlexibleVector<T> 
	::operator[] (int externalIndex_i)
//==================
{
  if (!(indexBelongsToVectorRange (externalIndex_i)))
  {
    BUG_Vector_MESSAGE (<< getErrorMsgAboutVectorIndexOutOfRange (externalIndex_i, __PRETTY_FUNCTION__));
    return dummy_element_;
  }

  return basicUsualVector_ [externalIndex_i - minIndex_];
} // T& ClassFlexibleVector<T>::operator[]



//==================
template <class	T>
T ClassFlexibleVector<T> 
	::operator[] (int externalIndex_i) const
//==================
{
  if (!(indexBelongsToVectorRange (externalIndex_i)))
  {
    BUG_Vector_MESSAGE (<< getErrorMsgAboutVectorIndexOutOfRange (externalIndex_i, __PRETTY_FUNCTION__));
    return dummy_element_;
  }

  return basicUsualVector_ [externalIndex_i - minIndex_];
} // T ClassFlexibleVector<T>::operator[] 







//==================
template <class	T>
T& ClassFlexibleVector<T> 
	::at (int 		externalIndex_i,
				const string 	fileName_i, 

⌨️ 快捷键说明

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