////////////////////////////////////////////////////////////////////////////// //文件 TMap.h //主要功能: // map模块类 #pragma once #include using namespace std; namespace NSet { template class TMap { public: // Construction TMap(); ~TMap(); // Attributes // number of elements int GetCount() const; int GetSize() const; bool IsEmpty() const; // Lookup bool Lookup(KEY key, VALUE& rValue) const; bool HasKey(KEY key); // Operations // Lookup and add if not there VALUE& operator[](KEY key); // add a new (key, value) pair,当已有增加的关键字时,增加将失败 bool Add(KEY key, VALUE newValue); // 为已有关键字设置一个新值,老值将被delete掉 bool SetAt(KEY key, VALUE newValue); // removing existing (key, ?) pair bool RemoveKey(KEY key); void RemoveAll(); void DeepErase(); //深度空间释放,包括指针指向的空间 // iterating all (key, value) pairs POSITION GetAtPositon(KEY key); POSITION GetStartPosition() /*const*/; bool GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) /*const*/; protected: typedef std::map _MAP; _MAP m; typename _MAP::iterator iterm; }; ///////////////////////////////////////////////////////////////////////////// //类实现 template TMap::TMap() { } template TMap::~TMap() { RemoveAll(); } template int TMap::GetCount() const { return GetSize(); } template int TMap::GetSize() const { return (int)m.size(); } template bool TMap::IsEmpty() const { return m.empty(); } template bool TMap::Lookup(KEY key, VALUE& rValue) const { typename _MAP::const_iterator it = m.find(key); if(it == m.end()) return false; rValue = (VALUE)(it->second); return true; } template bool TMap::HasKey(KEY key) { typename _MAP::const_iterator it = m.find(key); if(it == m.end()) return false; return true; } template VALUE& TMap::operator[](KEY key) { return m[key]; } template bool TMap::Add(KEY key, VALUE newValue) { //当map中已有插入的关键字时,insert操作是插入不了数据的 if(!HasKey(key)) { m[key] = newValue; return true; } return false; } template bool TMap::SetAt(KEY key, VALUE newValue) { iterm = m.find(key); if(iterm == m.end()) { return Add(key, newValue); } ////当VALUE类型为void*时,删除就会不正确 //VALUE val = (VALUE)(iterm->second); //if(val) // delete val; iterm->second = newValue; return true; } template bool TMap::RemoveKey(KEY key) { return m.erase(key) ? true:false; } template void TMap::RemoveAll() { m.clear(); } template void TMap::DeepErase() { KEY key; VALUE value; POSITION pos = GetStartPosition(); while(pos != NULL) { GetNextAssoc(pos, key, value); delete value; } RemoveAll(); } template POSITION TMap::GetAtPositon(KEY key) { iterm = m.find(key); if(iterm == m.end()) { return NULL; } return (POSITION)(&iterm); } template POSITION TMap::GetStartPosition() /*const*/ { iterm = m.begin(); if(iterm == m.end()) return NULL; return (POSITION)(&iterm); } template bool TMap::GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) /*const*/ { #ifndef _GDF_QT ENSURE(rNextPosition != NULL); #else Q_ASSERT(rNextPosition); #endif iterm = *(typename _MAP::iterator*)rNextPosition; if(iterm != m.end()) { rKey = iterm->first; rValue = (VALUE)iterm->second; iterm++; if(iterm == m.end()) { rNextPosition = NULL; } else { rNextPosition = (POSITION)(&iterm); } return true; } return false; } ////////////////////////////////////////////////////////////////// //字符串与指针对象映射定义 #ifndef _GDF_QT typedef TMap TMapStringToPtr; //属性列表 #endif }; //namespace using namespace NSet;