常用Hash算法(C语言的简单实现)
作者:jingxian 时间:2021-09-10 05:23:47
如下所示:
#include "GeneralHashFunctions.h"
unsigned int RSHash(char* str, unsigned int len)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = hash * a + (*str);
a = a * b;
}
return hash;
}
/* End Of RS Hash Function */
unsigned int JSHash(char* str, unsigned int len)
{
unsigned int hash = 1315423911;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash ^= ((hash << 5) + (*str) + (hash >> 2));
}
return hash;
}
/* End Of JS Hash Function */
unsigned int PJWHash(char* str, unsigned int len)
{
const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
const unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
const unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
const unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (hash << OneEighth) + (*str);
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return hash;
}
/* End Of P. J. Weinberger Hash Function */
unsigned int ELFHash(char* str, unsigned int len)
{
unsigned int hash = 0;
unsigned int x = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (hash << 4) + (*str);
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return hash;
}
/* End Of ELF Hash Function */
unsigned int BKDRHash(char* str, unsigned int len)
{
unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (hash * seed) + (*str);
}
return hash;
}
/* End Of BKDR Hash Function */
unsigned int SDBMHash(char* str, unsigned int len)
{
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (*str) + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
/* End Of SDBM Hash Function */
unsigned int DJBHash(char* str, unsigned int len)
{
unsigned int hash = 5381;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
/* End Of DJB Hash Function */
unsigned int DEKHash(char* str, unsigned int len)
{
unsigned int hash = len;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
}
return hash;
}
/* End Of DEK Hash Function */
unsigned int BPHash(char* str, unsigned int len)
{
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = hash << 7 ^ (*str);
}
return hash;
}
/* End Of BP Hash Function */
unsigned int FNVHash(char* str, unsigned int len)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash *= fnv_prime;
hash ^= (*str);
}
return hash;
}
/* End Of FNV Hash Function */
unsigned int APHash(char* str, unsigned int len)
{
unsigned int hash = 0xAAAAAAAA;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ (*str) * (hash >> 3)) :
(~((hash << 11) + ((*str) ^ (hash >> 5))));
}
return hash;
}
/* End Of AP Hash Function */
标签:hash,算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WPF实现时钟特效
2021-11-13 11:16:24
![](https://img.aspxhome.com/file/2023/6/102276_0s.jpg)
java实现监听u盘示例分享
2023-10-20 02:14:05
Spring Bean 依赖注入常见错误问题
2022-10-02 20:46:51
![](https://img.aspxhome.com/file/2023/5/65755_0s.jpg)
java笔记学习之操作符
2022-10-19 05:31:59
线程阻塞唤醒工具 LockSupport使用详解
2023-11-29 17:16:10
C#中 Json 序列化去掉null值的方法
2022-02-02 12:13:52
springboot之Jpa通用接口及公共方法使用示例
2023-02-17 16:18:52
![](https://img.aspxhome.com/file/2023/2/86772_0s.png)
深入JAVA对象深度克隆的详解
2022-01-05 14:03:13
Java中的几种读取properties配置文件的方式
2022-09-06 13:36:19
使Java的JButton文字隐藏功能的实现(不隐藏按钮的前提)
2022-06-01 23:54:33
![](https://img.aspxhome.com/file/2023/0/83130_0s.gif)
springboot html调用js无效400问题及解决
2023-06-24 02:11:54
![](https://img.aspxhome.com/file/2023/3/97943_0s.png)
Java面试题冲刺第二十四天--并发编程
2023-08-31 05:39:02
![](https://img.aspxhome.com/file/2023/7/58447_0s.png)
C#使用winform简单导出Excel的方法
2022-06-11 06:58:49
解决Android软键盘弹出覆盖h5页面输入框问题
2023-06-19 11:33:24
![](https://img.aspxhome.com/file/2023/7/83547_0s.jpg)
Android高仿IOS 滚轮选择控件
2023-07-13 10:51:24
![](https://img.aspxhome.com/file/2023/6/122216_0s.gif)
spring mvc 实现获取后端传递的值操作示例
2023-08-10 12:55:52
解决android studio 打开java文件 内容全变了的问题
2022-03-18 19:31:44
![](https://img.aspxhome.com/file/2023/2/96032_0s.jpg)
android开发之调用手机的摄像头使用MediaRecorder录像并播放
2021-12-27 12:50:18
Java中计算时间差的方法
2023-11-15 10:35:44
C#7.0中新特性汇总
2023-06-24 18:29:13