Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы2 / MOLab3 / MOLab3 / CVector
.h//////////////////////////////////
// Vector.h //
// //
//////////////////////////////////
// class CVector v2.01:0392 //
// //
//////////////////////////////////
/****************************************************************\
* Copyright (c) 2003-2004 by German K. V. ALL RIGHTS RESERVED. *
* Consult your license regarding permissions and restrictions. *
\****************************************************************/
/*===============================================================*\
|| This file is derived from software bearing the following ||
|| restrictions: ||
|| ||
|| Copyright (c) 2004 ||
|| GKV ||
|| ||
|| Permission to use, copy, modify, distribute and sell this ||
|| software and its documentation for any purpose is hereby ||
|| granted without fee, provided that the above copyright notice ||
|| appear in all copies and that both that copyright notice and ||
|| this permission notice appear in supporting documentation. ||
|| GKV makes no representations about the suitability of this ||
|| software for any purpose. It is provided "as is" without ||
|| express or implied warranty. ||
\*===============================================================*/
#pragma once
#include <stdexcept>
#include <iostream>
using namespace std;
//---------------------------------------------CVector----------------------------------------------//
////////////////////////
// Version: 2.01:0392 //
////////////////////////
#define VECTOR_BEST_GROW 4096
template <typename TYPE, int SIZE = 0>
class CVector
{
public:
typedef CVector<TYPE, SIZE> this_type;
protected:
TYPE* m_pData; // Array data
int m_nSize; // Current size of array
int m_nCount; // Avaible data
int m_nGrow; // Auto size icrease
public:
CVector();
CVector(TYPE defElem);
CVector(int nCount, TYPE defElem);
CVector(const this_type& _V);
~CVector();
// Operators...
public:
TYPE& operator[](int nPos) const;
this_type& operator= (const this_type& _V);
this_type& operator= (TYPE defElem);
this_type operator* (TYPE defElem);
this_type operator/ (TYPE defElem);
this_type operator+ ( void );
this_type operator+ (this_type& _V);
this_type operator- (this_type& _V);
this_type operator- ( void );
void operator*=(TYPE defElem);
void operator/=(TYPE defElem);
void operator+=(this_type& _V);
void operator+=(TYPE defElem);
void operator-=(this_type& _V);
void operator-=(TYPE defElem);
bool operator==(this_type _V) const;
bool operator!=(this_type _V) const;
public:
TYPE* GetData ();
int GetSize () const;
int GetCount() const;
int SetSize (int nNewSize);
int SetCount(int nNewCount);
int SetCount(int nNewCount, TYPE defElem);
int SetGrow (int nGrowBy = -1);
void FreeExtra();
void Swap(this_type& _V);
int Add(TYPE newElement);
int RemoveAll();
};
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::CVector()
: m_pData(NULL)
, m_nSize(0)
, m_nCount(0)
, m_nGrow(VECTOR_BEST_GROW)
{
try
{
RemoveAll();
SetCount(SIZE);
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::CVector(TYPE defElem)
: m_pData(NULL)
, m_nSize(0)
, m_nCount(0)
, m_nGrow(VECTOR_BEST_GROW)
{
try
{
RemoveAll();
SetCount(SIZE, defElem);
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::CVector(int nCount, TYPE defElem)
: m_pData(NULL)
, m_nSize(0)
, m_nCount(0)
, m_nGrow(VECTOR_BEST_GROW)
{
try
{
RemoveAll();
SetCount(nCount, defElem);
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::CVector(const this_type& _V)
: m_pData(NULL)
, m_nSize(0)
, m_nCount(0)
, m_nGrow(VECTOR_BEST_GROW)
{
try
{
RemoveAll();
SetCount(_V.GetCount());
for(int i = 0; i<m_nCount; i++)
m_pData[i] = _V[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::~CVector()
{
try
{
RemoveAll();
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
// Operators...
template <typename TYPE, int SIZE>
TYPE& CVector<TYPE, SIZE>::operator[](int nPos) const
{
try
{
if( (nPos >= m_nCount) || (nPos < 0) )
throw out_of_range( "The position is out of range!" );
return m_pData[nPos];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
return m_pData[0];
}
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type& CVector<TYPE, SIZE>::operator=(const CVector<TYPE, SIZE>::this_type& _V)
{
try
{
int nCount = _V.GetCount();
m_nSize = m_nCount = 0;
SetCount(nCount);
for(int i = 0; i<m_nCount; i++)
m_pData[i] = _V[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return (*this);
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type& CVector<TYPE, SIZE>::operator=(TYPE defElem)
{
try
{
for(int i=0; i<m_nCount; i++)
m_pData[i] = defElem;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return (*this);
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type CVector<TYPE, SIZE>::operator* (TYPE defElem)
{
this_type _Temp(m_nSize);
try
{
for(int i = 0; i < m_nCount; i++)
_Temp[i] = m_pData[i] * defElem;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return _Temp;
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type CVector<TYPE, SIZE>::operator/ (TYPE defElem)
{
this_type _Temp(m_nSize);
try
{
for(int i = 0; i < m_nCount; i++)
_Temp[i] = m_pData[i] / defElem;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return _Temp;
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type CVector<TYPE, SIZE>::operator+ ( void )
{
this_type _Temp(m_nCount);
try
{
for(int i = 0; i < m_nCount; i++)
_Temp[i] = + m_pData[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return _Temp;
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type CVector<TYPE, SIZE>::operator+ (CVector<TYPE, SIZE>::this_type& _V)
{
this_type _Temp(min(m_nCount, _V.GetCount()));
try
{
for(int i = 0; i < min(m_nCount, _V.GetCount()); i++)
_Temp[i] = m_pData[i] + _V[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return _Temp;
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type CVector<TYPE, SIZE>::operator- ( void )
{
this_type _Temp(m_nCount);
try
{
for(int i = 0; i < m_nCount; i++)
_Temp[i] = - m_pData[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return _Temp;
}
template <typename TYPE, int SIZE>
CVector<TYPE, SIZE>::this_type CVector<TYPE, SIZE>::operator- (CVector<TYPE, SIZE>::this_type& _V)
{
this_type _Temp(min(m_nCount, _V.GetCount()));
try
{
for(int i = 0; i < min(m_nCount, _V.GetCount()); i++)
_Temp[i] = m_pData[i] - _V[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return _Temp;
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::operator*=(TYPE defElem)
{
try
{
for(int i = 0; i < m_nCount; i++)
m_pData[i] *= defElem;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::operator/=(TYPE defElem)
{
try
{
for(int i = 0; i < m_nCount; i++)
m_pData[i] /= defElem;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::operator+=(CVector<TYPE, SIZE>::this_type& _V)
{
try
{
for(int i = 0; i < min(m_nCount, _V.GetCount()); i++)
m_pData[i] += _V[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::operator+=(TYPE defElem)
{
try
{
for(int i = 0; i < m_nCount; i++)
m_pData[i] += defElem;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::operator-=(CVector<TYPE, SIZE>::this_type& _V)
{
try
{
for(int i = 0; i < min(m_nCount, _V.GetCount()); i++)
m_pData[i] -= _V[i];
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::operator-=(TYPE defElem)
{
try
{
for(int i = 0; i < m_nCount; i++)
m_pData[i] -= defElem;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
bool CVector<TYPE, SIZE>::operator==(CVector<TYPE, SIZE>::this_type _V) const
{
try
{
if (m_nCount != _V.GetCount())
return false;
for (int i = 0; i < m_nCount; i++)
{
if (m_pData[i] != _V[i])
return false;
}
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return true;
}
template <typename TYPE, int SIZE>
bool CVector<TYPE, SIZE>::operator!=(CVector<TYPE, SIZE>::this_type _V) const
{
try
{
if (m_nCount != _V.GetCount())
return true;
for (int i = 0; i < m_nCount; i++)
{
if (m_pData[i] != _V[i])
return true;
}
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return false;
}
// End Operators..
template <typename TYPE, int SIZE>
TYPE* CVector<TYPE, SIZE>::GetData()
{
return m_pData;
}
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::GetSize() const
{
return m_nSize;
}
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::GetCount() const
{
return m_nCount;
}
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::SetSize(int nNewSize)
{
try
{
if (0 == nNewSize)
{
RemoveAll();
}
else
{
TYPE* pNewData = NULL;
pNewData = new TYPE[nNewSize];
if(pNewData==NULL)
length_error("Haven't free memory!");
int nMinSize = min(m_nSize, nNewSize);
for (int i = 0; i < nMinSize; i++)
pNewData[i] = m_pData[i];
RemoveAll();
m_pData = pNewData;
if (NULL == m_pData)
ASSERT(0);
}
m_nSize = nNewSize;
if (m_nCount > m_nSize)
m_nCount = m_nSize;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
delete[] m_pData;
m_nSize = -1;
ASSERT(0);
}
return m_nSize;
}
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::SetCount(int nNewCount)
{
try
{
if (nNewCount > m_nSize)
{
if (m_nSize > 0)
SetSize(nNewCount + m_nGrow);
else SetSize(nNewCount);
}
m_nCount = nNewCount;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return m_nCount;
};
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::SetCount(int nNewCount, TYPE defElem)
{
try
{
int nLastCount = m_nCount;
if (nNewCount > m_nSize)
{
if (m_nSize > 0)
SetSize(nNewCount + m_nGrow);
else SetSize(nNewCount);
}
for (int i = nLastCount; i < nNewCount; i++)
m_pData[i] = defElem;
m_nCount = nNewCount;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return m_nCount;
};
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::SetGrow(int nGrowBy)
{
return m_nGrow = nGrowBy > -1 ? nGrowBy : VECTOR_BEST_GROW;
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::FreeExtra()
{
SetSize(m_nCount);
}
template <typename TYPE, int SIZE>
void CVector<TYPE, SIZE>::Swap(CVector<TYPE, SIZE>::this_type& _V)
{
try
{
this_type buf = _V;
_V = *(this);
*(this) = buf;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
}
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::Add(TYPE newElement)
{
try
{
SetCount(m_nCount + 1, newElement);
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
ASSERT(0);
}
return m_nCount;
}
template <typename TYPE, int SIZE>
int CVector<TYPE, SIZE>::RemoveAll()
{
try
{
if (NULL == m_pData)
return m_nSize = m_nCount = 0;
for (int i = 0; i < m_nSize; i++)
{
m_pData[i].~TYPE();
}
delete[] m_pData;
m_pData = NULL;
m_nSize = 0;
m_nCount = 0;
}
catch (exception &e)
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid( e ).name( ) << endl;
delete[] m_pData;
m_nSize = m_nCount = -1;
ASSERT(0);
}
return m_nSize;
}
Соседние файлы в папке MOLab3