目录
1 -> unordered系列关联式容器
在c++98中,stl提供了底层为红黑树结构的一系列关联式容器,在查询时效率可达到o(n),即最差情况下需要比较红黑树的高度次,当树中的节点非常多时,查询效率也不理想。最好的查询是进行很少的比较次数就能将元素找到,因此在c++11中,stl又提供了4个unordered系列的关联式容器,这四个容器与红黑树结构的关联式容器使用方式基本类似,只是其底层结构不同。
1.1 -> unordered_map
1.1.1 -> unordered_map的文档介绍
- unordered_map是存储<key,value>键值对的关联式容器,其允许通过keys快速的索引到与其对应的value。
- 在unordered_map中,键值通常用于唯一地标识元素,而映射值是一个对象,其内容与此键关联。键和映射值的类型可能不同。
- 在内部unordered_map没有对<key,value>按照任何特定的顺序排序,为了能在常数范围内找到key所对应的value,unordered_map将相同的哈希值的键值对放在相同的桶中。
- unordered_map容器通过key访问单个元素要比map快,但它通常在遍历元素子集的范围迭代方面效率较低。
- unordered_map实现了直接访问操作符(operator[]),它允许使用key作为参数直接访问value。
- 它的迭代器至少是前向迭代器。
1.1.2 -> unordered_map的接口说明
1. unordered_map的构造
函数声明 | 功能介绍 |
unordered_map | 构造不同格式的unordered_map对象 |
2. unordered_map的容量
函数声明 | 功能介绍 |
bool empty() const | 检测unordered_map是否为空 |
size_t size() const | 获取unordered_map的有效元素个数 |
3. unordered_map的迭代器
函数声明 | 功能介绍 |
begin | 返回unordered_map第一个元素的迭代器 |
end | 返回unordered_map最后一个元素下一个位置的迭代器 |
cbegin | 返回unordered_map第一个元素的const迭代器 |
cend | 返回unordered_map最后一个元素下一个位置的const迭代器 |
4. unordered_map的元素访问
函数声明 | 功能介绍 |
operator[] | 返回与key对应的value,没有一个默认值 |
注意:该函数中实际调用哈希桶的插入操作,用参数key与v()构造一个默认值往底层哈希桶中插入,如果key不在哈希桶中,插入成功,返回v(),插入失败,说明key已经在哈希桶中,将key对应的value返回。
5. unordered_map的查询
函数声明 | 功能介绍 |
iterator find(const k& key) | 返回key在哈希桶中的位置 |
size_t count(const k& key) | 返回哈希桶中关键码为key的键值对的个数 |
注意:unordered_map中key是不能重复的,因此count函数的返回值最大为1。
6. unordered_map的修改操作
函数声明 | 功能介绍 |
insert | 向容器中插入键值对 |
erase | 删除容器中的键值对 |
void clear() | 清空容器中有效元素个数 |
void swap(unordered_map&) | 交换两个容器中的元素 |
7. unordered_map的桶操作
函数声明 | 功能介绍 |
size_t bucket count() const | 返回哈希桶中桶的总个数 |
size_t bucket size(size_t n) const | 返回n号桶中有效元素的总个数 |
size_t bucket(const k& key) | 返回元素key所在的桶号 |
1.2 -> unordered_set
2 -> 底层结构
unordered系列的关联式容器之所以效率比较高,是因为其底层使用了哈希结构。
2.1 -> 哈希概念
顺序结构以及平衡树中,元素关键码与其存储位置之间没有对应的关系,因此在查找一个元素时,必须要经过关键码的多次比较。顺序查找时间复杂度为o(n),平衡树中为树的高度,即o(n),搜索的效率取决于搜索过程中元素的比较次数。
理想的搜索方法:可以不经过任何比较,一次直接从表中得到要搜索的元素。如果构造一种存储结构,通过某种函数(hashfunc)使元素的存储位置与它的关键码之间能够建立——映射的关系,那么在查找时通过该函数可以很快找到该元素。
当向该结构中:
- 插入元素
根据待插入元素的关键码,以此函数计算出该元素的存储位置并按此位置进行存放。
- 搜索元素
对元素的关键码进行同样的计算,把求得的函数值当作元素的存储位置,在结构中按此位置取元素比较,若关键码相等,则搜索成功。
该方式即为哈希(散列)方法,哈希方法中使用的转换函数称为哈希(散列)函数,构造出的结构称为哈希表(hash table)(或者称散列表)。
例如:数据集合{1,7,6,4,5,9};
哈希函数设置为:hash(key) = key % capacity;capacity为存储元素底层空间的总大小。
用该方法进行搜索不必进行多次关键码的比较,因此搜索的速度比较快。
2.2 -> 哈希冲突
不同关键字通过相同哈希函数计算出相同的哈希地址,该种现象称为哈希冲突或哈希碰撞。
把具有不同关键码而具有相同哈希地址的数据元素称为“同义词”。
2.3 -> 哈希函数
引起哈希冲突的一个原因可能是:哈希函数设计不够合理。
哈希函数设计原则:
- 哈希函数的定义域必须包括需要存储的全部关键码,而如果散列表允许有m个地址时,其值域必须在0到m-1之间。
- 哈希函数计算出来的地址能均匀分布在整个空间中。
- 哈希函数应该比较简单。
常见哈希函数
1. 直接定址法--(常用)
取关键字的某个线性函数为散列地址:hash(key)= a*key + b。
优点:简单、均匀。
缺点:需要事先知道关键字的分布情况。
缺点:需要事先知道关键字的分布情况。
2. 除留余数法--(常用)
设散列表中允许的地址数为m,取一个不大于m,但最接近或者等于m的质数p作为除数,按照哈希函数:hash(key) = key% p(p<=m),将关键码转换成哈希地址。
3. 平方取中法
假设关键字为1234,对它平方就是1522756,抽取中间的3位227作为哈希地址;再比如关键字为4321,对它平方就是18671041,抽取中间的3位671(或710)作为哈希地址。
平方取中法比较适合:不知道关键字的分布,而位数又不是很大的情况。
4. 折叠法
折叠法是将关键字从左到右分割成位数相等的几部分(最后一部分位数可以短些),然后将这几部分叠加求和,并按散列表表长,取后几位作为散列地址。
折叠法适合事先不需要知道关键字的分布,适合关键字位数比较多的情况。
5. 随机数法
选择一个随机函数,取关键字的随机函数值为它的哈希地址,即h(key) = random(key),其中random为随机数函数。
通常应用于关键字长度不等时采用此法。
6. 数学分析法
设有n个d位数,每一位可能有r种不同的符号,这r种不同的符号在各位上出现的频率不一定
相同,可能在某些位上分布比较均匀,每种符号出现的机会均等,在某些位上分布不均匀只
有某几种符号经常出现。可根据散列表的大小,选择其中各种符号分布均匀的若干位作为散
列地址。
数字分析法通常适合处理关键字位数比较大的情况,如果事先知道关键字的分布且关键字的若干位分布较均匀的情况。
注意:哈希函数设计的越精妙,产生哈希冲突的可能性就越低,但是无法避免哈希冲突。
2.4 -> 哈希冲突解决
解决哈希冲突两种常见的方法是:闭散列和开散列。
2.4.1 -> 闭散列
闭散列:也叫开放定址法,当发生哈希冲突时,如果哈希表未被装满,说明在哈希表中必然还有
空位置,那么可以把key存放到冲突位置中的“下一个” 空位置中去。
1. 线性探测
比如2.1中的场景,现在需要插入元素44,先通过哈希函数计算哈希地址,hashaddr为4,因此44理论上应该插在该位置,但是该位置已经放了值为4的元素,即发生哈希冲突。
线性探测:从发生冲突的位置开始,依次向后探测,直到寻找到下一个空位置为止。
插入:
- 通过哈希函数获取待插入元素在哈希表中的位置。
- 如果该位置中没有元素则直接插入新元素,如果该位置中有元素发生哈希冲突,使用线性探测找到下一个空位置,插入新元素。
删除:
采用闭散列处理哈希冲突时,不能随便物理删除哈希表中已有的元素,若直接删除元素会影响其他元素的搜索。比如删除元素4,如果直接删除掉,44查找起来可能会受影响。因此线性探测采用标记的伪删除法来删除一个元素。
#define _crt_secure_no_warnings 1
#include <iostream>
using namespace std;
// 哈希表每个空间给个标记
// empty此位置空, exist此位置已经有元素, delete元素已经删除
enum state
{
empty, exist, delete
};
线性探测实现:
#define _crt_secure_no_warnings 1
#include <iostream>
using namespace std;
// 哈希表每个空间给个标记
// empty此位置空, exist此位置已经有元素, delete元素已经删除
enum state
{
empty, exist, delete
};
// 注意:假如实现的哈希表中元素唯一,即key相同的元素不再进行插入
// 为了实现简单,此哈希表中我们将比较直接与元素绑定在一起
template<class k, class v>
class hashtable
{
struct elem
{
pair<k, v> _val;
state _state;
};
public:
hashtable(size_t capacity = 3)
: _ht(capacity), _size(0)
{
for (size_t i = 0; i < capacity; ++i)
_ht[i]._state = empty;
}
bool insert(const pair<k, v>& val)
{
// 检测哈希表底层空间是否充足
// _checkcapacity();
size_t hashaddr = hashfunc(key);
// size_t startaddr = hashaddr;
while (_ht[hashaddr]._state != empty)
{
if (_ht[hashaddr]._state == exist && _ht[hashaddr]._val.first
== key)
return false;
hashaddr++;
if (hashaddr == _ht.capacity())
hashaddr = 0;
/*
// 转一圈也没有找到,注意:动态哈希表,该种情况可以不用考虑,哈希表中元
素个数到达一定的数量,哈希冲突概率会增大,需要扩容来降低哈希冲突,
因此哈希表中元素是不会存满的
if(hashaddr == startaddr)
return false;
*/
}
// 插入元素
_ht[hashaddr]._state = exist;
_ht[hashaddr]._val = val;
_size++;
return true;
}
int find(const k& key)
{
size_t hashaddr = hashfunc(key);
while (_ht[hashaddr]._state != empty)
{
if (_ht[hashaddr]._state == exist &&
_ht[hashaddr]._val.first == key)
return hashaddr;
hashaddr++;
}
return hashaddr;
}
bool erase(const k & key)
{
int index = find(key);
if (-1 != index)
{
_ht[index]._state = delete;
_size++;
return true;
}
return false;
}
size_t size()const;
bool empty() const;
void swap(hashtable<k, v, hf>&ht);
private:
size_t hashfunc(const k & key)
{
return key % _ht.capacity();
}
private:
vector<elem> _ht;
size_t _size;
};
线性探测的优点:实现非常简单。
线性探测的缺点:一旦发生哈希冲突,所有的冲突连在一起,容易产生数据“堆积”,即:不同关键码占据了可利用的空位置,使得寻找某关键码的位置需要多次比较,导致搜索效率降低。
2. 二次探测
线性探测的缺陷是产生冲突的数据堆积在一块,这与其找下一个空位置有关系,因为找空位置的方式就是挨着往后逐个去找,因此二次探测为了避免该问题。
研究表明:当表的长度为质数且表装载因子a不超过0.5时,新的表项一定能够插入,而且任何一个位置都不会被探查两次。因此只要表中有一半的空位置,就不会存在表满的问题。在搜索时可以不考虑表装满的情况,但在插入时必须确保表的装载因子a不超过0.5,如果超出必须考虑增容。
因此:闭散列最大的缺陷就是空间利用率比较低,这也是哈希的缺陷。
2.4.2 -> 开散列
1. 开散列概念
开散列法又叫链地址法(开链法),首先对关键码集合用散列函数计算散列地址,具有相同地址的关键码归于同一子集合,每一个子集合称为一个桶,各个桶中的元素通过一个单链表链接起来,各链表的头结点存储在哈希表中。
从上图可以看出,开散列中每个桶中放的都是发生哈希冲突的元素。
2. 开散列实现
#define _crt_secure_no_warnings 1
#include <iostream>
using namespace std;
template<class v>
struct hashbucketnode
{
hashbucketnode(const v& data)
: _pnext(nullptr), _data(data)
{}
hashbucketnode<v>* _pnext;
v _data;
};
// 所实现的哈希桶中key是唯一的
template<class v>
class hashbucket
{
typedef hashbucketnode<v> node;
typedef node* pnode;
public:
hashbucket(size_t capacity = 3) : _size(0)
{
_ht.resize(getnextprime(capacity), nullptr);
}
// 哈希桶中的元素不能重复
pnode* insert(const v& data)
{
// 确认是否需要扩容。。。
// _checkcapacity();
// 1. 计算元素所在的桶号
size_t bucketno = hashfunc(data);
// 2. 检测该元素是否在桶中
pnode pcur = _ht[bucketno];
while (pcur)
{
if (pcur->_data == data)
return pcur;
pcur = pcur->_pnext;
}
// 3. 插入新元素
pcur = new node(data);
pcur->_pnext = _ht[bucketno];
_ht[bucketno] = pcur;
_size++;
return pcur;
}
// 删除哈希桶中为data的元素(data不会重复),返回删除元素的下一个节点
pnode* erase(const v& data)
{
size_t bucketno = hashfunc(data);
pnode pcur = _ht[bucketno];
pnode pprev = nullptr, pret = nullptr;
while (pcur)
{
if (pcur->_data == data)
{
if (pcur == _ht[bucketno])
_ht[bucketno] = pcur->_pnext;
else
pprev->_pnext = pcur->_pnext;
pret = pcur->_pnext;
delete pcur;
_size--;
return pret;
}
}
return nullptr;
}
pnode* find(const v& data);
size_t size()const;
bool empty()const;
void clear();
bool bucketcount()const;
void swap(hashbucket<v, hf>& ht;
~hashbucket();
private:
size_t hashfunc(const v& data)
{
return data % _ht.capacity();
}
private:
vector<pnode*> _ht;
size_t _size; // 哈希表中有效元素的个数
};
3. 开散列增容
桶的个数是一定的,随着元素的不断插入,每个桶中元素的个数不断增多,极端情况下,可能会导致一个桶中链表节点非常多,会影响的哈希表的性能,因此在一定条件下需要对哈希表进行增容,那该条件怎么确认呢?开散列最好的情况是:每个哈希桶中刚好挂一个节点,再继续插入元素时,每一次都会发生哈希冲突,因此,在元素个数刚好等于桶的个数时,可以给哈希表增容。
void _checkcapacity()
{
size_t bucketcount = bucketcount();
if (_size == bucketcount)
{
hashbucket<v, hf> newht(bucketcount);
for (size_t bucketidx = 0; bucketidx < bucketcount; ++bucketidx)
{
pnode pcur = _ht[bucketidx];
while (pcur)
{
// 将该节点从原哈希表中拆出来
_ht[bucketidx] = pcur->_pnext;
// 将该节点插入到新哈希表中
size_t bucketno = newht.hashfunc(pcur->_data);
pcur->_pnext = newht._ht[bucketno];
newht._ht[bucketno] = pcur;
pcur = _ht[bucketidx];
}
}
newht._size = _size;
this->swap(newht);
}
}
4. 开散列的思考
(1)只能存储key为整形的元素,其他类型怎么解决?
// 哈希函数采用处理余数法,被模的key必须要为整形才可以处理,此处提供将key转化为整形的方法
// 整形数据不需要转化
template<class t>
class defhashf
{
public:
size_t operator()(const t& val)
{
return val;
}
};
// key为字符串类型,需要将其转化为整形
class str2int
{
public:
size_t operator()(const string& s)
{
const char* str = s.c_str();
unsigned int seed = 131; // 31 131 1313 13131 131313
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7fffffff);
}
};
// 为了实现简单,此哈希表中我们将比较直接与元素绑定在一起
template<class v, class hf>
class hashbucket
{
// ……
private:
size_t hashfunc(const v& data)
{
return hf()(data.first) % _ht.capacity();
}
};
(2)除留余数法,最好模一个素数,如何每次快速取一个类似两倍关系的素数?
size_t getnextprime(size_t prime)
{
const int primecount = 28;
static const size_t primelist[primecount] =
{
53ul, 97ul, 193ul, 389ul, 769ul,
1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
1572869ul, 3145739ul, 6291469ul, 12582917ul,
25165843ul,
50331653ul, 100663319ul, 201326611ul, 402653189ul,
805306457ul,
1610612741ul, 3221225473ul, 4294967291ul
};
size_t i = 0;
for (; i < primecount; ++i)
{
if (primelist[i] > prime)
return primelist[i];
}
return primelist[i];
}
5. 开散列与闭散列比较
应用链地址法处理溢出,需要增设链接指针,似乎增加了存储开销。事实上:由于开地址法必须保持大量的空闲空间以确保搜索效率,如二次探查法要求装载因子a <= 0.7,而表项所占空间又比指针大的多,所以使用链地址法反而比开地址法节省存储空间。
3 -> 模拟实现
3.1 -> 哈希表的改造
#pragma once
//hashfunc<int>
template<class k>
struct hashfunc
{
size_t operator()(const k& key)
{
return (size_t)key;
}
};
//hashfunc<string>
template<>
struct hashfunc<string>
{
size_t operator()(const string& key)
{
// bkdr
size_t hash = 0;
for (auto e : key)
{
hash *= 31;
hash += e;
}
//cout << key << ":" << hash << endl;
return hash;
}
};
namespace open_address
{
enum status
{
empty,
exist,
delete
};
template<class k, class v>
struct hashdata
{
pair<k, v> _kv;
status _s; //状态
};
//struct hashfuncstring
//{
// size_t operator()(const string& key)
// {
// // bkdr
// size_t hash = 0;
// for (auto e : key)
// {
// hash *= 31;
// hash += e;
// }
// cout << key << ":" << hash << endl;
// return hash;
// }
//};
template<class k, class v, class hash = hashfunc<k>>
class hashtable
{
public:
hashtable()
{
_tables.resize(10);
}
bool insert(const pair<k, v>& kv)
{
if (find(kv.first))
return false;
// 负载因子0.7就扩容
if (_n * 10 / _tables.size() == 7)
{
size_t newsize = _tables.size() * 2;
hashtable<k, v, hash> newht;
newht._tables.resize(newsize);
// 遍历旧表
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i]._s == exist)
{
newht.insert(_tables[i]._kv);
}
}
_tables.swap(newht._tables);
}
hash hf;
// 线性探测
size_t hashi = hf(kv.first) % _tables.size();
while (_tables[hashi]._s == exist)
{
hashi++;
hashi %= _tables.size();
}
_tables[hashi]._kv = kv;
_tables[hashi]._s = exist;
++_n;
return true;
}
hashdata<k, v>* find(const k& key)
{
hash hf;
size_t hashi = hf(key) % _tables.size();
while (_tables[hashi]._s != empty)
{
if (_tables[hashi]._s == exist
&& _tables[hashi]._kv.first == key)
{
return &_tables[hashi];
}
hashi++;
hashi %= _tables.size();
}
return null;
}
// 伪删除法
bool erase(const k& key)
{
hashdata<k, v>* ret = find(key);
if (ret)
{
ret->_s = delete;
--_n;
return true;
}
else
{
return false;
}
}
void print()
{
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i]._s == exist)
{
//printf("[%d]->%d\n", i, _tables[i]._kv.first);
cout << "[" << i << "]->" << _tables[i]._kv.first << ":" << _tables[i]._kv.second << endl;
}
else if (_tables[i]._s == empty)
{
printf("[%d]->\n", i);
}
else
{
printf("[%d]->d\n", i);
}
}
cout << endl;
}
private:
vector<hashdata<k, v>> _tables;
size_t _n = 0; // 存储的关键字的个数
};
void testht1()
{
hashtable<int, int> ht;
int a[] = { 4,14,24,34,5,7,1 };
for (auto e : a)
{
ht.insert(make_pair(e, e));
}
ht.insert(make_pair(3, 3));
ht.insert(make_pair(3, 3));
ht.insert(make_pair(-3, -3));
ht.print();
ht.erase(3);
ht.print();
if (ht.find(3))
{
cout << "3存在" << endl;
}
else
{
cout << "3不存在" << endl;
}
ht.insert(make_pair(3, 3));
ht.insert(make_pair(23, 3));
ht.print();
}
void testht2()
{
string arr[] = { "香蕉", "甜瓜","苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
//hashtable<string, int, hashfuncstring> ht;
hashtable<string, int> ht;
for (auto& e : arr)
{
//auto ret = ht.find(e);
hashdata<string, int>* ret = ht.find(e);
if (ret)
{
ret->_kv.second++;
}
else
{
ht.insert(make_pair(e, 1));
}
}
ht.print();
ht.insert(make_pair("apple", 1));
ht.insert(make_pair("sort", 1));
ht.insert(make_pair("abc", 1));
ht.insert(make_pair("acb", 1));
ht.insert(make_pair("aad", 1));
ht.print();
}
}
namespace hash_bucket
{
template<class t>
struct hashnode
{
hashnode<t>* _next;
t _data;
hashnode(const t& data)
:_data(data)
, _next(nullptr)
{}
};
// 前置声明
template<class k, class t, class keyoft, class hash>
class hashtable;
template<class k, class t, class ref, class ptr, class keyoft, class hash>
struct __htiterator
{
typedef hashnode<t> node;
typedef __htiterator<k, t, ref, ptr, keyoft, hash> self;
node* _node;
const hashtable<k, t, keyoft, hash>* _pht;
// vector<node*> * _ptb;
size_t _hashi;
__htiterator(node* node, hashtable<k, t, keyoft, hash>* pht, size_t hashi)
:_node(node)
, _pht(pht)
, _hashi(hashi)
{}
__htiterator(node* node, const hashtable<k, t, keyoft, hash>* pht, size_t hashi)
:_node(node)
, _pht(pht)
, _hashi(hashi)
{}
self& operator++()
{
if (_node->_next)
{
// 当前桶还有节点,走到下一个节点
_node = _node->_next;
}
else
{
// 当前桶已经走完了,找下一个桶开始
//keyoft kot;
//hash hf;
//size_t hashi = hf(kot(_node->_data)) % _pht._tables.size();
++_hashi;
while (_hashi < _pht->_tables.size())
{
if (_pht->_tables[_hashi])
{
_node = _pht->_tables[_hashi];
break;
}
++_hashi;
}
if (_hashi == _pht->_tables.size())
{
_node = nullptr;
}
}
return *this;
}
ref operator*()
{
return _node->_data;
}
ptr operator->()
{
return &_node->_data;
}
bool operator!=(const self& s)
{
return _node != s._node;
}
};
// unordered_set -> hashtable<k, k>
// unordered_map -> hashtable<k, pair<k, v>>
template<class k, class t, class keyoft, class hash>
class hashtable
{
typedef hashnode<t> node;
template<class k, class t, class ref, class ptr, class keyoft, class hash>
friend struct __htiterator;
public:
typedef __htiterator<k, t, t&, t*, keyoft, hash> iterator;
typedef __htiterator<k, t, const t&, const t*, keyoft, hash> const_iterator;
iterator begin()
{
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i])
{
return iterator(_tables[i], this, i);
}
}
return end();
}
iterator end()
{
return iterator(nullptr, this, -1);
}
const_iterator begin() const
{
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i])
{
return const_iterator(_tables[i], this, i);
}
}
return end();
}
// this-> const hashtable<k, t, keyoft, hash>*
const_iterator end() const
{
return const_iterator(nullptr, this, -1);
}
hashtable()
{
_tables.resize(10);
}
~hashtable()
{
for (size_t i = 0; i < _tables.size(); i++)
{
node* cur = _tables[i];
while (cur)
{
node* next = cur->_next;
delete cur;
cur = next;
}
_tables[i] = nullptr;
}
}
pair<iterator, bool> insert(const t& data)
{
hash hf;
keyoft kot;
iterator it = find(kot(data));
if (it != end())
return make_pair(it, false);
// 负载因子最大到1
if (_n == _tables.size())
{
vector<node*> newtables;
newtables.resize(_tables.size() * 2, nullptr);
// 遍历旧表
for (size_t i = 0; i < _tables.size(); i++)
{
node* cur = _tables[i];
while (cur)
{
node* next = cur->_next;
// 挪动到映射的新表
size_t hashi = hf(kot(cur->_data)) % newtables.size();
cur->_next = newtables[i];
newtables[hashi] = cur;
cur = next;
}
_tables[i] = nullptr;
}
_tables.swap(newtables);
}
size_t hashi = hf(kot(data)) % _tables.size();
node* newnode = new node(data);
// 头插
newnode->_next = _tables[hashi];
_tables[hashi] = newnode;
++_n;
return make_pair(iterator(newnode, this, hashi), true);
}
iterator find(const k& key)
{
hash hf;
keyoft kot;
size_t hashi = hf(key) % _tables.size();
node* cur = _tables[hashi];
while (cur)
{
if (kot(cur->_data) == key)
{
return iterator(cur, this, hashi);
}
cur = cur->_next;
}
return end();
}
bool erase(const k& key)
{
hash hf;
keyoft kot;
size_t hashi = hf(key) % _tables.size();
node* prev = nullptr;
node* cur = _tables[hashi];
while (cur)
{
if (kot(cur->_data) == key)
{
if (prev == nullptr)
{
_tables[hashi] = cur->_next;
}
else
{
prev->_next = cur->_next;
}
delete cur;
return true;
}
prev = cur;
cur = cur->_next;
}
return false;
}
void some()
{
size_t bucketsize = 0;
size_t maxbucketlen = 0;
size_t sum = 0;
double averagebucketlen = 0;
for (size_t i = 0; i < _tables.size(); i++)
{
node* cur = _tables[i];
if (cur)
{
++bucketsize;
}
size_t bucketlen = 0;
while (cur)
{
++bucketlen;
cur = cur->_next;
}
sum += bucketlen;
if (bucketlen > maxbucketlen)
{
maxbucketlen = bucketlen;
}
}
averagebucketlen = (double)sum / (double)bucketsize;
printf("all bucketsize:%d\n", _tables.size());
printf("bucketsize:%d\n", bucketsize);
printf("maxbucketlen:%d\n", maxbucketlen);
printf("averagebucketlen:%lf\n\n", averagebucketlen);
}
private:
vector<node*> _tables;
size_t _n = 0;
};
}
3.2 -> unordered_map
#pragma once
#include"hashtable.h"
namespace fyd
{
template<class k, class v, class hash = hashfunc<k>>
class unordered_map
{
struct mapkeyoft
{
const k& operator()(const pair<k, v>& kv)
{
return kv.first;
}
};
public:
typedef typename hash_bucket::hashtable<k, pair<const k, v>, mapkeyoft, hash>::iterator iterator;
iterator begin()
{
return _ht.begin();
}
iterator end()
{
return _ht.end();
}
pair<iterator, bool> insert(const pair<k, v>& kv)
{
return _ht.insert(kv);
}
v& operator[](const k& key)
{
pair<iterator, bool> ret = _ht.insert(make_pair(key, v()));
return ret.first->second;
}
const v& operator[](const k& key) const
{
pair<iterator, bool> ret = _ht.insert(make_pair(key, v()));
return ret.first->second;
}
iterator find(const k& key)
{
return _ht.find(key);
}
bool erase(const k& key)
{
return _ht.erase(key);
}
private:
hash_bucket::hashtable<k, pair<const k, v>, mapkeyoft, hash> _ht;
};
void test_map()
{
unordered_map<string, string> dict;
dict.insert(make_pair("sort", ""));
dict.insert(make_pair("string", "ַ"));
dict.insert(make_pair("insert", ""));
for (auto& kv : dict)
{
//kv.first += 'x';
kv.second += 'x';
cout << kv.first << ":" << kv.second << endl;
}
cout << endl;
string arr[] = { "㽶", "","ƻ", "", "ƻ", "", "ƻ", "ƻ", "", "ƻ", "㽶", "ƻ", "㽶" };
unordered_map<string, int> count_map;
for (auto& e : arr)
{
count_map[e]++;
}
for (auto& kv : count_map)
{
cout << kv.first << ":" << kv.second << endl;
}
cout << endl;
}
}
3.3 -> unordered_set
#pragma once
#include"hashtable.h"
namespace fyd
{
template<class k, class hash = hashfunc<k>>
class unordered_set
{
struct setkeyoft
{
const k& operator()(const k& key)
{
return key;
}
};
public:
typedef typename hash_bucket::hashtable<k, k, setkeyoft, hash>::const_iterator iterator;
typedef typename hash_bucket::hashtable<k, k, setkeyoft, hash>::const_iterator const_iterator;
/*iterator begin()
{
return _ht.begin();
}
iterator end()
{
return _ht.end();
}*/
const_iterator begin() const
{
return _ht.begin();
}
const_iterator end() const
{
return _ht.end();
}
pair<const_iterator, bool> insert(const k& key)
{
auto ret = _ht.insert(key);
return pair<const_iterator, bool>(const_iterator(ret.first._node, ret.first._pht, ret.first._hashi), ret.second);
}
iterator find(const k& key)
{
return _ht.find(key);
}
bool erase(const k& key)
{
return _ht.erase(key);
}
private:
hash_bucket::hashtable<k, k, setkeyoft, hash> _ht;
};
void test_set()
{
unordered_set<int> us;
us.insert(5);
us.insert(15);
us.insert(52);
us.insert(3);
unordered_set<int>::iterator it = us.begin();
while (it != us.end())
{
//*it += 5;
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : us)
{
cout << e << " ";
}
cout << endl;
}
}
感谢各位大佬支持!!!
互三啦!!!
发表评论