hashtable的C++实现,使用两种常用的解决冲突的方式,使用时需要自己提供针对HashedObj的hash函数。

1、分离连接法(separate chaining)

#include <vector>
#include <list>
using namespace std;template <typename HashedObj>
class HashTable
{public:explicit HashTable(int size = 101);void makeEmpty(){for(int i = 0; i < theLists.size(); i++)theLists[i].clear();}bool contains(const HashedObj & x) const{const list<HashedObj> & whichList = theLists[myhash(x)];return find(whichList.begin(), whichList.end(), x) != whichList.end();}bool remove(const HashedObj & x){list<HashedObj> & whichList = theLists[myhash(x)];typename list<HashedObj>::iterator itr = find(whichList.begin(), whichList.end(), x);if(itr == whichList.end())return false;whichList.erase(itr);--currentSize;return true;}bool insert(const HashedObj & x){list<HashedObj> & whichList = theLists[myhash(x)];if(find(whichList.begin(), whichList.end(), x) != whichList.end())return false;whichList.push_back(x);if(++currentSize > theLists.size())rehash();return true;}private:vector<list<HashedObj> > theLists;   // The array of Listsint  currentSize;void rehash(){vector<list<HashedObj> > oldLists = theLists;// Create new double-sized, empty tabletheLists.resize(2 * theLists.size());for(int j = 0; j < theLists.size(); j++)theLists[j].clear();// Copy table overcurrentSize = 0;for(int i = 0; i < oldLists.size(); i++){typename list<HashedObj>::iterator itr = oldLists[i].begin();while(itr != oldLists[i].end())insert(*itr++);}}int myhash(const HashedObj & x) const{int hashVal = hash(x);hashVal %= theLists.size();if(hashVal < 0)hashVal += theLists.size();return hashVal;}
};

2、使用探测法(probing hash tables)

template <typename HashedObj>
class HashTable
{public:explicit HashTable(int size = 101) : array(size){makeEmpty();}void makeEmpty(){currentSize = 0;for(int i = 0; i < array.size(); i++)array[i].info = EMPTY;}bool contains(const HashedObj & x) const{return isActive(findPos(x));}bool insert(const HashedObj & x){// Insert x as activeint currentPos = findPos(x);if(isActive(currentPos))return false;array[currentPos] = HashEntry(x, ACTIVE);if(++currentSize > array.size()/2)rehash();return true;}bool remove(const HashedObj & x){int currentPos = findPos(x);if(!isActive(currentPos))return false;array[currentPos].info = DELETED;return true;}enum EntryType { ACTIVE, EMPTY, DELETED };private:struct HashEntry{HashedObj element;EntryType info;HashEntry(const HashedObj & e = HashedObj(), EntryType i = EMPTY ): element(e), info(i) { }};vector<HashEntry> array;int currentSize;int findPos(const HashedObj & x) const{int offset = 1;int currentPos = myhash(x);while(array[currentPos].info != EMPTY && array[currentPos].element != x){currentPos += offset;  // Compute ith probeoffset += 2;if(currentPos >= array.size())currentPos -= array.size();}return currentPos;}bool isActive(int currentPos) const{return array[currentPos].info == ACTIVE;}void rehash(){vector<HashEntry> oldArray = array;// Create new double-sized, empty tablearray.resize(2*oldArray.size());for(int j = 0; j < array.size(); j++)array[j].info = EMPTY;// Copy table overcurrentSize = 0;for(int i = 0; i < oldArray.size(); i++)if(oldArray[i].info == ACTIVE)insert(oldArray[i].element);}int myhash(const HashedObj & x) const;
};

转载于:https://www.cnblogs.com/luxiaoxun/archive/2012/09/02/2667425.html

hashtable的C++实现相关推荐

  1. 【STL源码剖析读书笔记】【第5章】关联式容器之hashtable

    1.hashtable在插入.删除.搜寻操作上具有"常数平均时间"的表现,不依赖输入元素的随机性. 2.hashtable通过hashfunction将元素映射到不同的位置,但当不 ...

  2. HashMap 和 Hashtable 的 6 个区别,最后一个没几个人知道!

