www.pudn.com > WinGOS.rar > template.h


// TEMPLATE.cpp: implementation of the TEMPLATE class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#ifndef _GOS_TEMPLATE_H_ 
#define _GOS_TEMPLATE_H_ 
 
///////////////////////////////////////////////////////////////////////////// 
// CArray 
 
template 
class CArray 
{ 
public: 
// Construction 
	CArray(); 
	~CArray(); 
 
// Attributes 
	int GetSize() const; 
	BOOL IsEmpty() const; 
	void SetSize(int nNewSize); 
 
// Operations 
	// Clean up 
	void RemoveAll(); 
 
	// Direct Access to the element data (may return NULL) 
	TYPE* GetData(); 
 
	int Add(const TYPE& newElement); 
	int Append(const CArray& src); 
	void Copy(const CArray& src); 
 
	// overloaded operator helpers 
	TYPE& operator[](int nIndex); 
 
	// Operations that move elements around 
	void InsertAt(int nIndex, const TYPE& newElement, int nCount); 
	void RemoveAt(int nIndex, int nCount); 
	void InsertAt(int nIndex, CArray* pNewArray); 
	void InsertAt(int nIndex, const TYPE& newElement); 
	void RemoveAt(int nIndex); 
	void Move(int nIndex,int nNewIndex); 
 
// Implementation 
protected: 
	TYPE* m_pData;   // the actual array of data 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
// CList 
 
template 
class CList 
{ 
protected: 
	struct CNode 
	{ 
		CNode* pNext; 
		CNode* pPrev; 
		TYPE data; 
	}; 
public: 
// Construction 
	/* explicit */ CList(int nBlockSize = 10); 
	~CList(); 
 
// Attributes (head and tail) 
	// count of elements 
	int GetSize() const; 
	BOOL IsEmpty() const; 
 
	// peek at head or tail 
	TYPE& GetHead(); 
	TYPE& GetTail(); 
 
// Operations 
	// get head or tail (and remove it) - don't call on empty list ! 
	TYPE RemoveHead(); 
	TYPE RemoveTail(); 
 
	// add before head or after tail 
	POSITION AddHead(const TYPE &newElement); 
	POSITION AddTail(const TYPE &newElement); 
 
	// add another list of elements before head or after tail 
	void AddHead(CList* pNewList); 
	void AddTail(CList* pNewList); 
 
	// remove all elements 
	void RemoveAll(); 
 
	// iteration 
	POSITION GetHeadPosition() const; 
	POSITION GetTailPosition() const; 
	TYPE& GetNext(POSITION& rPosition); // return *Position++ 
	TYPE& GetPrev(POSITION& rPosition); // return *Position-- 
 
	// getting/modifying an element at a given position 
	TYPE& GetAt(POSITION position); 
	void SetAt(POSITION pos, const TYPE& newElement); 
	void RemoveAt(POSITION position); 
 
	// inserting before or after a given position 
	POSITION InsertBefore(POSITION position, const TYPE& newElement); 
	POSITION InsertAfter(POSITION position, const TYPE& newElement); 
 
	// helper functions (note: O(n) speed) 
	POSITION Find(const TYPE& searchValue, POSITION startAfter = NULL) const; 
		// defaults to starting at the HEAD, return NULL if not found 
	POSITION FindIndex(int nIndex) const; 
		// get the 'nIndex'th element (may return NULL) 
	int GetIndex(POSITION position) const; 
		// get the postion of element (may return -1) 
 
// Implementation 
protected: 
	CNode* m_pNodeHead; 
	CNode* m_pNodeTail; 
	CNode* m_pNodeFree; 
	CPlex* m_pBlocks; 
	int m_nCount; 
	int m_nBlockSize; 
 
	PVOID NewNode(CNode*, CNode*); 
	void FreeNode(CNode*); 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
// CMap 
 
template 
class CMap 
{ 
public: 
	// Association 
	class CAssoc 
	{ 
	public: 
		KEY key; 
		VALUE value; 
		CAssoc* pNext; 
	}; 
 
public: 
// Construction 
	/* explicit */ CMap(int nBlockSize = 10); 
	~CMap(); 
 
// Attributes 
	// number of elements 
	int GetSize() const; 
	BOOL IsEmpty() const; 
 
	// Lookup 
	BOOL Lookup(const KEY& key, VALUE& rValue) const; 
 
// Operations 
	// Lookup and add if not there 
	VALUE& operator[](const KEY& key); 
 
	// add a new (key, value) pair 
	void SetAt(const KEY& key, const VALUE& newValue); 
 
	// removing existing (key, ?) pair 
	BOOL RemoveKey(const KEY& key); 
	void RemoveAll(); 
 
	// iterating all (key, value) pairs 
	POSITION GetStartPosition() const; 
	void GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const; 
 
	// advanced features for derived classes 
	UINT GetHashTableSize() const; 
	void InitHashTable(UINT hashSize); 
 
// Implementation 
protected: 
	CAssoc** m_pHashTable; 
	int m_nCount; 
	CAssoc* m_pFreeList; 
	CPlex* m_pBlocks; 
	int m_nBlockSize; 
 
	CAssoc* NewAssoc(const KEY& key); 
	void FreeAssoc(CAssoc*); 
	PVOID GetAssocAt(const KEY& key, UINT& nHash)const; 
}; 
 
 
#endif // _GOS_TEMPLATE_H_