Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268848
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-03-29 17:25:07

1.线性探测

  1. #pragma once
  2. #include<string>
  3.  
  4. enum Status
  5. {
  6.     EMPTY,
  7.     EXISTS,
  8.     DELETE,
  9. };

  10. template<class K>
  11. struct HashFuner
  12. {
  13.     size_t operator()(const K &key,int capacity)
  14.     {
  15.         return key%capacity;
  16.     }
  17. };

  18. template <>
  19. struct HashFuner<string>
  20. {

  21.     static size_t BKDRHash(const char *s)
  22.     {
  23.         unsigned int seed = 131;
  24.         unsigned hash = 0;
  25.         while (*s)
  26.         {
  27.             hash = hash*seed + (*s++);

  28.         }
  29.         return (hash & 0x7FFFFFFF);
  30.     }
  31.     size_t operator()(const string &key,int capacity)
  32.     {
  33.         return BKDRHash(key.c_str())%capacity;
  34.     }
  35. };



  36. template<class K ,class Hashfuncer=HashFuner<K>>
  37. class HashTable
  38. {
  39. public:
  40.     HashTable()
  41.         :_array(NULL)
  42.         ,_size(0)
  43.         ,_capacity(0)
  44.         ,_status(NULL)
  45.     {

  46.     }
  47.     HashTable(size_t capacity)
  48.         :_array(new K[capacity])
  49.         , _size(0)
  50.         , _capacity(capacity)
  51.         , _status(new Status[capacity])
  52.     {
  53.         for (int i = 0; i < capacity; ++i)
  54.         {
  55.             _status[i] = EMPTY;
  56.         }
  57.     }
  58.     ~HashTable()
  59.     {
  60.         delete[] _array;
  61.         delete[] _status;
  62.     }


  63.     bool Insert(const K& key)
  64.     {
  65.         if (_size * 10 / _capacity > 8)//载荷因子大于0.8时要扩容
  66.         {
  67.             HashTable<K,Hashfuncer> *newHash = new HashTable<K, Hashfuncer>(_capacity * 3);

  68.             for (int i = 0; i < _capacity; ++i)
  69.             {
  70.                 if (_status[i] == EXISTS)
  71.                 {
  72.                     newHash->Insert(_array[i]);
  73.                 }
  74.             }
  75.             Swap(*newHash);
  76.         }
  77.         int index = Hashfuncer()(key,_capacity);
  78.         while (_status[index]==EXISTS)
  79.         {
  80.             index++;
  81.             if (index == _capacity)
  82.             {
  83.                 index = 0;
  84.             }
  85.         }
  86.         _array[index] = key;
  87.         _status[index] = EXISTS;
  88.         _size++;
  89.         return true;
  90.     }
  91.     bool Remove(K key)
  92.     {
  93.         int index = Hashfuncer()(key,_capacity);
  94.         while (_status[index] != EMPTY)
  95.         {
  96.             if (_array[index] == key)
  97.             {
  98.                 if (_status[index] == DELETE)
  99.                 {
  100.                     return false;
  101.                 }
  102.                 _status[index] = DELETE;
  103.                 return true;
  104.             }
  105.             index++;
  106.             if (index == _capacity)
  107.             {
  108.                 index = 0;
  109.             }
  110.         }
  111.         return false;
  112.     }
  113.     bool Find(K key)
  114.     {
  115.         int index = Hashfuncer()(key,_capacity);
  116.         while (_status[index] != EMPTY)
  117.         {
  118.             if (_array[index] == key)
  119.             {
  120.                 if (_status[index] == DELETE)
  121.                 {
  122.                     return false;
  123.                 }
  124.                 return true;
  125.             }
  126.             index++;
  127.             if (index == _capacity)
  128.             {
  129.                 index = 0;
  130.             }
  131.         }
  132.         return false;
  133.     }
  134.     void Print()
  135.     {
  136.         for (int i = 0; i < _capacity; ++i)
  137.         {
  138.             if (_status[i] == EXISTS)
  139.             {
  140.                 cout << '[' << _array[i]<< ']' << " ";
  141.             }
  142.             else
  143.                 cout << "[]" << " ";
  144.         }
  145.         cout << endl;
  146.     }

  147.     void Swap(HashTable<K, Hashfuncer> h)
  148.     {
  149.         swap(_array, h._array);
  150.         swap(_size, h._size);
  151.         swap(_capacity, h._capacity);
  152.         swap(_status, h._status);
  153.     }


  154. protected:
  155.     K* _array;
  156.     size_t _size;
  157.     size_t _capacity;
  158.     Status *_status;
  159. };

