• <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>
            隨筆-91  評論-137  文章-0  trackbacks-0
            繼上篇《簡單堆結構的實現》之后修改了下代碼,使內存的分配效率更高.
            首先將HeapApplyed和HeapUsed包含進一個結構體HEAPATTR,各占1位.
            1     struct HEAPATTR
            2     {
            3         BYTE HeapApplyed : 1;
            4         BYTE HeapUsed : 1;
            5     }*HeapAttr;
            然后添加了一個HeapCurrent變量用于提高檢索速度.
            1 int        HeapCurrent;
            相應的構造函數和析構函數修改為.
             1     Heap(const int Size = 1024 * 300)
             2         : HeapLength(Size),HeapCurrent(0),HeapLeft(Size)
             3     {
             4         HeapData = new CHAR[HeapLength];
             5         HeapAttr = new HEAPATTR[HeapLength];
             6         memset(HeapData,0,HeapLength);
             7         memset(HeapAttr,0,HeapLength);
             8     }
             9 
            10     ~Heap()
            11     {
            12         delete[] HeapData;
            13         delete[] HeapAttr;
            14     }
            注意:一個HEAPATTR結構占1個字節,實際上使用的只有2位,編譯器自動為其補齊6位形成1字節(既然只有TRUE和FALSE沒理由要用HeapLength*2字節)
            修改GetEmptyAddr算法,使其提升效率.
             1     CHAR* GetEmptyAddr(const int Size)
             2     {
             3         int Left = 0;
             4         for(int i=HeapCurrent;i<HeapLength;i++)
             5         {
             6             if(HeapAttr[i].HeapApplyed && !HeapAttr[i].HeapUsed) Left++;
             7             else Left = 0;
             8             if(Left == Size) return HeapData + i - Left + 1;
             9         }
            10         if(HeapCurrent > 0 && HeapCurrent > Size)
            11         {
            12             Left = 0;
            13             for(int i=0;i<HeapCurrent;i++)
            14             {
            15                 if(HeapAttr[i].HeapApplyed && !HeapAttr[i].HeapUsed) Left++;
            16                 else Left = 0;
            17                 if(Left == Size) return HeapData + i - Left + 1;
            18             }
            19         }
            20         return 0;
            21     }
            同時也將GetEmptyLeft修改為.
             1     inline int GetEmptyLeft(int Size)
             2     {
             3         int Left = 0;
             4         for(int i=HeapCurrent;i<HeapLength;i++)
             5         {
             6             if(!HeapAttr[i].HeapApplyed) Left++;
             7             else Left = 0;
             8             if(Left == Size) return i - Left + 1;
             9         }
            10         if(HeapCurrent > 0 && HeapCurrent > Size)
            11         {
            12             Left = 0;
            13             for(int i=0;i<HeapCurrent;i++)
            14             {
            15                 if(!HeapAttr[i].HeapApplyed) Left++;
            16                 else Left = 0;
            17                 if(Left == Size) return i - Left + 1;
            18             }
            19         }
            20         return 0;
            21     }
            添加DumpFile函數生成Dump文件.
            1     BOOL DumpFile(CHAR* FileName)
            2     {
            3         FILE* fp = fopen(FileName,"wt+");
            4         fwrite(HeapData,HeapLength,sizeof(CHAR),fp);
            5         fclose(fp);
            6         return TRUE;
            7     }

            最后給出完整代碼
            posted on 2011-02-22 17:29 lwch 閱讀(1904) 評論(1)  編輯 收藏 引用 所屬分類: 數據結構

            評論:
            # re: 堆結構的實現2 2013-05-03 15:58 | tb
            實現比較簡單啊   回復  更多評論
              
            亚洲国产精品无码成人片久久| 91精品国产高清久久久久久91 | 香蕉久久久久久狠狠色| 青青热久久综合网伊人| 亚洲国产精品婷婷久久| 大香网伊人久久综合网2020| 99久久99久久| 青青青青久久精品国产h| 岛国搬运www久久| 九九久久精品国产| 人人狠狠综合88综合久久| 日韩亚洲国产综合久久久| 99久久综合国产精品免费| 色综合久久无码中文字幕| 久久久久亚洲av无码专区导航| 51久久夜色精品国产| 久久久久亚洲AV成人网人人网站| 日韩中文久久| 国产精品久久久久9999高清| 久久国产成人午夜AV影院| 久久精品国产男包| 久久91亚洲人成电影网站| 国产精品免费看久久久香蕉| 久久亚洲AV成人无码| 狠狠色丁香婷综合久久| 一本色道久久综合狠狠躁篇| 久久久久人妻一区二区三区| 国产成人99久久亚洲综合精品 | 久久香综合精品久久伊人| AV无码久久久久不卡网站下载| 久久精品中文字幕第23页| 色婷婷综合久久久久中文| 久久久久亚洲精品男人的天堂 | 久久精品国产亚洲av日韩| 久久99精品久久久久久野外| 一本久久a久久精品vr综合| 久久精品无码一区二区三区日韩 | 亚洲精品乱码久久久久久按摩| 国产日韩久久免费影院| 国产麻豆精品久久一二三| 99999久久久久久亚洲|