hash表-平方探测,数据结构与算法分析第四版的(增加了上文的nextPrime)
来源:互联网 发布:毫州康美中药城淘宝店 编辑:程序博客网 时间:2024/05/29 19:55
/*************************************************************************> File Name: HashTable2.h> Author:keson> Mail:keson@bupt.edu.cn> Created Time: 2014年11月26日 星期三 11时05分23秒 ************************************************************************/#include<iostream>#include<vector>#include<string>using namespace std;class hashValue{ public: size_t operator()(const string &key) { size_t hashVal=0; for(auto c:key) hashVal=37*hashVal+c; return hashVal; }};template<typename HashedObj>class HashTable2{public: explicit HashTable2(int size=101):array(nextPrime(size)) {makeEmpty();} bool contains(const HashedObj &x) const { return isActive(findPos(x)); } void makeEmpty() { currentSize=0; for(auto &entry:array) entry.info=EMPTY; } bool insert(const HashedObj &x) { int currentPos=findPos(x); if(isActive(currentPos)) return false; array[currentPos].element=x; array[currentPos].info=ACTIVE; if(++currentSize>array.size()/2) rehash(); return true; } bool insert(HashedObj &&x) { int currentPos=findPos(x); if(isActive(currentPos)) return false; array[currentPos].element=std::move(x); array[currentPos].info=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}; void print() { for(auto entry:array) if(entry.info==ACTIVE) cout<<entry.element<<endl; }private: struct HashEntry { HashedObj element; EntryType info; HashEntry(const HashedObj &e=HashedObj{},EntryType i=EMPTY) :element{e},info{i} {} HashEntry(HashedObj &&e,EntryType i=EMPTY) :element{std::move(e)},info{i} {} }; vector<HashEntry> array; int currentSize; bool isActive(int currentPos) const { return array[currentPos].info==ACTIVE; } int findPos(const HashedObj &x) const { int offset=1; int currentPos=myhash(x); //the order can't change. while(array[currentPos].info!=EMPTY&& array[currentPos].element!=x) { currentPos+=offset; //Compute ith probe offset+=2; if(currentPos>=array.size()) currentPos-=array.size(); } return currentPos; } bool isPrime(int num) { if (num == 2 || num == 3) { return true; } if (num % 6 != 1 && num % 6 != 5) { return false; } for (int i = 5; i*i <= num; i += 6) { if (num % i == 0 || num % (i+2) == 0) { return false; } } return true; } int nextPrime(int n) { bool state=isPrime(n); while(!state) { state=isPrime(++n); } return n; } void rehash() { vector<HashEntry> oldArray=array; //Create new double-sized,empty table array.resize(nextPrime(2*oldArray.size())); for(int j=0;j<array.size();j++) array[j].info=EMPTY; currentSize=0; for(int i=0;i<oldArray.size();i++) if(oldArray[i].info==ACTIVE) insert(oldArray[i].element); } size_t myhash(const HashedObj &x) const { static hashValue hf; return hf(x)%array.size(); }};int main(){ HashTable2<string> a; a.insert("Hello"); a.print();}
0 0
- hash表-平方探测,数据结构与算法分析第四版的(增加了上文的nextPrime)
- 平方探测法的证明《数据结构与算法分析(C语言描述)第二版》P119
- BinarySearchTree ----数据结构与算法分析第四版的
- 算法与数据结构-Hash表的理解
- 数据结构与算法之散列(线性/平方/双平方探测法)<八>
- 平方探测法hash
- 使用分离链接的哈系表—数据结构与算法分析第四版上的
- Hash表及hash算法的分析
- 高效的nextPrime算法(参考l链接中的高效的素数判断)
- 【数据结构】哈希表的线性探测算法
- 线性探测-Hash表的创建-查找
- java数据结构——Hash的实现(数组) 线性探测的方法解决冲突
- pat甲1078. Hashing(hash+Quadratic probing 平方探测)
- 数据结构与算法学习之链表的增加、删除
- 《数据结构与算法分析》读书笔记——hash表
- 数据结构与算法分析——Hash表
- 【数据结构与算法】Hash表
- 平方探测和线性探测解决散列表冲突的区别(优点及缺点)
- 快排变种题
- MY IDEA
- C++静态成员和静态方法
- Android特点
- NYOJ82迷宫寻宝(一)
- hash表-平方探测,数据结构与算法分析第四版的(增加了上文的nextPrime)
- Java对两个int类型求百分比的方法
- 博客搬家了
- Android 手机定位慢的解决方法
- 第十四周项目二—带姓名的成绩单
- 第13周上机实践项目5——字符串操作(2)
- tcp通信中的bind
- BZOJ 2434 NOI2011 阿狸的打字机 fail树+树状数组
- lucene应用