用哈希表实现CMap
#pragma once
#define NULL 0
template<typename VALUE,typename KEY,typename ARG_VALUE = const VALUE&,typename ARG_KEY = const KEY&>
class CMap
{
protected:
struct SNode
{
VALUE value;
KEY key;
SNode *pNext;
};
private:
SNode * *m_pHaxi;
int m_nCount;
public:
CMap(const int Count = 10);
~CMap();
void RemoveAll();
bool RemoveAt(ARG_KEY key);
void SetAt(ARG_KEY key, ARG_VALUE value);
bool LookUp(ARG_KEY key, VALUE &value);
VALUE &operator[](ARG_KEY key);
};
template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
typename ARG_KEY = const KEY&>
CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::CMap(const int Count)
{
m_nCount = Count;
m_pHaxi = new SNode*[Count];
int i = 0;
while (i < Count)
m_pHaxi[i++] = NULL;
}
template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
typename ARG_KEY = const KEY&>
CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::~CMap()
{
RemoveAll();
}
template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
typename ARG_KEY = const KEY&>
void CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::RemoveAll()
{
int i = 0;
while (i < m_nCount)
{
SNode *p = m_pHaxi[i], *Del;
while (p)
{
Del = p;
p = p->pNext;
delete Del;
}
m_pHaxi[i++] = NULL;
}
}
template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
typename ARG_KEY = const KEY&>
bool CMap<VALUE, KEY, ARG_VALUE, ARG_KEY> ::RemoveAt(ARG_KEY key)
{
//方法一:
//int n = key % m_nCount;
//SNode *p = m_pHaxi[n], *Del = p;
要删除头结点
//if (p->key == key)
//{
// m_pHaxi[n] = p->pNext;
// delete p;
// return true;
//}
不存在该链表
//else if (!p)
// return false;
//while (p)
//{
// p = p->pNext;
// if (p->key == key)
// {
// Del->pNext = p->pNext;
// delete p;
// return true;
// }
// Del = Del->pNext;
//}
//return false;
//方法二:
int n = key % m_nCount;
//判断头结点是否存在
if (!m_pHaxi[n])
return false;
SNode* *p = &m_pHaxi[n];
while (*p)
{
if ((*p)->key == key)
break;
p = &((*p)->pNext);
}
if (!*p)
return false;
SNode *p1 = *p;
*p = (*p)->pNext;
delete[]p1;
return true;
}
template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
typename ARG_KEY = const KEY&>
void CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::SetAt(ARG_KEY key, ARG_VALUE value)
{
//方法一:尾插法
//int n = key % m_nCount;
//SNode *pNew = new SNode;
//pNew->value = value;
//pNew->key = key;
//pNew->pNext = m_pHaxi[n];
//m_pHaxi[n] = pNew;
//头插法
(*this)[key] = value; //调用operator函数
}
template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
typename ARG_KEY = const KEY&>
bool CMap<VALUE, KEY, ARG_VALUE, ARG_KEY>::LookUp(ARG_KEY key, VALUE &value)
{
int n = key % m_nCount;
SNode *p = m_pHaxi[n];
while (p)
{
if (p->key == key)
{
value = p->value;
return true;
}
p = p->pNext;
}
return false;
}
template<typename VALUE, typename KEY, typename ARG_VALUE = const VALUE&,
typename ARG_KEY = const KEY&>
VALUE &CMap<VALUE, KEY, ARG_VALUE, ARG_KEY> ::operator[](ARG_KEY key)
{
int n = key % m_nCount;
SNode* *p = &m_pHaxi[n];
while (*p)
{
if ((*p)->key == key)
return (*p)->value;
p = &((*p)->pNext);
}
SNode *pNew = new SNode;
pNew->key = key;
pNew->pNext = NULL;
*p = pNew;
return pNew->value;
}