C语言总结 在这 常见八大排序 在这
作者和朋友建立的社区: 非科班转码社区-CSDN社区云 💖 💛 💙
期待hxd的支持哈 🎉 🎉 🎉
最后是打鸡血环节: 想多了都是问题,做多了都是答案 🚀 🚀 🚀
最近作者和好友建立了一个公众号
公众号介绍:
专注于自学编程领域。由USTC、WHU、SDU等高校学生、ACM竞赛选手、CSDN万粉博主、双非上岸BAT学长原创。分享业内资讯、硬核原创资源、职业规划等,和大家一起努力、成长。( 二维码在文章底部哈! )
关于哈希
说到哈希,我们自然而然就要想到unordered_map/set。
在C++98中,STL提供了底层为红黑树结构的一系列关联式容器,在查询时效率可达到 ,即最差情况下需要比较红黑树的高度次,当树中的节点非常多时,查询效率也不理想。最好的查询是,进行很少的比较次数就能够将元素找到,因此在C++11中,STL又提供了4个unordered系列的关联式容器,这四个容器与红黑树结构的关联式容器使用方式基本类似,只是其底层结构不同(哈希),本文中只对unordered_mapunordered_set进行介绍,unordered_multimap和unordered_multiset可查看文档介绍。
unordered_map
unordered_map的文档介绍
 
 http://www.cplusplus.com/reference/unordered_map/unordered_map/?kw=unordered_map
1. unordered_map是存储<key, value>键值对的关联式容器,其允许通过keys快速的索引到与其对应的
value。
2. 在unordered_map中,键值通常用于惟一地标识元素,而映射值是一个对象,其内容与此键关联。键
和映射值的类型可能不同。
3. 在内部,unordered_map没有对<key, value>按照任何特定的顺序排序, 为了能在常数范围内找到key所
对应的value,unordered_map将相同哈希值的键值对放在相同的桶中。
4. unordered_map容器通过key访问单个元素要比map快,但它通常在遍历元素子集的范围迭代方面效率
较低。
5. unordered_maps实现了直接访问操作符(operator[]),它允许使用key作为参数直接访问value。
6. 它的迭代器至少是前向迭代器。
unordered_set
http://www.cplusplus.com/reference/unordered_set/unordered_set/?kw=unordered_set
底层结构
unordered系列的关联式容器之所以效率比较高,是因为其底层使用了哈希结构。
哈希概念
顺序结构以及平衡树中,元素关键码与其存储位置之间没有对应的关系,因此在查找一个元素时,必须要经
过关键码的多次比较。顺序查找时间复杂度为O(N),平衡树中为树的高度,即O( log2N),搜索的效率取于搜索过程中元素的比较次数。
理想的搜索方法:可以不经过任何比较,一次直接从表中得到要搜索的元素。 如果构造一种存储结构,通过某种函数(hashFunc)使元素的存储位置与它的关键码之间能够建立一一映射的关系,那么在查找时通过该函数可以很快找到该元素。
当向该结构中:
插入元素
根据待插入元素的关键码,以此函数计算出该元素的存储位置并按此位置进行存放。
搜索元素
对元素的关键码进行同样的计算,把求得的函数值当做元素的存储位置,在结构中按此位置取元素比较,若关键码相等,则搜索成功。
该方式即为哈希(散列)方法,哈希方法中使用的转换函数称为哈希(散列)函数,构造出来的结构称为哈希表(Hash Table)(或者称散列表)。
例如:数据集合{1,7,6,4,5,9};
哈希函数设置为:hash(key) = key % capacity; capacity为存储元素底层空间总的大小。
 
 
   用该方法进行搜索不必进行多次关键码的比较,因此搜索的速度比较快。但是如果向同一个位置多次插入就会位置不够
哈希冲突
对于两个数据元素的关键字 ki和kj (i != j),有ki != kj ,但有:Hash(ki) == Hash(kj),即:不同关键字通过
相同哈希哈数计算出相同的哈希地址,该种现象称为哈希冲突或哈希碰撞。
把具有不同关键码而具有相同哈希地址的数据元素称为“同义词”。
发生哈希冲突该如何处理呢?那么就需要我们之前提到的hash函数了。
哈希函数
引起哈希冲突的一个原因可能是:哈希函数设计不够合理。 哈希函数设计原则:
哈希函数的定义域必须包括需要存储的全部关键码,而如果散列表允许有m个地址时,其值域必须在0
到m-1之间。
哈希函数计算出来的地址能均匀分布在整个空间中。
哈希函数应该比较简单。
常见哈希函数
直接定制法--(常用)
取关键字的某个线性函数为散列地址:Hash(Key)= A*Key + B 优点:简单、均匀 缺点:需要事先
知道关键字的分布情况 使用场景:适合查找比较小且连续的情况 面试题:字符串中第一个只出现一次
字符。
除留余数法--(常用)
设散列表中允许的地址数为m,取一个不大于m,但最接近或者等于m的质数p作为除数,按照哈希函
数:Hash(key) = key% p(p<=m),将关键码转换成哈希地址。
哈希冲突解决
解决哈希冲突两种常见的方法是:闭散列和开散列。
闭散列
闭散列:也叫开放定址法,当发生哈希冲突时,如果哈希表未被装满,说明在哈希表中必然还有空位置,那
么可以把key存放到冲突位置中的“下一个” 空位置中去。
1. 线性探测
线性探测:从发生冲突的位置开始,依次向后探测,直到寻找到下一个空位置为止。
插入
通过哈希函数获取待插入元素在哈希表中的位置
如果该位置中没有元素则直接插入新元素,如果该位置中有元素发生哈希冲突,使用线性探
测找到下一个空位置,插入新元素
 
 
   删除
采用闭散列处理哈希冲突时,不能随便物理删除哈希表中已有的元素,若直接删除元素会影响其他
元素的搜索。比如删除元素4,如果直接删除掉,44查找起来可能会受影响。因此线性探测采用标
记的伪删除法来删除一个元素。
   
    - 
     
      
     
     
      
       // 哈希表每个空间给个标记
      
     
