一些常用的字符串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 ...
随机推荐
- iOS开发——UI篇Swift篇&UISegmentedControl
UISegmentedControl override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString / ...
- ComboBox控件
一.怎样加入�/删除Combo Box内容1,在Combo Box控件属性的Data标签里面加入�,一行表示Combo Box下拉列表中的一行.换行用ctrl+回车.2,在程序初始化时动态加入� ...
- ChinaASP.Upload 错误 '80040002' You must add our copyright info
ChinaASP.Upload 错误 '80040002' You must add our copyright info: http://www.chinaasp.com 修改 第一步:在“开始-运 ...
- cocos2d-x中CCScale9Sprite的另一种实现
cocos2d 2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间部分进行拉伸,来满足一些控件的自适应(PS: 比如包括按钮,对话框,最直观 ...
- oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT_数据库的几种锁
问题如下: SQL> conn scott/tiger@vm_databaseConnected to Oracle Database 11g Enterprise Edition Releas ...
- Golang学习 - sort 包
------------------------------------------------------------ // 满足 Interface 接口的类型可以被本包的函数进行排序. type ...
- 在vim中设置将tab自动转化为4个空格
在vim中,我们只需要简单配置一下就ok了,打开~/.vimrc加上下面的几行(如果已经有了,修改一下数值就行了). set tabstop=4set softtabstop=4set shiftwi ...
- Solr DIH dataconfig配置
1. 配置文件data-config.xml定义了数据库的基本配置,以及导出数据的映射规则,即导出数据库表中对应哪些字段的值,以及对特定字段的值做如何处理 </pre><p>& ...
- oc语言复制视频文件
void copyFile() { NSString *home=NSHomeDirectory(); NSString *path=[NSString stringWithFormat:@" ...
- javascript组件开发
最近忙于重构项目,今天周末把在重构中的一些思想记记: 一.javascript的组件开发:基类的封装 由于这次重构项目需要对各种组件进行封装,并且这些组件的实现方式都差不多,所以想到对组件封装一个ba ...