www.pudn.com > WinGOS.rar > Array.h


// CArray.h: interface for the CArray class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#ifndef _GOS_ARRAY_H_ 
#define _GOS_ARRAY_H_ 
 
///////////////////////////////////////////////////////////////////////////// 
// CArray inline functions 
 
template 
__inline int CArray::GetSize() const 
	{ return _msize(m_pData)/sizeof(TYPE);} 
template 
__inline BOOL CArray::IsEmpty() const 
	{ return !(_msize(m_pData)/sizeof(TYPE)); } 
template 
__inline void CArray::RemoveAll() 
	{ free(m_pData);m_pData=NULL; } 
template 
__inline TYPE* CArray::GetData() 
	{ return m_pData; } 
template 
__inline TYPE& CArray::operator[](int nIndex) 
	{ return m_pData[nIndex]; } 
template 
__inline void CArray::InsertAt(int nIndex, const TYPE& newElement) 
	{ InsertAt(nIndex,newElement,1); } 
template 
__inline void CArray::RemoveAt(int nIndex) 
	{ RemoveAt(nIndex,1); } 
template 
__inline void CArray::SetSize(int nNewSize) 
	{ m_pData=(TYPE*)realloc(m_pData,nNewSize*sizeof(TYPE)); } 
 
///////////////////////////////////////////////////////////////////////////// 
// CArray out-of-line functions 
 
template 
CArray::CArray() 
{ 
	m_pData = NULL; 
} 
 
template 
CArray::~CArray() 
{ 
	free(m_pData); 
} 
 
template 
int CArray::Add(const TYPE& newElement) 
{  
	int nIndex = GetSize(); 
	m_pData=(TYPE*)realloc(m_pData,(nIndex+1)*sizeof(TYPE)); 
	m_pData[nIndex]=newElement; 
	return nIndex;  
} 
 
template 
int CArray::Append(const CArray& src) 
{ 
	int nSize = GetSize(); 
	int nSrcSize=src.GetSize(); 
	SetSize(nSize + nSrcSize); 
	CopyMemory(&m_pData[nSize],src.m_pData,nSrcSize*sizeof(TYPE)); 
	return nOldSize; 
} 
 
template 
void CArray::Copy(const CArray& src) 
{ 
	int nSrcSize=src.GetSize(); 
	SetSize(nSrcSize); 
	CopyMemory(m_pData,src.m_pData,nSrcSize*sizeof(TYPE)); 
} 
 
 
template 
void CArray::InsertAt(int nIndex, const TYPE& newElement, int nCount) 
{ 
	int nSize=GetSize(); 
	SetSize(nSize + nCount); 
	if (nIndex < nSize) 
	{ 
		CopyMemory(&m_pData[nIndex+nCount], &m_pData[nIndex], 
			(nSize-nIndex) * sizeof(TYPE)); 
	} 
	while (nCount--) 
	{ 
		m_pData[nIndex++] = newElement; 
	} 
} 
 
template 
void CArray::RemoveAt(int nIndex, int nCount) 
{ 
	int nSize=GetSize(); 
	int nMoveCount = nSize - (nIndex + nCount); 
	if (nMoveCount) 
	{ 
		CopyMemory(&m_pData[nIndex], &m_pData[nIndex + nCount], 
			nMoveCount * sizeof(TYPE)); 
	} 
	SetSize(nSize-nCount); 
} 
 
template 
void CArray::InsertAt(int nIndex, CArray* pNewArray) 
{ 
	int nSize=GetSize(); 
	int nCount=pNewArray->GetSize(); 
	SetSize(nSize + nCount); 
	if (nIndex < nSize) 
	{ 
		CopyMemory(&m_pData[nIndex+nCount], &m_pData[nIndex], 
			(nSize-nIndex) * sizeof(TYPE)); 
	} 
	CopyMemory(&m_pData[nIndex], pNewArray->m_pData), 
		nCount * sizeof(TYPE)); 
} 
 
template 
void Move(int nIndex,int nNewIndex) 
{ 
	TYPE tElement; 
	tElement=m_pData[nIndex]; 
	if(nNewIndex > nIndex) 
	{ 
		CopyMemory(&m_pData[nIndex],&m_pData[nIndex+1],sizeof(TYPE)*(nNewIndex-nIndex)); 
	} 
	else (nNewIndex < nIndex) 
	{ 
		CopyMemory(&m_pData[nNewIndex+1],&m_pData[nNewIndex],sizeof(TYPE)*(nIndex-nNewIndex)); 
	} 
	m_pData[nNewIndex]=tElement; 
} 
 
#endif // _GOS_ARRAY_H_