擴展及加強CMap

1.可支持CString和LPCTSTR做鍵值

2.當值爲指針類型時,該MAP能進行自析構(可選的,可以不讓它自動析構,默認爲自動析構),即在MAP析構時針對每一個值進行delete操作。

 3.添加了Find,RFind,CopyTo,ReleaseAll 常用函數。

 

可根據 CMapT2T(值爲一般類型)和 CMapT2P(值爲指針類型),方便構建自定義的Map類型,

已經預定義了:

typedef  CMapT2T<int,int> CMapN2N;
typedef  CMapT2T<int,CString> CMapN2S;
template<typename T_val> class CMapN2P:public CMapT2P<int,T_val>{};
template<typename T_val> class CMapS2P:public CMapT2P<CString,T_val>{};

 

用法如:CMapS2P<MyClass*> mapMyClass;//這是個自動析構的MAP

 

///////////////////////////////////////////////// Extends CMap 
template<>
inline UINT AFXAPI HashKey<LPCTSTR>(LPCTSTR key)
{
	LPCTSTR lpszKey = key;
	UINT nHash = 0;
	while (*lpszKey)
		nHash = (nHash<<5) + nHash + *lpszKey++;
	return nHash;
}
template<> inline UINT AFXAPI HashKey<CString>(CString key){return HashKey<LPCTSTR>(key);}

template<typename T_map,typename T_key,typename T_val>
inline void MapReleaseAll(T_map& map)
{
	POSITION   pos = map.GetStartPosition();    
	T_key key;	T_val val;
	while(pos != NULL){
		map.GetNextAssoc(pos,key,val);
		delete ((T_val*)val);
	} 	
	map.RemoveAll();
}
template<typename T>
inline bool IsIntegerType()
{
	if (typeid(int)==typeid(T) || typeid(long)==typeid(T) || typeid(UINT)==typeid(T)|| typeid(ULONG)==typeid(T))
		return true;
	return false;
}

template<typename T_map,typename T_key,typename T_val>
inline T_val MapFind(T_map &map,T_key key,bool *pIsFind=NULL){
	T_val ret;
	if(pIsFind)*pIsFind=true;
	if(!map.Lookup(key,ret)){
		if(pIsFind)*pIsFind=false;
	}
	return ret;
}
template<typename T_map,typename T_key,typename T_val>
inline T_key MapRFind(T_map&map,T_val val,bool *pIsFind){
	if(!pIsFind)
		ASSERT(FALSE);
	POSITION pos = map.GetStartPosition();
	T_key keyTemp;		T_val valTemp;
	*pIsFind=false;
	while(pos){
		map.GetNextAssoc(pos, keyTemp, valTemp);
		if (val==valTemp){
			*pIsFind=true;
			break;
		}
	}
	return keyTemp;
}

template<typename T_key,typename T_val >
class  CMapT2X:public CMap<T_key ,T_key,T_val,T_val>{
public:
	CMapT2X(){}
	virtual~CMapT2X(void){RemoveAll();}
	bool Add(T_key key,T_val val){(*this)[key]=val ;return true;}
	T_val Find(T_key key,bool *pIsFind=NULL){
		T_val ret;
		if(pIsFind)*pIsFind=true;
		if(!Lookup(key,ret)){
			if(pIsFind)*pIsFind=false;
		}
		return ret;
	}
	T_key RFind(T_val val ,bool& isFind){return MapRFind<CMapT2X<T_key,T_val>,T_key,T_val>(*this, val,&isFind);}
	bool CopyTo(CMapT2X<T_key,T_val> &mapTo){
		mapTo.nStart=this->nStart;
		mapTo.nEnd=this->nEnd;
		mapTo.nDefault=this->nDefault;
		mapTo.name=this->name;
		POSITION pos = GetStartPosition();
		T_key    keyTemp;
		T_val   valTemp;
		while(pos){
			GetNextAssoc(pos, keyTemp, valTemp);
			mapTo[keyTemp]=valTemp;
		}
		return true;
	}

	/*
	virtual void ReleaseAll(){
			return RemoveAll();
		}*/
	
public:
	int nStart;//nStart,nEnd,nDefault僅針對T_key是整形類型時纔有意義
	int nEnd;
	int nDefault;
	CString name;
};

template<typename T_key,typename T_val> 
class  CMapT2P:public CMapT2X<T_key ,T_val>{//當值的類型是指針時,可以根據bAutoDestruct是否讓本MAP自析構
public:
	T_val Find(T_key key,bool *pIsFind=NULL){
		T_val ret;
		if(!Lookup(key,ret)){
			if(pIsFind)*pIsFind=false;
			ret=NULL;
		}
		return ret;
	}
	void ReleaseAll()
	{
		POSITION   pos = GetStartPosition();    
		T_key key;	T_val val;
		while(pos != NULL){
			GetNextAssoc(pos,key,val);
			if(val!=NULL)delete (val);
		} 	
		RemoveAll();
	}
};
typedef  CMapT2X<int,int> CMapN2N;
typedef  CMapT2X<int,CString> CMapN2S;
typedef  CMapT2X<CString,CString> CMapS2S;
typedef  CMapT2X<CString,int> CMapS2N;
template<typename T_val> class CMapN2P:public CMapT2P<int,T_val>{};
template<typename T_val> class CMapS2P:public CMapT2P<CString,T_val>{};
///////////////////////////////////////////////// END Extends CMap 

發佈了67 篇原創文章 · 獲贊 10 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章