Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdexcept>
  3.  
  4. template <class T>
  5. class CustomVector
  6. {
  7. public:
  8.     CustomVector();
  9.     CustomVector(int elementsCount);
  10.     ~CustomVector();
  11.  
  12.     const T& operator[] (const int index) const;
  13.  
  14.     T& operator[] (const int index);
  15.  
  16.     int Count() const;
  17.     int Capacity() const;
  18.  
  19.     void Clear();
  20.     void PushBack(const T& element);
  21.     void PopBack();
  22.     void Erase(const int index);
  23.  
  24. private:
  25.     const int defaultCapacity = 10;
  26.  
  27.     T* items;
  28.     int count;
  29.     int capacity;
  30. };
  31.  
  32. template <class T>
  33. CustomVector<T>::CustomVector()
  34.     : items(nullptr),
  35.     count(0),
  36.     capacity(defaultCapacity)
  37. {
  38.     items = static_cast<T*>(malloc(sizeof(T) * capacity));
  39.     if (items == nullptr) throw new std::bad_alloc();
  40.     new (items) T[capacity]();
  41. }
  42.  
  43. template <class T>
  44. CustomVector<T>::CustomVector(int elementsCount)
  45.     : items(nullptr),
  46.     count(elementsCount),
  47.     capacity(elementsCount)
  48. {
  49.     if (elementsCount <= 0) throw std::invalid_argument("Received invalid elements count.");
  50.  
  51.     items = static_cast<T*>(malloc(sizeof(T) * capacity));
  52.     if (items == nullptr) throw new std::bad_alloc();
  53.     new (items) T[capacity]();
  54. }
  55.  
  56. template <class T>
  57. CustomVector<T>::~CustomVector()
  58. {
  59.     delete[] items;
  60. }
  61.  
  62. template <class T>
  63. const T& CustomVector<T>::operator[] (const int index) const
  64. {
  65.     if ((count == 0) || (index > count - 1) || (index < 0)) throw new std::invalid_argument("Received invalid index.");
  66.     return items[index];
  67. }
  68.  
  69. template <class T>
  70. T& CustomVector<T>::operator[] (const int index)
  71. {
  72.     if ((count == 0) || (index > count - 1) || (index < 0)) throw new std::invalid_argument("Received invalid index.");
  73.     return items[index];
  74. }
  75.  
  76. template <class T>
  77. int CustomVector<T>::Count() const
  78. {
  79.     return count;
  80. }
  81.  
  82. template <class T>
  83. int CustomVector<T>::Capacity() const
  84. {
  85.     return capacity;
  86. }
  87.  
  88. template <class T>
  89. void CustomVector<T>::Clear()
  90. {
  91.     T* newItems = static_cast<T*>(realloc(items, sizeof(T) * defaultCapacity));
  92.     if (newItems == nullptr) {
  93.         free(items);
  94.         items = nullptr;
  95.         throw new std::bad_alloc();
  96.     }
  97.     items = newItems;
  98.     count = 0;
  99.     capacity = defaultCapacity;
  100. }
  101.  
  102. template <class T>
  103. void CustomVector<T>::PushBack(const T& element)
  104. {
  105.     if (count == capacity) {
  106.         T* newItems = static_cast<T*>(realloc(items, sizeof(T) * (capacity << 1)));
  107.         if (newItems == nullptr) {
  108.             free(items);
  109.             items = nullptr;
  110.             throw new std::bad_alloc();
  111.         }
  112.  
  113.         items = newItems;
  114.         capacity <<= 1;
  115.     }
  116.  
  117.     items[count++] = element;
  118. }
  119.  
  120. template <class T>
  121. void CustomVector<T>::PopBack()
  122. {
  123.     Erase(count - 1);
  124. }
  125.  
  126. template <class T>
  127. void CustomVector<T>::Erase(const int index)
  128. {
  129.     if ((count == 0) || (index > count - 1) || (index < 0)) throw new std::invalid_argument("Received invalid index.");
  130.  
  131.     memmove(items + index, items + index + 1, (count - (index + 1)) * sizeof(T));
  132.     count--;
  133.  
  134.     if ((count < capacity / 2) && (capacity > defaultCapacity))
  135.     {
  136.         T* newItems = static_cast<T*>(realloc(items, sizeof(T) * (capacity >> 1)));
  137.         if (newItems == nullptr) {
  138.             free(items);
  139.             items = nullptr;
  140.             throw new std::bad_alloc();
  141.         }
  142.  
  143.         items = newItems;
  144.         capacity >>= 1;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement