Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #ifndef DARRAY_H__
  2. #define DARRAY_H__
  3.  
  4. #include <iostream>
  5.  
  6. template<typename T>
  7. class Darray
  8. {
  9. public:
  10. Darray()
  11. :m_tab{new T[8U]},
  12. m_size{8U},
  13. m_capacity{8U}
  14. {
  15. for (std::size_t i = 0; i < m_capacity; ++i)
  16. m_tab[i] = T{};
  17. }
  18.  
  19. Darray(std::size_t initial_capacity)
  20. :m_tab{new T[initial_capacity]},
  21. m_size{initial_capacity},
  22. m_capacity{initial_capacity}
  23. {
  24. for (std::size_t i = 0; i < m_capacity; ++i)
  25. m_tab[i] = T{};
  26. }
  27.  
  28. Darray(const Darray& another)
  29. :m_tab{new T[another.m_capacity]},
  30. m_size{another.m_size},
  31. m_capacity{another.m_capacity}
  32. {
  33. for (std::size_t i = 0; i < m_size; ++i)
  34. m_tab[i] = another[i];
  35. }
  36.  
  37. Darray& operator=(const Darray& another)
  38. {
  39. m_size = another.m_size;
  40. m_capacity = another.m_capacity;
  41. delete[] m_tab;
  42. m_tab = new T[m_capacity];
  43. for (std::size_t i = 0; i < m_size; ++i)
  44. m_tab[i] = another[i];
  45. return *this;
  46. }
  47.  
  48. Darray(Darray&& another)
  49. :m_tab{another.m_tab},
  50. m_size{another.m_size},
  51. m_capacity{another.m_capacity}
  52. {
  53. another.m_size = another.m_capacity = 0;
  54. another.m_tab = nullptr;
  55. }
  56.  
  57. Darray& operator=(Darray&& another)
  58. {
  59. m_size = another.m_size;
  60. m_capacity = another.m_capacity;
  61. delete[] m_tab;
  62. m_tab = another.m_tab;
  63.  
  64. another.m_size = another.m_capacity = 0;
  65. another.m_tab = nullptr;
  66.  
  67. return *this;
  68. }
  69.  
  70. T* begin()
  71. {
  72. return &m_tab[0];
  73. }
  74.  
  75. const T* begin() const
  76. {
  77. return &m_tab[size];
  78. }
  79.  
  80. T* end()
  81. {
  82. return &m_tab[m_size];
  83. }
  84.  
  85. const T* end() const
  86. {
  87. return &m_tab[m_size];
  88. }
  89.  
  90. size_t size() const
  91. {
  92. return m_size;
  93. }
  94.  
  95. size_t capacity() const
  96. {
  97. return m_capacity;
  98. }
  99.  
  100. void resize(size_t new_size)
  101. {
  102. if (new_size <= m_capacity)
  103. return;
  104.  
  105. T* tp = new T[new_size];
  106. m_capacity = new_size;
  107.  
  108. unsigned i;
  109. for (i = 0; i < m_size; ++i)
  110. tp[i] = m_tab[i];
  111.  
  112. for (; i < m_capacity; ++i)
  113. m_tab[i] = T{};
  114.  
  115. delete[] m_tab;
  116. m_tab = tp;
  117. }
  118.  
  119. void push_back(const T& value)
  120. {
  121. if(m_size == m_capacity)
  122. resize(2 * m_capacity);
  123.  
  124. m_tab[m_size++] = value;
  125. }
  126.  
  127. T& operator[](size_t pos)
  128. {
  129. return m_tab[pos];
  130. }
  131.  
  132. const T& operator[](size_t pos) const
  133. {
  134. return m_tab[pos];
  135. }
  136.  
  137. ~Darray()
  138. {
  139. delete[] m_tab;
  140. }
  141.  
  142. private:
  143. T* m_tab;
  144. std::size_t m_size;
  145. std::size_t m_capacity;
  146. };
  147.  
  148. #endif // DARRAY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement