一些常用的字符串hash函数
unsigned int RSHash(const std::string& str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0; for(std::size_t i = 0; i < str.length(); i++)
{
hash = hash * a + str[i];
a = a * b; // 这保证对于每个字符串 处理出的值 相同的概率很小 a 可能会自然爆 long long 没关系 爆就让它爆
} return hash; } /* End Of RS Hash Function */
unsigned int JSHash(const std::string& str)
{
unsigned int hash = 1315423911; for(std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((hash << 5) + str[i] + (hash >> 2));
} return hash;
}
/* End Of JS Hash Function */
// 同上 只是处理方式不同
unsigned int PJWHash(const std::string& 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; for(std::size_t i = 0; i < str.length(); i++)
{
hash = (hash << OneEighth) + str[i]; if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
} return hash;
}
/* End Of P. J. Weinberger Hash Function */ unsigned int ELFHash(const std::string& str)
{
unsigned int hash = 0;
unsigned int x = 0; for(std::size_t i = 0; i < str.length(); i++)
{
hash = (hash << 4) + str[i];
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
} return hash;
}
/* End Of ELF Hash Function */ unsigned int BKDRHash(const std::string& str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0; for(std::size_t i = 0; i < str.length(); i++)
{
hash = (hash * seed) + str[i];
} return hash;
}
/* End Of BKDR Hash Function */ unsigned int SDBMHash(const std::string& str)
{
unsigned int hash = 0; for(std::size_t i = 0; i < str.length(); i++)
{
hash = str[i] + (hash << 6) + (hash << 16) - hash;
} return hash;
}
/* End Of SDBM Hash Function */ unsigned int DJBHash(const std::string& str)
{
unsigned int hash = 5381; for(std::size_t i = 0; i < str.length(); i++)
{
hash = ((hash << 5) + hash) + str[i];
} return hash;
}
/* End Of DJB Hash Function */ unsigned int DEKHash(const std::string& str)
{
unsigned int hash = static_cast<unsigned int>(str.length()); for(std::size_t i = 0; i < str.length(); i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ str[i];
} return hash;
}
/* End Of DEK Hash Function */ unsigned int BPHash(const std::string& str)
{
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = hash << 7 ^ str[i];
} return hash;
}
/* End Of BP Hash Function */ unsigned int FNVHash(const std::string& str)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash *= fnv_prime;
hash ^= str[i];
} return hash;
}
/* End Of FNV Hash Function */ unsigned int APHash(const std::string& str)
{
unsigned int hash = 0xAAAAAAAA; for(std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str[i] * (hash >> 3)) :
(~((hash << 11) + (str[i] ^ (hash >> 5))));
} return hash;
}
/* End Of AP Hash Function */
处哥曰:Hash 精髓所在 是 搞出来的数据别让他重复就好~~~
一些常用的字符串hash函数的更多相关文章
- 各种字符串Hash函数比较(转)
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- [转]各种字符串Hash函数比较
转自:https://www.byvoid.com/zht/blog/string-hash-compare 常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些 ...
- 【转】各种字符串Hash函数比较
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- [T]各种字符串Hash函数比较
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- 各种字符串Hash函数比较
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- 各种字符串Hash函数(转)
/// @brief BKDR Hash Function /// @detail 本 算法由于在Brian Kernighan与Dennis Ritchie的<The C Programmin ...
- 长度有限制的字符串hash函数
长度有限制的字符串hash函数 DJBHash是一种非常流行的算法,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33,原型如下 hash(i) = hash ...
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- LoadRunner中常用的字符串操作函数
LoadRunner中常用的字符串操作函数有: strcpy(destination_string, source_string); strc ...
随机推荐
- 提高HTML5 canvas性能的几种方法
简介 HTML5 canvas 最初起源于苹果(Apple)的一项实验,现在已经成为了web中受到广泛支持的2D快速模式绘图(2Dimmediate mode graphic)的标准.许多开发者现在利 ...
- oc-29-可变数组
/** 数组长度不固定,可以随便往里面添加或者删除元素. 1.创建数组 NSMutableArray *arrayM = [NSMutableArray array] 2.给数组添加元素(只能是OC对 ...
- [STAThread]的含义
Posted on 2007-07-07 10:06 桦林 阅读(33100) 评论(10) 编辑 收藏 [STAThread]STAThread:Single Thread Apar ...
- 分治法(一)(zt)
这篇文章将讨论: 1) 分治策略的思想和理论 2) 几个分治策略的例子:合并排序,快速排序,折半查找,二叉遍历树及其相关特性. 说明:这几个例子在前面都写过了,这里又拿出来,从算法设计的策略的角度把它 ...
- Remap BMW F11 2010 all ECUs with E-Sys and ENET cable
Just wanted to share some experiences remaping all the ECUs in my F11 2010 BMW, hopefully other BMW ...
- Operation与GCD的不同
最大并发数: 什么是并发数? 同时执行的任务数.比如同时开启三个线程执行三个任务,并发数就是3. 最大并发数相关的方法: -(NSInteger)maxConcurrentOperationCount ...
- javacv实战篇
看到之前有248位小伙伴看我写的水文还是比较受鼓舞的,所以决定把这个细写一下: 就是javacv怎么实际应用一下: 其实无外乎导包,写测试代码. 这样 那我们先导包儿. javacv-1.2-bin( ...
- NIS客户端限制用户登录
公司所有账号信息由一台 NIS Server 统一管理,但是有几台 NIS Client 只允许某几个用户登录.这里通过PAM机制来实现该需求. 1. 需要配置的文件 (/etc/pam.d/目录下) ...
- ionic 中使用ion-slide-box
ion-slide-box 用法: <ion-slide-box class="slide" auto-play="true" does-continue ...
- GSS5 spoj 2916. Can you answer these queries V 线段树
gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...