• <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 閱讀(1936) 評論(1)  編輯 收藏 引用 所屬分類: 數據結構

            評論:
            # re: 堆結構的實現2 2013-05-03 15:58 | tb
            實現比較簡單啊   回復  更多評論
              
            亚洲人成网站999久久久综合| 久久天天婷婷五月俺也去| 国内精品久久久人妻中文字幕| 久久精品中文字幕久久| 午夜精品久久久久久影视777| 亚洲国产精品无码成人片久久| 99精品国产在热久久无毒不卡 | 久久精品国产91久久麻豆自制| 久久精品国产清自在天天线| 久久精品人人做人人爽电影 | 国产激情久久久久影院老熟女免费| 一本久久免费视频| 久久久精品免费国产四虎| 久久精品国产色蜜蜜麻豆| 久久精品国产福利国产琪琪| 久久精品99久久香蕉国产色戒 | 久久久精品久久久久久| 99国产精品久久久久久久成人热| 中文字幕无码久久人妻| 久久精品国产黑森林| 国产91久久综合| www.久久热.com| 国产精品一久久香蕉国产线看观看| 深夜久久AAAAA级毛片免费看| 国产免费久久精品丫丫| 97久久超碰国产精品2021| 亚洲第一极品精品无码久久| 久久影视综合亚洲| 色综合久久综精品| 18岁日韩内射颜射午夜久久成人| 国产精品久久国产精品99盘 | 无码国产69精品久久久久网站| 久久精品综合网| 日本五月天婷久久网站| 色综合久久久久综合99| 亚洲午夜福利精品久久| 亚洲国产精品无码久久青草| 亚洲精品99久久久久中文字幕| 亚洲欧美日韩久久精品| 久久婷婷色综合一区二区| 精品伊人久久久|