Advertisement
expired6978

BSTArrayFunctor

Feb 27th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. template<class T>
  2. class BSTArrayFunctor
  3. {
  4. public:
  5.     BSTArrayFunctor(tArray<T> * arr, UInt32 growSize = 10, UInt32 shrinkSize = 10) : m_array(arr)
  6.     {
  7.         m_growSize = growSize;
  8.         if(m_growSize == 0)
  9.             m_growSize = 1;
  10.  
  11.         m_shrinkSize = shrinkSize;
  12.     }
  13.  
  14.     bool Allocate(UInt32 numEntries)
  15.     {
  16.         if(!m_array)
  17.             return false;
  18.  
  19.         m_array->arr.entries = (T *)FormHeap_Allocate(sizeof(T) * numEntries);
  20.         if(!m_array->arr.entries) return false;
  21.  
  22.         for(UInt32 i = 0; i < numEntries; i++)
  23.             new (&m_array->arr.entries[i]) T;
  24.  
  25.         m_array->arr.capacity = numEntries;
  26.         count = numEntries;
  27.         return true;
  28.     };
  29.  
  30.     bool Push(T & entry)
  31.     {
  32.         if(!m_array) return false;
  33.  
  34.         if(m_array->count + 1 > m_array->arr.capacity) {
  35.             if(!Grow(m_growSize))
  36.                 return false;
  37.         }
  38.  
  39.         m_array->arr.entries[m_array->count] = entry;
  40.         m_array->count++;
  41.         return true;
  42.     };
  43.  
  44.     bool Insert(UInt32 index, T & entry)
  45.     {
  46.         if(!m_array->arr.entries || index < m_array->count)
  47.             return false;
  48.  
  49.         m_array->arr.entries[index] = entry;
  50.         return true;
  51.     };
  52.  
  53.     bool Remove(UInt32 index)
  54.     {
  55.         if(!m_array->arr.entries || index < m_array->count)
  56.             return false;
  57.  
  58.         m_array->arr.entries[index] = NULL;
  59.         if(index + 1 < m_array->count) {
  60.             UInt32 remaining = m_array->count - index;
  61.             memmove_s(&m_array->arr.entries[index + 1], sizeof(T) * remaining, &m_array->arr.entries[index], sizeof(T) * remaining); // Move the rest up
  62.         }
  63.         m_array->count--;
  64.  
  65.         if(m_array->arr.capacity > m_array->count + m_shrinkSize)
  66.             Shrink();
  67.  
  68.         return true;
  69.     };
  70.  
  71.     bool Shrink()
  72.     {
  73.         if(!m_array || m_array->count == m_array->arr.capacity) return false;
  74.  
  75.         try {
  76.             UInt32 newSize = m_array->count;
  77.             T * oldArray = m_array->arr.entries;
  78.             T * newArray = (T *)FormHeap_Allocate(sizeof(T) * newSize); // Allocate new block
  79.             memmove_s(newArray, sizeof(T) * newSize, m_array->arr.entries, sizeof(T) * newSize); // Move the old block
  80.             m_array->arr.entries = newArray;
  81.             m_array->arr.capacity = m_array->count;
  82.             FormHeap_Free(oldArray); // Free the old block
  83.             return true;
  84.         }
  85.         catch(...) {
  86.             return false;
  87.         }
  88.  
  89.         return false;
  90.     }
  91.  
  92.     bool Grow(UInt32 entries)
  93.     {
  94.         if(!m_array) return false;
  95.  
  96.         try {
  97.             UInt32 oldSize = m_array->arr.capacity;
  98.             UInt32 newSize = oldSize + entries;
  99.             T * oldArray = m_array->arr.entries;
  100.             T * newArray = (T *)FormHeap_Allocate(sizeof(T) * m_array->arr.capacity + entries); // Allocate new block
  101.             memmove_s(newArray, sizeof(T) * newSize, m_array->arr.entries, sizeof(T) * m_array->arr.capacity); // Move the old block
  102.             m_array->arr.entries = newArray;
  103.             m_array->arr.capacity = newSize;
  104.             FormHeap_Free(oldArray); // Free the old block
  105.  
  106.             for(UInt32 i = oldSize; i < newSize; i++) // Allocate the rest of the free blocks
  107.                 new (&m_array->arr.entries[i]) T;
  108.            
  109.             return true;
  110.         }
  111.         catch(...) {
  112.             return false;
  113.         }
  114.  
  115.         return false;
  116.     };
  117.  
  118. private:
  119.     tArray<T> * m_array;
  120.     UInt32  m_growSize;
  121.     UInt32  m_shrinkSize;
  122. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement