|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
//<2F>ļ<EFBFBD> TMap.h
|
|
|
|
|
|
//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>:
|
|
|
|
|
|
// mapģ<70><C4A3><EFBFBD><EFBFBD>
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
namespace NSet
|
|
|
|
|
|
{
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
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<69><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵĹؼ<C4B9><D8BC><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD>ʧ<EFBFBD><CAA7>
|
|
|
|
|
|
bool Add(KEY key, VALUE newValue);
|
|
|
|
|
|
// Ϊ<><CEAA><EFBFBD>йؼ<D0B9><D8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>delete<74><65>
|
|
|
|
|
|
bool SetAt(KEY key, VALUE newValue);
|
|
|
|
|
|
|
|
|
|
|
|
// removing existing (key, ?) pair
|
|
|
|
|
|
bool RemoveKey(KEY key);
|
|
|
|
|
|
void RemoveAll();
|
|
|
|
|
|
void DeepErase(); //<2F><><EFBFBD>ȿռ<C8BF><D5BC>ͷţ<CDB7><C5A3><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD>Ŀռ<C4BF>
|
|
|
|
|
|
|
|
|
|
|
|
// 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<KEY, VALUE> _MAP;
|
|
|
|
|
|
_MAP m;
|
|
|
|
|
|
typename _MAP::iterator iterm;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
//<2F><>ʵ<EFBFBD><CAB5>
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
TMap<KEY, VALUE>::TMap()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
TMap<KEY, VALUE>::~TMap()
|
|
|
|
|
|
{
|
|
|
|
|
|
RemoveAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
int TMap<KEY, VALUE>::GetCount() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetSize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
int TMap<KEY, VALUE>::GetSize() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return (int)m.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
bool TMap<KEY, VALUE>::IsEmpty() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m.empty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
bool TMap<KEY, VALUE>::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<class KEY, class VALUE>
|
|
|
|
|
|
bool TMap<KEY, VALUE>::HasKey(KEY key)
|
|
|
|
|
|
{
|
|
|
|
|
|
typename _MAP::const_iterator it = m.find(key);
|
|
|
|
|
|
if(it == m.end())
|
|
|
|
|
|
return false;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
VALUE& TMap<KEY, VALUE>::operator[](KEY key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m[key];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
bool TMap<KEY, VALUE>::Add(KEY key, VALUE newValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
//<2F><>map<61><70><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD>Ĺؼ<C4B9><D8BC><EFBFBD>ʱ<EFBFBD><CAB1>insert<72><74><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2>벻<EFBFBD><EBB2BB><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>
|
|
|
|
|
|
if(!HasKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
m[key] = newValue;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
bool TMap<KEY, VALUE>::SetAt(KEY key, VALUE newValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
iterm = m.find(key);
|
|
|
|
|
|
if(iterm == m.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return Add(key, newValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
////<2F><>VALUE<55><45><EFBFBD><EFBFBD>Ϊvoid*ʱ<><CAB1>ɾ<EFBFBD><C9BE><EFBFBD>ͻ<CDBB><E1B2BB>ȷ
|
|
|
|
|
|
//VALUE val = (VALUE)(iterm->second);
|
|
|
|
|
|
//if(val)
|
|
|
|
|
|
// delete val;
|
|
|
|
|
|
iterm->second = newValue;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
bool TMap<KEY, VALUE>::RemoveKey(KEY key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m.erase(key) ? true:false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
void TMap<KEY, VALUE>::RemoveAll()
|
|
|
|
|
|
{
|
|
|
|
|
|
m.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
void TMap<KEY, VALUE>::DeepErase()
|
|
|
|
|
|
{
|
|
|
|
|
|
KEY key;
|
|
|
|
|
|
VALUE value;
|
|
|
|
|
|
POSITION pos = GetStartPosition();
|
|
|
|
|
|
while(pos != NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
GetNextAssoc(pos, key, value);
|
|
|
|
|
|
delete value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RemoveAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
POSITION TMap<KEY, VALUE>::GetAtPositon(KEY key)
|
|
|
|
|
|
{
|
|
|
|
|
|
iterm = m.find(key);
|
|
|
|
|
|
if(iterm == m.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (POSITION)(&iterm);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
POSITION TMap<KEY, VALUE>::GetStartPosition() /*const*/
|
|
|
|
|
|
{
|
|
|
|
|
|
iterm = m.begin();
|
|
|
|
|
|
if(iterm == m.end())
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
return (POSITION)(&iterm);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class KEY, class VALUE>
|
|
|
|
|
|
bool TMap<KEY, VALUE>::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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
//<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>䶨<EFBFBD><E4B6A8>
|
|
|
|
|
|
#ifndef _GDF_QT
|
|
|
|
|
|
typedef TMap<CString, void*> TMapStringToPtr; //<2F><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
}; //namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace NSet;
|