String hash functions

来源:互联网 发布:redis 数据库设计实例 编辑:程序博客网 时间:2024/05/18 02:40

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 longhash(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 longsdbm(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 aterriblehashing 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'sSorting and Searching, so it stuck. It is now found mixedwith otherwise respectable code, eg. cnews. sigh.

unsigned longhash(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

0 0
原创粉丝点击