- 
     
      
     
     
      
       // EMPTY此位置空, EXIST此位置已经有元素, DELETE元素已经删除
      
     
- 
     
      
     
     
      
       enum State{EMPTY, EXIST, DELETE};
      
     
哈希表什么情况下进行扩容?如何扩容?
 
 
   线性探测的实现
   
    - 
     
      
     
     
      
       // 注意:假如实现的哈希表中元素唯一,即key相同的元素不再进行插入
      
     
- 
     
      
     
     
      
       // 为了实现简单,此哈希表中我们将比较直接与元素绑定在一起
      
     
- 
     
      
     
     
      
       #include <iostream>
      
     
- 
     
      
     
     
      
       #include <vector>
      
     
- 
     
      
     
     
      
       #include <string>
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       using namespace std;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       namespace Close_Hash
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           enum State { EMPTY, EXIST, DELETE };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           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), _totalSize(0)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   for (size_t i = 0; i < capacity; ++i)
      
     
- 
     
      
     
     
      
                       _ht[i]._state = EMPTY;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               // 插入
      
     
- 
     
      
     
     
      
               bool Insert(const pair<K, V>& val)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   //插入重复的直接跳过
      
     
- 
     
      
     
     
      
                   if (Find(val.first) < _ht.capacity())
      
     
- 
     
      
     
     
      
                       return false;
      
     
- 
     
      
     
     
      
                   //扩容
      
     
- 
     
      
     
     
      
                   CheckCapcity();
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   //插入的值都是之前没有的
      
     
- 
     
      
     
     
      
                   size_t hashi = HashFunc(val.first);
      
     
- 
     
      
     
     
      
                   while (_ht[hashi]._state != EMPTY)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       //线性探测
      
     
- 
     
      
     
     
      
                       hashi++;
      
     
- 
     
      
     
     
      
                       if (hashi == _ht.capacity())
      
     
- 
     
      
     
     
      
                           hashi = 0;
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
                   _ht[hashi]._state = EXIST;
      
     
- 
     
      
     
     
      
                   _ht[hashi]._val = val;
      
     
- 
     
      
     
     
      
                   _size++;
      
     
- 
     
      
     
     
      
                   return true;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               void CheckCapcity()
      
     
- 
     
      
     
     
      
               {   //7 hy == 10size
      
     
- 
     
      
     
     
      
                   //负载因子大于70%扩容为二倍
      
     
- 
     
      
     
     
      
                   if (10 * _size >= 7 * _ht.capacity())
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       //用一个临时变量
      
     
- 
     
      
     
     
      
                       HashTable<K, V> newHT(_ht.capacity() * 2);
      
     
- 
     
      
     
     
      
                       for (size_t i = 0; i < _ht.capacity(); i++)
      
     
- 
     
      
     
     
      
                       {
      
     
- 
     
      
     
     
      
                           if (_ht[i]._state == EXIST)
      
     
- 
     
      
     
     
      
                           {
      
     
- 
     
      
     
     
      
                               newHT.Insert(_ht[i]._val);
      
     
- 
     
      
     
     
      
                           }
      
     
- 
     
      
     
     
      
                       }
      
     
- 
     
      
     
     
      
                       this->Swap(newHT);
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               // 查找
      
     
- 
     
      
     
     
      
               size_t Find(const K& key)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   size_t findi = HashFunc(key);
      
     
- 
     
      
     
     
      
                   size_t pos = findi;
      
     
- 
     
      
     
     
      
                   while (_ht[findi]._state != EMPTY)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       if (_ht[findi]._state == EXIST && _ht[findi]._val.first == key)
      
     
- 
     
      
     
     
      
                           return findi;
      
     
- 
     
      
     
     
      
                       else
      
     
- 
     
      
     
     
      
                           findi++;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                       if (findi == _ht.capacity())
      
     
- 
     
      
     
     
      
                           findi = 0;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                       if (findi == pos)
      
     
- 
     
      
     
     
      
                           return -1;
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
                   return -1;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               // 删除
      
     
- 
     
      
     
     
      
               bool Erase(const K& key)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   //先看有没有
      
     
- 
     
      
     
     
      
                   size_t hashi = Find(key);
      
     
- 
     
      
     
     
      
                   if (hashi > _ht.capacity())
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       //说明没有
      
     
- 
     
      
     
     
      
                       return false;
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
                   //下来就说明有并且通过Find找到了
      
     
- 
     
      
     
     
      
                   _ht[hashi]._state = DELETE;
      
     
- 
     
      
     
     
      
                   _size--;
      
     
- 
     
      
     
     
      
                   return true;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               size_t Size()const
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return _size;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               bool Empty() const
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return _size == 0;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               void Swap(HashTable<K, V>& ht)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   swap(_size, ht._size);
      
     
- 
     
      
     
     
      
                   swap(_totalSize, ht._totalSize);
      
     
- 
     
      
     
     
      
                   _ht.swap(ht._ht);
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           private:
      
     
- 
     
      
     
     
      
               size_t HashFunc(const K& key)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return key % _ht.capacity();
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           private:
      
     
- 
     
      
     
     
      
               vector<Elem> _ht;
      
     
- 
     
      
     
     
      
               size_t _size;
      
     
- 
     
      
     
     
      
               size_t _totalSize;  // 哈希表中的所有元素:有效和已删除, 扩容时候要用到
      
     
- 
     
      
     
     
      
           };
      
     
- 
     
      
     
     
      
       }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       //int main()
      
     
- 
     
      
     
     
      
       //{
      
     
- 
     
      
     
     
      
       //    Close_Hash::HashTable<int, string> hs;
      
     
- 
     
      
     
     
      
       //    hs.Insert(make_pair(2, "123456"));
      
     
