String hash functions
2015-06-01 22:49
656 查看
1. djb2
This algorithm (k=33) was first reported by dan bernstein many yearsago in comp.lang.c. another version of this algorithm (now favoredby bernstein) uses xor:
hash(i) = hash(i - 1) * 33 ^ str[i];the magic of number 33 (why it works better than many otherconstants, prime or not) has never been adequately explained.
unsigned long
hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
2. sdbm
This algorithm was created for sdbm (a public-domain reimplementation of ndbm)database library. it was found to do well in scrambling bits, causing betterdistribution of the keys and fewer splits. it also happens to be a goodgeneral hashing function with
good distribution. the actual function ishash(i) = hash(i - 1) * 65599 + str[i]; what is included below is thefaster version used in gawk. [there is even a faster, duff-device version] themagic constant 65599 was picked out of thin air while experimenting
withdifferent constants, and turns out to be a prime. this is one of thealgorithms used in berkeley db (see sleepycat) and elsewhere.
static unsigned long
sdbm(str)
unsigned char *str;
{
unsigned long hash = 0;
int c;
while (c = *str++)
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
3. lose lose
This hash function appeared in K&R (1st ed) but at least the reader waswarned: "This is not the best possible algorithm, but it has the merit ofextreme simplicity." This is an understatement; It is a
terriblehashing algorithm, and it could have been much better without sacrificing its"extreme simplicity." [see the second edition!] Many C programmersuse this function without actually testing it, or checking something likeKnuth's
Sorting and Searching, so it stuck. It is now found mixedwith otherwise respectable code, eg. cnews. sigh.
unsigned long
hash(unsigned char *str)
{
unsigned int hash = 0;
int c;
while (c = *str++)
hash += c;
return hash;
}
[FROM] http://www.cse.yorku.ca/~oz/hash.html
This algorithm (k=33) was first reported by dan bernstein many yearsago in comp.lang.c. another version of this algorithm (now favoredby bernstein) uses xor:
hash(i) = hash(i - 1) * 33 ^ str[i];the magic of number 33 (why it works better than many otherconstants, prime or not) has never been adequately explained.
unsigned long
hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
2. sdbm
This algorithm was created for sdbm (a public-domain reimplementation of ndbm)database library. it was found to do well in scrambling bits, causing betterdistribution of the keys and fewer splits. it also happens to be a goodgeneral hashing function with
good distribution. the actual function ishash(i) = hash(i - 1) * 65599 + str[i]; what is included below is thefaster version used in gawk. [there is even a faster, duff-device version] themagic constant 65599 was picked out of thin air while experimenting
withdifferent constants, and turns out to be a prime. this is one of thealgorithms used in berkeley db (see sleepycat) and elsewhere.
static unsigned long
sdbm(str)
unsigned char *str;
{
unsigned long hash = 0;
int c;
while (c = *str++)
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
3. lose lose
This hash function appeared in K&R (1st ed) but at least the reader waswarned: "This is not the best possible algorithm, but it has the merit ofextreme simplicity." This is an understatement; It is a
terriblehashing algorithm, and it could have been much better without sacrificing its"extreme simplicity." [see the second edition!] Many C programmersuse this function without actually testing it, or checking something likeKnuth's
Sorting and Searching, so it stuck. It is now found mixedwith otherwise respectable code, eg. cnews. sigh.
unsigned long
hash(unsigned char *str)
{
unsigned int hash = 0;
int c;
while (c = *str++)
hash += c;
return hash;
}
[FROM] http://www.cse.yorku.ca/~oz/hash.html
相关文章推荐
- c语言实现hashmap(转载)
- Linux C函数参考手册(PDF版)
- Lua教程(十七):C API简介
- Ruby中Hash的11个问题解答
- C#实现打造气泡屏幕保护效果
- Ruby简明教程之数组和Hash介绍
- C/C++数据对齐详细解析
- C 语言基础教程(我的C之旅开始了)[三]
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- C字符串操作函数实现方法小结
- C/C++ 宏详细解析
- C/C++实现对STORM运行信息查看及控制的方法
- 用C#生成不重复的随机数的代码
- 在C#中生成与PHP一样的MD5 Hash Code的方法
- js中hash和ico的关联分析
- Javascript SHA-1:Secure Hash Algorithm
- 在c和c++中实现函数回调