Advertisement
BeloMaximka

Belov_HW_O25

Oct 7th, 2021 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #pragma once
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. template <class T>
  6. class Array
  7. {
  8. private:
  9.     T* arr = nullptr;
  10.     int size = 0;
  11.     int grow = 1;
  12.     int bound = 0;
  13.  
  14.     void CheckIndex(int index)const;
  15.  
  16. public:
  17.     Array<T>() = default;
  18.     Array<T>(int size, int grow = 1);
  19.     Array<T>(initializer_list<T> values, int grow = 1);
  20.     Array<T>(const Array<T>& arr);
  21.     Array<T>(Array&& arr);
  22.  
  23.     int GetSize()const;
  24.     void SetSize(int size, int grow = 1);
  25.  
  26.     int GetUpperBound()const;
  27.     bool IsEmpty()const;
  28.  
  29.     void FreeExtra();
  30.     void RemoveAll();
  31.  
  32.     T GetAt(int index)const;
  33.     void SetAt(int index, T value);
  34.  
  35.     void Add(T value);
  36.     void Append(const Array& arr);
  37.  
  38.     T* GetData()const;
  39.     void InsertAt(int index, T value);
  40.     void InsertAt(int index, initializer_list<T> values);
  41.     void RemoveAt(int index);
  42.     void RemoveAt(int first, int last);
  43.  
  44.     Array<T>& operator=(const Array& arr);
  45.     const T& operator[](int index)const;
  46.     T& operator[](int index);
  47.  
  48.     ~Array<T>();
  49. };
  50.  
  51. template<class T>
  52. inline void Array<T>::CheckIndex(int index)const
  53. {
  54.     if (index < 0 || index > bound - 1) throw "Index out of bounds";
  55. }
  56.  
  57. template<class T>
  58. inline int Array<T>::GetSize()const
  59. {
  60.     return size;
  61. }
  62.  
  63. template<class T>
  64. inline void Array<T>::SetSize(int size, int grow)
  65. {
  66.     if (grow < 1) throw "Grow must be greater than zero.";
  67.     if (arr == nullptr)
  68.     {
  69.         arr = new T[size]{};
  70.         this->size = size;
  71.         this->grow = grow;
  72.         bound = size;
  73.     }
  74.     else
  75.     {
  76.         T* arr_new = new T[size]{};
  77.         int smaller_size = this->size < size ? this->size : size;
  78.         copy(arr, arr + smaller_size, arr_new);
  79.  
  80.         bound = smaller_size;
  81.         this->size = size;
  82.         this->grow = grow;
  83.  
  84.         delete[] arr;
  85.         arr = arr_new;
  86.     }
  87. }
  88.  
  89. template<class T>
  90. inline int Array<T>::GetUpperBound()const
  91. {
  92.     return bound - 1;
  93. }
  94.  
  95. template<class T>
  96. inline bool Array<T>::IsEmpty()const
  97. {
  98.     return bound == 0;
  99. }
  100.  
  101. template<class T>
  102. inline void Array<T>::FreeExtra()
  103. {
  104.     if (bound == size) return;
  105.     T* arr_new = new T[bound]{};
  106.     copy(arr, arr + bound, arr_new);
  107.  
  108.     delete[] arr;
  109.     arr = arr_new;
  110.     size = bound;
  111. }
  112.  
  113. template<class T>
  114. inline void Array<T>::RemoveAll()
  115. {
  116.     delete[] arr;
  117.     arr = nullptr;
  118.     size = bound = 0;
  119. }
  120.  
  121. template<class T>
  122. inline T Array<T>::GetAt(int index)const
  123. {
  124.     CheckIndex(index);
  125.     return arr[index];
  126. }
  127.  
  128. template<class T>
  129. inline void Array<T>::SetAt(int index, T value)
  130. {
  131.     CheckIndex(index);
  132.     arr[index] = value;
  133. }
  134.  
  135. template<class T>
  136. inline void Array<T>::Add(T value)
  137. {
  138.     if (bound == size) SetSize(size + grow, grow);
  139.     arr[bound++ - 1] = value;
  140. }
  141.  
  142. template<class T>
  143. inline void Array<T>::Append(const Array& arr)
  144. {
  145.     if (bound + arr.bound > size) SetSize(bound + arr.bound, grow);
  146.     copy(arr.arr, arr.arr + arr.bound, this->arr + bound);
  147.     bound += arr.bound;
  148. }
  149.  
  150. template<class T>
  151. inline T* Array<T>::GetData() const
  152. {
  153.     return arr;
  154. }
  155.  
  156. template<class T>
  157. inline void Array<T>::InsertAt(int index, T value)
  158. {
  159.     if (index < 0 && index > bound) throw "Index out of bounds";
  160.     if (bound == size) SetSize(size + grow, grow);
  161.     copy(arr + index, arr + bound, arr + index + 1);
  162.     arr[index] = value;
  163.     bound++;
  164. }
  165.  
  166. template<class T>
  167. inline void Array<T>::InsertAt(int index, initializer_list<T> values)
  168. {
  169.     if (index < 0 && index > bound) throw "Index out of bounds";
  170.     if (size - bound < values.size()) SetSize(bound + values.size(), grow);
  171.     copy(arr + index, arr + bound, arr + index + values.size() - 1);
  172.     copy(values.begin(), values.end(), arr + index);
  173.     bound++;
  174. }
  175.  
  176. template<class T>
  177. inline void Array<T>::RemoveAt(int index)
  178. {
  179.     CheckIndex(index);
  180.     if (index != bound - 1)
  181.         copy(arr + index + 1, arr + bound, arr + index);
  182.     bound--;
  183. }
  184.  
  185. template<class T>
  186. inline void Array<T>::RemoveAt(int first, int last)
  187. {
  188.     CheckIndex(first);
  189.     CheckIndex(last);
  190.     if (first == last); return;
  191.     if (first > last) swap(first, last);
  192.     copy(arr, arr + first, arr);
  193.     copy(arr + last, arr + bound, arr);
  194.     bound -= last - first;
  195. }
  196.  
  197. template<class T>
  198. inline Array<T>& Array<T>::operator=(const Array& arr)
  199. {
  200.     if (this == &arr) return *this;
  201.     if (this->arr != nullptr) delete[] this->arr;
  202.  
  203.     SetSize(arr.size, arr.grow);
  204.     copy(arr.arr, arr.arr + bound, this->arr);
  205.     return *this;
  206. }
  207.  
  208. template<class T>
  209. inline const T& Array<T>::operator[](int index) const
  210. {
  211.     CheckIndex(index);
  212.     return arr[index];
  213. }
  214.  
  215. template<class T>
  216. inline T& Array<T>::operator[](int index)
  217. {
  218.     CheckIndex(index);
  219.     return arr[index];
  220. }
  221.  
  222. template<class T>
  223. inline Array<T>::Array(int size, int grow)
  224. {
  225.     SetSize(size, grow);
  226. }
  227.  
  228. template<class T>
  229. inline Array<T>::Array(initializer_list<T> values, int grow)
  230. {
  231.     SetSize(values.size(), grow);
  232.     copy(values.begin(), values.end(), arr);
  233.     bound = size;
  234. }
  235.  
  236. template<class T>
  237. inline Array<T>::Array(const Array<T>& arr)
  238. {
  239.     SetSize(arr.size, arr.grow);
  240.     copy(arr.arr, arr.arr + arr.bound, this->arr);
  241.     bound = arr.bound;
  242. }
  243.  
  244. template<class T>
  245. inline Array<T>::Array(Array&& arr) :size(arr.size), grow(grow), bound(bound)
  246. {
  247.     arr = arr.arr;
  248.     arr.arr = nullptr;
  249.     arr.size = arr.bound = 0;
  250. }
  251.  
  252. template<class T>
  253. inline Array<T>::~Array()
  254. {
  255.     if (arr != nullptr) delete[] arr;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement