⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 templates.h

📁 类似VC自带的那个windeff,来源于codeproject
💻 H
📖 第 1 页 / 共 2 页
字号:
	T&		operator[](int nIndex);

	// Operations that move elements around
	void	InsertAt(int nIndex, T newElement, int nCount = 1);
	void	InsertSorted(T newElement);
	void	RemoveAt(int nIndex, int nCount = 1);
	void	InsertAt(int nStartIndex, CTemplateObjArray* pNewArray);
	void	Remove(T oldElement);

// Implementation
protected:
	T*  m_pData;   // the actual array of data
	int m_nSize;     // # of elements (upperBound - 1)
	int m_nMaxSize;  // max allocated
	int m_nGrowBy;   // grow amount

};


template <class T> CTemplateObjArray<T>::CTemplateObjArray()
{
	m_pData = NULL;
	m_nSize = m_nMaxSize = m_nGrowBy = 0;
}

template <class T> CTemplateObjArray<T>::~CTemplateObjArray()
{
	delete[] m_pData;
	m_pData = NULL;
}

template <class T> int CTemplateObjArray<T>::GetSize() const
{ 
	return m_nSize; 
}

template <class T> int CTemplateObjArray<T>::GetUpperBound() const
{ 
	return m_nSize-1; 
}

template <class T> void CTemplateObjArray<T>::SetSize(int nNewSize, int nGrowBy)
{
	if (nNewSize < 0)
		return;

	if (nGrowBy != -1)
		m_nGrowBy = nGrowBy;  // set new size

	if (nNewSize == 0)
	{
		// shrink to nothing
		delete[] m_pData;
		m_pData = NULL;
		m_nSize = m_nMaxSize = 0;
	}
	else if (m_pData == NULL)
	{
		// create one with exact size
		m_pData = new T[nNewSize];

		m_nSize = m_nMaxSize = nNewSize;
	}
	else if (nNewSize <= m_nMaxSize)
	{
		// it fits
		if (nNewSize > m_nSize)
		{
			T empty;
			for (int i=m_nSize; i<nNewSize; i++)
				m_pData[i] = empty;
		}

		m_nSize = nNewSize;
	}
	else
	{
		// otherwise, grow array
		int nGrowBy = m_nGrowBy;
		if (nGrowBy == 0)
		{
			// heuristically determine growth when nGrowBy == 0
			//  (this avoids heap fragmentation in many situations)
			nGrowBy = (m_nSize/8 > 4)? m_nSize/8 : 4;
		}
		int nNewMax;
		if (nNewSize < m_nMaxSize + nGrowBy)
			nNewMax = m_nMaxSize + nGrowBy;  // granularity
		else
			nNewMax = nNewSize;  // no slush

		T* pNewData = new T[nNewMax];
		
		for (int i=0; i<m_nSize; i++)
		{
			pNewData[i] = m_pData[i]; // = operator required on T class
		}

		// get rid of old stuff (note: no destructors called)
		delete[] m_pData;
		m_pData = pNewData;
		m_nSize = nNewSize;
		m_nMaxSize = nNewMax;
	}
}


template <class T> void CTemplateObjArray<T>::FreeExtra()
{
	if (m_nSize != m_nMaxSize)
	{
		// shrink to desired size
		T* pNewData = NULL;
		if (m_nSize != 0)
		{
			// copy new data from old
			pNewData =  new T[m_nSize];
			for (int i=0; i<m_nSize; i++)
				pNewData[i] = m_pData[i];
		}

		// get rid of old stuff (note: no destructors called)
		delete[] m_pData;
		m_pData = pNewData;
		m_nMaxSize = m_nSize;
	}
}

template <class T> void CTemplateObjArray<T>::RemoveAll()
{ 
	SetSize(0); 
}

template <class T> T CTemplateObjArray<T>::GetAt(int nIndex) const
{ 
	T empty;
	if (nIndex<0 || nIndex>=m_nSize)
		return empty;
	else
		return m_pData[nIndex]; 
}

template <class T> void CTemplateObjArray<T>::SetAt(int nIndex, T newElement)
{ 
	if (nIndex >= 0 && nIndex < m_nSize)
		m_pData[nIndex] = newElement; 
}

template <class T> T& CTemplateObjArray<T>::ElementAt(int nIndex)
{ 
	static T pVoid=NULL;

	if (nIndex<0 || nIndex>=m_nSize)
		return pVoid;
	else
		return m_pData[nIndex]; 
}


template <class T> void CTemplateObjArray<T>::SetAtGrow(int nIndex, T newElement)
{
	if (nIndex < 0)
		return;

	if (nIndex >= m_nSize)
		SetSize(nIndex+1);
	m_pData[nIndex] = newElement;
}

template <class T> int CTemplateObjArray<T>::Add(T newElement)
{ 
	int nIndex = m_nSize;
	SetAtGrow(nIndex, newElement);
	return nIndex; 
}

template <class T> void CTemplateObjArray<T>::Remove(T oldElement)
{ 
	for( int i = 0; i < m_nSize; i++)
	{
		if( m_pData[i] == oldElement)
		{
			if( i < m_nSize - 1)
				m_pData[i] = m_pData[i+1];
			m_nSize--;
			return;
		}
	}
}

