各种经典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;
}
分享到:
相关推荐
MurmurHash算法由Austin Appleby创建于2008年,现已应用到Hadoop、libstdc 、nginx、libmemcached,Redis,Memcached,Cassandra,HBase,Lucene等开源系统。2011年Appleby被Google雇佣,随后Google推出其变种的...
采用java实现的常用hash算法归总。
Hash函数集合,包含主流的hash函数: nginx_hash算法,OpenSSL_hash算法,RSHash,JSHash,PJWHash,ELFHash,BKDRHash,DJBHash,DEKHash,APHash等等!
C语言实现hash算法源码,实现了sha256,sha384,sha512三种哈希算法,项目中用到的,提取出来测试使用的。
python版本的各种hash算法
经典 hash 算法 各种经典的hash算法
一个hash算法的工具类,里面包含了一些常用的hash算法
Hash算法有三种,分别为平均哈希算法(aHash)、感知哈希算法你(pHash)和差异哈哈希算法(dHash)。 本代码针对平均哈希算法(aHash)
Hash算法
这是几种经典的Hash算法的实现(源代码),里面源代码和文字解说都有
Geohash算法实现,经纬度到geohash编码的实现
一致性 hash 算法介绍
常见的HASH算法
其中只有一个thomas wang 的整数hash 算法,其余都是字符串的hash算法。
Ketama算法是一致性hash算法的一个优秀实现。增删节点后数据命中率及均分率都很高。
hash算法C代码实现 标准接口函数 方便修改hash函数
fasthash,go写的一个hash算法,比标准hash算法的速度更快,占用内存更低
hash算法中的sha1算法的讲解与实现,c++代码,可以直接运行,注释清晰,直接使用。
工具类
各种数的hash算法,包括整数,浮点数,字符串等