- 
     
      
     
     
      
       //    hs.Insert(make_pair(5, "666"));
      
     
- 
     
      
     
     
      
       //    hs.Insert(make_pair(7, "999"));
      
     
- 
     
      
     
     
      
       //    hs.Insert(make_pair(8, "333"));
      
     
- 
     
      
     
     
      
       //    hs.Insert(make_pair(12, "2222"));
      
     
- 
     
      
     
     
      
       //
      
     
- 
     
      
     
     
      
       //    cout << hs.Find(5) << endl;
      
     
- 
     
      
     
     
      
       //    cout << hs.Erase(5) << endl;
      
     
- 
     
      
     
     
      
       //    cout << hs.Erase(5) << endl;
      
     
- 
     
      
     
     
      
       //    cout<< hs.Find(5) << endl;
      
     
- 
     
      
     
     
      
       //    cout << hs.Size() << endl;
      
     
- 
     
      
     
     
      
       //    return 0;
      
     
- 
     
      
     
     
      
       //}
      
     
  注意是%size,不是capacity,因为【】访问会检查是否小于size,这也是用容器时先 .capacity 但是不能 cin 的原因,要 resize 才可以 cin。
线性探测优点:实现非常简单,
线性探测缺点:一旦发生哈希冲突,所有的冲突连在一起,容易产生数据“堆积”,即:不同关键码占据
了可利用的空位置,使得寻找某关键码的位置需要许多次比较,导致搜索效率降低。如何缓解呢?
二次探测
线性探测的缺陷是产生冲突的数据堆积在一块,这与其找下一个空位置有关系,因为找空位置的方式就是挨着往后逐个去找,因此二次探测为了避免该问题,找下一个空位置的方法为:Hi = (H0 + i^2 )% m,或者:Hi = (H0 - i^2)% m。其中:i = 1,2,3…, H0 是通过散列函数Hash(x)对元素的关键码 key 进行计算得到的位置,m是表的大小。 对于如果要插入44,产生冲突,使用解决后的情况为:
 
 
   研究表明:当表的长度为质数且表装载因子a不超过0.5时,新的表项一定能够插入,而且任何一个位置
都不会被探查两次。因此只要表中有一半的空位置,就不会存在表满的问题。在搜索时可以不考虑表装
满的情况,但在插入时必须确保表的装载因子a不超过0.5,如果超出必须考虑增容。
因此:比散列最大的缺陷就是空间利用率比较低,这也是哈希的缺陷。
开散列
开散列概念
开散列法又叫链地址法(开链法),首先对关键码集合用散列函数计算散列地址,具有相同地址的关键码
归于同一子集合,每一个子集合称为一个桶,各个桶中的元素通过一个单链表链接起来,各链表的头结
点存储在哈希表中。
 
 
   从上图可以看出,开散列中每个桶中放的都是发生哈希冲突的元素。
开散列增容
桶的个数是一定的,随着元素的不断插入,每个桶中元素的个数不断增多,极端情况下,可能会导致一
个桶中链表节点非常多,会影响的哈希表的性能,因此在一定条件下需要对哈希表进行增容,那该条件
怎么确认呢?开散列最好的情况是:每个哈希桶中刚好挂一个节点,再继续插入元素时,每一次都会发
生哈希冲突,因此,在元素个数刚好等于桶的个数时,可以给哈希表增容。
开散列实现
   
    - 
     
      
     
     
      
       #define _CRT_SECURE_NO_WARNINGS 1
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       #include <iostream>
      
     
- 
     
      
     
     
      
       #include <vector>
      
     
- 
     
      
     
     
      
       #include <string>
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       using namespace std;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<class T>
      
     
- 
     
      
     
     
      
       class HashFunc
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
       public:
      
     
- 
     
      
     
     
      
           size_t operator()(const T& val)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return val;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<>
      
     
- 
     
      
     
     
      
       class HashFunc<string>
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
       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;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<class V>
      
     
- 
     
      
     
     
      
       struct HashBucketNode
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           HashBucketNode(const V& data)
      
     
- 
     
      
     
     
      
               : _pNext(nullptr), _data(data)
      
     
- 
     
      
     
     
      
           {}
      
     
- 
     
      
     
     
      
           HashBucketNode<V>* _pNext;
      
     
- 
     
      
     
     
      
           V _data;
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       //本文所实现的哈希桶中key是唯一的
      
     
- 
     
      
     
     
      
       template<class V, class HF = HashFunc<V>>
      
     
- 
     
      
     
     
      
       class HashBucket
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           typedef HashBucketNode<V> Node;
      
     
- 
     
      
     
     
      
           typedef Node* PNode;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           typedef HashBucket<V, HF> Self;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       public:
      
     
- 
     
      
     
     
      
           HashBucket(size_t capacity)
      
     
- 
     
      
     
     
      
               : _table(capacity)
      
     
- 
     
      
     
     
      
               , _size(0)
      
     
- 
     
      
     
     
      
           {}
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           ~HashBucket()
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               Clear();
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           // 哈希桶中的元素不能重复
      
     
- 
     
      
     
     
      
           Node* Insert(const V& data)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               //有数据了发返回nullptr
      
     
- 
     
      
     
     
      
               if (Find(data) != nullptr)
      
     
- 
     
      
     
     
      
                   return nullptr;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               CheckCapcity();
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               size_t hashi = HashFunc(data);
      
     
- 
     
      
     
     
      
               Node* newnode = new Node(data);
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               newnode->_pNext = _table[hashi];
      
     
- 
     
      
     
     
      
               _table[hashi] = newnode;
      
     
- 
     
      
     
     
      
               _size++;
      
     
- 
     
      
     
     
      
               return newnode;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           // 删除哈希桶中为data的元素(data不会重复)
      
     
- 
     
      
     
     
      
           bool Erase(const V& data)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               if (Find(data) == nullptr)
      
     
- 
     
      
     
     
      
                   return false;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               size_t hashi = HashFunc(data);
      
     
