Advertisement
expired6978

BSTAllocator

Sep 5th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 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.         if(m_array->arr.entries)
  20.             FormHeap_Free(m_array->arr.entries);
  21.  
  22.         m_array->arr.entries = (T *)FormHeap_Allocate(sizeof(T) * numEntries);
  23.         if(!m_array->arr.entries) return false;
  24.  
  25.         for(UInt32 i = 0; i < numEntries; i++)
  26.             new (&m_array->arr.entries[i]) T;
  27.  
  28.         m_array->arr.capacity = numEntries;
  29.         m_array->count = numEntries;
  30.         return true;
  31.     };
  32.  
  33.     void Set(T * entries, UInt32 count)
  34.     {
  35.         // Not enough space to hold the remaining items, free the old list and allocate
  36.         if (count > m_array->arr.capacity) {
  37.             // Free the existing array
  38.             if (m_array->arr.entries)
  39.                 FormHeap_Free(m_array->arr.entries);
  40.  
  41.             UInt32 newCapacity = count;
  42.             if (count % m_growSize > 0) // Amount we are allocating to is less the growth size
  43.                 newCapacity = count + (count % m_growSize);
  44.  
  45.             m_array->arr.entries = (T *)FormHeap_Allocate(sizeof(T) * newCapacity);
  46.             memcpy_s(m_array->arr.entries, sizeof(T) * count, entries, sizeof(T) * count);
  47.  
  48.             if (newCapacity > count) {
  49.                 for (UInt32 i = count; i < newCapacity; i++) // Allocate the remaining capacity
  50.                     new (&m_array->arr.entries[i]) T;
  51.             }
  52.  
  53.             m_array->arr.capacity = newCapacity;
  54.             m_array->count = count;
  55.         }
  56.         else
  57.         {
  58.             memcpy_s(m_array->arr.entries, sizeof(T) * count, entries, sizeof(T) * count);
  59.  
  60.             if (m_array->arr.capacity > count) {
  61.                 for (UInt32 i = count; i < m_array->arr.capacity; i++) // Allocate the remaining empty capacity
  62.                     new (&m_array->arr.entries[i]) T;
  63.             }
  64.  
  65.             m_array->count = count;
  66.         }
  67.     }
  68.  
  69.     bool Push(T & entry)
  70.     {
  71.         if(!m_array) return false;
  72.  
  73.         if(m_array->count + 1 > m_array->arr.capacity) {
  74.             if(!Grow(m_growSize))
  75.                 return false;
  76.         }
  77.  
  78.         m_array->arr.entries[m_array->count] = entry;
  79.         m_array->count++;
  80.         return true;
  81.     };
  82.  
  83.     bool Insert(UInt32 index, T & entry)
  84.     {
  85.         if(!m_array->arr.entries || index < m_array->count)
  86.             return false;
  87.  
  88.         m_array->arr.entries[index] = entry;
  89.         return true;
  90.     };
  91.  
  92.     bool Remove(UInt32 index)
  93.     {
  94.         if(!m_array->arr.entries || index < m_array->count)
  95.             return false;
  96.  
  97.         m_array->arr.entries[index] = NULL;
  98.         if(index + 1 < m_array->count) {
  99.             UInt32 remaining = m_array->count - index;
  100.             memmove_s(&m_array->arr.entries[index + 1], sizeof(T) * remaining, &m_array->arr.entries[index], sizeof(T) * remaining); // Move the rest up
  101.         }
  102.         m_array->count--;
  103.  
  104.         if(m_array->arr.capacity > m_array->count + m_shrinkSize)
  105.             Shrink();
  106.  
  107.         return true;
  108.     };
  109.  
  110.     bool Shrink()
  111.     {
  112.         if(!m_array || m_array->count == m_array->arr.capacity) return false;
  113.  
  114.         try {
  115.             UInt32 newSize = m_array->count;
  116.             T * oldArray = m_array->arr.entries;
  117.             T * newArray = (T *)FormHeap_Allocate(sizeof(T) * newSize); // Allocate new block
  118.             memmove_s(newArray, sizeof(T) * newSize, m_array->arr.entries, sizeof(T) * newSize); // Move the old block
  119.             m_array->arr.entries = newArray;
  120.             m_array->arr.capacity = m_array->count;
  121.             FormHeap_Free(oldArray); // Free the old block
  122.             return true;
  123.         }
  124.         catch(...) {
  125.             return false;
  126.         }
  127.  
  128.         return false;
  129.     }
  130.  
  131.     bool Grow(UInt32 entries)
  132.     {
  133.         if(!m_array) return false;
  134.  
  135.         try {
  136.             UInt32 oldSize = m_array->arr.capacity;
  137.             UInt32 newSize = oldSize + entries;
  138.             T * oldArray = m_array->arr.entries;
  139.             T * newArray = (T *)FormHeap_Allocate(sizeof(T) * m_array->arr.capacity + entries); // Allocate new block
  140.             memmove_s(newArray, sizeof(T) * newSize, m_array->arr.entries, sizeof(T) * m_array->arr.capacity); // Move the old block
  141.             m_array->arr.entries = newArray;
  142.             m_array->arr.capacity = newSize;
  143.             FormHeap_Free(oldArray); // Free the old block
  144.  
  145.             for(UInt32 i = oldSize; i < newSize; i++) // Allocate the rest of the free blocks
  146.                 new (&m_array->arr.entries[i]) T;
  147.            
  148.             return true;
  149.         }
  150.         catch(...) {
  151.             return false;
  152.         }
  153.  
  154.         return false;
  155.     };
  156.  
  157. private:
  158.     tArray<T> * m_array;
  159.     UInt32  m_growSize;
  160.     UInt32  m_shrinkSize;
  161. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement