CuckooHash的代码
来源:互联网 发布:错误的数据统计 编辑:程序博客网 时间:2024/06/04 00:33
#include <iostream>#include <string>#include <cmath>using namespace std;template<class KeyT>class CuckooHash<int>{private: long size; int *keyBucket1; int *keyBucket2; enum { MaxLoop = 1000 }; long cantInsertNum;private: int hHashOne(int& irKey) { long hashKey = 0; hashKey = irKey%size; hashKey = hashKey&bucket; return lengthHashKey; } bool isPrime(int n) { if (n <= 0) return false; int last = sqrt((double)n); for (i = 2; i <= last; i++) { if (n%i == 0) { return false; } } return true; } int getMinPrime(int num) { while (!isPrime(num)) num++; return num; } bool refreshHash(int key, int deeps) { if (deeps <= 0) return false; long hashKey1 = hasOne(key); long hashKey2 = hasTwo(key); if (key == keyBucket(hashKey1)) { if (keyBucket2[hashKey2] == 0) { keyBucket2[hashKey2] = key; return true; } else { if (refreshHash(keyBucket2[hashKey2], deep - 1)) { keyBucket2[hashKey2] = key; return true; } } } else if(key==keyBucket2[hashKey2]) { if (keyBucket1[hashKey1] == 0) { keyBucket1[hashKey1] = key; return true; } else { if (refreshHash(keyBucket1[hashKey1], deep - 1)) { keyBucket1[hashKey1] = key; return true; } } } return false; }public: CuckooHash(int num) { bucket = num; keyBucket1 = NULL; keyBuck2 = NULL; cantInsertNum = 0; } void initHashTable() { bucket = getMinPrime(bucket); keyBucket1 = new int[bucket]; memset(keyBucket1, 0, sizeof(int)*bucket); keyBucket2 = new int[bucket]; memset(keyBucket2, 0, sizeof(int)*bucket); } ~CuckooHash() { if (keyBucket1) delete[] keyBucket1; if (keyBucket2) delete[] keyBucket2; } void insert(int& key) { if (find(key))return; long hashKey1 = hashOne(key); long hashKey2 = hashTwo(key); if (keyBucket1[hashKey1] == 0) keyBucket1[hashKey1] = key; else if (keyBucket2[hashKey2] == 0) keyBucket2[hashKey2] = key; else { if (refreshHash(keyBucket1[hashKey1], MaxLoop)) keyBucket1[hashKey1] = key; else if (refreshHash(keyBucket2[hashKey2]), MaxLoop) keyBucket2[hashKey2] = key; else cantInsertNum++; } cout << "After insert : " << key << endl; cout << hashKey1 << " " << hashKey2 << endl; PrintBucket4Test(); } bool find(int &key) { int hashKey1 = hashOne(key); if (keyBucket1&&keyBucket1[hashKey1] == key) return true; int hashKey2 = hashTwo(key); if (keyBucket2&&keyBucket2[hashKey2] == key) return true; return false; } void PrintBucket4Test() { for (int i = 0; i < bucket; i++) { cout << keyBucket1[i] << ' '; } cout << endl; for (int i = 0; i < bucket; i++) { cout << keyBucket2[i] << ' '; } cout << endl; }};
原来的代码是用匈牙利命名法命名的,实在不好理解,自己重新写了一份。
阅读全文
0 0
- CuckooHash的代码
- 运行代码的代码
- 运行代码的代码
- 什么是代码后面的代码
- JS:运行代码的代码
- 好的代码:傻瓜型代码
- 代码框 的代码
- 网页,运行代码,复制代码,代码另存的代码
- 优秀的代码 vs. 糟糕的代码
- 优秀的代码 vs. 糟糕的代码
- 什么样的代码是合格的代码
- 优秀的代码VS糟糕的代码
- 代码维护:改进代码的一些方法
- javascript 运行代码的代码 By Stabx
- 代码点和代码单元的关系
- 一段输出代码本身的SQL代码
- 分页的页面代码和后台代码
- 在线运行代码框的代码
- 【源码解读】Screencap源码分析-基础篇
- 汇编语言: 试编制一个程序,把BX寄存器内的二进制数以十六进制数的形式在屏幕上显示出来
- 文件系统及软链接和硬链接
- java 界面设计的用途: AWT Swing SWT 如何选择这三个工具包
- POJ3233_Matrix Power Series_递推法|矩阵快速幂
- CuckooHash的代码
- grep sed 和 awk
- 汇编语言: 试编制一个程序,把BX寄存器内的二进制数以八进制数的形式在屏幕上显示出来
- Python中的一些用法
- JSP application对象的使用
- ListView的两种多选模式
- [一天几个linux命令] 文件种类与扩展名
- Git的远程仓库
- main函数调用自身