• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            無(wú)我

            讓內(nèi)心永遠(yuǎn)燃燒著偉大的光明的精神之火!
            靈活的思考,嚴(yán)謹(jǐn)?shù)膶?shí)現(xiàn)
            豪邁的氣魄、頑強(qiáng)的意志和周全的思考

            hash函數(shù)——djb2、sdbm、lose lose

            本文內(nèi)容轉(zhuǎn)自于http://www.cse.yorku.ca/~oz/hash.html。因?yàn)樗麑?duì)給出了幾個(gè)非常好的hash函數(shù),而其中的sdbm就是我們將剖析的eSNACC用的hash的原型。文章是英文的,但是通俗易懂,就摘錄在此了。

             

            Hash Functions

            A comprehensive collection of hash functions, a hash visualiser and some test results [see Mckenzie et al. Selecting a Hashing Algorithm, SP&E 20(2):209-224, Feb 1990] will be available someday. If you just want to have a good hash function, and cannot wait, djb2 is one of the best string hash functions i know. it has excellent distribution and speed on many different sets of keys and table sizes. you are not likely to do better with one of the "well known" functions such as PJW, K&R[1], etc. Also see tpop pp. 126 for graphing hash functions.


             

            djb2

            this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. another version of this algorithm (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why it works better than many other constants, 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;
                }
            


             

            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 better distribution of the keys and fewer splits. it also happens to be a good general hashing function with good distribution. the actual function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below is the faster version used in gawk. [there is even a faster, duff-device version] the magic constant 65599 was picked out of thin air while experimenting with different constants, and turns out to be a prime. this is one of the algorithms 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;
                }

            lose lose

            This hash function appeared in K&R (1st ed) but at least the reader was warned: "This is not the best possible algorithm, but it has the merit of extreme simplicity." This is an understatement; It is a terrible hashing algorithm, and it could have been much better without sacrificing its "extreme simplicity." [see the second edition!] Many C programmers use this function without actually testing it, or checking something like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise respectable code, eg. cnews. sigh. [see also: tpop]

                unsigned long
                hash(unsigned char *str)
                {
            	unsigned int hash = 0;
            	int c;
            
            	while (c = *str++)
            	    hash += c;
            
            	return hash;
                }
            

             

             

            posted on 2012-04-26 08:52 Tim 閱讀(2609) 評(píng)論(1)  編輯 收藏 引用 所屬分類(lèi): C/C++語(yǔ)言

            評(píng)論

            # re: hash函數(shù)——djb2、sdbm、lose lose[未登錄](méi) 2012-04-27 09:35 Tina

            頂!  回復(fù)  更多評(píng)論   

            <2008年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導(dǎo)航

            統(tǒng)計(jì)

            公告

            本博客原創(chuàng)文章,歡迎轉(zhuǎn)載和交流。不過(guò)請(qǐng)注明以下信息:
            作者:TimWu
            郵箱:timfly@yeah.net
            來(lái)源:www.shnenglu.com/Tim
            感謝您對(duì)我的支持!

            留言簿(9)

            隨筆分類(lèi)(173)

            IT

            Life

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            久久综合五月丁香久久激情| 欧美伊人久久大香线蕉综合69| 久久只这里是精品66| 亚洲精品无码久久久久AV麻豆| 亚洲第一永久AV网站久久精品男人的天堂AV| 激情综合色综合久久综合| 久久久久亚洲AV成人网人人网站 | 久久精品国产99久久香蕉| 久久艹国产| 青青草原精品99久久精品66| 99久久国产亚洲高清观看2024| 久久综合狠狠综合久久97色| 久久99国产精品尤物| 日韩欧美亚洲综合久久影院Ds| 国内精品伊人久久久久AV影院| 久久综合久久伊人| 亚洲伊人久久大香线蕉苏妲己| 久久伊人精品一区二区三区| 久久婷婷综合中文字幕| 国内精品久久久久久久久电影网 | 久久国产精品成人影院| 日日狠狠久久偷偷色综合免费 | 国产一区二区三区久久精品| 青青草国产成人久久91网| 国产精品99久久久精品无码| 久久艹国产| 久久国产成人午夜AV影院| 国产美女久久久| 国产精品久久久久久| 一本一道久久综合狠狠老| 午夜精品久久久久久影视riav| 国产精品美女久久久网AV| 久久久久久久尹人综合网亚洲| 热综合一本伊人久久精品| 久久这里只有精品首页| 亚洲国产另类久久久精品| 色青青草原桃花久久综合| 色播久久人人爽人人爽人人片AV| 久久精品成人一区二区三区| 国产亚洲精久久久久久无码AV| 中文字幕一区二区三区久久网站|