#ifndef ARRAY_HEAD_FILE
#define ARRAY_HEAD_FILE
#pragma once
#include <afx.h>
//数组模板类
template<class TYPE,class ARG_TYPE=const TYPE&>
class CWHArray
{
//变量定义
protected:
TYPE * m_pData; //数组指针
int m_nMaxCount; //缓冲数目
int m_nElementCount; //元素数目
//函数定义
public:
//构造函数
CWHArray();
//析构函数
virtual ~CWHArray();
//信息函数
public:
//是否为空
bool IsEmpty() const;
//获取数目
int GetCount() const;
//功能数组
public:
//获取缓冲
TYPE * GetData();
//获取缓冲
const TYPE * GetData() const;
//增加元素
int Add(ARG_TYPE newElement);
//拷贝数组
void Copy(const CWHArray & Src);
//追加数组
int Append(const CWHArray & Src);
//获取元素
TYPE & GetAt(int nIndex);
//获取元素
const TYPE & GetAt(int nIndex) const;
//获取元素
TYPE & ElementAt(int nIndex);
//获取元素
const TYPE & ElementAt(int nIndex) const;
//操作函数
public:
//设置大小
void SetSize(int nNewSize);
//设置元素
void SetAt(int nIndex, ARG_TYPE newElement);
//设置元素
void SetAtGrow(int nIndex, ARG_TYPE newElement);
//插入数据
void InsertAt(INT_PTR nIndex, const CWHArray & Src);
//插入数据
void InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1);
//删除数据
void RemoveAt(int nIndex, int nCount = 1);
//删除元素
void RemoveAll();
//操作重载
public:
//操作重载
TYPE & operator[](int nIndex);
//操作重载
const TYPE & operator[](int nIndex) const;
//内存函数
public:
//释放内存
void FreeMemory();
//申请内存
void AllocMemory(int nNewCount);
};
//构造函数
template<class TYPE, class ARG_TYPE>
CWHArray<TYPE, ARG_TYPE>::CWHArray()
{
m_pData = NULL;
m_nMaxCount = 0;
m_nElementCount = 0;
}
//析构函数
template<class TYPE, class ARG_TYPE>
CWHArray<TYPE,ARG_TYPE>::~CWHArray()
{
if (m_pData != NULL)
{
for (int i = 0; i<m_nElementCount; i++)
(m_pData + i)->~TYPE();
delete[](char *)m_pData;
m_pData = NULL;
}
}
//是否为空
template<class TYPE, class ARG_TYPE>
inline bool CWHArray<TYPE, ARG_TYPE>::IsEmpty() const
{
return (m_pData == NULL);
}
//获取数目
template<class TYPE, class ARG_TYPE>
inline int CWHArray<TYPE, ARG_TYPE>::GetCount() const
{
return m_nElementCount;
}
//获取缓冲
template<class TYPE, class ARG_TYPE>
inline TYPE* CWHArray<TYPE, ARG_TYPE>::GetData()
{
return m_pData;
}
//获取缓冲
template<class TYPE, class ARG_TYPE>
inline const TYPE* CWHArray<TYPE, ARG_TYPE>::GetData() const
{
return m_pData;
}
//增加元素
template<class TYPE, class ARG_TYPE>
int CWHArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement)
{
int nIndex = m_nElementCount;
SetAtGrow(nIndex, newElement);
return nIndex;
}
//拷贝数组
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::Copy(const CWHArray & Src)
{
//效验参数
ASSERT(this != &Src);
if (this == &Src) return;
//拷贝数组
AllocMemory(Src.m_nElementCount);
if (m_nElementCount>0)
{
for (int i = 0; i<m_nElementCount; i++)
(m_pData + i)->~TYPE();
memset(m_pData, 0, m_nElementCount * sizeof(TYPE));
}
for (int i = 0; i<Src.m_nElementCount; i++)
m_pData[i] = Src.m_pData[i];
m_nElementCount = Src.m_nElementCount;
}
//追加数组
template<class TYPE, class ARG_TYPE>
int CWHArray<TYPE, ARG_TYPE>::Append(const CWHArray & Src)
{
//效验参数
ASSERT(this != &Src);
if (this == &Src) AfxThrowInvalidArgException();
//拷贝数组
if (Src.m_nElementCount>0)
{
AllocMemory(m_nElementCount + Src.m_nElementCount);
for (int i = 0; i<Src.m_nElementCount; i++)
m_pData[m_nElementCount + i] = Src.m_pData[i];
m_nElementCount += Src.m_nElementCount;
}
return m_nElementCount;
}
//获取元素
template<class TYPE, class ARG_TYPE>
TYPE & CWHArray<TYPE, ARG_TYPE>::GetAt(int nIndex)
{
//效验参数
ASSERT(nIndex < m_nElementCount&&nIndex >= 0);
if(nIndex>=m_nElementCount|| nIndex<0)
AfxThrowInvalidArgException();
return m_pData[nIndex];
}
//获取元素
template<class TYPE, class ARG_TYPE>
const TYPE & CWHArray<TYPE, ARG_TYPE>::GetAt(int nIndex) const
{
//效验参数
ASSERT(nIndex < m_nElementCount&&nIndex >= 0);
if (nIndex >= m_nElementCount || nIndex<0)
AfxThrowInvalidArgException();
return m_pData[nIndex];
}
//获取元素
template<class TYPE, class ARG_TYPE>
TYPE & CWHArray<TYPE, ARG_TYPE>::ElementAt(int nIndex)
{
//效验参数
ASSERT(nIndex < m_nElementCount&&nIndex >= 0);
if (nIndex >= m_nElementCount || nIndex<0)
AfxThrowInvalidArgException();
return m_pData[nIndex];
}
//获取元素
template<class TYPE, class ARG_TYPE>
const TYPE & CWHArray<TYPE, ARG_TYPE>::ElementAt(int nIndex) const
{
//效验参数
ASSERT(nIndex < m_nElementCount&&nIndex >= 0);
if (nIndex >= m_nElementCount || nIndex<0)
AfxThrowInvalidArgException();
return m_pData[nIndex];
}
//设置大小
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::SetSize(int nNewSize)
{
//效验参数
ASSERT(nNewSize >= 0);
if (nNewSize<0) AfxThrowInvalidArgException();
//设置大小
if (nNewSize > m_nElementCount)
{
AllocMemory(nNewSize);
//申请的空间需要new一下 调TYPE的构造
for (INT_PTR i = m_nElementCount; i<nNewSize; i++)
new ((VOID *)(m_pData + i)) TYPE;
}
else
{
//不调析构将会内存泄漏
for (int i = nNewSize; i < m_nElementCount; i++)
(m_pData + i)->~TYPE();
memset(m_pData + nNewSize, 0, (m_nElementCount - nNewSize) * sizeof(TYPE));
}
m_nElementCount = nNewSize;
}
//设置元素
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::SetAt(int nIndex, ARG_TYPE newElement)
{
ASSERT((nIndex >= 0) && (nIndex<m_nElementCount));
if ((nIndex >= 0) && (nIndex<m_nElementCount)) m_pData[nIndex] = newElement;
else AfxThrowInvalidArgException();
return;
}
//设置元素
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
{
//效验参数
ASSERT(nIndex >= 0);
if (nIndex<0) AfxThrowInvalidArgException();
//设置元素
if (nIndex >= m_nElementCount) SetSize(m_nElementCount + 1);
m_pData[nIndex] = newElement;
return;
}
//插入数据
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, const CWHArray & Src)
{
//效验参数
ASSERT(nIndex >= 0);
if (nIndex<0) AfxThrowInvalidArgException();
if (Src.m_nElementCount>0)
{
int nCount = Src.m_nElementCount;
//申请数组
if (nIndex<m_nElementCount)
{
int nOldCount = m_nElementCount;
SetSize(m_nElementCount + Src.m_nElementCount);
for (int i = 0; i<nCount; i++) (m_pData + nOldCount + i)->~TYPE();
memmove(m_pData + nIndex + nCount, m_pData + nIndex, (nOldCount - nIndex) * sizeof(TYPE));
memset(m_pData + nIndex, 0, Src.m_nElementCount * sizeof(TYPE));
for (int i = 0; i<Src.m_nElementCount; i++) new (m_pData + nIndex + i) TYPE();
}
else SetSize(nIndex + nCount);
//拷贝数组
ASSERT((nIndex + Src.m_nElementCount) <= m_nElementCount);
int i = 0;
while (nCount--)
m_pData[nIndex++] = Src.m_pData[i++];
}
return;
}
//插入数据
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount)
{
//效验参数
ASSERT(nIndex >= 0);
ASSERT(nCount>0);
if ((nIndex<0) || (nCount <= 0)) AfxThrowInvalidArgException();
//申请数组
if (nIndex<m_nElementCount)
{
INT_PTR nOldCount = m_nElementCount;
SetSize(m_nElementCount + nCount);
for (INT_PTR i = 0; i<nCount; i++) (m_pData + nOldCount + i)->~TYPE();
memmove(m_pData + nIndex + nCount, m_pData + nIndex, (nOldCount - nIndex) * sizeof(TYPE));
memset(m_pData + nIndex, 0, nCount * sizeof(TYPE));
for (INT_PTR i = 0; i<nCount; i++) new (m_pData + nIndex + i) TYPE();
}
else SetSize(nIndex + nCount);
//拷贝数组
ASSERT((nIndex + nCount) <= m_nElementCount);
while (nCount--) m_pData[nIndex++] = newElement;
return;
}
//删除数据
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount = 1)
{
//效验参数
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nElementCount);
if ((nIndex<0) || (nCount<0) || ((nIndex + nCount>m_nElementCount))) AfxThrowInvalidArgException();
//删除数据
INT_PTR nMoveCount = m_nElementCount - (nIndex + nCount);
for (INT_PTR i = 0; i<nCount; i++) (m_pData + nIndex + i)->~TYPE();
if (nMoveCount>0) memmove(m_pData + nIndex, m_pData + nIndex + nCount, nMoveCount * sizeof(TYPE));
m_nElementCount -= nCount;
return;
}
//删除元素
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::RemoveAll()
{
if (m_nElementCount>0)
{
for (INT_PTR i = 0; i<m_nElementCount; i++)
(m_pData + i)->~TYPE();
memset(m_pData, 0, m_nElementCount * sizeof(TYPE));
m_nElementCount = 0;
}
return;
}
template<class TYPE, class ARG_TYPE>
TYPE & CWHArray<TYPE, ARG_TYPE>:: operator[](int nIndex)
{
return GetAt(nIndex);
}
template<class TYPE, class ARG_TYPE>
const TYPE & CWHArray<TYPE, ARG_TYPE>:: operator[](int nIndex)const
{
return ElementAt(nIndex);
}
//释放内存
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::FreeMemory()
{
if(m_nMaxCount>0&&m_pData!=NULL&&m_nMaxCount!=m_nElementCount)
{
//注意这里不能直接 new TYPE[nNewCount] 否者将会造成野指针
TYPE* pNewData = (TYPE*) new char[(m_nElementCount * sizeof(TYPE))];
memcpy(pNewData, m_pData, sizeof(TYPE)*m_nElementCount);
delete[] m_pData;
m_pData = pNewData;
m_nMaxCount = m_nElementCount;
}
return;
}
//申请内存
template<class TYPE, class ARG_TYPE>
void CWHArray<TYPE, ARG_TYPE>::AllocMemory(int nNewCount)
{
ASSERT(nNewCount > 0);
if (nNewCount > m_nMaxCount)
{
//申请内存
//注意这里不能直接 new TYPE[nNewCount] 否者将会造成野指针
TYPE* pNewData = (TYPE*) new char[(nNewCount * sizeof(TYPE))];
if (m_pData != NULL)
{
memcpy(pNewData, m_pData, (sizeof(TYPE)*m_nElementCount));
memset(pNewData + m_nElementCount, 0, sizeof(TYPE)*(nNewCount - m_nElementCount));
delete[] m_pData;
}
m_pData = pNewData;
m_nMaxCount = nNewCount;
}
return;
}
#endif
动态数组的封装管理
最新推荐文章于 2023-11-14 20:52:46 发布