`
kingj
  • 浏览: 421494 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

各种经典的hash算法

 
阅读更多

各种经典hash算法

#include <stdio.h>

#include <iostream>

#include <sstream>

#include <string>

 

using namespace std;

 

class HashLib

{

    public:

        long RSHash(string str)

        {

            int b = 378551;

            int a = 63689;

            long hash = 0;

            for(int i = 0; i < str.length(); i++)

            {

                hash = hash * a + str.c_str()[i];

                a = a * b;

            }

            return hash;

        }

        long JSHash(string str)

        {

            long hash = 1315423911;

            for(int i = 0; i < str.length(); i++)

            {

                hash ^= ((hash << 5) + str.c_str()[i] + (hash >> 2));

            }

            return hash;

        }

        long PJWHash(string str)

        {

            long BitsInUnsignedInt = (long)(4 * 8);

            long ThreeQuarters = (long)((BitsInUnsignedInt * 3) / 4);

            long OneEighth = (long)(BitsInUnsignedInt / 8);

            long HighBits = (long)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);

            long hash = 0;

            long test = 0;

            for(int i = 0; i < str.length(); i++)

            {

                hash = (hash << OneEighth) + str.c_str()[i];

                if((test = hash & HighBits) != 0)

                {

                    hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));

                }

            }

            return hash;

        }

        long ELFHash(string str)

        {

            long hash = 0;

            long x = 0;

            for(int i = 0; i < str.length(); i++)

            {

                hash = (hash << 4) + str.c_str()[i];

                if((x = hash & 0xF0000000L) != 0)

                {

                    hash ^= (x >> 24);

                }

                hash &= ~x;

            }

            return hash;

        }

        long BKDRHash(string str)

        {

            long seed = 131; // 31 131 1313 13131 131313 etc..

            long hash = 0;

            for(int i = 0; i < str.length(); i++)

            {

                hash = (hash * seed) + str.c_str()[i];

            }

            return hash;

        }

        long SDBMHash(string str)

        {

            long hash = 0;

            for(int i = 0; i < str.length(); i++)

            {

                hash = str.c_str()[i] + (hash << 6) + (hash << 16) - hash;

            }

            return hash;

        }

        long DJBHash(string str)

        {

            long hash = 5381;

            for(int i = 0; i < str.length(); i++)

            {

                hash = ((hash << 5) + hash) + str.c_str()[i];

            }

            return hash;

        }

        long DEKHash(string str)

        {

            long hash = str.length();

            for(int i = 0; i < str.length(); i++)

            {

                hash = ((hash << 5) ^ (hash >> 27)) ^ str.c_str()[i];

            }

            return hash;

        }

        long BPHash(string str)

        {

            long hash = 0;

            for(int i = 0; i < str.length(); i++)

            {

                hash = hash << 7 ^ str.c_str()[i];

            }

            return hash;

        }

        long FNVHash(string str)

        {

            long fnv_prime = 0x811C9DC5;

            long hash = 0;

            for(int i = 0; i < str.length(); i++)

            {

                hash *= fnv_prime;

                hash ^= str.c_str()[i];

            }

            return hash;

        }

        long APHash(string str)

        {

            long hash = 0xAAAAAAAA;

            for(int i = 0; i < str.length(); i++)

            {

                if ((i & 1) == 0)

                {

                    hash ^= ((hash << 7) ^ str.c_str()[i] ^ (hash >> 3));

                }

                else

                {

                    hash ^= (~((hash << 11) ^ str.c_str()[i] ^ (hash >> 5)));

                }

            }

            return hash;

        }

};

 

 

 

int main(int argc, char **argv)

{

    string a = "h";

    HashLib hashlib;

    stringstream tmp;

    for(int i = 0; i < 10 ; i++)

    {

        cout << "----------------------"<< endl;

        tmp << i;

        a = tmp.str();

        cout << a <<  endl; 

        cout << hashlib.JSHash(a) << endl;

        cout << hashlib.RSHash(a) << endl;

        cout << hashlib.PJWHash(a) << endl;

        cout << hashlib.ELFHash(a) << endl;

        cout << hashlib.BKDRHash(a) << endl;

        cout << hashlib.SDBMHash(a) << endl;

        cout << hashlib.DEKHash(a) << endl;

        cout << hashlib.BPHash(a) << endl;

        cout << hashlib.FNVHash(a) << endl;

        cout << hashlib.APHash(a) << endl;

    }

    return 2;

 

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics