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
- String hash functions
- Javascript hash functions to convert string into integer hash
- Hash Functions
- Hash Functions
- Hash Functions
- Hash Functions for Hash Table Lookup
- Hash Functions for Hash Table Lookup
- Hash Functions for Hash Table Lookup
- Hash Table: Hash Functions--Data Structure
- Learning Perl: 6.3. Hash Functions
- java之Secure hash functions
- Perl String Functions
- C++ string functions
- tchar string functions
- Simple String Compression Functions
- Basic String Handling Functions
- Oracle SQL string functions
- XPath(6) string functions
- python中函数返回值为func 和func() 的区别
- twemproxy
- Twitter“鲸鱼”故障技术剖析
- [010] 百度地图API之根据经纬度查询地址信息(Android)
- jdbc AbstractDao的一些代码片段
- String hash functions
- hdu 2159 FATE(二维完全背包)
- [011] 百度地图API之POI搜索-发现你身边的兴趣点,如超市、餐厅、ATM...(Android)
- 购物车中拼接的json数据
- 让自己的过去现在将来掌握在自己手中
- [012] 百度地图API之公交换乘方案搜索(Android)
- vs2013 no devices attached
- FOJ 1063 三维扫描 // DFS
- swing进度条