Vector.h 4.7 KB

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