Cuckoo hash is a hash conflict solution. Its purpose is to use a simple hash function to evenly distribute hash keys.
The basic idea is to use two hash functions to handle collisions, so that each key corresponds to two locations.
The insert operation is as follows:
1. generate two hash key values, hashk1 and hashk2, for the key value hash. If one of the two corresponding locations is empty, insert the key directly.
2. Otherwise, insert the key value at any position and kick out the key value at that position.
3. The key value to be kicked out must be inserted again until no key is kicked out.
The search method is relatively simple.
The code is implemented as follows:
// Cuckoo_hash.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <string>#include <cmath>#include <iostream>using namespace std;template<class KeyT>class CuckooHash;template<>class CuckooHash<int>{private:int lnBucket;//size of bucketint *mpKeyBucket1;//the first bucket for first hashint *mpKeyBucket2;//the second bucket for second hashenum {MaxLoop = 1000};//used to control rehash loopint lnCantInsertNum;private://first hash functionint hHashOne(int& irKey){int lHashKey = 0;lHashKey = irKey % lnBucket;return lHashKey;}//second hash functionint hHashTwo(int& irKey){int lHashKey = 0;lHashKey = irKey / lnBucket;lHashKey = lHashKey % lnBucket;return lHashKey;}//todo: juge one num is Prime NUM or notbool hIsPrime(int inN){if(inN <= 0) return false;int last = sqrt((double)inN);for(int i = 2; i<= last; i++){if(inN % i == 0)return false;}return true;}int hGetMinPrime(int inNum){while( !hIsPrime(inNum) ) inNum ++;return inNum; }//try to rehash all the other keybool hReHash(int iKey, int deeps){if(deeps <= 0) return false;int lHashKey1 = hHashOne(iKey);int lHashKey2 = hHashTwo(iKey);if(iKey == mpKeyBucket1[lHashKey1]){if(mpKeyBucket2[lHashKey2] == 0){mpKeyBucket2[lHashKey2] = iKey;return true;}else{if( hReHash(mpKeyBucket2[lHashKey2], deeps - 1) ){mpKeyBucket2[lHashKey2] = iKey;return true;}}}else if(iKey == mpKeyBucket2[lHashKey2]){if(mpKeyBucket1[lHashKey1] == 0){mpKeyBucket1[lHashKey1] = iKey;return true;}else{if( hReHash(mpKeyBucket1[lHashKey1], deeps - 1)){mpKeyBucket1[lHashKey1] = iKey;return true;}}}return false;}public:CuckooHash(int inNum){lnBucket = inNum;mpKeyBucket1 = NULL;mpKeyBucket2 = NULL;lnCantInsertNum = 0;}void InitHashTable(){lnBucket = hGetMinPrime(lnBucket);mpKeyBucket1 = new int[lnBucket];memset(mpKeyBucket1, 0, sizeof(int) * lnBucket);mpKeyBucket2 = new int[lnBucket];memset(mpKeyBucket2, 0, sizeof(int) * lnBucket);}~CuckooHash(){if(mpKeyBucket1)delete[] mpKeyBucket1;if(mpKeyBucket2)delete[] mpKeyBucket2;}void Insert(int& irKey){if(find(irKey)) return;int lHashKey1 = hHashOne(irKey);int lHashKey2 = hHashTwo(irKey);if(mpKeyBucket1[lHashKey1] == 0)mpKeyBucket1[lHashKey1] = irKey;else if(mpKeyBucket2[lHashKey2] == 0)mpKeyBucket2[lHashKey2] = irKey;else{if(hReHash(mpKeyBucket1[lHashKey1], MaxLoop))mpKeyBucket1[lHashKey1] = irKey;else if(hReHash(mpKeyBucket2[lHashKey2], MaxLoop))mpKeyBucket2[lHashKey2] = irKey;elselnCantInsertNum ++;}cout << "After insert : " << irKey << endl;cout << lHashKey1 << " " << lHashKey2 << endl;PrintBucket4Test();}bool find(int& irKey){int lHashKey1 = hHashOne(irKey);if(mpKeyBucket1 && mpKeyBucket1[lHashKey1] == irKey)return true;int lHashKey2 = hHashTwo(irKey);if(mpKeyBucket2 && mpKeyBucket2[lHashKey2] == irKey)return true;return false;}void PrintBucket4Test(){for(int i = 0; i<lnBucket; i++ )cout << mpKeyBucket1[i] << ' ';cout << endl;for(int i = 0; i<lnBucket; i++ )cout << mpKeyBucket2[i] << ' ';cout << endl;}};int _tmain(int argc, _TCHAR* argv[]){CuckooHash<int> CKHash(12);CKHash.InitHashTable();int a[] = {20, 50, 53, 75, 100, 67, 105, 3, 36, 39, 6};for(int i = 0; i< sizeof(a)/sizeof(int); i++){CKHash.Insert(a[i]);}int b;cin >> b;return 0;}
The Reference Links are as follows:
Http://www.it-c.dk/people/pagh/papers/cuckoo-undergrad.pdf
Http://www.it-c.dk/people/pagh/papers/cuckoo-jour.pdf
Http://en.wikipedia.org/wiki/Cuckoo_hashing
Http://hi.baidu.com/algorithms/blog/item/eb89b582add48f95f703a61e.html