Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include "Array.h"
  3. #include <exception>
  4.  
  5. using namespace std;
  6.  
  7. template <class T>
  8. Array<T>::Array() {
  9. List = nullptr;
  10. this->Size = this->Capacity = 0;
  11. }
  12.  
  13. template <class T>
  14. Array<T>::Array(int capacity) {
  15. this->Size = 0;
  16. this->Capacity = capacity;
  17. List = new T *[capacity];
  18. }
  19.  
  20. template <class T>
  21. Array<T>::Array(const Array<T>& otherArray) {
  22. this->Capacity = otherArray.Capacity;
  23. this->Size = otherArray.Size;
  24. List = new T *[Capacity];
  25. for (int i = 0; i < this->Size; ++i) {
  26. List[i] = new T;
  27. *List[i] = *otherArray.List[i];
  28. }
  29. }
  30.  
  31. template <class T>
  32. T& Array<T>::operator[] (int index) {
  33. if (index < 0 || index > this->Capacity)
  34. if (index < 0 || index >= Size) throw "Index out of range!!!";
  35. return *List[index];
  36. }
  37.  
  38. template <class T>
  39. Array<T>::~Array() {
  40. delete List;
  41. this->Capacity = this->Size = 0;
  42. }
  43.  
  44. template <class T>
  45. const Array<T>& Array<T>::operator+=(const T& newElem) {
  46. if (Size == Capacity)
  47. reallocation(newElem);
  48. List[Size] = new T;
  49. *List[Size++] = newElem;
  50.  
  51. return (*this);
  52. }
  53.  
  54. template <class T>
  55. const Array<T>& Array<T>::Insert(int index, const T& newElem) {
  56. if (index < 0 || index >= Size + 1) throw "Index out of range";
  57. if (Size == Capacity)
  58. reallocation(newElem);
  59.  
  60. for (int i = Size; i > index; --i)
  61. List[i] = List[i - 1];
  62.  
  63. List[index] = new T;
  64. *List[index] = newElem;
  65. this->Size++;
  66.  
  67. return (*this);
  68. }
  69.  
  70. template <class T>
  71. const Array<T>& Array<T>::Delete(int index) {
  72. if (index < 0 || index >= Size) throw "Index out of range";
  73. delete List[index];
  74. for (int i = index; i < Size - 1; --i)
  75. List[i] = List[i + 1];
  76. this->Size--;
  77. return *this;
  78. }
  79.  
  80. template <class T>
  81. bool Array<T>::operator=(const Array<T>& otherArray) {
  82. this->Capacity = otherArray.Capacity;
  83. this->Size = otherArray.Size;
  84.  
  85. List = new T *[Capacity];
  86. for (int i = 0; i < Size; ++i) {
  87. List[i] = new T;
  88. *List[i] = *otherArray.List[i];
  89. }
  90. }
  91.  
  92. template <class T>
  93. void Array<T>::Sort() {
  94.  
  95. }
  96.  
  97. template <class T>
  98. void Array<T>::Sort(int(*compare)(const T&, const T&)) {
  99.  
  100. }
  101.  
  102. template <class T>
  103. void Array<T>::Sort(Compare* comparator) {
  104.  
  105. }
  106.  
  107. template <class T>
  108. int Array<T>::GetSize() {
  109. return this->Size;
  110. }
  111.  
  112. template<class T>
  113. int Array<T>::GetCapacity() {
  114. return this->Capacity;
  115. }
  116.  
  117. template<class T>
  118. void reallocation(int newElems) {
  119. int newCapacity = min(2 * Capacity, Size + newElems);
  120. T** newBlock = new T *[newCapacity];
  121.  
  122. for (int i = 0; i < Size; ++i)
  123. newBlock[i] = List[i];
  124. this->Capacity = newCapacity;
  125. this->List = newBlock;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement