CArrayT

出自 ProgWiki
前往: 導覽搜尋

用途

  • 陣列模板,打消 MFC / ATL / STL 的陣列差異。

程式碼

//File: ArrayT.h
//Description: template CArrayT from 
//						CArray of MFC
//						CAtlArray of ATL
//						std::vector of STL
 
#ifndef __ARRAYT_H__
#define __ARRAYT_H__
 
#if _MSC_VER > 1000
	#pragma once
#endif
 
 
//-------------------------------------------------------------------------------
// TODO: CArrayT is use MFC
//-------------------------------------------------------------------------------
 
#if !defined(CArrayT) && defined(_AFX) && defined(_Use_MFC)
 
#include <afxtempl.h>
 
template <class TYPE>
class CArrayT : public CArray<TYPE>
{
public:
	//TODO: (MFC) typedef
	typedef TYPE*				iterator;
	typedef TYPE*				const_iterator;
 
	static int CompareAsc(const void *pArg1, const void *pArg2)
	{
		TYPE w1	= *(TYPE*)pArg1;
		TYPE w2	= *(TYPE*)pArg2;
 
		return w1 == w2 ? 0 :(w2 > w1 ? - 1 : 1);
	}
	static int CompareDesc(const void *pArg1, const void *pArg2)
	{
		TYPE w1	= *(TYPE*)pArg1;
		TYPE w2	= *(TYPE*)pArg2;
 
		return w1 == w2 ? 0 :(w1 > w2 ? - 1 : 1);		
	}
public:
	//TODO:(MFC) Sort
	void Sort(bool bAscending)
	{
		int nSize = GetSize();
		if (nSize == 0)
			return;
 
		::qsort(	(void*)&((*this)[0]),
					nSize,
					sizeof(TYPE),
					bAscending ? CompareAsc : CompareDesc);	
	}
};
 
#endif	// for MFC
 
 
//-------------------------------------------------------------------------------
// TODO: CArrayT is use ATL
//-------------------------------------------------------------------------------
#if !defined(CArrayT) && defined(ATL) && defined(_Use_ATL)
 
#include <atlcoll.h>	
 
template <typename TYPE> 
class CArrayT : public CAtlArray<TYPE>
{
public:
	//TODO: (ATL) typedef
	typedef TYPE*				iterator;
	typedef TYPE*				const_iterator;
 
	static int CompareAsc(const void *pArg1, const void *pArg2)
	{
		TYPE w1	= *(TYPE*)pArg1;
		TYPE w2	= *(TYPE*)pArg2;
 
		return w1 == w2 ? 0 :(w2 > w1 ? - 1 : 1);
	}
	static int CompareDesc(const void *pArg1, const void *pArg2)
	{
		TYPE w1	= *(TYPE*)pArg1;
		TYPE w2	= *(TYPE*)pArg2;
 
		return w1 == w2 ? 0 :(w1 > w2 ? - 1 : 1);		
	}
public:
	//TODO:(MFC) Sort
	void Sort(bool bAscending)
	{
		int nSize = GetSize();
		if (nSize == 0)
			return;
 
		::qsort(	(void*)&((*this)[0]),
					nSize,
					sizeof(TYPE),
					bAscending ? CompareAsc : CompareDesc);	
	}
};
 
 
#endif	// for ATL
 
 
//-------------------------------------------------------------------------------
// TODO: CArrayT is use STL
//-------------------------------------------------------------------------------
#if !defined(CArrayT)
 
#include <vector>
 
template <typename TYPE>
class CArrayT : private std::vector<TYPE>
{
public:
	//TODO: (STL) typedef
	typedef std::vector<TYPE>::iterator iterator;
	typedef std::vector<TYPE>			inherited;
 
protected:
	iterator GetIterFromIndex(int uIndex)
	{
		iterator iter = begin();
		iter += uIndex;
		//	int t = 0; while (t != uIndex) {iter++;t++;}
		return iter;
	}
 
public:
	//TODO: (STL) Attributes
	int	GetCount() const
	{
		return size();
	}
	int	GetSize() const
	{
		return size();
	}
	int GetUpperBound() const
	{
		return size() - 1;
	}
	bool IsEmpty() const
	{
		return empty();
	}
	void SetSize(int nNewSize)
	{
		resize(nNewSize);
	}
	//void	SetSize(int nNewSize, int nGrowBy = -1) 
	//{
	//	if (nGrowBy != -1)
	//	{
	//		int nNewSize2 =;
	//	}
	//	else
	//	{
	//		resize(nNewSize);
	//	}
	//}
 
 
	//TODO: (STL) Operations
	void RemoveAll()
	{
		clear();
	}
	//void	FreeExtra()								{	}
 
	//TODO: (STL) Element Access
	TYPE& ElementAt(int nIndex)
	{
		return at(nIndex);
	}
	TYPE& ElementAt(int nIndex) const
	{
		return at(nIndex);
	}
	TYPE& GetAt(int nIndex )
	{
		return at(nIndex);
	}
	TYPE& GetAt(int nIndex) const
	{
		return at(nIndex);
	}
	void SetAt(int nIndex, const TYPE& data)
	{
		TYPE& x = at(nIndex);
		x = data;
	}
	iterator GetData() const
	{
		return begin();
	}
	iterator GetData()
	{
		return begin();
	}
 
	//TODO: (STL) Growing the Array
	int Add(const TYPE& x)
	{
		push_back(x);
		return GetUpperBound();
	}
	int Append(const CArrayT<TYPE>& src)
	{
		int nOldSize = this.GetSize();
		int nCount = src.GetSize();
		SetSize(nOldSize + nCount);
		for (int i=nOldSize; i<(nOldSize+nCount); i++)
			inherited::operator[](i) = src[i];
		return nOldSize;
	}
	void Copy(const CArrayT<TYPE>& src)
	{
		int nCount = src.GetSize();
		SetSize(nCount);
		for (int i=0; i<nCount; i++)
			inherited::operator[](i) = src[i];
 
	}
	//void SetAtGrow( int nIndex, TYPE& newElement)	{	}
 
	//TODO: (STL) Insertion/Removal
	void InsertAt(int uIndex, const TYPE& x)
	{
		insert(GetIterFromIndex(uIndex), x);
	}
	void RemoveAt(int uIndex)
	{
		erase(GetIterFromIndex(uIndex));
	}
 
	//TODO: (STL) Operators
	TYPE& operator[](int iIndex)
	{
		return inherited::operator[](iIndex);
	}
	TYPE operator[](int iIndex) const
	{
		return inherited::operator[](iIndex);
	}
 
	//TODO: (STL) Sort
	void Sort(bool bAscending)
	{
		if (bAscending)
			std::sort( begin(), end(), std::less<TYPE> ());
		else
			std::sort( begin(), end(), std::greater<TYPE> ());
	}
};
 
#endif	// for STL
 
#endif	// __ARRAYT_H__ (whole file)