Vector.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifndef VECTOR_H
  2. #define VECTOR_H
  3. #include <cassert>
  4. #include <new>
  5. #include <utility>
  6. template<typename T>
  7. class Vector {
  8. T* data;
  9. int capacity;
  10. int elements;
  11. public:
  12. Vector() : data(nullptr), capacity(0), elements(0) {
  13. }
  14. explicit Vector(int n, const T& t) : Vector() {
  15. for(int i = 0; i < n; i++) {
  16. push_back(t);
  17. }
  18. }
  19. explicit Vector(int n) : Vector(n, T()) {
  20. }
  21. ~Vector() {
  22. // placement new needs explicit destructor calling
  23. for(int i = 0; i < elements; i++) {
  24. data[i].~T();
  25. }
  26. // casting this pointer not back to its origin type yields a crash
  27. delete[] reinterpret_cast<char*>(data);
  28. }
  29. // allocate new storage for copies
  30. Vector(const Vector& other)
  31. : data(allocate(other.capacity)), capacity(other.capacity),
  32. elements(other.elements) {
  33. // copy data into new storage
  34. for(int i = 0; i < elements; i++) {
  35. new(data + i) T(other.data[i]);
  36. }
  37. }
  38. // this handles copy and move assigment
  39. // copy: replace current resources with other made by the copy constructor
  40. // move: replace current resources with other made by the move constructor
  41. // other gets the old data and is destroyed by the destructor after going
  42. // out of scope
  43. Vector& operator=(Vector other) {
  44. swap(*this, other);
  45. return *this;
  46. }
  47. // construct a default vector so the other vector gets valid values from the
  48. // swap
  49. Vector(Vector&& other) : Vector() {
  50. swap(*this, other);
  51. }
  52. void reserve(int size) {
  53. if(size <= capacity) {
  54. return;
  55. }
  56. Vector v;
  57. v.capacity = size;
  58. v.data = allocate(size);
  59. for(int i = 0; i < elements; i++) {
  60. v.push_back(std::move(data[i]));
  61. }
  62. swap(*this, v);
  63. }
  64. void resize(int size, const T& t) {
  65. // elements will be equal to size after this call but not the capacity
  66. if(size > elements) {
  67. // fill until the given size is reached
  68. for(int i = elements; i < size; i++) {
  69. push_back(t);
  70. }
  71. } else if(size < elements) {
  72. // remove objects until the size matches
  73. for(int i = size; i < elements; i++) {
  74. data[i].~T();
  75. }
  76. elements = size;
  77. }
  78. }
  79. void resize(int size) {
  80. // calling resize(size, T()); would break structures which can be
  81. // default constructed but not copied e.g. unique pointers elements will
  82. // be equal to size after this call but not the capacity
  83. if(size > elements) {
  84. // fill until the given size is reached
  85. for(int i = elements; i < size; i++) {
  86. push_back(T());
  87. }
  88. } else if(size < elements) {
  89. // remove objects until the size matches
  90. for(int i = size; i < elements; i++) {
  91. data[i].~T();
  92. }
  93. elements = size;
  94. }
  95. }
  96. void push_back(const T& t) {
  97. ensureCapacity();
  98. new(data + elements++) T(t);
  99. }
  100. void push_back(T&& t) {
  101. ensureCapacity();
  102. // && would be lost without std::move
  103. new(data + elements++) T(std::move(t));
  104. }
  105. T& operator[](int index) {
  106. return data[index];
  107. }
  108. const T& operator[](int index) const {
  109. return data[index];
  110. }
  111. T& at(int index) {
  112. // std states "at" is [] with range check
  113. assert(index >= 0 && index < elements);
  114. return (*this)[index];
  115. }
  116. const T& at(int index) const {
  117. // std states "at" is [] with range check
  118. assert(index >= 0 && index < elements);
  119. return (*this)[index];
  120. }
  121. int size() const {
  122. return elements;
  123. }
  124. T* begin() {
  125. return data;
  126. }
  127. T* end() {
  128. return data + elements;
  129. }
  130. const T* begin() const {
  131. return data;
  132. }
  133. const T* end() const {
  134. return data + elements;
  135. }
  136. void erase(T* from, T* to) {
  137. T* remove = from;
  138. T* move = to;
  139. T* stop = end();
  140. // fill the hole by moving following objects as long as possible
  141. while(move != stop) {
  142. *remove = std::move(*move);
  143. remove++;
  144. move++;
  145. }
  146. // remove left over objects
  147. while(remove != stop) {
  148. remove->~T();
  149. remove++;
  150. elements--;
  151. }
  152. }
  153. void erase(T* start) {
  154. erase(start, start + 1);
  155. }
  156. T* as_array() {
  157. return begin();
  158. }
  159. const T* as_array() const {
  160. return begin();
  161. }
  162. void erase_by_swap(int index) {
  163. elements--;
  164. if(index != elements) {
  165. data[index] = std::move(data[elements]);
  166. }
  167. data[elements].~T();
  168. }
  169. int length() const {
  170. return capacity;
  171. }
  172. private:
  173. static T* allocate(int length) {
  174. return reinterpret_cast<T*>(new char[sizeof(T) * length]);
  175. }
  176. static void swap(Vector& a, Vector& b) {
  177. std::swap(a.data, b.data);
  178. std::swap(a.capacity, b.capacity);
  179. std::swap(a.elements, b.elements);
  180. }
  181. void ensureCapacity() {
  182. if(elements >= capacity) {
  183. // doubling the size amortizes costs
  184. reserve(capacity == 0 ? 1 : capacity * 2);
  185. }
  186. }
  187. };
  188. #endif