- 
     
      
     
     
      
               Node* cur = Find(data);
      
     
- 
     
      
     
     
      
               //这样就把要删除的数据都放到了第一个位置
      
     
- 
     
      
     
     
      
               if (cur != _table[hashi])
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   std::swap(cur->_data, _table[hashi]->_data);
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
               cur = _table[hashi];
      
     
- 
     
      
     
     
      
               _table[hashi] = cur->_pNext;
      
     
- 
     
      
     
     
      
               delete cur;
      
     
- 
     
      
     
     
      
               _size--;
      
     
- 
     
      
     
     
      
               return true;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           Node* Find(const V& data)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               size_t hashi = HashFunc(data);
      
     
- 
     
      
     
     
      
               Node* cur = _table[hashi];
      
     
- 
     
      
     
     
      
               while (cur != nullptr)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   if (cur->_data == data)
      
     
- 
     
      
     
     
      
                       return cur;
      
     
- 
     
      
     
     
      
                   else
      
     
- 
     
      
     
     
      
                       cur = cur->_pNext;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
               return nullptr;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           size_t Size()const
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _size;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           bool Empty()const
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return 0 == _size;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           void Clear()
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               for (int i = 0; i < _table.capacity(); i++)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   Node* cur = _table[i];
      
     
- 
     
      
     
     
      
                   while (cur)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       _table[i] = cur->_pNext;
      
     
- 
     
      
     
     
      
                       delete cur;
      
     
- 
     
      
     
     
      
                       cur = _table[i];
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           size_t BucketCount()const
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _table.capacity();
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           void Swap(Self& ht)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               _table.swap(ht._table);
      
     
- 
     
      
     
     
      
               swap(_size, ht._size);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       private:
      
     
- 
     
      
     
     
      
           size_t HashFunc(const V& data)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return HF()(data) % _table.capacity();
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           void CheckCapcity()
      
     
- 
     
      
     
     
      
           {   //7 hy == 10size
      
     
- 
     
      
     
     
      
               //负载因子大于70%扩容为二倍
      
     
- 
     
      
     
     
      
               if (10 * _size >= 7 * _table.capacity())
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   HashBucket<V> newTb(_table.capacity() * 2);
      
     
- 
     
      
     
     
      
                   Node* cur = nullptr;
      
     
- 
     
      
     
     
      
                   for (size_t i = 0; i < _table.capacity(); i++)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       if (_table[i] != nullptr)
      
     
- 
     
      
     
     
      
                       {
      
     
- 
     
      
     
     
      
                           cur = _table[i];
      
     
- 
     
      
     
     
      
                           while (cur != nullptr)
      
     
- 
     
      
     
     
      
                           {
      
     
- 
     
      
     
     
      
                               newTb.Insert(cur->_data);
      
     
- 
     
      
     
     
      
                               cur = cur->_pNext;
      
     
- 
     
      
     
     
      
                           }
      
     
- 
     
      
     
     
      
                       }
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
                   Swap(newTb);
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       private:
      
     
- 
     
      
     
     
      
           vector<Node*> _table;
      
     
- 
     
      
     
     
      
           size_t _size;      // 哈希表中有效元素的个数
      
     
- 
     
      
     
     
      
       };
      
     
  开散列与闭散列比较
应用链地址法处理溢出,需要增设链接指针,似乎增加了存储开销。事实上: 由于开地址法必须保持大
量的空闲空间以确保搜索效率,如二次探查法要求装载因子a <= 0.7,而表项所占空间又比指针大的
多,所以使用链地址法反而比开地址法节省存储空间。
总结/注意
我们实现的是最简单的,对于key我们发现是直接用的,但是如果不是int呢?是string呢或者是自定义类型呢?那么我们就需要增加一个模板参数hashfunc了,我们可以对string进行特化(因为常用),对于自定义类型,我们就可以让使用者自己去设置key是谁。
 
 
   关于typename
 
 
   通过类域去取一个东西,他有可能是静态成员变量也有可能是内嵌类型,当是内嵌类型的时候就要加typename。
HashTable.h
   
    - 
     
      
     
     
      
       #pragma once
      
     
- 
     
      
     
     
      
       #include <vector>
      
     
