当前位置:   article > 正文

模拟实现unordered_set和unordered_map

模拟实现unordered_set和unordered_map

改造哈希表结构

1. 模板参数列表的改造

// K:关键码类型
// V: 不同容器V的类型不同,如果是unordered_map,V代表一个键值对,如果是
unordered_set,V 为 K
// KeyOfValue: 因为V的类型不同,通过value取key的方式就不同,详细见
unordered_map/set的实现
// Hash: 哈希函数仿函数对象类型,哈希函数使用除留余数法,需要将Key转换为整形数字才能取模
template<class K, class V, class KeyOfValue, class HF = DefHashF<T> >
class HashBucket;

2. 增加迭代器操作

  1. // 为了实现简单,在哈希桶的迭代器类中需要用到hashBucket本身,
  2. template<class K, class V, class KeyOfValue, class HF>
  3. class HashBucket;
  4. // 注意:因为哈希桶在底层是单链表结构,所以哈希桶的迭代器不需要--操作
  5. template <class K, class V, class KeyOfValue, class HF>
  6. struct HBIterator
  7. {
  8. typedef HashBucket<K, V, KeyOfValue, HF> HashBucket;
  9. typedef HashBucketNode<V>* PNode;
  10. typedef HBIterator<K, V, KeyOfValue, HF> Self;
  11. HBIterator(PNode pNode = nullptr, HashBucket* pHt = nullptr);
  12. Self& operator++()
  13. {
  14. // 当前迭代器所指节点后还有节点时直接取其下一个节点
  15. if (_pNode->_pNext)
  16. _pNode = _pNode->_pNext;
  17. else
  18. {
  19. // 找下一个不空的桶,返回该桶中第一个节点
  20. size_t bucketNo = _pHt->HashFunc(KeyOfValue()(_pNode -
  21. > _data)) + 1;
  22. for (; bucketNo < _pHt->BucketCount(); ++bucketNo)
  23. {
  24. if (_pNode = _pHt->_ht[bucketNo])
  25. break;
  26. }
  27. }
  28. return *this;
  29. }
  30. Self operator++(int);
  31. V& operator*();
  32. V* operator->();
  33. bool operator==(const Self& it) const;
  34. bool operator!=(const Self& it) const;
  35. PNode _pNode;       // 当前迭代器关联的节点
  36. HashBucket* _pHt;     // 哈希桶--主要是为了找下一个空桶时候方便
  37. };

 3. 增加通过key获取value操作

  1. template<class K, class V, class KeyOfValue, class HF = DefHashF<T> >
  2. class HashBucket
  3. {
  4. friend HBIterator<K, V, KeyOfValue, HF>;
  5. // ......
  6. public:
  7. typedef HBIterator<K, V, KeyOfValue, HF> Iterator;
  8. //
  9. // ...
  10. // 迭代器
  11. Iterator Begin()
  12. {
  13. size_t bucketNo = 0;
  14. for (; bucketNo < _ht.capacity(); ++bucketNo)
  15. {
  16. if (_ht[bucketNo])
  17. break;
  18. }
  19. if (bucketNo < _ht.capacity())
  20. return Iterator(_ht[bucketNo], this);
  21. else
  22. return Iterator(nullptr, this);
  23. }
  24. Iterator End() { return Iterator(nullptr, this); }
  25. Iterator Find(const K& key);
  26. Iterator Insert(const V& data);
  27. Iterator Erase(const K& key);
  28. // 为key的元素在桶中的个数
  29. size_t Count(const K& key)
  30. {
  31. if (Find(key) != End())
  32. return 1;
  33. return 0;
  34. }
  35. size_t BucketCount()const { return _ht.capacity(); }
  36. size_t BucketSize(size_t bucketNo)
  37. {
  38. size_t count = 0;
  39. PNode pCur = _ht[bucketNo];
  40. while (pCur)
  41. {
  42. count++;
  43. pCur = pCur->_pNext;
  44. }
  45. return count;
  46. }
  47. // ......
  48. };

unordered_map&unordered_set/哈希表封装unordered_xxx系列 · 杰编程/CPlusPlus - 码云 - 开源中国 (gitee.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/955622
推荐阅读
相关标签
  

闽ICP备14008679号