template <class T> int CTemplateObjArray<T>::Append(const CTemplateObjArray& src)
{
	int nOldSize = m_nSize;
	
	if (this != &src)
	{
		SetSize(m_nSize + src.m_nSize);

		long nSize = src.m_nSize;
		for (int i=0; i<nSize; i++)
			m_pData[i+nOldSize] = src[i];
	}
	return nOldSize;
}

template <class T> void CTemplateObjArray<T>::Copy(const CTemplateObjArray& src)
{
	if (this == &src)
		return;

	SetSize(src.m_nSize);

	long nSize = src.m_nSize;
	for (int i=0; i<nSize; i++)
		m_pData[i] = src[i];
}

template <class T> T CTemplateObjArray<T>::operator[](int nIndex) const
{ 
	return GetAt(nIndex); 
}

template <class T> T& CTemplateObjArray<T>::operator[](int nIndex)
{ 
	return ElementAt(nIndex); 
}


template <class T> void CTemplateObjArray<T>::InsertAt(int nIndex, T newElement, int nCount)
{
	if (nIndex<0 || nCount<=0)
		return;

	if (nIndex >= m_nSize)
	{
		// adding after the end of the array
		SetSize(nIndex + nCount);  // grow so nIndex is valid
	}
	else
	{
		// inserting in the middle of the array
		int nOldSize = m_nSize;
		SetSize(m_nSize + nCount);  // grow it to new size

		int i;
		for (i=nOldSize-1; i>=nIndex; i--)
		{
			m_pData[i+nCount] = m_pData[i];
		}

		// re-init slots we copied from
	
		T empty;

		for (i=nIndex; i<nIndex+nCount; i++)
			m_pData[i] = empty;

	}

	// insert new value in the gap
	while (nCount-- && (nIndex + nCount <= m_nSize))
		m_pData[nIndex++] = newElement;
}


template <class T> void CTemplateObjArray<T>::RemoveAt(int nIndex, int nCount)
{
	if (nIndex >= 0 || nCount >= 0 || (nIndex + nCount <= m_nSize) )
	{
		// just remove a range
		int nMoveCount = m_nSize - (nIndex + nCount);

		if (nMoveCount)
		{

			int i;
			for (i=nIndex; i<nIndex+nMoveCount; i++)
			{
				m_pData[i] = m_pData[i+nCount];
			}

			T empty;
			for (i=m_nSize-nCount; i<m_nSize; i++)
				m_pData[i] = empty;

		}

		m_nSize -= nCount;
	}
}


template <class T> void CTemplateObjArray<T>::InsertAt(int nStartIndex, CTemplateObjArray* pNewArray)
{
	if (pNewArray && nStartIndex>=0 && pNewArray->GetSize() > 0)
	{
		InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
		for (int i = 0; i < pNewArray->GetSize(); i++)
			SetAt(nStartIndex + i, pNewArray->GetAt(i));
	}
}


/////////////////////////////////////////////////////////////////////////////


typedef CTemplateArray<void*> ArrayLPVoid;
typedef CTemplateArray<long>  ArraySLong;
typedef CTemplateArray<long>  ArrayInt;
typedef CTemplateArray<char*>  ArrayString;

class CSortedArray : public CTemplateObjArray<CString>
{
	// Methods
public:

	// InsertSorted
	//
	// purpose : this methods enhances InsertAt(i, szNewElement) by automatically
	//           providing a sorting mechanism ( o(n2) ).
	//
	void InsertSorted(CString &szNewElement)
	{
		if (m_nSize==0)
		{
			Add( szNewElement );
			return;
		}

		long nbSize = GetSize();
		for (long i=0; i<nbSize; i++)
		{
			if (GetAt(i).CompareNoCase( szNewElement )<0)
			{
				InsertAt(i, szNewElement);
				return;
			}
		}

		// no insertion, so add it
		Add( szNewElement );
	
	}
};


struct SINGLEVALUEPARAM
{
	CString szName, szValue;
};



struct DOUBLEVALUEPARAM
{
	CString m_pParamName;
	CString m_pParamValue1, m_pParamValue2;
};

typedef CTemplateAutoArray<SINGLEVALUEPARAM*>	Param1Array;
typedef CTemplateAutoArray<DOUBLEVALUEPARAM*>	Param2Array;




class FindableArray : public CTemplateAutoArray<SINGLEVALUEPARAM*>
{
public:
	CString GetValue(/*in*/char *szName, BOOL bUseCase=FALSE)
	{
		CString s;
		long nSize = GetSize();
		if (nSize==0) return s;

		BOOL bFound = FALSE;
		long i=0;
		
		while (!bFound && i<nSize)
		{
			SINGLEVALUEPARAM *p = GetAt(i++);
			if (p)
				bFound = bUseCase ? p->szName.Compare(szName) : p->szName.CompareNoCase(szName) ;
		}
		i--;
		if (bFound)
		{
			SINGLEVALUEPARAM *p = GetAt(i++);
			if (p)
				s = p->szValue;
		}

		return s;
	}
};









class DomXmlElement;
typedef CTemplateAutoArray<DomXmlElement*>			XmlElementArray;

/////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -