Advertisement
Androide28

MPageArray

Jan 6th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #ifndef _MPAGEARRAY_H
  2. #define _MPAGEARRAY_H
  3.  
  4. //#pragma once
  5.  
  6. #include <vector>
  7. #include <list>
  8. #include <queue>
  9. using namespace std;
  10.  
  11. #include <crtdbg.h>
  12.  
  13.  
  14. //// PageArray ¿¡ »ðÀԵǴ µ¥ÀÌÅÍ¿¡ Index¸¦ ±â¾ïÇÏ´Â ¿ëµµ·Î »ç¿ë.
  15. class MPageArrayCursor {
  16. protected:
  17.     int     m_nIndex;
  18. public:
  19.     MPageArrayCursor(int nIndex)    { m_nIndex = nIndex; }
  20.     virtual ~MPageArrayCursor();
  21.     int GetIndex()                  { return m_nIndex; }
  22.     void SetIndex(int nIndex)       { m_nIndex = nIndex; }
  23. };
  24.  
  25.  
  26. template<class T>
  27. class MPageArray {
  28. //protected:
  29. public:
  30.     int             m_nPageSize;
  31.     int             m_nMaxIndex;
  32.     vector<T>       m_Array;
  33.     priority_queue< int,vector<int>,greater<int> >  m_UnusedQueue;
  34.  
  35. public:
  36.     MPageArray()
  37.     {
  38.         m_nPageSize = 0;
  39.         SetMaxIndex(-1);
  40.     }
  41.     virtual ~MPageArray()
  42.     {
  43.         m_Array.clear();
  44.     }
  45.  
  46.     void Reserve(int nPageSize, int nTotalSize) // ¸¶Áö¸· Page¸¦ ´Ù ¸øä¿ì°í ³¡³¯°æ¿ì´ëºñ ¿©ºÐÀ»µÐ´Ù.
  47.     {
  48.         m_nPageSize = nPageSize;
  49.        
  50.         int nReserveSize = nTotalSize;;
  51.         int nMod = nTotalSize % nPageSize;
  52.         if (nMod > 0)
  53.             nReserveSize = nTotalSize - nMod + nPageSize;
  54.  
  55.         m_Array.resize(nReserveSize);
  56.         SetMaxIndex(nTotalSize-1);
  57.  
  58.         for (int i=0; i<nTotalSize; i++) {
  59.             m_Array[i] = 0;
  60.             m_UnusedQueue.push(i);
  61.         }
  62.         for(int j=nTotalSize; j<nReserveSize; j++) {    // À׿©ºÐÀÇ ÃʱâÈ­
  63.             m_Array[j] = 0;
  64. //          m_UnusedQueue.push(j);
  65.         }
  66.     }
  67.  
  68.     int GetMaxIndex()               { return m_nMaxIndex; }
  69.     void SetMaxIndex(int nIndex)    { m_nMaxIndex = nIndex; }
  70.     int GetPageSize()               { return m_nPageSize; }
  71.  
  72.     void Set(int i, T pData, MPageArrayCursor* pCursor=NULL)
  73.     {
  74.         _ASSERT(GetMaxIndex() >= i);
  75.         _ASSERT((size_t)i<m_Array.capacity());
  76.  
  77.         if (pCursor && pCursor->GetIndex()>=0) {
  78.             m_Array[pCursor->GetIndex()] = NULL;
  79.             pCursor->SetIndex(i);
  80.         }
  81.  
  82.         m_Array[i] = pData;
  83.     }
  84.     T Get(int i)
  85.     {
  86.         return m_Array[i];
  87.     }
  88.     T Get(int nPage, int i)
  89.     {
  90.         int nIndex = GetPageSize()*nPage+i;
  91.         if (nIndex > GetMaxIndex())
  92.             return 0;
  93.         return m_Array[nIndex];
  94.     }
  95.  
  96.     void Add(T pData, MPageArrayCursor* pCursor=NULL)
  97.     {
  98.         int nFreeSlot = m_UnusedQueue.top();
  99.         m_UnusedQueue.pop();
  100.  
  101.         Set(nFreeSlot, pData, pCursor);
  102.     }
  103.     void Remove(int i, MPageArrayCursor* pCursor=NULL)
  104.     {
  105.         m_Array[i] = NULL;
  106.         m_UnusedQueue.push(i);
  107.  
  108.         if (pCursor && pCursor->GetIndex()>=0) {
  109.             pCursor->SetIndex(-1);
  110.         }
  111.     }
  112.     void Remove(T pData)
  113.     {
  114.         if (pData == NULL) return;
  115.  
  116.         for (int i=0; i<=GetMaxIndex(); i++) {
  117.             if (pData == m_Array[i]) {
  118.                 Remove(i);
  119.                 return;
  120.             }
  121.         }
  122.     }
  123. };
  124.  
  125.  
  126. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement