【数据结构】c++实现HashTable(开链法)

  1. #include <iostream>  
  2. #include <vector>  
  3. using namespace std;  
  4.   
  5. template <class K, class V>  
  6. struct HashTableNode  
  7. {  
  8.     K _key;  
  9.     V _value;  
  10.     HashTableNode<K, V>* _next;  
  11.   
  12.     HashTableNode(const K&key, const V&value)  
  13.         :_key(key)  
  14.         , _value(value)  
  15.         , _next(NULL)  
  16.     {}  
  17. };  
  18.   
  19. template <class K>  
  20. struct DefaultHashFunc  
  21. {  
  22.     size_t operator()(const K&key)  
  23.     {  
  24.         return key;  
  25.     }  
  26. };  
  27.   
  28. template <class K, class V, class HashFun = DefaultHashFunc<K>>  
  29. class HashTableBucket  
  30. {  
  31.     typedef HashTableNode<K, V> Node;  
  32. public:  
  33.     HashTableBucket()  
  34.         :_size(0)  
  35.     {}  
  36.     HashTableBucket(size_t size)  
  37.         :_size(0)  
  38.     {  
  39.         _tables.resize(size);  
  40.     }  
  41.   
  42.     bool Insert(const K&key, const V&value)  
  43.     {  
  44.         _CheckExpand();  
  45.         size_t index = HashFunc(key, _tables.size());  
  46.         Node* begin = _tables[index];  
  47.         while (begin)  
  48.         {  
  49.             if (begin->_key == key)  
  50.                 return false;  
  51.             begin = begin->_next;  
  52.         }  
  53.   
  54.         Node* tmp = new Node(key, value);  
  55.         tmp->_next = _tables[index];  
  56.         _tables[index] = tmp;  
  57.   
  58.         ++_size;  
  59.         return true;  
  60.     }  
  61.     size_t HashFunc(const K&key, size_t capacity)  
  62.     {  
  63.         HashFun hf;  
  64.         return hf(key) % capacity;  
  65.     }  
  66.     void PrintTables()  
  67.     {  
  68.         for (size_t i = 0; i < _tables.size(); ++i)  
  69.         {  
  70.             printf("Tables[%d]->",i);  
  71.             Node* cur = _tables[i];  
  72.             while (cur)  
  73.             {  
  74.                 cout << "[" << cur->_key << ":" << cur->_value << "]"<<"->";  
  75.                 cur = cur->_next;  
  76.   
  77.             }  
  78.             cout << "NULL"<<endl;  
  79.         }  
  80.         cout << endl;  
  81.     }  
  82. protected:  
  83.     size_t _GetNextPrime(size_t size)  
  84.     {  
  85.         static const int _PrimeSize = 28;  
  86.         static const unsigned long _PrimeList[_PrimeSize] =  
  87.         {  
  88.             53ul, 97ul, 193ul, 389ul, 769ul,  
  89.             1543ul, 3079ul, 6151ul, 12289ul, 24593ul,  
  90.             49157ul, 98317ul, 196613ul, 393241ul, 786433ul,  
  91.             1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,  
  92.             50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,  
  93.             1610612741ul, 3221225473ul, 4294967291ul  
  94.         };  
  95.   
  96.         for (size_t i = 0; i < _PrimeSize; ++i)  
  97.         {  
  98.             if (_PrimeList[i] > size)  
  99.             {  
  100.                 return _PrimeList[i];  
  101.             }  
  102.         }  
  103.   
  104.         return _PrimeList[_PrimeSize - 1];  
  105.     }  
  106.   
  107.     void _CheckExpand()  
  108.     {  
  109.         // 载荷因子到1时进行增容,保证效率  
  110.         if (_size == _tables.size())  
  111.         {  
  112.             size_t newSize = _GetNextPrime(_size);  
  113.             if (_size == newSize)  
  114.                 return;  
  115.   
  116.             vector<Node*> newTables;  
  117.             newTables.resize(newSize);  
  118.   
  119.             for (size_t i = 0; i < _tables.size(); ++i)  
  120.             {  
  121.                 Node* cur = _tables[i];  
  122.                 while (cur)  
  123.                 {  
  124.                     // 取节点  
  125.                     Node* tmp = cur;  
  126.                     cur = cur->_next;  
  127.   
  128.                     // 头插  
  129.                     size_t newIndex = HashFunc(tmp->_key, newTables.size());  
  130.                     tmp->_next = newTables[newIndex];  
  131.                     newTables[newIndex] = tmp;  
  132.                 }  
  133.   
  134.                 _tables[i] = NULL;  
  135.             }  
  136.   
  137.             _tables.swap(newTables);  
  138.         }  
  139.     }  
  140. protected:  
  141.     vector<HashTableNode<K, V>*> _tables;  
  142.     size_t _size;  
  143. };  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值