- 
     
      
     
     
      
       #include <string>
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       using namespace std;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<class K>
      
     
- 
     
      
     
     
      
       struct DefaultHash
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           size_t operator()(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return (size_t)key;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<>
      
     
- 
     
      
     
     
      
       struct DefaultHash<string>
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           size_t operator()(const string& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               // BKDR
      
     
- 
     
      
     
     
      
               size_t hash = 0;
      
     
- 
     
      
     
     
      
               for (auto ch : key)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   hash = hash * 131 + ch;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               return hash;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       namespace Bucket
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           template<class T>
      
     
- 
     
      
     
     
      
           struct HashNode
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               T _data;
      
     
- 
     
      
     
     
      
               HashNode<T>* _next;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               HashNode(const T& data)
      
     
- 
     
      
     
     
      
                   :_data(data)
      
     
- 
     
      
     
     
      
                   , _next(nullptr)
      
     
- 
     
      
     
     
      
               {}
      
     
- 
     
      
     
     
      
           };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           template<class K, class T, class KeyOfT, class HashFunc>
      
     
- 
     
      
     
     
      
           class HashTable;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           template<class K, class T, class KeyOfT, class HashFunc>
      
     
- 
     
      
     
     
      
           class __HTIterator
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               typedef HashNode<T> Node;
      
     
- 
     
      
     
     
      
               typedef __HTIterator<K, T, KeyOfT, HashFunc> Self;
      
     
- 
     
      
     
     
      
           public:
      
     
- 
     
      
     
     
      
               Node* _node;
      
     
- 
     
      
     
     
      
               HashTable<K, T, KeyOfT, HashFunc>* _pht;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               __HTIterator() {}
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               __HTIterator(Node* node, HashTable<K, T, KeyOfT, HashFunc>* pht)
      
     
- 
     
      
     
     
      
                   :_node(node)
      
     
- 
     
      
     
     
      
                   , _pht(pht)
      
     
- 
     
      
     
     
      
               {}
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               Self& operator++()
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   if (_node->_next)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       _node = _node->_next;
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
                   else
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       KeyOfT kot;
      
     
- 
     
      
     
     
      
                       HashFunc hf;
      
     
- 
     
      
     
     
      
                       size_t hashi = hf(kot(_node->_data)) % _pht->_tables.size();
      
     
- 
     
      
     
     
      
                       ++hashi;
      
     
- 
     
      
     
     
      
                       //找下一个不为空的桶
      
     
- 
     
      
     
     
      
                       for (; hashi < _pht->_tables.size(); ++hashi)
      
     
- 
     
      
     
     
      
                       {
      
     
- 
     
      
     
     
      
                           if (_pht->_tables[hashi])
      
     
- 
     
      
     
     
      
                           {
      
     
- 
     
      
     
     
      
                               _node = _pht->_tables[hashi];
      
     
- 
     
      
     
     
      
                               break;
      
     
- 
     
      
     
     
      
                           }
      
     
- 
     
      
     
     
      
                       }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                       // 没有找到不为空的桶,用nullptr去做end标识
      
     
- 
     
      
     
     
      
                       if (hashi == _pht->_tables.size())
      
     
- 
     
      
     
     
      
                       {
      
     
- 
     
      
     
     
      
                           _node = nullptr;
      
     
- 
     
      
     
     
      
                       }
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   return *this;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               T& operator*()
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return _node->_data;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               T* operator->()
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return &_node->_data;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               bool operator!=(const Self& s) const
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return _node != s._node;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               bool operator==(const Self& s) const
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return _node == s._node;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
           };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           // unordered_map ->HashTable<K, pair<K, V>, MapKeyOfT> _ht;
      
     
- 
     
      
     
     
      
           // unordered_set ->HashTable<K, K, SetKeyOfT> _ht;
      
     
- 
     
      
     
     
      
           template<class K, class T, class KeyOfT, class HashFunc>
      
     
- 
     
      
     
     
      
           class HashTable
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               template<class K, class T, class KeyOfT, class HashFunc>
      
     
- 
     
      
     
     
      
               friend class __HTIterator;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               typedef HashNode<T> Node;
      
     
- 
     
      
     
     
      
           public:
      
     
- 
     
      
     
     
      
               typedef __HTIterator<K, T, KeyOfT, HashFunc> iterator;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               iterator begin()
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   for (size_t i = 0; i < _tables.size(); ++i)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       Node* cur = _tables[i];
      
     
- 
     
      
     
     
      
                       if (cur)
      
     
- 
     
      
     
     
      
                       {
      
     
- 
     
      
     
     
      
                           return iterator(cur, this);
      
     
- 
     
      
     
     
      
                       }
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   return end();
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               iterator end()
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return iterator(nullptr, this);
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               ~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;
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               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
      
     
- 
     
      
     
     
      
                   };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   // 获取比prime大那一个素数
      
     
- 
     
      
     
     
      
                   size_t i = 0;
      
     
- 
     
      
     
     
      
                   for (; i < PRIMECOUNT; ++i)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       if (primeList[i] > prime)
      
     
- 
     
      
     
     
      
                           return primeList[i];
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   return primeList[i];
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               pair<iterator, bool> Insert(const T& data)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   HashFunc hf;
      
     
- 
     
      
     
     
      
                   KeyOfT kot;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   iterator pos = Find(kot(data));
      
     
- 
     
      
     
     
      
                   if (pos != end())
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       return make_pair(pos, false);
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   // 负载因子 == 1 扩容
      
     
- 
     
      
     
     
      
                   if (_tables.size() == _n)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       //size_t newSize = _tables.size() == 0 ? 11 : _tables.size() * 2;
      
     
- 
     
      
     
     
      
                       size_t newSize = GetNextPrime(_tables.size());
      
     
- 
     
      
     
     
      
                       if (newSize != _tables.size())
      
     
- 
     
      
     
     
      
                       {
      
     
- 
     
      
     
     
      
                           vector<Node*> newTable;
      
     
- 
     
      
     
     
      
                           newTable.resize(newSize, 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)) % newSize;
      
     
- 
     
      
     
     
      
                                   cur->_next = newTable[hashi];
      
     
- 
     
      
     
     
      
                                   newTable[hashi] = cur;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                                   cur = next;
      
     
- 
     
      
     
     
      
                               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                               _tables[i] = nullptr;
      
     
- 
     
      
     
     
      
                           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                           newTable.swap(_tables);
      
     
- 
     
      
     
     
      
                       }
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   size_t hashi = hf(kot(data));
      
     
- 
     
      
     
     
      
                   hashi %= _tables.size();
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   // 头插到对应的桶即可
      
     
- 
     
      
     
     
      
                   Node* newnode = new Node(data);
      
     
- 
     
      
     
     
      
                   newnode->_next = _tables[hashi];
      
     
- 
     
      
     
     
      
                   _tables[hashi] = newnode;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   ++_n;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   return make_pair(iterator(newnode, this), false);;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               iterator Find(const K& key)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   if (_tables.size() == 0)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       return iterator(nullptr, this);
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   KeyOfT kot;
      
     
- 
     
      
     
     
      
                   HashFunc hf;
      
     
- 
     
      
     
     
      
                   size_t hashi = hf(key);
      
     
- 
     
      
     
     
      
                   //size_t hashi = HashFunc()(key);
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   hashi %= _tables.size();
      
     
- 
     
      
     
     
      
                   Node* cur = _tables[hashi];
      
     
- 
     
      
     
     
      
                   while (cur)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       if (kot(cur->_data) == key)
      
     
