You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
4.3 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//////////////////////////////////////////////////////////////////////////////
//文件 TMap.h
//主要功能:
// map模块类
#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当已有增加的关键字时增加将失败
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<KEY, VALUE> _MAP;
_MAP m;
typename _MAP::iterator iterm;
};
/////////////////////////////////////////////////////////////////////////////
//类实现
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)
{
//当map中已有插入的关键字时insert操作是插入不了数据的
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);
}
////当VALUE类型为void*时,删除就会不正确
//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;
}
//////////////////////////////////////////////////////////////////
//字符串与指针对象映射定义
#ifndef _GDF_QT
typedef TMap<CString, void*> TMapStringToPtr; //属性列表
#endif
}; //namespace
using namespace NSet;