转一下常见的常用的哈希函数
2016-07-04 17:57
162 查看
转载自 http://blog.csdn.net/alongela/article/details/8247713
const int MOD = 10007;
unsigned int RSHash(const char* str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
while (*str)
{
hash = hash * a + *str++;
a *= b;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int JSHash(const char* str)
{
unsigned int hash = 1315423911;
while (*str)
{
hash ^= ((hash << 5) + *str++ + (hash >> 2));
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int PJWHash(const char* str)
{
unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
unsigned int HighBits = (unsigned int)(0xffffffff) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
while (*str)
{
hash = (hash << OneEighth) + *str++;
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int ELFHash(const char* str)
{
unsigned int hash = 0;
unsigned int x = 0;
while (*str)
{
hash = (hash << 4) + *str++;
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int BKDRHash(const char* str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = (hash * seed) + *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int SDBMHash(const char* str)
{
unsigned int hash = 0;
while (*str)
{
hash = *str++ + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int DJBHash(const char* str)
{
unsigned int hash = 5381;
while (*str)
{
hash = ((hash << 5) + hash) + *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int DEKHash(const char* str)
{
unsigned int hash = static_cast<unsigned int>(strlen(str));
while (*str)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int BPHash(const char* str)
{
unsigned int hash = 0;
while (*str)
{
hash = hash << 7 ^ *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int FNVHash(const char* str)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
while (*str)
{
hash *= fnv_prime;
hash ^= *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int APHash(const char* str)
{
unsigned int hash = 0xAAAAAAAA;
for(int i = 0; str[i] != 0; ++i)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str[i] * (hash >> 3)) :
(~((hash << 11) + (str[i] ^ (hash >> 5))));
}
return (hash & 0x7fffffff) % MOD;
}
转载自 http://blog.csdn.net/alongela/article/details/8247713
const int MOD = 10007;
unsigned int RSHash(const char* str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
while (*str)
{
hash = hash * a + *str++;
a *= b;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int JSHash(const char* str)
{
unsigned int hash = 1315423911;
while (*str)
{
hash ^= ((hash << 5) + *str++ + (hash >> 2));
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int PJWHash(const char* str)
{
unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
unsigned int HighBits = (unsigned int)(0xffffffff) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
while (*str)
{
hash = (hash << OneEighth) + *str++;
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int ELFHash(const char* str)
{
unsigned int hash = 0;
unsigned int x = 0;
while (*str)
{
hash = (hash << 4) + *str++;
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int BKDRHash(const char* str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = (hash * seed) + *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int SDBMHash(const char* str)
{
unsigned int hash = 0;
while (*str)
{
hash = *str++ + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int DJBHash(const char* str)
{
unsigned int hash = 5381;
while (*str)
{
hash = ((hash << 5) + hash) + *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int DEKHash(const char* str)
{
unsigned int hash = static_cast<unsigned int>(strlen(str));
while (*str)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int BPHash(const char* str)
{
unsigned int hash = 0;
while (*str)
{
hash = hash << 7 ^ *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int FNVHash(const char* str)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
while (*str)
{
hash *= fnv_prime;
hash ^= *str++;
}
return (hash & 0x7fffffff) % MOD;
}
unsigned int APHash(const char* str)
{
unsigned int hash = 0xAAAAAAAA;
for(int i = 0; str[i] != 0; ++i)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str[i] * (hash >> 3)) :
(~((hash << 11) + (str[i] ^ (hash >> 5))));
}
return (hash & 0x7fffffff) % MOD;
}
转载自 http://blog.csdn.net/alongela/article/details/8247713
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Mootools 1.2教程 定时器和哈希简介
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记