版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lql0716/article/details/77823946
哈希表、散列表
author@jason_ql(lql0716)
http://blog.csdn.net/lql0716
1 哈希表原理
哈希表,也称散列表,其原理就是根据所给的值
x ,利用给定的哈希函数index=f(x) 计算该函数值,所得的函数值作为索引值index ,从而在查表的时候可以快速获取其位置。哈希表是以空间换时间,即空间复杂度较高,时间复杂度很低,为
O(1) 完美的
哈希函数
可以使得键值与索引值一一对应,但是,有时候多个键值对应一个索引值也是不可避免的,这时候就会发生哈希碰撞
,解决碰撞的方法有拉链法
、线性探针法
、链地址法
。键值
:可以是字符串,也可以是数字,还可以是其两者的组合
2 哈希函数
- 哈希表的核心是哈希函数,可以把哈希函数看成一个伪随机数生成器。
- 哈希函数的核心就是尽可能的保证一一映射,索引值的分布尽可能的平均。
2.1 哈希函数原理
哈希函数的构造方法有很多,常用的有:直接定址法
、数字分析法
、平方取中法
、折叠法
、除留余数法
、随机数法
.
- 除留余数哈希连地址法示例
键值
key= [ 19,14,23,01,68,20,84,27,55,11,10,79 ]
哈希函数:index=key%13
对应的索引值index= [ 6, 1, 10, 1, 3, 7, 6, 1, 3, 11, 10, 1 ]
哈希表如下图:
2.2 经典哈希函数代码
2.2.1 RSHash
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;
}
return hash;
}
/* End Of RS Hash Function */
2.2.2 JSHash
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 */
2.2.3 PJWHash
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 */
2.2.4 ELFHash
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 */
2.2.5 BKDRHash
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 */
2.2.6 SDBMHash
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 */
2.2.7 DJBHash
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 */
2.2.8 DEKHash
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 */
2.2.9 BPHash
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 */
2.2.10 FNVHash
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 */
2.2.11 APHash
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 */
2.3 哈希表代码
- 链地址法哈希表