• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

            Wild Magic 中 Hash Table實(shí)現(xiàn)
            這里所指“指紋”是指:通過一個(gè)唯一的“量”(如:一個(gè)object的物理地址的指針值),在二叉樹中快速的查找到相對應(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)對鏈表的訪問運(yùn)算量達(dá)到和數(shù)組訪問相當(dāng)?shù)男?br>10*/

            11namespace PX2
            12{
            13    /// 哈希表模板類
            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)[未登錄]  回復(fù)  更多評論   

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

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

            2012-08-04 14:07 by 是否
            能給注釋一下嗎
            91久久成人免费| 国产精品久久久久天天影视| 亚洲а∨天堂久久精品9966| 婷婷久久五月天| 99久久婷婷国产综合亚洲| 国产成人综合久久久久久| 日韩亚洲国产综合久久久| 久久国产劲爆AV内射—百度| 精品熟女少妇a∨免费久久| 精品久久人人妻人人做精品 | 久久狠狠一本精品综合网| 无码精品久久一区二区三区| 亚洲色大成网站www久久九| 91精品婷婷国产综合久久| 亚洲国产高清精品线久久 | 久久精品午夜一区二区福利| 欧美激情精品久久久久| 蜜臀久久99精品久久久久久| 久久久久久亚洲精品成人| 久久久精品波多野结衣| 久久综合狠狠综合久久综合88 | 欧美与黑人午夜性猛交久久久| 亚洲国产日韩欧美久久| 国内精品久久久久久野外| 人妻中文久久久久| 久久免费高清视频| 亚洲精品无码久久千人斩| 国产精品免费久久| 久久精品夜夜夜夜夜久久| 久久久久99这里有精品10| 99久久免费国产精品| 日韩精品久久久肉伦网站| 亚洲精品无码久久不卡| 精品午夜久久福利大片| 伊人久久大香线蕉AV色婷婷色| 久久免费大片| 国产精品综合久久第一页| 国产成人精品久久免费动漫 | 久久久久久曰本AV免费免费| 激情综合色综合久久综合| 97久久精品国产精品青草|