测试:

  1. #include <iostream>
  2. using namespace std;
  3. #include "HashTable.h"

  4. void Test()
  5. {
  6.     HashTable<string>ht1(3);
  7.     ht1.Insert("a");
  8.     ht1.Insert("b");
  9.     ht1.Insert("c");
  10.     ht1.Insert("d");
  11.     ht1.Insert("d");
  12.     ht1.Print();
  13.     ht1.Remove("c");
  14.     ht1.Print();
  15.     
  16.     if(ht1.Find("d"))
  17.     {
  18.         cout<<"find success!"<<endl;
  19.     }
  20.     if(ht1.Find("c"))
  21.     {
  22.         cout<<"aa"<<endl;
  23.     }

  24. }
  25. int main()
  26. {
  27.     Test();
  28.     return 0;
  29. }
2.二次探测

  1. #pragma once

  2. enum Status
  3. {
  4.     EXISTS,
  5.     DELETE,
  6.     EMPTY,
  7. };
  8. static size_t BKDRHash(const char *s)
  9. {
  10.     unsigned int seed = 131;
  11.     unsigned hash = 0;
  12.     while (*s)
  13.     {
  14.         hash = hash*seed + (*s++);

  15.     }
  16.     return (hash & 0x7FFFFFFF);
  17. }
  18. template<class K>
  19. size_t Hashfun0(const K &key, int _capacity)
  20. {

  21.     return BKDRHash(key.c_str()) % _capacity;
  22. }
  23. template<class K>
  24. size_t Hashfun2(const K & preHashNum, int i)
  25. {
  26.     return preHashNum + 2 * i - 1;
  27. }


  28. template<class K>
  29. struct HashFuner
  30. {
  31.     size_t operator()(const K &key)
  32.     {
  33.         return key;
  34.     }
  35. };
  36. template<>
  37. struct HashFuner<string>
  38. {
  39.     size_t operator()(const string &key, int _capacity)
  40.     {
  41.         return Hashfun0(key, _capacity);
  42.     }
  43. };

  44. template<class K,class V>
  45. class KeyValueNode
  46. {
  47. public:
  48.     KeyValueNode()
  49.     {}
  50.     KeyValueNode(const K& key,const V& value)
  51.         :_key(key)
  52.         ,_value(value)
  53.     {}
  54.     K _key;
  55.     V _value;
  56. };

  57. template<class K, class V, class HashFun = HashFuner<K>>
  58. class HashTable
  59. {
  60. public:
  61.     typedef KeyValueNode<K, V> KVNode;

  62.     HashTable()
  63.         :_tables(NULL)
  64.         , _size(0)
  65.         , _capacity(0)
  66.         , _status(NULL)
  67.     {}
  68.     HashTable(size_t capacity)
  69.         :_tables(new KVNode[capacity])
  70.         , _size(0)
  71.         , _capacity(capacity)
  72.         , _status(new Status[capacity])
  73.     {
  74.         for (int i = 0; i < _capacity; ++i)
  75.         {
  76.             _status[i] = EMPTY;
  77.         }
  78.     }
  79.     ~HashTable()
  80.     {
  81.         delete[] _tables;
  82.         delete[] _status;
  83.         _capacity = 0;
  84.         _size = 0;
  85.     }
  86.     bool Insert(const K& key,const V& value)
  87.     {
  88.         if (_size * 10 / _capacity > 8)
  89.         {
  90.             HashTable<K, V, HashFun> *newtables = new HashTable<K, V, HashFun>(3 * _capacity);
  91.             for (int i = 0; i < _capacity; ++i)
  92.             {
  93.                 newtables->Insert(_tables[i]._key, _tables[i]._value);
  94.             }
  95.             Swap(*newtables);
  96.         }
  97.         int i = 1;
  98.         int index = HashFun()(key, _capacity);
  99.         while (_status[index] == EXISTS)
  100.         {
  101.             index = Hashfun2(index, i++);
  102.             if (index >= _capacity)
  103.             {
  104.                 index = index%_capacity;
  105.             }
  106.         }
  107.         _tables[index] = KVNode(key, value);
  108.         _status[index] = EXISTS;
  109.         _size++;
  110.         return true;
  111.     }
  112.     bool Remove(const K& key)
  113.     {
  114.         int index = HashFun()(key, _capacity);
  115.         int i = 1;
  116.         while (_status[index] != EMPTY)
  117.         {
  118.             if (_array[index] == key)
  119.             {
  120.                 if (_status[index] == DELETE)
  121.                 {
  122.                     return false;
  123.                 }
  124.                 _status[index] = DELETE;
  125.                 return true;
  126.             }
  127.             index = Hashfun2(index, i++);
  128.             if (index >= _capacity)
  129.             {
  130.                 index = index%_capacity;
  131.             }
  132.         }
  133.         return false;
  134.     }
  135.     KVNode<K,V>* Find(const K& key)
  136.     {
  137.         int index = HashFun()(key, _capacity);
  138.         int i = 1;
  139.         while (_status[index] != EMPTY)
  140.         {
  141.             if (_tables[index] == key)
  142.             {
  143.                 if (_status[index] == DELETE)
  144.                 {
  145.                     return NULL;
  146.                 }
  147.                 return _tables[index];
  148.             }
  149.             index = Hashfun2(index, i++);
  150.             if (index >= _capacity)
  151.             {
  152.                 index = index%_capacity;
  153.             }
  154.         }
  155.         return NULL;
  156.     }
  157.     void Print()
  158.     {
  159.         for (int i = 0; i < _capacity; ++i)
  160.         {
  161.             if (_status[i] == EXISTS)
  162.             {
  163.                 cout << '[' << _tables[i]._key.c_str() << ":" << _tables[i]._value.c_str() << ']' << " ";
  164.             }
  165.             else
  166.                 cout << "[]" << " ";
  167.         }
  168.         cout << endl;
  169.     }
  170.     void Swap(HashTable<K, V, HashFun> h)
  171.     {
  172.         swap(_tables, h._tables);
  173.         swap(_size, h._size);
  174.         swap(_capacity, h._capacity);
  175.         swap(_status, h._status);
  176.     }
  177. protected:
  178.     KVNode *_tables;
  179.     size_t _size;
  180.     size_t _capacity;
  181.     Status *_status;
  182. };
测试:

  1. void Test2()//二次
  2. {
  3.     HashTable<string, string>ht1(3);
  4.     ht1.Insert("a", "a");
  5.     ht1.Insert("b", "a");
  6.     ht1.Insert("c", "a");
  7.     ht1.Insert("d", "a");
  8.     ht1.Insert("d", "a");
  9.     ht1.Print();

  10.     HashTableNode<string, string>* ret = ht1.Find("a");
  11. }
  12. int main()
  13. {
  14.     Test2();
  15.     return 0;
  16. }



阅读(1213) | 评论(0) | 转发(0) |
0

上一篇:堆Heap(最小堆)

下一篇:BitSet位图

给主人留下些什么吧!~~