- 
     
      
     
     
      
                       {
      
     
- 
     
      
     
     
      
                           return iterator(cur, this);
      
     
- 
     
      
     
     
      
                       }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                       cur = cur->_next;
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   return iterator(nullptr, this);
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               bool Erase(const K& key)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   if (_tables.size() == 0)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       return false;
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
                   HashFunc hf;
      
     
- 
     
      
     
     
      
                   KeyOfT kot;
      
     
- 
     
      
     
     
      
                   size_t hashi = hf(key);
      
     
- 
     
      
     
     
      
                   hashi %= _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;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           private:
      
     
- 
     
      
     
     
      
               // 指针数组
      
     
- 
     
      
     
     
      
               vector<Node*> _tables;
      
     
- 
     
      
     
     
      
               size_t _n = 0;
      
     
- 
     
      
     
     
      
           };
      
     
- 
     
      
     
     
      
       }
      
     
  UnorderedMap.h
   
    - 
     
      
     
     
      
       #pragma once
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       #include "HashTable.h"
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<class K, class V, class HashFunc = DefaultHash<K>>
      
     
- 
     
      
     
     
      
       class unordered_map
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           struct MapKeyOfT
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               const K& operator()(const pair<K, V>& kv)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return kv.first;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
           };
      
     
- 
     
      
     
     
      
       public:
      
     
