您的位置:首页 > 编程语言 > Go语言

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hash c algorithm