    HashMap 和 Hashtable 是 Java 开发程序员必须要掌握的,也是在各种 Java 面试场合中必须会问到的. 但你对这两者的区别了解有多少呢? 现在,栈长我给大家总结一下,或许有你不明 ...

  3. java 中的 Enumeration 在Vector,Hashtable和web中的应用

    public interface Enumeration<E> 实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个.连续调用 nextElement方法将返回一系列的 ...

  4. 在C#中应用哈希表(Hashtable)

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...

  5. 调试JDK源码-Hashtable实现原理以及线程安全的原因

    调试JDK源码-一步一步看HashMap怎么Hash和扩容 调试JDK源码-ConcurrentHashMap实现原理 调试JDK源码-HashSet实现原理 调试JDK源码-调试JDK源码-Hash ...

  6. C语言的HashTable简单实现

    原文地址:http://blog.csdn.net/zmxiangde_88/article/details/8025541 HashTable是在实际应用中很重要的一个结构,下面讨论一个简单的实现, ...

  7. HashTable原理与实现

    memcached中hashtable部分的源码,hash部分的源码主要分布在assoc.h/c.hash.h/c中,总得来说代码比较简单,这里就稍微介绍一下. hashtable通常包括哈希函数和解 ...

  8. JAVA - HashMap和HashTable

    1. HashMap 1)  hashmap的数据结构 Hashmap本质就是一个数组,只是当key值重复时,使用链表的方式来存储重复的key值(拉链法),注意:链表中存放的仍然是key值.如下图示: ...

  9. 一个古老的问题HashMap与Hashtable区别

    HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable.可能你觉得HashTable很好用,为什么不用呢? ...

  10. 面试之Hashtable和ConcurrentHashMap

    那么要如何保证HashMap的线程安全呢? 方法有很多,比如使用Hashtable或者Collections.synchronizedMap,但是这两位选手都有一个共同的问题:性能.因为不管是读还是写 ...

最新文章

  1. 循序渐进学习Linux--第二天更新
  2. Visual Studio Code 保存代码时报Applying code action Organize Imports
  3. 【微型计算机原理与接口技术】计算机中的信息表示
  4. linux学习笔记(13)终端清屏
  5. python文本词频统计是字典吗,只使用字典python3计算.txt文件中的词频
  6. 文件被后台程序占用无法删除_Windows系统中,教你彻底删除C盘的顽固文件,瞬间多出10个G...
  7. 【Linux】一步一步学Linux——stty命令(243)
  8. springboot对接支付宝支付接口(详细开发步骤总结)
  9. 数据结构与算法(一):时间复杂度和空间复杂度
  10. Errors occured, no packages were upgraded. ⇒ ERROR: Failed to install packages to new root.
  11. 脑科学发展到今天,距离记忆编辑提取还有多远
  12. 游戏原画,掌握角色设计2个小技巧,你也能画好角色
  13. python计算单词长度_python – 返回字符串中的单词长度
  14. lzw压缩 java_Java压缩之LZW算法字典压缩与解压讲解
  15. 饼图大小调整_PPT制作简约饼图,学会这一个就够了!
  16. 【建议收藏】2021年中高级Android大厂面试秘籍,为你保驾护航金三银四,直通大厂
  17. iOS 关于自定义转场动画,以UITabBarController为例
  18. 开发环境 - pip install cv2下载失败的解决办法
  19. java 气泡图_java报表开发制作气泡图
  20. 小白学习Winform 遇到的问题总结

热门文章

  1. 伙伴系统二叉树可视化笔记
  2. 三分法:有相同元素值快速排序(双向扫描出现3个指针)
  3. Codeforces Round #179 (Div. 2): D. Greg and Graph(Floyd)
  4. bzoj 1605: [Usaco2008 Open]Crisis on the Farm 牧场危机(DP)
  5. bzoj 3626: [LNOI2014]LCA(离线差分+树链剖分)
  6. python数据存储系列教程——python中mongodb数据库操作:连接、增删查改、多级路径
  7. 选择排序算法(C++版)
  8. MySQL(四)InnoDB中一棵B+树能存多少行数据
  9. 一个简化的插件框架c#
  10. python - super 寻找继承关系