• C++ STL(九) -------- 哈希表封装unordered_map和unordered_set


    目录

    1.哈希表源代码

    2.哈希表模板参数的控制

    3.string类型无法取模的问题

    4.哈希表默认成员函数实现

    5.哈希表正向迭代器的实现

    6.哈希表的实现进行补充 

    7.unordered_set的模拟实现 

    8.unordered_map的模拟实现 

    9.封装之后的代码


     

    1.哈希表源代码

    • 对一个KV模型的哈希表进行封装,同时模拟实现出C++STL库当中的unordered_map和unordered_set
    1. //每个哈希桶中存储数据的结构
    2. template<class K, class V>
    3. struct HashNode
    4. {
    5. pair _kv;
    6. HashNode* _next;
    7. //构造函数
    8. HashNode(const pair& kv)
    9. :_kv(kv)
    10. , _next(nullptr)
    11. {}
    12. };
    13. //哈希表
    14. template<class K, class V>
    15. class HashTable
    16. {
    17. typedef HashNode Node; //哈希结点类型
    18. public:
    19. //获取本次增容后哈希表的大小
    20. size_t GetNextPrime(size_t prime)
    21. {
    22. const int PRIMECOUNT = 28;
    23. //素数序列
    24. const size_t primeList[PRIMECOUNT] =
    25. {
    26. 53ul, 97ul, 193ul, 389ul, 769ul,
    27. 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
    28. 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
    29. 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
    30. 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
    31. 1610612741ul, 3221225473ul, 4294967291ul
    32. };
    33. size_t i = 0;
    34. for (i = 0; i < PRIMECOUNT; i++)
    35. {
    36. if (primeList[i] > prime)
    37. return primeList[i];
    38. }
    39. return primeList[i];
    40. }
    41. //插入函数
    42. bool Insert(const pair& kv)
    43. {
    44. //1、查看哈希表中是否存在该键值的键值对
    45. Node* ret = Find(kv.first);
    46. if (ret) //哈希表中已经存在该键值的键值对(不允许数据冗余)
    47. {
    48. return false; //插入失败
    49. }
    50. //2、判断是否需要调整哈希表的大小
    51. if (_n == _table.size()) //哈希表的大小为0,或负载因子超过1
    52. {
    53. //增容
    54. //a、创建一个新的哈希表,新哈希表的大小设置为原哈希表的2倍(若哈希表大小为0,则将哈希表的初始大小设置为10)
    55. vector newtable;
    56. newtable.resize(GetNextPrime(_table.size()));
    57. //b、将原哈希表当中的结点插入到新哈希表
    58. for (size_t i = 0; i < _table.size(); i++)
    59. {
    60. if (_table[i]) //桶不为空
    61. {
    62. Node* cur = _table[i];
    63. while (cur) //将该桶的结点取完为止
    64. {
    65. Node* next = cur->_next; //记录cur的下一个结点
    66. size_t index = cur->_kv.first%newtable.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    67. //将该结点头插到新哈希表中编号为index的哈希桶中
    68. cur->_next = newtable[index];
    69. newtable[index] = cur;
    70. cur = next; //取原哈希表中该桶的下一个结点
    71. }
    72. _table[i] = nullptr; //该桶取完后将该桶置空
    73. }
    74. }
    75. //c、交换这两个哈希表
    76. _table.swap(newtable);
    77. }
    78. //3、将键值对插入哈希表
    79. size_t index = kv.first % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    80. Node* newnode = new Node(kv); //根据所给数据创建一个待插入结点
    81. //将该结点头插到新哈希表中编号为index的哈希桶中
    82. newnode->_next = _table[index];
    83. _table[index] = newnode;
    84. //4、哈希表中的有效元素个数加一
    85. _n++;
    86. return true;
    87. }
    88. //查找函数
    89. HashNode* Find(const K& key)
    90. {
    91. if (_table.size() == 0) //哈希表大小为0,查找失败
    92. {
    93. return nullptr;
    94. }
    95. size_t index = key % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    96. //遍历编号为index的哈希桶
    97. HashNode* cur = _table[index];
    98. while (cur) //直到将该桶遍历完为止
    99. {
    100. if (cur->_kv.first == key) //key值匹配,则查找成功
    101. {
    102. return cur;
    103. }
    104. cur = cur->_next;
    105. }
    106. return nullptr; //直到该桶全部遍历完毕还没有找到目标元素,查找失败
    107. }
    108. //删除函数
    109. bool Erase(const K& key)
    110. {
    111. //1、通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    112. size_t index = key % _table.size();
    113. //2、在编号为index的哈希桶中寻找待删除结点
    114. Node* prev = nullptr;
    115. Node* cur = _table[index];
    116. while (cur) //直到将该桶遍历完为止
    117. {
    118. if (cur->_kv.first == key) //key值匹配,则查找成功
    119. {
    120. //3、若找到了待删除结点,则删除该结点
    121. if (prev == nullptr) //待删除结点是哈希桶中的第一个结点
    122. {
    123. _table[index] = cur->_next; //将第一个结点从该哈希桶中移除
    124. }
    125. else //待删除结点不是哈希桶的第一个结点
    126. {
    127. prev->_next = cur->_next; //将该结点从哈希桶中移除
    128. }
    129. delete cur; //释放该结点
    130. //4、删除结点后,将哈希表中的有效元素个数减一
    131. _n--;
    132. return true; //删除成功
    133. }
    134. prev = cur;
    135. cur = cur->_next;
    136. }
    137. //假删除可能会导致迭代器失效
    138. return false; //直到该桶全部遍历完毕还没有找到待删除元素,删除失败
    139. }
    140. private:
    141. vector _table; //哈希表
    142. size_t _n = 0; //哈希表中的有效元素个数
    143. };

                            

                                            

    2.哈希表模板参数的控制

    (1)用一份哈希表代码同时封装出K模型和KV模型的容器,我们必定要对哈希表的模板参数进行控制。 

    ①与原哈希表的模板参数进行区分,这里将哈希表的第二个模板参数的名字改为T

    1. template<class K, class T>
    2. class HashTable

                     

    ②如果上层使用的是unordered_set容器,那么传入哈希表的模板参数就是key和key

    1. template<class K>
    2. class unordered_set
    3. {
    4. public:
    5. //...
    6. private:
    7. HashTable _ht; //传入底层哈希表的是K和K
    8. };

                     

     ③如果上层使用的是unordered_map容器,那么传入哈希表的模板参数就是key以及key和value构成的键值对

    1. template<class K, class V>
    2. class unordered_map
    3. {
    4. public:
    5. //...
    6. private:
    7. HashTable> _ht; //传入底层哈希表的是K以及K和V构成的键值对
    8. };

                     

    ④哈希表中的模板参数T的类型到底是什么,完全却决于上层所使用容器的种类。

                             

    ⑤更改模板参数后,哈希结点的定义如下 

    1. //哈希结点的定义
    2. template<class T>
    3. struct HashNode
    4. {
    5. T _data;
    6. HashNode* _next;
    7. //构造函数
    8. HashNode(const T& data)
    9. :_data(data)
    10. , _next(nullptr)
    11. {}
    12. };

                             

    (2)仿函数获取键值

    现在由于我们在哈希结点当中存储的数据类型是T,这个T可能就是一个键值,也可能是一个键值对,对于底层的哈希表来说,它并不知道哈希结点当中存储的数据究竟是什么类型,因此需要由上层容器提供一个仿函数,用于获取T类型数据当中的键值。 

                             

    ①unordered_map容器需要向底层哈希表提供一个仿函数,该仿函数返回键值对当中的键值。 

    1. template<class K, class V>
    2. class unordered_map
    3. {
    4. //仿函数
    5. struct MapKeyOfT
    6. {
    7. const K& operator()(const pair& kv) //返回键值对当中的键值key
    8. {
    9. return kv.first;
    10. }
    11. };
    12. public:
    13. //...
    14. private:
    15. HashTable, MapKeyOfT> _ht;
    16. };

                             

     ②unordered_set容器传入哈希表的T就是键值,但是底层哈希表并不知道上层容器的种类,底层哈希表在获取键值时会统一通过传入的仿函数进行获取,因此unordered_set容器也需要向底层哈希表提供一个仿函数。

    1. template<class K>
    2. class unordered_set
    3. {
    4. //仿函数
    5. struct SetKeyOfT
    6. {
    7. const K& operator()(const K& key) //返回键值key
    8. {
    9. return key;
    10. }
    11. };
    12. public:
    13. //...
    14. private:
    15. HashTable _ht;
    16. };

    ·· 

    ③底层哈希表的模板参数现在需要增加一个,用于接收上层容器提供的仿函数。

    1. template<class K, class T, class KeyOfT>
    2. class HashTable

                            

                            

                             

    3.string类型无法取模的问题

    (1)字符串无法取模,是哈希问题中最常见的问题

    • 经过上面的分析后,我们让哈希表增加了一个模板参数,此时无论上层容器是unordered_set还是unordered_map,我们都能够通过上层容器提供的仿函数获取到元素的键值。
    • 但是在我们日常编写的代码中,用字符串去做键值key是非常常见的事,比如我们用unordered_map容器统计水果出现的次数时,就需要用各个水果的名字作为键值。
    • 而字符串并不是整型,也就意味着字符串不能直接用于计算哈希地址,我们需要通过某种方法将字符串转换成整型后,才能代入哈希函数计算哈希地址。
    • 但遗憾的是,我们无法找到一种能实现字符串和整型之间一对一转换的方法,因为在计算机中,整型的大小是有限的,比如用无符号整型能存储的最大数字是4294967295,而众多字符能构成的字符串的种类却是无限的。
    • 鉴于此,无论我们用什么方法将字符串转换成整型,都会存在哈希冲突,只是产生冲突的概率不同而已。
       

                             

     (2)BKDRHash算法无论是在实际效果还是编码实现中,效果都是最突出的

     ①现在我们需要在哈希表的模板参数中再增加一个仿函数,用于将键值key转换成对应的整型。

    1. template<class K, class T, class KeyOfT, class HashFunc = Hash>
    2. class HashTable

                             

    ②若是上层没有传入该仿函数,我们则使用默认的仿函数,该默认仿函数直接返回键值key即可,但是用字符串作为键值key是比较常见的,因此我们可以针对string类型写一个类模板的特化,此时当键值key为string类型时,该仿函数就会根据BKDRHash算法返回一个对应的整型。 

    1. template<class K>
    2. struct Hash
    3. {
    4. size_t operator()(const K& key) //返回键值key
    5. {
    6. return key;
    7. }
    8. };
    9. //string类型的特化
    10. template<>
    11. struct Hash
    12. {
    13. size_t operator()(const string& s) //BKDRHash算法
    14. {
    15. size_t value = 0;
    16. for (auto ch : s)
    17. {
    18. value = value * 131 + ch;
    19. }
    20. return value;
    21. }
    22. };

                                    

                                                    

    4.哈希表默认成员函数实现

    (1)构造函数

    • _table会自动调用vector的默认构造函数进行初始化。
    • _n会根据我们所给的缺省值被设置为0。
    1. vector _table; //哈希表
    2. size_t _n = 0; //哈希表中的有效元素个数

                             

    •  我们不需要编写构造函数,使用默认生成的构造函数就足够了,但是由于我们后面需要编写拷贝构造函数,编写了拷贝构造函数后,默认的构造函数就不会生成了,此时我们需要使用default关键字显示指定生成默认构造函数。
    1. //构造函数
    2. HashTable() = default; //显示指定生成默认构造函数

            

    (2)拷贝构造函数 

    哈希表的拷贝构造函数(深拷贝)实现逻辑如下:

    1. 将哈希表的大小调整为ht._table的大小。
    2. 将ht._table每个桶当中的结点一个个拷贝到自己的哈希表中。
    3. 更改哈希表当中的有效数据个数。
    1. //拷贝构造函数
    2. HashTable(const HashTable& ht)
    3. {
    4. //1、将哈希表的大小调整为ht._table的大小
    5. _table.resize(ht._table.size());
    6. //2、将ht._table每个桶当中的结点一个个拷贝到自己的哈希表中(深拷贝)
    7. for (size_t i = 0; i < ht._table.size(); i++)
    8. {
    9. if (ht._table[i]) //桶不为空
    10. {
    11. Node* cur = ht._table[i];
    12. while (cur) //将该桶的结点取完为止
    13. {
    14. Node* copy = new Node(cur->_data); //创建拷贝结点
    15. //将拷贝结点头插到当前桶
    16. copy->_next = _table[i];
    17. _table[i] = copy;
    18. cur = cur->_next; //取下一个待拷贝结点
    19. }
    20. }
    21. }
    22. //3、更改哈希表当中的有效数据个数
    23. _n = ht._n;
    24. }

                             

    (3)赋值运算符重载函数 

    • 现代写法
    •  实现赋值运算符重载函数时,可以通过参数间接调用拷贝构造函数,之后将拷贝构造出来的哈希表和当前哈希表的两个成员变量分别进行交换即可,当赋值运算符重载函数调用结束后,拷贝构造出来的哈希表会因为出了作用域而被自动析构,此时原哈希表之前的数据也就顺势被释放了。
    1. //赋值运算符重载函数
    2. HashTable& operator=(HashTable ht)
    3. {
    4. //交换哈希表中两个成员变量的数据
    5. _table.swap(ht._table);
    6. swap(_n, ht._n);
    7. return *this; //支持连续赋值
    8. }

                     

    (4)析构函数 

    •  哈希表当中存储的结点都是new出来的,因此在哈希表被析构时必须进行结点的释放。在析构哈希表时我们只需要依次取出非空的哈希桶,遍历哈希桶当中的结点并进行释放即可。
    1. //析构函数
    2. ~HashTable()
    3. {
    4. //将哈希表当中的结点一个个释放
    5. for (size_t i = 0; i < _table.size(); i++)
    6. {
    7. if (_table[i]) //桶不为空
    8. {
    9. Node* cur = _table[i];
    10. while (cur) //将该桶的结点取完为止
    11. {
    12. Node* next = cur->_next; //记录下一个结点
    13. delete cur; //释放结点
    14. cur = next;
    15. }
    16. _table[i] = nullptr; //将该哈希桶置空
    17. }
    18. }
    19. }

                    

                                            

    5.哈希表正向迭代器的实现

    (1)哈希表的正向迭代器实际上就是对哈希结点指针进行了封装,但是由于在实现++运算符重载时,可能需要在哈希表中去寻找下一个非空哈希桶,因此每一个正向迭代器中都应该存储哈希表的地址。 

    1. //正向迭代器
    2. template<class K, class T, class KeyOfT, class HashFunc = Hash>
    3. struct __HTIterator
    4. {
    5. typedef HashNode Node; //哈希结点的类型
    6. typedef HashTable HT; //哈希表的类型
    7. typedef __HTIterator Self; //正向迭代器的类型
    8. Node* _node; //结点指针
    9. HT* _pht; //哈希表的地址
    10. };

     

    (2)其他的实现

    1. //构造函数
    2. __HTIterator(Node* node, HT* pht)
    3. :_node(node) //结点指针
    4. , _pht(pht) //哈希表地址
    5. {}
    6. T& operator*()
    7. {
    8. return _node->_data; //返回哈希结点中数据的引用
    9. }
    10. T* operator->()
    11. {
    12. return &_node->_data; //返回哈希结点中数据的地址
    13. }
    14. bool operator!=(const Self& s) const
    15. {
    16. return _node != s._node; //判断两个结点的地址是否不同
    17. }
    18. bool operator==(const Self& s) const
    19. {
    20. return _node == s._node; //判断两个结点的地址是否相同
    21. }

                     

     (3)++运算符重载

    • 哈希表的迭代器类型是单向迭代器,没有反向迭代器,即没有实现- -运算符的重载,若是想让哈希表支持双向遍历,可以考虑将哈希桶中存储的单链表结构换为双链表结构。 

                    

    • 若当前结点不是当前哈希桶中的最后一个结点,则++后走到当前哈希桶的下一个结点。
    • 若当前结点是当前哈希桶的最后一个结点,则++后走到下一个非空哈希桶的第一个结点。
    1. //前置++
    2. Self& operator++()
    3. {
    4. if (_node->_next) //该结点不是当前哈希桶中的最后一个结点
    5. {
    6. _node = _node->_next; //++后变为当前哈希桶中的下一个结点
    7. }
    8. else //该结点是当前哈希桶中的最后一个结点
    9. {
    10. KeyOfT kot;
    11. HashFunc hf;
    12. size_t index = hf(kot(_node->_data)) % _pht->_table.size(); //通过哈希函数计算出当前所处哈希桶编号index(除数不能是capacity)
    13. index++; //从下一个位置开始找一个非空的哈希桶
    14. while (index < _pht->_table.size()) //直到将整个哈希表找完
    15. {
    16. if (_pht->_table[index]) //当前哈希桶非空
    17. {
    18. _node = _pht->_table[index]; //++后变为当前哈希桶中的第一个结点
    19. return *this;
    20. }
    21. index++; //当前哈希桶为空桶,找下一个哈希桶
    22. }
    23. _node = nullptr; //哈希表中已经没有空桶了,++后变为nullptr
    24. }
    25. return *this;
    26. }

                            

                     

    (4)哈希表的其他实现方式

    • 这种方式的单链表对于+ +实现更简单了

                             

     

    6.哈希表的实现进行补充 

    •  进行正向迭代器类型的typedef,需要注意的是,为了让外部能够使用typedef后的正向迭代器类型iterator,我们需要在public区域进行typedef。
    • 由于正向迭代器中++运算符重载函数在寻找下一个结点时,会访问哈希表中的成员变量_table,而_table成员变量是哈希表的私有成员,因此我们需要将正向迭代器类声明为哈希表类的友元。
    • 将哈希表中查找函数返回的结点指针,改为返回由结点指针和哈希表地址构成的正向迭代器。
    • 将哈希表中插入函数的返回值类型,改为由正向迭代器类型和布尔类型所构成的键值对。
    1. //哈希表的实现
    2. template<class K, class T, class KeyOfT, class HashFunc = Hash>
    3. class HashTable
    4. {
    5. //将正向迭代器类声明为哈希表类的友元
    6. template<class K, class T, class KeyOfT, class HashFunc>
    7. friend struct __HTIterator;
    8. typedef HashNode Node; //哈希结点类型
    9. public:
    10. typedef __HTIterator iterator; //正向迭代器的类型
    11. iterator begin()
    12. {
    13. size_t i = 0;
    14. while (i < _table.size()) //找到第一个非空哈希桶
    15. {
    16. if (_table[i]) //该哈希桶非空
    17. {
    18. return iterator(_table[i], this); //返回该哈希桶中的第一个结点的正向迭代器
    19. }
    20. i++;
    21. }
    22. return end(); //哈希桶中无数据,返回end()
    23. }
    24. iterator end()
    25. {
    26. return iterator(nullptr, this); //返回nullptr的正向迭代器
    27. }
    28. private:
    29. vector _table; //哈希表
    30. size_t _n = 0; //哈希表中的有效元素个数
    31. };

                    

                                    

    7.unordered_set的模拟实现 

    1. template<class K>
    2. class unordered_set
    3. {
    4. //仿函数
    5. struct SetKeyOfT
    6. {
    7. const K& operator()(const K& key) //返回键值key
    8. {
    9. return key;
    10. }
    11. };
    12. public:
    13. //现在没有实例化,没办法到HashTable里面找iterator,所以typename就是告诉编译器这里是一个类型,实例化以后再去取
    14. typedef typename HashTable::iterator iterator;
    15. iterator begin()
    16. {
    17. return _ht.begin();
    18. }
    19. iterator end()
    20. {
    21. return _ht.end();
    22. }
    23. //插入函数
    24. pairbool> insert(const K& key)
    25. {
    26. return _ht.Insert(key);
    27. }
    28. //删除函数
    29. void erase(const K& key)
    30. {
    31. _ht.Erase(key);
    32. }
    33. //查找函数
    34. iterator find(const K& key)
    35. {
    36. return _ht.Find(key);
    37. }
    38. private:
    39. HashTable _ht;
    40. };

                    

    8.unordered_map的模拟实现 

    1. template<class K, class V>
    2. class unordered_map
    3. {
    4. //仿函数
    5. struct MapKeyOfT
    6. {
    7. const K& operator()(const pair& kv) //返回键值对当中的键值key
    8. {
    9. return kv.first;
    10. }
    11. };
    12. public:
    13. typedef typename HashTable, MapKeyOfT>::iterator iterator;
    14. iterator begin()
    15. {
    16. return _ht.begin();
    17. }
    18. iterator end()
    19. {
    20. return _ht.end();
    21. }
    22. //插入函数
    23. pairbool> insert(const pair& kv)
    24. {
    25. return _ht.Insert(kv);
    26. }
    27. //赋值运算符重载
    28. V& operator[](const K& key)
    29. {
    30. pairbool> ret = insert(make_pair(key, V()));
    31. iterator it = ret.first;
    32. return it->second;
    33. }
    34. //删除函数
    35. void erase(const K& key)
    36. {
    37. _ht.Erase(key);
    38. }
    39. //查找函数
    40. iterator find(const K& key)
    41. {
    42. return _ht.Find(key);
    43. }
    44. private:
    45. HashTable, MapKeyOfT> _ht;
    46. };

                            

                     

    9.封装之后的代码

    (1)哈希表的代码 

    1. //哈希结点的定义
    2. template<class T>
    3. struct HashNode
    4. {
    5. T _data;
    6. HashNode* _next;
    7. //构造函数
    8. HashNode(const T& data)
    9. :_data(data)
    10. , _next(nullptr)
    11. {}
    12. };
    13. template<class K>
    14. struct Hash
    15. {
    16. size_t operator()(const K& key) //返回键值key
    17. {
    18. return key;
    19. }
    20. };
    21. //string类型的特化
    22. template<>
    23. struct Hash
    24. {
    25. size_t operator()(const string& s) //BKDRHash算法
    26. {
    27. size_t value = 0;
    28. for (auto ch : s)
    29. {
    30. value = value * 131 + ch;
    31. }
    32. return value;
    33. }
    34. };
    35. //哈希表的实现
    36. template<class K, class T, class KeyOfT, class HashFunc = Hash>
    37. class HashTable
    38. {
    39. //将正向迭代器类声明为哈希表类的友元
    40. template<class K, class T, class KeyOfT, class HashFunc>
    41. friend struct __HTIterator;
    42. //friend struct __HTIterator;
    43. typedef HashNode Node; //哈希结点类型
    44. public:
    45. typedef __HTIterator iterator; //正向迭代器的类型
    46. iterator begin()
    47. {
    48. size_t i = 0;
    49. while (i < _table.size()) //找到第一个非空哈希桶
    50. {
    51. if (_table[i]) //该哈希桶非空
    52. {
    53. return iterator(_table[i], this); //返回该哈希桶中的第一个结点的正向迭代器
    54. }
    55. i++;
    56. }
    57. return end(); //哈希桶中无数据,返回end()
    58. }
    59. iterator end()
    60. {
    61. return iterator(nullptr, this); //返回nullptr的正向迭代器
    62. }
    63. //构造函数
    64. HashTable() = default; //显示指定生成默认构造
    65. //拷贝构造函数
    66. HashTable(const HashTable& ht)
    67. {
    68. //1、将哈希表的大小调整为ht._table的大小
    69. _table.resize(ht._table.size());
    70. //2、将ht._table每个桶当中的结点一个个拷贝到自己的哈希表中(深拷贝)
    71. for (size_t i = 0; i < ht._table.size(); i++)
    72. {
    73. if (ht._table[i]) //桶不为空
    74. {
    75. Node* cur = ht._table[i];
    76. while (cur) //将该桶的结点取完为止
    77. {
    78. Node* copy = new Node(cur->_data); //创建拷贝结点
    79. //将拷贝结点头插到当前桶
    80. copy->_next = _table[i];
    81. _table[i] = copy;
    82. cur = cur->_next; //取下一个待拷贝结点
    83. }
    84. }
    85. }
    86. //3、更改哈希表当中的有效数据个数
    87. _n = ht._n;
    88. }
    89. //赋值运算符重载函数
    90. HashTable& operator=(HashTable ht)
    91. {
    92. //交换哈希表中两个成员变量的数据
    93. _table.swap(ht._table);
    94. swap(_n, ht._n);
    95. return *this; //支持连续赋值
    96. }
    97. //析构函数
    98. ~HashTable()
    99. {
    100. //将哈希表当中的结点一个个释放
    101. for (size_t i = 0; i < _table.size(); i++)
    102. {
    103. if (_table[i]) //桶不为空
    104. {
    105. Node* cur = _table[i];
    106. while (cur) //将该桶的结点取完为止
    107. {
    108. Node* next = cur->_next; //记录下一个结点
    109. delete cur; //释放结点
    110. cur = next;
    111. }
    112. _table[i] = nullptr; //将该哈希桶置空
    113. }
    114. }
    115. }
    116. //获取本次增容后哈希表的大小
    117. size_t GetNextPrime(size_t prime)
    118. {
    119. const int PRIMECOUNT = 28;
    120. //素数序列
    121. const size_t primeList[PRIMECOUNT] =
    122. {
    123. 53ul, 97ul, 193ul, 389ul, 769ul,
    124. 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
    125. 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
    126. 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
    127. 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
    128. 1610612741ul, 3221225473ul, 4294967291ul
    129. };
    130. size_t i = 0;
    131. for (i = 0; i < PRIMECOUNT; i++)
    132. {
    133. if (primeList[i] > prime)
    134. return primeList[i];
    135. }
    136. return primeList[i];
    137. }
    138. //插入函数
    139. pairbool> Insert(const T& data)
    140. {
    141. KeyOfT kot;
    142. //1、查看哈希表中是否存在该键值的键值对
    143. iterator ret = Find(kot(data));
    144. if (ret != end()) //哈希表中已经存在该键值的键值对(不允许数据冗余)
    145. {
    146. return make_pair(ret, false); //插入失败
    147. }
    148. //2、判断是否需要调整哈希表的大小
    149. if (_n == _table.size()) //哈希表的大小为0,或负载因子超过1
    150. {
    151. //增容
    152. //a、创建一个新的哈希表,新哈希表的大小设置为原哈希表的2倍(若哈希表大小为0,则将哈希表的初始大小设置为10)
    153. HashFunc hf;
    154. vector newtable;
    155. //size_t newsize = _table.size() == 0 ? 10 : _table.size() * 2;
    156. //newtable.resize(newsize);
    157. newtable.resize(GetNextPrime(_table.size()));
    158. //b、将原哈希表当中的结点插入到新哈希表
    159. for (size_t i = 0; i < _table.size(); i++)
    160. {
    161. if (_table[i]) //桶不为空
    162. {
    163. Node* cur = _table[i];
    164. while (cur) //将该桶的结点取完为止
    165. {
    166. Node* next = cur->_next; //记录cur的下一个结点
    167. size_t index = hf(kot(cur->_data))%newtable.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    168. //将该结点头插到新哈希表中编号为index的哈希桶中
    169. cur->_next = newtable[index];
    170. newtable[index] = cur;
    171. cur = next; //取原哈希表中该桶的下一个结点
    172. }
    173. _table[i] = nullptr; //该桶取完后将该桶置空
    174. }
    175. }
    176. //c、交换这两个哈希表
    177. _table.swap(newtable);
    178. }
    179. //3、将键值对插入哈希表
    180. HashFunc hf;
    181. size_t index = hf(kot(data)) % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    182. Node* newnode = new Node(data); //根据所给数据创建一个待插入结点
    183. //将该结点头插到新哈希表中编号为index的哈希桶中
    184. newnode->_next = _table[index];
    185. _table[index] = newnode;
    186. //4、哈希表中的有效元素个数加一
    187. _n++;
    188. return make_pair(iterator(newnode, this), true);
    189. }
    190. //查找函数
    191. iterator Find(const K& key)
    192. {
    193. if (_table.size() == 0) //哈希表大小为0,查找失败
    194. {
    195. return end();
    196. }
    197. KeyOfT kot;
    198. HashFunc hf;
    199. size_t index = hf(key) % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    200. //遍历编号为index的哈希桶
    201. HashNode* cur = _table[index];
    202. while (cur) //直到将该桶遍历完为止
    203. {
    204. if (kot(cur->_data) == key) //key值匹配,则查找成功
    205. {
    206. return iterator(cur, this);
    207. }
    208. cur = cur->_next;
    209. }
    210. return end(); //直到该桶全部遍历完毕还没有找到目标元素,查找失败
    211. }
    212. //删除函数
    213. bool Erase(const K& key)
    214. {
    215. KeyOfT kot;
    216. HashFunc hf;
    217. //1、通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    218. size_t index = hf(key) % _table.size();
    219. //2、在编号为index的哈希桶中寻找待删除结点
    220. Node* prev = nullptr;
    221. Node* cur = _table[index];
    222. while (cur) //直到将该桶遍历完为止
    223. {
    224. if (kot(cur->_data) == key) //key值匹配,则查找成功
    225. {
    226. //3、若找到了待删除结点,则删除该结点
    227. if (prev == nullptr) //待删除结点是哈希桶中的第一个结点
    228. {
    229. _table[index] = cur->_next; //将第一个结点从该哈希桶中移除
    230. }
    231. else //待删除结点不是哈希桶的第一个结点
    232. {
    233. prev->_next = cur->_next; //将该结点从哈希桶中移除
    234. }
    235. delete cur; //释放该结点
    236. //4、删除结点后,将哈希表中的有效元素个数减一
    237. _n--;
    238. return true; //删除成功
    239. }
    240. prev = cur;
    241. cur = cur->_next;
    242. }
    243. //假删除可能会导致迭代器失效
    244. return false; //直到该桶全部遍历完毕还没有找到待删除元素,删除失败
    245. }
    246. private:
    247. vector _table; //哈希表
    248. size_t _n = 0; //哈希表中的有效元素个数
    249. };

                     

    (2)正向迭代器的代码

    1. //前置声明, HashTable和__HTIterator互相依赖
    2. template<class K, class T, class KeyOfT, class HashFunc>
    3. class HashTable;
    4. //正向迭代器
    5. template<class K, class T, class KeyOfT, class HashFunc = Hash>
    6. struct __HTIterator
    7. {
    8. typedef HashNode Node; //哈希结点的类型
    9. typedef HashTable HT; //哈希表的类型
    10. typedef __HTIterator Self; //正向迭代器的类型
    11. Node* _node; //结点指针
    12. HT* _pht; //哈希表的地址
    13. //构造函数
    14. __HTIterator(Node* node, HT* pht)
    15. :_node(node) //结点指针
    16. , _pht(pht) //哈希表地址
    17. {}
    18. T& operator*()
    19. {
    20. return _node->_data; //返回哈希结点中数据的引用
    21. }
    22. T* operator->()
    23. {
    24. return &_node->_data; //返回哈希结点中数据的地址
    25. }
    26. bool operator!=(const Self& s) const
    27. {
    28. return _node != s._node; //判断两个结点的地址是否不同
    29. }
    30. bool operator==(const Self& s) const
    31. {
    32. return _node == s._node; //判断两个结点的地址是否相同
    33. }
    34. //前置++
    35. Self& operator++()
    36. {
    37. if (_node->_next) //该结点不是当前哈希桶中的最后一个结点
    38. {
    39. _node = _node->_next; //++后变为当前哈希桶中的下一个结点
    40. }
    41. else //该结点是当前哈希桶中的最后一个结点
    42. {
    43. KeyOfT kot;
    44. HashFunc hf;
    45. size_t index = hf(kot(_node->_data)) % _pht->_table.size(); //通过哈希函数计算出当前所处哈希桶编号index(除数不能是capacity)
    46. index++; //从下一个位置开始找一个非空的哈希桶
    47. while (index < _pht->_table.size()) //直到将整个哈希表找完
    48. {
    49. if (_pht->_table[index]) //当前哈希桶非空
    50. {
    51. _node = _pht->_table[index]; //++后变为当前哈希桶中的第一个结点
    52. return *this;
    53. }
    54. index++; //当前哈希桶为空桶,找下一个哈希桶
    55. }
    56. _node = nullptr; //哈希表中已经没有空桶了,++后变为nullptr
    57. }
    58. return *this;
    59. }
    60. };

                     

    (3)unordered_set的代码

    1. namespace XM //防止命名冲突
    2. {
    3. template<class K>
    4. class unordered_set
    5. {
    6. //仿函数
    7. struct SetKeyOfT
    8. {
    9. const K& operator()(const K& key) //返回键值key
    10. {
    11. return key;
    12. }
    13. };
    14. public:
    15. //现在没有实例化,没办法到HashTable里面找iterator,所以typename就是告诉编译器这里是一个类型,实例化以后再去取
    16. typedef typename HashTable::iterator iterator;
    17. iterator begin()
    18. {
    19. return _ht.begin();
    20. }
    21. iterator end()
    22. {
    23. return _ht.end();
    24. }
    25. //插入函数
    26. pairbool> insert(const K& key)
    27. {
    28. return _ht.Insert(key);
    29. }
    30. //删除函数
    31. void erase(const K& key)
    32. {
    33. _ht.Erase(key);
    34. }
    35. //查找函数
    36. iterator find(const K& key)
    37. {
    38. return _ht.Find(key);
    39. }
    40. private:
    41. HashTable _ht;
    42. };
    43. }

                     

    (4)unordered_map的代码 

    1. namespace XM //防止命名冲突
    2. {
    3. template<class K, class V>
    4. class unordered_map
    5. {
    6. //仿函数
    7. struct MapKeyOfT
    8. {
    9. const K& operator()(const pair& kv) //返回键值对当中的键值key
    10. {
    11. return kv.first;
    12. }
    13. };
    14. public:
    15. typedef typename HashTable, MapKeyOfT>::iterator iterator;
    16. iterator begin()
    17. {
    18. return _ht.begin();
    19. }
    20. iterator end()
    21. {
    22. return _ht.end();
    23. }
    24. //插入函数
    25. pairbool> insert(const pair& kv)
    26. {
    27. return _ht.Insert(kv);
    28. }
    29. //赋值运算符重载
    30. V& operator[](const K& key)
    31. {
    32. pairbool> ret = insert(make_pair(key, V()));
    33. iterator it = ret.first;
    34. return it->second;
    35. }
    36. //删除函数
    37. void erase(const K& key)
    38. {
    39. _ht.Erase(key);
    40. }
    41. //查找函数
    42. iterator find(const K& key)
    43. {
    44. return _ht.Find(key);
    45. }
    46. private:
    47. HashTable, MapKeyOfT> _ht;
    48. };
    49. }

  • 相关阅读:
    基于SpringBoot的的师生健康信息管理系统
    一篇文章带你了解Python常用自动化测试框架 —— Pytest
    阿里云Linux中安装MySQL,并使用navicat连接以及报错解决
    java飞机航班信息查询系统演示视频2021计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    用AI打造一个属于自己的歌手,让她C位霸气出道
    数据结构学习笔记——数据结构概论
    如何在 Rocky Linux 上安装 Apache Kafka?
    SSM+教学网站 毕业设计-附源码211611
    Java23种设计模式-创建型模式之单例模式
    vue项目npm run build报错Error: Cannot find module ‘@vue/cli-plugin-babel‘的解决方法
  • 原文地址:https://blog.csdn.net/m0_52169086/article/details/126709111