#ifndef _ARRAY_TEMPLATE_HEAD_FILE #define _ARRAY_TEMPLATE_HEAD_FILE #pragma once #include "cocos2d.h" #include "PlatformHeader.h" ////////////////////////////////////////////////////////////////////////// //数组模板类 template class CArrayTemplate { //变量定义 protected: TYPE *m_pData; //数组指针 int m_nMaxCount; //缓冲数目 int m_nGrowCount; //增长数目 int m_nElementCount; //元素数目 //函数定义 public: //构造函数 CArrayTemplate(void); //析构函数 virtual ~CArrayTemplate(void); //信息函数 public: //是否空组 bool IsEmpty() const; //获取数目 int GetCount() const; //获取上限 int GetUpperBound() const; //功能函数 public: //获取缓冲 TYPE * GetData(); //获取缓冲 const TYPE * GetData() const; //释放内存 void FreeExtra(); //增加元素 int Add(ARG_TYPE newElement); //增加不同的值 int AddDistinct( ARG_TYPE newElement ); //拷贝数组 int Copy(const CArrayTemplate & Src); //追加数组 int Append(const CArrayTemplate & 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 nIndex, const CArrayTemplate & Src); //插入数据 void InsertAt(int nIndex, ARG_TYPE newElement, int nCount=1); //删除数据 void RemoveAt(int nIndex, int nCount=1); //删除指定元素 int RemoveItem( ARG_TYPE newElement ); //删除元素 void RemoveAll(); //降序排序,要求对象类型重载 > void Sort(int nLower, int nUpper); //快速排序法的数据分区,要求对象类型重载 > int Partition(int nLower, int nUpper); //交换变量 void SwitchValue( int a, int b ); //排序 template void SortEx(int nLower, int nUpper,FUNC_SHARRAY_COMPARE fp) { //是否排序完毕 if (nLower < nUpper) { // 分区排序 int nSplit = PartitionEx(nLower, nUpper,fp); SortEx(nLower, nSplit-1,fp);//左区排序 SortEx(nSplit+1, nUpper,fp);//右区排序 } } //快速排序法的数据分区 template int PartitionEx(int nLower, int nUpper,FUNC_SHARRAY_COMPARE fp) { int nLeft = nLower + 1; TYPE &RefObj = m_pData[nLower]; int nRight = nUpper; TYPE Swap; while (nLeft <= nRight) { while (nLeft <= nRight && fp(m_pData[nLeft],RefObj)) { nLeft++; } while (nLeft <= nRight && !fp(m_pData[nRight],RefObj)) { nRight--; } if (nLeft < nRight) { Swap = m_pData[nLeft]; m_pData[nLeft] = m_pData[nRight]; m_pData[nRight] = Swap; nLeft++; nRight--; } } Swap = m_pData[nLower]; m_pData[nLower] = m_pData[nRight]; m_pData[nRight] = Swap; return nRight; } //操作重载 public: //操作重载 TYPE & operator[](int nIndex); //操作重载 const TYPE & operator[](int nIndex) const; //内部函数 private: //申请内存 void AllocMemory(int nNewCount); }; ////////////////////////////////////////////////////////////////////////// // CArrayTemplate 内联函数 //是否空组 template inline bool CArrayTemplate::IsEmpty() const { //ASSERT_VALID(this); return (m_nElementCount==0); } //获取数目 template inline int CArrayTemplate::GetCount() const { //ASSERT_VALID(this); return m_nElementCount; } //获取上限 template inline int CArrayTemplate::GetUpperBound() const { //ASSERT_VALID(this); return m_nElementCount-1; } //增加元素 template inline int CArrayTemplate::Add(ARG_TYPE newElement) { int nIndex=m_nElementCount; SetAtGrow(nIndex,newElement); return nIndex; } //操作重载 template inline TYPE & CArrayTemplate::operator[](int nIndex) { return ElementAt(nIndex); } //操作重载 template inline const TYPE & CArrayTemplate::operator[](int nIndex) const { return GetAt(nIndex); } ////////////////////////////////////////////////////////////////////////// // CArrayTemplate 外联函数 //构造函数 template CArrayTemplate::CArrayTemplate() { m_pData=NULL; m_nMaxCount=0; m_nGrowCount=0; m_nElementCount=0; return; } //构造函数 template CArrayTemplate::~CArrayTemplate() { if (m_pData!=NULL) { for (int i=0;i~TYPE(); } delete [] (BYTE *)m_pData; m_pData=NULL; } return; } //获取缓冲 template TYPE * CArrayTemplate::GetData() { ASSERT_VALID(this); return m_pData; } //获取缓冲 template const TYPE * CArrayTemplate::GetData() const { ASSERT_VALID(this); return m_pData; } //释放内存 template void CArrayTemplate::FreeExtra() { ASSERT_VALID(this); if (m_nElementCount!=m_nMaxCount) { TYPE * pNewData=NULL; if (m_nElementCount!=0) { pNewData=(TYPE *) new BYTE[m_nElementCount*sizeof(TYPE)]; //memcpy(pNewData,m_pData,m_nElementCount*sizeof(TYPE)); //old,flowing is modify by myz memmove(pNewData,m_pData,m_nElementCount*sizeof(TYPE)); } delete [] (BYTE *)m_pData; m_pData=pNewData; m_nMaxCount=m_nElementCount; } return; } //拷贝数组 template int CArrayTemplate::Copy(const CArrayTemplate & Src) { //效验参数 ASSERT_VALID(this); ASSERT(this!=&Src); if (this==&Src) { return 0; } //拷贝数组 AllocMemory(Src.m_nElementCount); if (m_nElementCount>0) { for (int i=0;i~TYPE(); } memset(m_pData,0,m_nElementCount*sizeof(TYPE)); } for (int i=0;i int CArrayTemplate::Append(const CArrayTemplate & Src) { //效验参数 ASSERT_VALID(this); ASSERT(this!=&Src); //if (this==&Src) AfxThrowInvalidArgException(); //拷贝数组 if (Src.m_nElementCount>0) { AllocMemory(m_nElementCount+Src.m_nElementCount); for (int i=0;i TYPE & CArrayTemplate::GetAt(int nIndex) { ASSERT((nIndex>=0)&&(nIndex=0)&&(nIndex const TYPE & CArrayTemplate::GetAt(int nIndex) const { ASSERT((nIndex>=0)&&(nIndex=0)&&(nIndex TYPE & CArrayTemplate::ElementAt(int nIndex) { ASSERT((nIndex>=0)&&(nIndex=0)&&(nIndex const TYPE & CArrayTemplate::ElementAt(int nIndex) const { ASSERT((nIndex>=0)&&(nIndex=0)&&(nIndex void CArrayTemplate::SetSize(int nNewSize) { //效验参数 //ASSERT_VALID(this); ASSERT(nNewSize>=0); //if (nNewSize<0) AfxThrowInvalidArgException(); //设置大小 AllocMemory(nNewSize); if (nNewSize>m_nElementCount) { #pragma push_macro("new") #undef new for (int i=m_nElementCount;i~TYPE(); } memset(m_pData+nNewSize,0,(m_nElementCount-nNewSize)*sizeof(TYPE)); } m_nElementCount=nNewSize; return; } //设置元素 template void CArrayTemplate::SetAt(int nIndex, ARG_TYPE newElement) { ASSERT((nIndex>=0)&&(nIndex=0)&&(nIndex void CArrayTemplate::SetAtGrow(int nIndex, ARG_TYPE newElement) { //效验参数 //ASSERT_VALID(this); ASSERT(nIndex>=0); //if (nIndex<0) AfxThrowInvalidArgException(); //设置元素 if (nIndex>=m_nElementCount) { SetSize(m_nElementCount+1); } m_pData[nIndex]=newElement; return; } //插入数据 template void CArrayTemplate::InsertAt(int nIndex, const CArrayTemplate & Src) { //效验参数 //ASSERT_VALID(this); ASSERT(nIndex>=0); //if (nIndex<0) AfxThrowInvalidArgException(); int nCount = Src.m_nElementCount; if (nCount>0) { //申请数组 if (nIndex~TYPE(); } memmove(m_pData+nIndex+nCount,m_pData+nIndex,(nOldCount-nIndex)*sizeof(TYPE)); memset(m_pData+nIndex,0,nCount*sizeof(TYPE)); #pragma push_macro("new") #undef new for (int i=0;i void CArrayTemplate::InsertAt(int nIndex, ARG_TYPE newElement, int nCount) { //效验参数 //ASSERT_VALID(this); ASSERT(nIndex>=0); ASSERT(nCount>0); //if ((nIndex<0)||(nCount<=0)) AfxThrowInvalidArgException(); //申请数组 if (nIndex~TYPE(); } memmove(m_pData+nIndex+nCount,m_pData+nIndex,(nOldCount-nIndex)*sizeof(TYPE)); memset(m_pData+nIndex,0,nCount*sizeof(TYPE)); #pragma push_macro("new") #undef new for (int i=0;i void CArrayTemplate::RemoveAt(int nIndex, int nCount) { //效验参数 //ASSERT_VALID(this); ASSERT(nIndex>=0); ASSERT(nCount>=0); ASSERT(nIndex+nCount<=m_nElementCount); //if ((nIndex<0)||(nCount<0)||((nIndex+nCount>m_nElementCount))) AfxThrowInvalidArgException(); //删除数据 int nMoveCount=m_nElementCount-(nIndex+nCount); for (int i=0;i~TYPE(); } if (nMoveCount>0) { memmove(m_pData+nIndex,m_pData+nIndex+nCount,nMoveCount*sizeof(TYPE)); } m_nElementCount-=nCount; return; } //删除元素 template void CArrayTemplate::RemoveAll() { //ASSERT_VALID(this); if (m_nElementCount>0) { for (int i=0;i~TYPE(); } memset(m_pData,0,m_nElementCount*sizeof(TYPE)); m_nElementCount=0; } return; } //降序排序,要求对象类型重载 > template void CArrayTemplate::Sort(int nLower, int nUpper) { //是否排序完毕 if (nLower < nUpper) { // 分区排序 int nSplit = Partition(nLower, nUpper); Sort(nLower, nSplit-1);//左区排序 Sort(nSplit+1, nUpper);//右区排序 } } //快速排序法的数据分区 template int CArrayTemplate::Partition(int nLower, int nUpper) { int nLeft = nLower + 1; TYPE &RefObj = m_pData[nLower]; int nRight = nUpper; TYPE Swap; while (nLeft <= nRight) { while (nLeft <= nRight && (*m_pData[nLeft]) > (*RefObj)) { nLeft++; } while (nLeft <= nRight && !((*m_pData[nRight]) > (*RefObj))) { nRight--; } if (nLeft < nRight) { Swap = m_pData[nLeft]; m_pData[nLeft] = m_pData[nRight]; m_pData[nRight] = Swap; nLeft++; nRight--; } } Swap = m_pData[nLower]; m_pData[nLower] = m_pData[nRight]; m_pData[nRight] = Swap; return nRight; } //申请内存 template void CArrayTemplate::AllocMemory(int nNewCount) { //效验参数 //ASSERT_VALID(this); ASSERT(nNewCount>=0); if (nNewCount>m_nMaxCount) { //计算数目 int nGrowCount=m_nGrowCount; if (nGrowCount==0) { nGrowCount=(m_nElementCount>>3); nGrowCount=(nGrowCount<4)?4:((nGrowCount>1024)?1024:nGrowCount); } nNewCount+=nGrowCount; //申请内存 TYPE * pNewData=(TYPE *) new BYTE[nNewCount*sizeof(TYPE)]; //memcpy(pNewData,m_pData,m_nElementCount*sizeof(TYPE)); //old modify by myz memmove(pNewData,m_pData,m_nElementCount*sizeof(TYPE)); memset(pNewData+m_nElementCount,0,(nNewCount-m_nElementCount)*sizeof(TYPE)); delete [] (BYTE *)m_pData; //设置变量 m_pData=pNewData; m_nMaxCount=nNewCount; } } //交换变量 template void CArrayTemplate::SwitchValue( int a, int b ) { //ASSERT_VALID(this); ASSERT(a int CArrayTemplate::AddDistinct( ARG_TYPE newElement ) { for ( int i=0; i int CArrayTemplate::RemoveItem( ARG_TYPE newElement ) { for ( int i=0; i CSHPtrArray; typedef CArrayTemplate CSHCPtrArray; //删除CArrayTemplate指针数组 template void GGDeletePtrArray( CArrayTemplate &ary ) { if (!ary.IsEmpty()) { for ( int i=ary.GetCount()-1; i>=0; i-- ) { T *&pObj=ary[i]; if (pObj!=NULL) { delete pObj; } } ary.RemoveAll(); } } //释放CArrayTemplate指针数组 template void GGReleasePtrArray( CArrayTemplate &ary ) { if (!ary.IsEmpty()) { for ( int i=ary.GetCount()-1; i>=0; i-- ) { T *&pObj=ary[i]; if (pObj!=NULL) { pObj->Release(); } } ary.RemoveAll(); } } ////////////////////////////////////////////////////////////////////////// #endif