📄 array.h
字号:
// CArray.h: interface for the CArray class.
//
//////////////////////////////////////////////////////////////////////
#ifndef _GOS_ARRAY_H_
#define _GOS_ARRAY_H_
/////////////////////////////////////////////////////////////////////////////
// CArray<TYPE> inline functions
template<class TYPE>
__inline int CArray<TYPE>::GetSize() const
{ return _msize(m_pData)/sizeof(TYPE);}
template<class TYPE>
__inline BOOL CArray<TYPE>::IsEmpty() const
{ return !(_msize(m_pData)/sizeof(TYPE)); }
template<class TYPE>
__inline void CArray<TYPE>::RemoveAll()
{ free(m_pData);m_pData=NULL; }
template<class TYPE>
__inline TYPE* CArray<TYPE>::GetData()
{ return m_pData; }
template<class TYPE>
__inline TYPE& CArray<TYPE>::operator[](int nIndex)
{ return m_pData[nIndex]; }
template<class TYPE>
__inline void CArray<TYPE>::InsertAt(int nIndex, const TYPE& newElement)
{ InsertAt(nIndex,newElement,1); }
template<class TYPE>
__inline void CArray<TYPE>::RemoveAt(int nIndex)
{ RemoveAt(nIndex,1); }
template<class TYPE>
__inline void CArray<TYPE>::SetSize(int nNewSize)
{ m_pData=(TYPE*)realloc(m_pData,nNewSize*sizeof(TYPE)); }
/////////////////////////////////////////////////////////////////////////////
// CArray<TYPE> out-of-line functions
template<class TYPE>
CArray<TYPE>::CArray()
{
m_pData = NULL;
}
template<class TYPE>
CArray<TYPE>::~CArray()
{
free(m_pData);
}
template<class TYPE>
int CArray<TYPE>::Add(const TYPE& newElement)
{
int nIndex = GetSize();
m_pData=(TYPE*)realloc(m_pData,(nIndex+1)*sizeof(TYPE));
m_pData[nIndex]=newElement;
return nIndex;
}
template<class TYPE>
int CArray<TYPE>::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<class TYPE>
void CArray<TYPE>::Copy(const CArray& src)
{
int nSrcSize=src.GetSize();
SetSize(nSrcSize);
CopyMemory(m_pData,src.m_pData,nSrcSize*sizeof(TYPE));
}
template<class TYPE>
void CArray<TYPE>::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<class TYPE>
void CArray<TYPE>::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<class TYPE>
void CArray<TYPE>::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<class TYPE>
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_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -