• <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>
            posts - 6, comments - 7, trackbacks - 0, articles - 0
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            在維基百科中 hash 的解釋為:
            散列函數(shù)(或散列算法,英語(yǔ)Hash Function)是一種從任何一種數(shù)據(jù)中創(chuàng)建小的數(shù)字“指紋”的方法。該函數(shù)將數(shù)據(jù)打亂混合,重新創(chuàng)建一個(gè)叫做散列值的指紋。散列值通常用來(lái)代表一個(gè)短的隨機(jī)字母和數(shù)字組成的字符串。好的散列函數(shù)在輸入域中很少出現(xiàn)散列沖突。在散列表數(shù)據(jù)處理中,不抑制沖突來(lái)區(qū)別數(shù)據(jù),會(huì)使得數(shù)據(jù)庫(kù)記錄更難找到。

            Wild Magic 中 Hash Table實(shí)現(xiàn)
            這里所指“指紋”是指:通過(guò)一個(gè)唯一的“量”(如:一個(gè)object的物理地址的指針值),在二叉樹(shù)中快速的查找到相對(duì)應(yīng)的變量。
            具體代碼:
            THashTable.hpp
             1#ifndef _HASHTABLE_HPP_
             2#define _HASHTABLE_HPP_
             3
             4#include "FoundationLIB.hpp"
             5
             6#include "System.hpp"
             7
             8/*
             9* 利用hash函數(shù) 實(shí)現(xiàn)對(duì)鏈表的訪(fǎng)問(wèn)運(yùn)算量達(dá)到和數(shù)組訪(fǎng)問(wèn)相當(dāng)?shù)男?br>10*/

            11namespace PX2
            12{
            13    /// 哈希表模板類(lèi)
            14    template<class TKEY, class TVALUE>
            15    class THashTable
            16    {
            17    public:
            18        THashTable ( int iTableSize );
            19        ~THashTable ();
            20
            21        int GetQuantity () const;
            22
            23        bool Insert ( const TKEY& rtKey, const TVALUE& rtValue );
            24
            25        TVALUE* Find ( const TKEY& rtKey ) const;
            26
            27        bool Remove ( const TKEY& rtKey );
            28        void RemoveAll ();
            29
            30        TVALUE* GetFirst ( TKEY* ptKey ) const;
            31        TVALUE* GetNext ( TKEY* ptKey ) const;
            32
            33        int (*UserHashFunction) ( const TKEY& );
            34
            35    private:
            36        class HashItem
            37        {
            38        public:
            39            TKEY m_tKey;
            40            TVALUE m_tValue;
            41            HashItem* m_pkNext;
            42        }
            ;
            43
            44        int HashFunction ( const TKEY& rtKey ) const;
            45
            46        int m_iTableSize;
            47        int m_iQuantity;
            48        HashItem** m_apkTable;
            49
            50        mutable int m_iIndex;
            51        mutable HashItem* m_pkItem;
            52    }
            ;
            53
            54#include "THashTable.inl"
            55}

            56#endif//_HASHTABLE_HPP_

            THashTable.inl
              1template <class TKEY, class TVALUE>
              2THashTable <TKEY, TVALUE> ::THashTable( int iTableSize )
              3{
              4    assert( iTableSize > 0);
              5
              6    m_iTableSize = iTableSize;
              7    m_iQuantity = 0;
              8    m_iIndex = 0;
              9    m_pkItem = 0;
             10    m_apkTable = NEW HashItem*[m_iTableSize];
             11
             12    memset(m_apkTable, 0, m_iTableSize * sizeof(HashItem*));
             13    UserHashFunction = 0;
             14}

             15//-------------------------------------------------------------------------------
             16template <class TKEY, class TVALUE>
             17THashTable<TKEY, TVALUE> ::~THashTable()
             18{
             19    RemoveAll();
             20    DELETE [] m_apkTable;
             21}

             22//-------------------------------------------------------------------------------
             23template <class TKEY, class TVALUE>
             24int THashTable<TKEY, TVALUE> ::GetQuantity () const
             25{
             26    return m_iQuantity;
             27}

             28//-------------------------------------------------------------------------------
             29template <class TKEY, class TVALUE>
             30bool THashTable<TKEY, TVALUE> ::Insert( const TKEY& rtKey,
             31                                       const TVALUE& rtValue )
             32{
             33    int iIndex = HashFunction( rtKey );
             34    HashItem* pkItem = m_apkTable[iIndex];
             35
             36    while (pkItem)
             37    {
             38        if (rtKey == pkItem->m_tKey)
             39        {
             40            return false;
             41        }

             42
             43        pkItem = pkItem->m_pkNext;
             44    }

             45
             46    pkItem = NEW HashItem;
             47    pkItem->m_tKey = rtKey;
             48    pkItem->m_tValue = rtValue;
             49    pkItem->m_pkNext = m_apkTable[iIndex];
             50    m_apkTable[iIndex] = pkItem;
             51    ++m_iQuantity;
             52
             53    return true;
             54}

             55//-------------------------------------------------------------------------------
             56template <class TKEY, class TVALUE>
             57TVALUE* THashTable<TKEY, TVALUE> ::Find( const TKEY& rtKey ) const
             58{
             59
             60    int iIndex = HashFunction( rtKey );
             61    HashItem* pkItem = m_apkTable[iIndex];
             62
             63    while( pkItem )
             64    {
             65        if (rtKey == pkItem->m_tKey )
             66        {
             67            return &pkItem->m_tValue;
             68        }

             69
             70        pkItem = pkItem->m_pkNext;
             71    }

             72    return 0;
             73}

             74//-------------------------------------------------------------------------------
             75template <class TKEY, class TVALUE>
             76bool THashTable<TKEY, TVALUE> ::Remove ( const TKEY& rtKey)
             77{
             78    int iIndex = HashFunction( rtKey );
             79    HashItem* pkItem = m_apkTable[iIndex];
             80
             81    if (!pkItem)
             82    {
             83        return false;
             84    }

             85
             86    if (rtKey == pkItem->m_tKey)
             87    {
             88        HashItem* pkSave = pkItem;
             89        m_apkTable[iIndex] = pkItem->m_pkNext;
             90        DELETE pkSave;
             91        --m_iQuantity;
             92
             93        return true;
             94    }

             95
             96    HashItem* pkPrev = pkItem;
             97    HashItem* pkCurr = pkItem->m_pkNext;
             98    while ( pkPrev && rtKey != pkCurr->m_tKey)
             99    {
            100        pkPrev = pkCurr;
            101        pkCurr = pkCurr->m_pkNext;
            102    }

            103
            104    if (pkCurr)
            105    {
            106        pkPrev->m_pkNext = pkCurr->m_pkNext;
            107        DELETE pkCurr;
            108        m_iQuantity--;
            109        return true;
            110    }

            111
            112    return false;
            113}

            114//-------------------------------------------------------------------------------
            115template <class TKEY, class TVALUE>
            116void THashTable<TKEY,TVALUE> ::RemoveAll ()
            117{
            118    if (m_iQuantity > 0)
            119    {
            120        for (int iIndex = 0; iIndex < m_iTableSize; iIndex++)
            121        {
            122            while (m_apkTable[iIndex])
            123            {
            124                HashItem* pkSave = m_apkTable[iIndex];
            125                m_apkTable[iIndex] = m_apkTable[iIndex]->m_pkNext;
            126                DELETE pkSave;
            127                if (--m_iQuantity == 0)
            128                {
            129                    return;
            130                }

            131            }

            132        }

            133    }

            134}

            135//-------------------------------------------------------------------------------
            136template <class TKEY, class TVALUE>
            137TVALUE* THashTable<TKEY,TVALUE> ::GetFirst ( TKEY* ptKey ) const
            138{
            139    if (m_iQuantity > 0)
            140    {
            141        for (m_iIndex = 0; m_iIndex < m_iTableSize; m_iIndex++)
            142        {
            143            if (m_apkTable[m_iIndex])
            144            {
            145                m_pkItem = m_apkTable[m_iIndex];
            146                *ptKey = m_pkItem->m_tKey;
            147                return &m_pkItem->m_tValue;
            148            }

            149        }

            150    }

            151
            152    return 0;
            153}

            154//-------------------------------------------------------------------------------
            155template <class TKEY, class TVALUE>
            156TVALUE* THashTable<TKEY,TVALUE> ::GetNext ( TKEY* ptKey ) const
            157{
            158    if (m_iQuantity > 0)
            159    {
            160        m_pkItem = m_pkItem->m_pkNext;
            161        if (m_pkItem)
            162        {
            163            *ptKey = m_pkItem->m_tKey;
            164            return &m_pkItem->m_tValue;
            165        }

            166
            167        for (m_iIndex++; m_iIndex < m_iTableSize; m_iIndex++)
            168        {
            169            if (m_apkTable[m_iIndex])
            170            {
            171                m_pkItem = m_apkTable[m_iIndex];
            172                *ptKey = m_pkItem->m_tKey;
            173                return &m_pkItem->m_tValue;
            174            }

            175        }

            176    }

            177
            178    return 0;
            179}

            180//-------------------------------------------------------------------------------
            181template <class TKEY, class TVALUE>
            182int THashTable<TKEY,TVALUE> ::HashFunction (const TKEY& rtKey) const
            183{
            184    if (UserHashFunction)
            185    {
            186        return (*UserHashFunction)(rtKey);
            187    }

            188
            189    // 默認(rèn)Hash 函數(shù) 采用黃金分割
            190    static double s_dHashMultiplier = 0.5*(sqrt(5.0)-1.0);
            191    unsigned int uiKey;
            192    System::Memcpy(&uiKey, sizeof(unsigned int), &rtKey, sizeof(unsigned int));
            193    uiKey %= m_iTableSize;
            194    double dFraction = fmod(s_dHashMultiplier*uiKey,1.0);
            195    return (int)floor(m_iTableSize*dFraction);
            196}

            197//-------------------------------------------------------------------------------
            198
            199//  end

            Feedback

            # re: Wild Magic 中 Hash Table實(shí)現(xiàn)[未登錄](méi)  回復(fù)  更多評(píng)論   

            2010-05-03 13:33 by megax
            這個(gè)HashClass應(yīng)用太局限了。

            # re: Wild Magic 中 Hash Table實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

            2012-08-04 14:07 by 是否
            能給注釋一下嗎
            99久久99久久久精品齐齐| 欧美亚洲国产精品久久高清| 99久久精品毛片免费播放| 国产女人aaa级久久久级| 欧美日韩精品久久久久| 精品免费久久久久久久| 色综合久久中文字幕综合网| 国产人久久人人人人爽 | 欧美一级久久久久久久大片| 久久久久久精品免费看SSS | 99久久精品免费| 一本色道久久综合亚洲精品| 久久免费美女视频| 久久无码AV中文出轨人妻| 久久伊人精品青青草原高清| 无码国内精品久久人妻蜜桃| 日韩一区二区三区视频久久| 中文字幕成人精品久久不卡| 久久亚洲AV成人出白浆无码国产| 精品熟女少妇aⅴ免费久久| 国产精品久久久久久吹潮| 久久伊人精品一区二区三区| 久久天天躁狠狠躁夜夜2020老熟妇| 99久久国语露脸精品国产| 性欧美丰满熟妇XXXX性久久久| 久久久久亚洲AV成人网| 88久久精品无码一区二区毛片| 亚洲AV无码一区东京热久久| 久久久久久亚洲精品影院| 久久久久国产日韩精品网站| 国产精品美女久久久久AV福利| 国产精品一久久香蕉国产线看| 狼狼综合久久久久综合网| 久久91精品国产91| 国产激情久久久久久熟女老人| 思思久久99热免费精品6| 日韩精品无码久久一区二区三| 热综合一本伊人久久精品| 久久99热这里只有精品66| 久久久久亚洲AV片无码下载蜜桃| 国产成人综合久久精品红|