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;    }};

原来的代码是用匈牙利命名法命名的,实在不好理解,自己重新写了一份。