当前位置:   article > 正文

unordered_map模拟实现

unordered_map模拟实现

1 哈希表的改造

1. 模板参数列表的改造

  1. // K:关键码类型
  2. // V: 不同容器V的类型不同,如果是unordered_map,V代表一个键值对,如果是unordered_set,V 为 K
  3. // KeyOfValue: 因为V的类型不同,通过value取key的方式就不同,详细见unordered_map/set的实现
  4. // HF: 哈希函数仿函数对象类型,哈希函数使用除留余数法,需要将Key转换为整形数字才能取模
  5. template<class K, class V, class KeyOfValue, class HF = DefHashF<T> >
  6. 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. Iterator End(){ return Iterator(nullptr, this);}
  24. Iterator Find(const K& key);
  25. Iterator Insert(const V& data);
  26. Iterator Erase(const K& key);
  27. // 为key的元素在桶中的个数
  28. size_t Count(const K& key)
  29. {
  30. if(Find(key) != End())
  31. return 1;
  32. return 0;
  33. }
  34. size_t BucketCount()const{ return _ht.capacity();}
  35. size_t BucketSize(size_t bucketNo)
  36. {
  37. size_t count = 0;
  38. PNode pCur = _ht[bucketNo];
  39. while(pCur)
  40. {
  41. count++;
  42. pCur = pCur->_pNext;
  43. }
  44. return count;
  45. }
  46. // ......
  47. };

2 unordered_map 

  1. // unordered_map中存储的是pair<K, V>的键值对,K为key的类型,V为value的类型,HF哈希
  2. 函数类型
  3. // unordered_map在实现时,只需将hashbucket中的接口重新封装即可
  4. template<class K, class V, class HF = DefHashF<K>>
  5. class unordered_map
  6. {
  7. typedef pair<K, V> ValueType;
  8. typedef HashBucket<K, ValueType, KeyOfValue, HF> HT;
  9. // 通过key获取value的操作
  10. struct KeyOfValue
  11. {
  12. const K& operator()(const ValueType& data)
  13. { return data.first;}
  14. };
  15. public:
  16. typename typedef HT::Iterator iterator;
  17. public:
  18. unordered_map(): _ht()
  19. {}
  20. iterator begin(){ return _ht.Begin();}
  21. iterator end(){ return _ht.End();}
  22. // capacity
  23. size_t size()const{ return _ht.Size();}
  24. bool empty()const{return _ht.Empty();}
  25. ///
  26. // Acess
  27. V& operator[](const K& key)
  28. {
  29. return (*(_ht.InsertUnique(ValueType(key, V())).first)).second;
  30. }
  31. const V& operator[](const K& key)const;
  32. //
  33. // lookup
  34. iterator find(const K& key){ return _ht.Find(key);}
  35. size_t count(const K& key){ return _ht.Count(key);}
  36. /
  37. // modify
  38. pair<iterator, bool> insert(const ValueType& valye)
  39. { return _ht.Insert(valye);}
  40. iterator erase(iterator position)
  41. { return _ht.Erase(position);}
  42. // bucket
  43. size_t bucket_count(){ return _ht.BucketCount();}
  44. size_t bucket_size(const K& key){ return _ht.BucketSize(key);}
  45. private:
  46. HT _ht;
  47. };

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

闽ICP备14008679号