• <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>

            無我

            讓內心永遠燃燒著偉大的光明的精神之火!
            靈活的思考,嚴謹的實現
            豪邁的氣魄、頑強的意志和周全的思考

            hash函數——djb2、sdbm、lose lose

            本文內容轉自于http://www.cse.yorku.ca/~oz/hash.html。因為他對給出了幾個非常好的hash函數,而其中的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 閱讀(2644) 評論(1)  編輯 收藏 引用 所屬分類: C/C++語言

            評論

            # re: hash函數——djb2、sdbm、lose lose[未登錄] 2012-04-27 09:35 Tina

            頂!  回復  更多評論   

            <2012年4月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            公告

            本博客原創文章,歡迎轉載和交流。不過請注明以下信息:
            作者:TimWu
            郵箱:timfly@yeah.net
            來源:www.shnenglu.com/Tim
            感謝您對我的支持!

            留言簿(9)

            隨筆分類(173)

            IT

            Life

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            久久婷婷五月综合色奶水99啪 | 国产精品九九久久免费视频| 一本色道久久88精品综合| 久久人人爽人人爽人人片AV高清| 少妇久久久久久被弄到高潮 | 久久中文字幕人妻熟av女| 久久精品免费网站网| 人人狠狠综合久久亚洲高清| 麻豆亚洲AV永久无码精品久久| 狠狠色丁香久久综合婷婷| 久久久久久亚洲精品无码| 久久精品国产亚洲av麻豆蜜芽| 99久久人妻无码精品系列蜜桃| 久久强奷乱码老熟女| 亚洲av成人无码久久精品| AA级片免费看视频久久| 天堂久久天堂AV色综合| 久久精品成人免费国产片小草| 精品久久久无码人妻中文字幕| 狠狠色综合网站久久久久久久| 日产精品久久久一区二区| 久久久久国产精品嫩草影院| 久久久久久九九99精品| 中文字幕精品久久久久人妻| 国产一久久香蕉国产线看观看 | 国产精品一区二区久久精品无码 | 久久精品国产福利国产秒| 一本久久免费视频| 久久久精品久久久久特色影视| 国产精品99久久99久久久| 国产69精品久久久久观看软件| 亚洲一区中文字幕久久| 老色鬼久久亚洲AV综合| 亚洲人成网站999久久久综合| 久久精品国产一区二区| 久久无码av三级| 国产美女久久精品香蕉69| 亚洲国产美女精品久久久久∴| 99久久这里只精品国产免费| 久久精品亚洲乱码伦伦中文 | 国内精品伊人久久久久777|