- 
     
      
     
     
      
           typedef typename Bucket::HashTable<K, pair<K, V>, MapKeyOfT, HashFunc>::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);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           iterator find(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _ht.Find(key);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           bool erase(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _ht.Erase(key);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           V& operator[](const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               pair<iterator, bool> ret = insert(make_pair(key, V()));
      
     
- 
     
      
     
     
      
               return ret.first->second;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       private:
      
     
- 
     
      
     
     
      
           Bucket::HashTable<K, pair<K, V>, MapKeyOfT, HashFunc> _ht;
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       void test_map()
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           unordered_map<string, string> dict;
      
     
- 
     
      
     
     
      
           dict.insert(make_pair("sort", ""));
      
     
- 
     
      
     
     
      
           dict.insert(make_pair("left", ""));
      
     
- 
     
      
     
     
      
           dict.insert(make_pair("left", "ʣ"));
      
     
- 
     
      
     
     
      
           dict["string"];
      
     
- 
     
      
     
     
      
           dict["left"] = "ʣ";
      
     
- 
     
      
     
     
      
           dict["string"] = "ַ";
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           unordered_map<string, string>::iterator it = dict.begin();
      
     
- 
     
      
     
     
      
           while (it != dict.end())
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               cout << it->first << " " << it->second << endl;
      
     
- 
     
      
     
     
      
               ++it;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           cout << endl;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           for (auto& kv : dict)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               cout << kv.first << " " << kv.second << endl;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       }
      
     
  UnorderedSet.h
   
    - 
     
      
     
     
      
       #pragma once
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       #include "HashTable.h"
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<class K, class HashFunc = DefaultHash<K>>
      
     
- 
     
      
     
     
      
       class unordered_set
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           struct SetKeyOfT
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               const K& operator()(const K& key)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return key;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
           };
      
     
- 
     
      
     
     
      
       public:
      
     
- 
     
      
     
     
      
           typedef typename Bucket::HashTable<K, K, SetKeyOfT, HashFunc>::iterator iterator;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           iterator begin()
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _ht.begin();
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           iterator end()
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _ht.end();
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           pair<iterator, bool> insert(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _ht.Insert(key);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           iterator find(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _ht.Find(key);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           bool erase(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _ht.Erase(key);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       private:
      
     
- 
     
      
     
     
      
           Bucket::HashTable<K, K, SetKeyOfT, HashFunc> _ht;
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       struct Date
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           Date(int year = 1, int month = 1, int day = 1)
      
     
- 
     
      
     
     
      
               :_year(year)
      
     
- 
     
      
     
     
      
               , _month(month)
      
     
- 
     
      
     
     
      
               , _day(day)
      
     
- 
     
      
     
     
      
           {}
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           bool operator==(const Date& d) const
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               return _year == d._year
      
     
- 
     
      
     
     
      
                   && _month == d._month
      
     
- 
     
      
     
     
      
                   && _day == d._day;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           int _year;
      
     
- 
     
      
     
     
      
           int _month;
      
     
- 
     
      
     
     
      
           int _day;
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       struct DateHash
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           size_t operator()(const Date& d)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               //return d._year + d._month + d._day;
      
     
- 
     
      
     
     
      
               size_t hash = 0;
      
     
- 
     
      
     
     
      
               hash += d._year;
      
     
- 
     
      
     
     
      
               hash *= 131;
      
     
- 
     
      
     
     
      
               hash += d._month;
      
     
- 
     
      
     
     
      
               hash *= 1313;
      
     
- 
     
      
     
     
      
               hash += d._day;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               //cout << hash << endl;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               return hash;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       void test_set()
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           unordered_set<int> s;
      
     
- 
     
      
     
     
      
           //set<int> s;
      
     
- 
     
      
     
     
      
           s.insert(2);
      
     
- 
     
      
     
     
      
           s.insert(3);
      
     
- 
     
      
     
     
      
           s.insert(1);
      
     
- 
     
      
     
     
      
           s.insert(2);
      
     
- 
     
      
     
     
      
           s.insert(5);
      
     
- 
     
      
     
     
      
           s.insert(12);
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           //unordered_set<int>::iterator it = s.begin();
      
     
- 
     
      
     
     
      
           unordered_set<int>::iterator it;
      
     
- 
     
      
     
     
      
           it = s.begin();
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           //auto it = s.begin();
      
     
- 
     
      
     
     
      
           while (it != s.end())
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               cout << *it << " ";
      
     
- 
     
      
     
     
      
               ++it;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
           cout << endl;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           for (auto e : s)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               cout << e << " ";
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
           cout << endl;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           unordered_set<Date, DateHash> sd;
      
     
- 
     
      
     
     
      
           sd.insert(Date(2022, 3, 4));
      
     
- 
     
      
     
     
      
           sd.insert(Date(2022, 4, 3));
      
     
- 
     
      
     
     
      
       }
      
     
  哈希的应用
位图
位图概念
所谓位图,就是用每一位来存放某种状态,适用于海量数据,数据无重复的场景。通常是用来判断某个
数据存不存在的。
给40亿个不重复的无符号整数,没排过序。给一个无符号整数,如何快速判断一个数是否在这40亿个数
中。【腾讯】
1. 遍历,时间复杂度O(N)
2. 排序(O(NlogN)),利用二分查找: logN
3. 位图解决
数据是否在给定的整形数据中,结果是在或者不在,刚好是两种状态,那么可以使用一个二进制比
特位来代表数据是否存在的信息,如果二进制比特位为1,代表存在,为0代表不存在。比如:
 
 
   位图的实现
   
    - 
     
      
     
     
      
       // N个比特位的位图  10  16
      
     
- 
     
      
     
     
      
       template<size_t N>
      
     
- 
     
      
     
     
      
       class bitset
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
       public:
      
     
- 
     
      
     
     
      
           bitset()
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               // +1保证足够比特位,最多浪费8个
      
     
- 
     
      
     
     
      
               _bits.resize(N / 8 + 1, 0);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           //x映射的位标记成1
      
     
- 
     
      
     
     
      
           void set(size_t x)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               // x映射的比特位在第几个char对象
      
     
- 
     
      
     
     
      
               size_t i = x / 8;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               // x在char第几个比特位
      
     
- 
     
      
     
     
      
               size_t j = x % 8;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               _bits[i] |= (1 << j);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           void reset(size_t x)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               // x映射的比特位在第几个char对象
      
     
- 
     
      
     
     
      
               size_t i = x / 8;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               // x在char第几个比特位
      
     
- 
     
      
     
     
      
               size_t j = x % 8;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               //! && ||
      
     
- 
     
      
     
     
      
               //~ &  | 
      
     
- 
     
      
     
     
      
               _bits[i] &= (~(1 << j));
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           bool test(size_t x)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               // x映射的比特位在第几个char对象
      
     
- 
     
      
     
     
      
               size_t i = x / 8;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               // x在char第几个比特位
      
     
- 
     
      
     
     
      
               size_t j = x % 8;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               return _bits[i] & (1 << j);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       private:
      
     
- 
     
      
     
     
      
           std::vector<char> _bits;
      
     
- 
     
      
     
     
      
           //vector<int> _bits;
      
     
- 
     
      
     
     
      
       };
      
     
  
   
    - 
     
      
     
     
      
       template<size_t N>
      
     
- 
     
      
     
     
      
           class two_bitset
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
           public:
      
     
- 
     
      
     
     
      
               void set(size_t x)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   int in1 = _bs1.test(x);
      
     
- 
     
      
     
     
      
                   int in2 = _bs2.test(x);
      
     
- 
     
      
     
     
      
                   if (in1 == 0 && in2 == 0)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       _bs2.set(x);
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
                   else if (in1 == 0 && in2 == 1)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       _bs1.set(x);
      
     
- 
     
      
     
     
      
                       _bs2.reset(x);
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               bool is_once(size_t x)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return _bs1.test(x) == 0 && _bs2.test(x) == 1;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           private:
      
     
- 
     
      
     
     
      
               bitset<N> _bs1;
      
     
- 
     
      
     
     
      
               bitset<N> _bs2;
      
     
- 
     
      
     
     
      
           };
      
     
  位图的应用
1. 快速查找某个数据是否在一个集合中
2. 排序 + 去重
3. 求两个集合的交集、并集等
4. 操作系统中磁盘块标记
布隆过滤器
布隆过滤器提出
我们在使用新闻客户端看新闻时,它会给我们不停地推荐新的内容,它每次推荐时要去重,去掉那些已经看
过的内容。问题来了,新闻客户端推荐系统如何实现推送去重的? 用服务器记录了用户看过的所有历史记
录,当推荐系统推荐新闻时会从每个用户的历史记录里进行筛选,过滤掉那些已经存在的记录。 如何快速查
找呢?
1. 用哈希表存储用户记录,缺点:浪费空间
2. 用位图存储用户记录,缺点:位图一般只能处理整形,如果内容编号是字符串,就无法处理了。
3. 将哈希与位图结合,即布隆过滤器
布隆过滤器概念
布隆过滤器是由布隆(Burton Howard Bloom)在1970年提出的 一种紧凑型的、比较巧妙的概率型数据结
构,特点是高效地插入和查询,可以用来告诉你 “某样东西一定不存在或者可能存在”,它是用多个哈希函
数,将一个数据映射到位图结构中。此种方式不仅可以提升查询效率,也可以节省大量的内存空间。
 
 
   布隆过滤器的插入
 
 
   布隆过滤器的查找
布隆过滤器的思想是将一个元素用多个哈希函数映射到一个位图中,因此被映射到的位置的比特位一定为1。
所以可以按照以下方式进行查找:分别计算每个哈希值对应的比特位置存储的是否为零,只要有一个为零,
代表该元素一定不在哈希表中,否则可能在哈希表中。
注意:布隆过滤器如果说某个元素不存在时,该元素一定不存在,如果该元素存在时,该元素可能存在,因
为有些哈希函数存在一定的误判。
比如:在布隆过滤器中查找"alibaba"时,假设3个哈希函数计算的哈希值为:1、3、7,刚好和其他元素的比
特位重叠,此时布隆过滤器告诉该元素存在,但实该元素是不存在的。
布隆过滤器删除
布隆过滤器不能直接支持删除工作,因为在删除一个元素时,可能会影响其他元素。
比如:删除上图中"tencent"元素,如果直接将该元素所对应的二进制比特位置0,“baidu”元素也被删除了,
因为这两个元素在多个哈希函数计算出的比特位上刚好有重叠。
一种支持删除的方法:将布隆过滤器中的每个比特位扩展成一个小的计数器,插入元素时给k个计数器(k个哈
希函数计算出的哈希地址)加一,删除元素时,给k个计数器减一,通过多占用几倍存储空间的代价来增加删
除操作。
缺陷:
1. 无法确认元素是否真正在布隆过滤器中
2. 存在计数回绕
布隆过滤器优点
1. 增加和查询元素的时间复杂度为:O(K), (K为哈希函数的个数,一般比较小),与数据量大小无关。
2. 哈希函数相互之间没有关系,方便硬件并行运算。
3. 布隆过滤器不需要存储元素本身,在某些对保密要求比较严格的场合有很大优势。
4. 在能够承受一定的误判时,布隆过滤器比其他数据结构有这很大的空间优势。
5. 数据量很大时,布隆过滤器可以表示全集,其他数据结构不能。
6. 使用同一组散列函数的布隆过滤器可以进行交、并、差运算。
布隆过滤器缺陷
1. 有误判率,即存在假阳性(False Position),即不能准确判断元素是否在集合中(补救方法:再建立一个白
名单,存储可能会误判的数据)。
2. 不能获取元素本身。
3. 一般情况下不能从布隆过滤器中删除元素。
4. 如果采用计数方式删除,可能会存在计数回绕问题。
   
    - 
     
      
     
     
      
       #include<string>
      
     
- 
     
      
     
     
      
       using namespace std;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       struct BKDRHash
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           size_t operator()(const string& s)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               // BKDR
      
     
- 
     
      
     
     
      
               size_t value = 0;
      
     
- 
     
      
     
     
      
               for (auto ch : s)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   value *= 31;
      
     
- 
     
      
     
     
      
                   value += ch;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
               return value;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       struct APHash
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           size_t operator()(const string& s)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               size_t hash = 0;
      
     
- 
     
      
     
     
      
               for (long i = 0; i < s.size(); i++)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   if ((i & 1) == 0)
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       hash ^= ((hash << 7) ^ s[i] ^ (hash >> 3));
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
                   else
      
     
- 
     
      
     
     
      
                   {
      
     
- 
     
      
     
     
      
                       hash ^= (~((hash << 11) ^ s[i] ^ (hash >> 5)));
      
     
- 
     
      
     
     
      
                   }
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
               return hash;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       struct DJBHash
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           size_t operator()(const string& s)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               size_t hash = 5381;
      
     
- 
     
      
     
     
      
               for (auto ch : s)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   hash += (hash << 5) + ch;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
               return hash;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       struct JSHash
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
           size_t operator()(const string& s)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               size_t hash = 1315423911;
      
     
- 
     
      
     
     
      
               for (auto ch : s)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   hash ^= ((hash << 5) + ch + (hash >> 2));
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
      
               return hash;
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
      
       };
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       template<size_t M,
      
     
- 
     
      
     
     
      
           class K = string,
      
     
