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

            評論:
            # re: 堆結構的實現2 2013-05-03 15:58 | tb
            實現比較簡單啊   回復  更多評論
              
            久久久久免费精品国产| 久久综合久久性久99毛片| 久久人爽人人爽人人片AV| 久久AV高潮AV无码AV| 精品国产乱码久久久久久人妻| 日日噜噜夜夜狠狠久久丁香五月| 午夜精品久久久久久毛片| av国内精品久久久久影院| 9191精品国产免费久久| 久久精品国产清自在天天线| 成人久久精品一区二区三区| 狠狠精品干练久久久无码中文字幕 | 99国产精品久久| 日批日出水久久亚洲精品tv| MM131亚洲国产美女久久| 四虎国产精品成人免费久久| 99999久久久久久亚洲| 久久人人爽人人爽人人片AV麻烦| 久久精品国产99国产精品澳门| 久久久久18| 亚洲天堂久久精品| 国产精品久久久久AV福利动漫| 人妻无码精品久久亚瑟影视| 色成年激情久久综合| 久久久久亚洲精品无码蜜桃| 九九精品久久久久久噜噜| 久久WWW免费人成—看片| 精品熟女少妇a∨免费久久| 欧美国产成人久久精品| 久久久中文字幕日本| 国产69精品久久久久99| 国产91久久综合| AA级片免费看视频久久| 久久亚洲高清观看| 久久亚洲国产午夜精品理论片| 亚洲国产精品无码久久一区二区 | 久久久久九九精品影院| 精品久久久久久久久中文字幕| 久久国产精品一国产精品金尊| 久久综合狠狠综合久久| 久久99精品久久只有精品|