C++利用开散列哈希表封装unordered_set,unordered_map
一.前言
1.之前我们已经实现了开散列的哈希表,今天我们来用它封装unordered_set,unordered_map
2.本文的封装比利用红黑树封装set和map更加复杂
建议大家先去看我的红黑树封装set和map再来看本文
因为有很多地方跟红黑树封装set和map时是同样的思路和方法,所以本文不会太去赘述一遍
1.开散列的哈希表完整代码
namespace hash_bucket
{
//HashFunc<int>
template<class K>
//整型的哈希函数
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
//HashFunc<string>
//string的哈希函数
template<>
struct HashFunc<string>
{
size_t operator()(const string& key)
{
// BKDR
size_t hash = 0;
for (auto e : key)
{
hash *= 131;
hash += e;
}
return hash;
}
};
template<class K, class V>
struct HashNode
{
HashNode* _next;
pair<K, V> _kv;
HashNode(const pair<K, V>& kv)
:_kv(kv)
, _next(nullptr)
{
}
};
template<class K, class V, class Hash = HashFunc<K>>
class HashTable
{
typedef HashNode<K, V> Node;
public:
HashTable()
{
_tables.resize(10);
}
~HashTable()
{
for (int i = 0; i < _tables.size(); i++)
{
Node* cur = _tables[i];
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_tables[i] = nullptr;
}
}
bool Insert(const pair<K, V>& kv)
{
//先查找在不在
//如果在,返回false,插入失败
if (Find(kv.first))
{
return false;
}
//扩容
if (_n == _tables.size())
{
//开辟新的哈希表
HashTable newtable;
int newcapacity = _tables.size() * 2;
//扩2倍
newtable._tables.resize(newcapacity);
//转移数据
for (int i = 0; i < _tables.size(); i++)
{
Node* cur = _tables[i];
while (cur)
{
Node* next = cur->_next;
int hashi = hash(cur->_kv.first) % newtable._tables.size();
cur->_next = newtable._tables[hashi];
newtable._tables[hashi] = cur;
cur = next;
}
//防止出现野指针导致重复析构...
_tables[i] = nullptr;
}
//交换两个vector,从而做到交换两个哈希表
//通过学习vector的模拟实现,我们知道vector进行交换时只交换first,finish,end_of_storage
_tables.swap(newtable._tables);
}
//1.利用哈希函数计算需要插入到那个桶里面
int hashi = hash(kv.first) % _tables.size();
//头插
Node* newnode = new Node(kv);
newnode->_next = _tables[hashi];
_tables[hashi] = newnode;
++_n;
return true;
}
Node* Find(const K& key)
{
int hashi = hash(key) % _tables.size();
Node* cur = _tables[hashi];
while (cur)
{
if (cur->_kv.first == key)
{
return cur;
}
cur = cur->_next;
}
return nullptr;
}
bool Erase(const K& key)
{
int hashi = hash(key) % _tables.size();
Node* cur = _tables[hashi], * prev = nullptr;
while (cur)
{
if (cur->_kv.first == key)
{
if (cur == _tables[hashi])
{
_tables[hashi] = cur->_next;
}
else
{
prev->_next = cur->_next;
}
return true;
}
prev = cur;
cur = cur->_next;
}
return false;
}
private:
//哈希表是一个指针数组
vector<Node*> _tables;
size_t _n = 0;
Hash hash;
};
}
二.模板参数
1.HashNode的改造
因为unordered_set是Key模型的容器
unordered_map是Key-Value模型的容器,所以需要对节点结构体进行改造
template<class V>
struct HashNode
{
HashNode* _next;
V _kv;
HashNode(const V& kv)
:_kv(kv)
, _next(nullptr)
{
}
};
2.封装unordered_set和unordered_map的第一步
1.对于模板参数V:
如果是unordered_set:传入底层哈希表的就是Key,Key
如果是unordered_map:传入底层哈希表的就是Key,pair<const Key,Value>
2.为了取出关键字Key,需要传入仿函数
如果是unordered_set:仿函数返回Key
如果是unordered_map:仿函数返回pair<const Key,Value>的first
3.哈希函数需要传给unordered_set和unordered_map
由unordered_set和unordered_map传给底层的哈希表
1.unordered_set
namespace hash_bucket
{
template<class K ,class Hash = HashFunc<K>>
class unordered_set
{
struct SetofKey
{
const K& operator()(const K& k)
{
return k;
}
};
private:
HashTable<K, K,SetofKey,Hash> _ht;
};
}
2.unordered_map
namespace hash_bucket
{
template<class K,class V, class Hash = HashFunc<K>>
class unordered_map
{
struct MapofKey
{
const K& operator()(const pair<const K, V>& k)
{
return k.first;
}
};
private:
HashTable<K, pair<const K, V>, MapofKey,Hash> _ht;
};
}
3.HashTable
哈希表增加模板参数
1.K:就是关键字
2.V:就是具体存放的数据类型(unordered_set就是Key , unordered_map就是pair<const Key,Value>)
3.KeyofT:不同容器传入的取出其关键字的仿函数
如果是unordered_set:仿函数返回Key
如果是unordered_map:仿函数返回pair<const Key,Value>的first
4.Hash:仿函数,哈希函数,用于计算下标的
template<class K, class V,class KeyofT, class Hash>
class HashTable
{
......
private:
//哈希表是一个指针数组
vector<Node*> _tables;
size_t _n = 0;
Hash hash;//哈希函数的仿函数对象
KeyofT _kot;//KeyofT的仿函数对象
};
三.string的哈希函数的模板特化
因为string类型的哈希映射太常用了,
所以这里使用了模板特化,以免每次要存放string时都要指名传入string的哈希函数
//HashFunc<int>
template<class K>
//整型的哈希函数
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
//HashFunc<string>
//string的哈希函数
template<>
struct HashFunc<string>
{
size_t operator()(const string& key)
{
// BKDR
size_t hash = 0;
for (auto e : key)
{
hash *= 131;
hash += e;
}
return hash;
}
};
四.迭代器类
1.这里的哈希表只支持正向迭代器,不支持反向迭代器
1.operator++运算符重载
1.动图演示+分析
++有2种情况:
1.如果当前节点所在的当前哈希桶的后面还有节点,那么直接走到next节点即可
如果当前节点所在的当前哈希桶的后面没有节点了,那么就要走到下一个不为空的哈希桶才可以
如果后面没有不为空的哈希桶了,返回nullptr
2.需要哈希表的地址,怎么办?
我们可以在迭代器里面加入一个哈希表的指针,要求你给我传入你这个哈希表的地址,让我找到你这个哈希表(其实也可以加入一个vector<Node*>的指针,这样就不用传入哈希表指针了,这里以传入哈希表指针来演示,为了介绍如何解决双向依赖问题和友元声明问题)
同时也可以加入一个_hashi代表当前迭代器位于哈希表当中的下标
不过我们发现:
此时出现了一种鸡生蛋,蛋生鸡的双向依赖问题了
我们的迭代器有一个成员:哈希表的指针
哈希表有一个typedef后的类型:迭代器
我们之前的vector,list,set,map的迭代器都是单向依赖关系
只存在容器依赖迭代器而已,可是这里容器和迭代器双向依赖啊,怎么办呢?
1.解决双向依赖问题
我们可以将哈希表前置声明一下
//HashTable的前置声明
template<