- 
     
      
     
     
      
           class HashFunc1 = BKDRHash,
      
     
- 
     
      
     
     
      
           class HashFunc2 = APHash,
      
     
- 
     
      
     
     
      
           class HashFunc3 = DJBHash,
      
     
- 
     
      
     
     
      
           class HashFunc4 = JSHash>
      
     
- 
     
      
     
     
      
           class BloomFilter
      
     
- 
     
      
     
     
      
       {
      
     
- 
     
      
     
     
      
       public:
      
     
- 
     
      
     
     
      
           void Set(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               size_t hash1 = HashFunc1()(key) % M;
      
     
- 
     
      
     
     
      
               size_t hash2 = HashFunc2()(key) % M;
      
     
- 
     
      
     
     
      
               size_t hash3 = HashFunc3()(key) % M;
      
     
- 
     
      
     
     
      
               size_t hash4 = HashFunc4()(key) % M;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               //cout << hash1 << " " << hash2 << " " << hash3 << endl;
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               _bs.set(hash1);
      
     
- 
     
      
     
     
      
               _bs.set(hash2);
      
     
- 
     
      
     
     
      
               _bs.set(hash3);
      
     
- 
     
      
     
     
      
               _bs.set(hash4);
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           bool Test(const K& key)
      
     
- 
     
      
     
     
      
           {
      
     
- 
     
      
     
     
      
               size_t hash1 = HashFunc1()(key) % M;
      
     
- 
     
      
     
     
      
               if (_bs.test(hash1) == false)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return false;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               size_t hash2 = HashFunc2()(key) % M;
      
     
- 
     
      
     
     
      
               if (_bs.test(hash2) == false)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return false;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               size_t hash3 = HashFunc3()(key) % M;
      
     
- 
     
      
     
     
      
               if (_bs.test(hash3) == false)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return false;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               size_t hash4 = HashFunc4()(key) % M;
      
     
- 
     
      
     
     
      
               if (_bs.test(hash4) == false)
      
     
- 
     
      
     
     
      
               {
      
     
- 
     
      
     
     
      
                   return false;
      
     
- 
     
      
     
     
      
               }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
               return true; // 存在误判
      
     
- 
     
      
     
     
      
           }
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
           bool Reset(const K& key);
      
     
- 
     
      
     
     
       
      
     
- 
     
      
     
     
      
       private:
      
     
- 
     
      
     
     
      
           bitset<M> _bs;
      
     
- 
     
      
     
     
      
       };
      
     
  海量数据处理
哈希切割
给一个超过100G大小的log file, log中存着IP地址, 设计算法找到出现次数最多的IP地址?
如何找到top K的IP?
 
 
   布隆过滤器
给两个文件,分别有100亿个query,我们只有1G内存,如何找到两个文件交集?分别给出精确算法和
近似算法。
 
 
   最后的最后,创作不易,希望读者三连支持 💖
赠人玫瑰,手有余香 💖
转载:https://blog.csdn.net/weixin_62700590/article/details/128600535
查看评论
					 
					