Main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include <cassert>
  2. #include <iostream>
  3. #include <vector>
  4. // All implementations in this class use ints insteads of size_t because size_t
  5. // is interely slow compared to ints. C++20 also adds new methods to call for
  6. // signed container sizes. Several online resources also suggest to use unsigned
  7. // types mostly for bit operations and nothing else.
  8. // This struct is used in the automated tests at the end of the file.
  9. // It counts all instances with different numbers to ensure each object is
  10. // constructed and destructed the same amount of times.
  11. // A also has a non trivial constructor to test the vector
  12. struct A {
  13. static int instances;
  14. int a;
  15. A(int a) : a(a) {
  16. // std::cout << "construct A " << a << "\n";
  17. instances += a;
  18. }
  19. A(const A& other) : a(other.a) {
  20. // std::cout << "copy construct A " << a << "\n";
  21. instances += a;
  22. }
  23. A(A&& other) : a(other.a) {
  24. // std::cout << "move construct A " << a << "\n";
  25. instances += a;
  26. }
  27. A& operator=(const A& other) {
  28. instances -= a;
  29. a = other.a;
  30. instances += a;
  31. // std::cout << "copy assignment A " << a << "\n";
  32. return *this;
  33. }
  34. A& operator=(A&& other) {
  35. instances -= a;
  36. a = other.a;
  37. instances += a;
  38. // std::cout << "move assignment A " << a << "\n";
  39. return *this;
  40. }
  41. ~A() {
  42. // std::cout << "destruct A " << a << "\n";
  43. instances -= a;
  44. }
  45. };
  46. int A::instances = 0;
  47. template<typename T>
  48. class Vector {
  49. T* data;
  50. int capacity;
  51. int elements;
  52. public:
  53. Vector() : data(nullptr), capacity(0), elements(0) {
  54. }
  55. Vector(int n, const T& t) : Vector() {
  56. for(int i = 0; i < n; i++) {
  57. push_back(t);
  58. }
  59. }
  60. Vector(int n) : Vector(n, T()) {
  61. }
  62. ~Vector() {
  63. // placement new needs explicit destructor calling
  64. for(int i = 0; i < elements; i++) {
  65. data[i].~T();
  66. }
  67. // casting this pointer not back to its origin type yields a crash
  68. delete[] reinterpret_cast<char*>(data);
  69. }
  70. // allocate new storage for copies
  71. Vector(const Vector& other)
  72. : data(allocate(other.capacity)), capacity(other.capacity),
  73. elements(other.elements) {
  74. // copy data into new storage
  75. for(int i = 0; i < elements; i++) {
  76. new(data + i) T(other.data[i]);
  77. }
  78. }
  79. // this handles copy and move assigment
  80. // copy: replace current resources with other made by the copy constructor
  81. // move: replace current resources with other made by the move constructor
  82. // other gets the old data and is destroyed by the destructor after going
  83. // out of scope
  84. Vector& operator=(Vector other) {
  85. swap(*this, other);
  86. return *this;
  87. }
  88. // construct a default vector so the other vector gets valid values from the
  89. // swap
  90. Vector(Vector&& other) : Vector() {
  91. swap(*this, other);
  92. }
  93. void reserve(int size) {
  94. if(size <= capacity) {
  95. return;
  96. }
  97. Vector v;
  98. v.capacity = size;
  99. v.data = allocate(size);
  100. for(int i = 0; i < elements; i++) {
  101. v.push_back(std::move(data[i]));
  102. }
  103. swap(*this, v);
  104. }
  105. void resize(int size, const T& t) {
  106. // elements will be equal to size after this call but not the capacity
  107. if(size > elements) {
  108. // fill until the given size is reached
  109. for(int i = elements; i < size; i++) {
  110. push_back(t);
  111. }
  112. } else if(size < elements) {
  113. // remove objects until the size matches
  114. for(int i = size; i < elements; i++) {
  115. data[i].~T();
  116. }
  117. elements = size;
  118. }
  119. }
  120. void resize(int size) {
  121. resize(size, T());
  122. }
  123. void push_back(const T& t) {
  124. ensureCapacity();
  125. new(data + elements++) T(t);
  126. }
  127. void push_back(T&& t) {
  128. ensureCapacity();
  129. // && would be lost without std::move
  130. new(data + elements++) T(std::move(t));
  131. }
  132. T& operator[](int index) {
  133. return data[index];
  134. }
  135. const T& operator[](int index) const {
  136. return data[index];
  137. }
  138. T& at(int index) {
  139. // std states "at" is [] with range check
  140. assert(index >= 0 && index < elements);
  141. return (*this)[index];
  142. }
  143. const T& at(int index) const {
  144. // std states "at" is [] with range check
  145. assert(index >= 0 && index < elements);
  146. return (*this)[index];
  147. }
  148. int size() const {
  149. return elements;
  150. }
  151. T* begin() {
  152. return data;
  153. }
  154. T* end() {
  155. return data + elements;
  156. }
  157. const T* begin() const {
  158. return data;
  159. }
  160. const T* end() const {
  161. return data + elements;
  162. }
  163. void erase(T* from, T* to) {
  164. T* remove = from;
  165. T* move = to;
  166. T* stop = end();
  167. // fill the hole by moving following objects as long as possible
  168. while(move != stop) {
  169. *remove = std::move(*move);
  170. remove++;
  171. move++;
  172. }
  173. // remove left over objects
  174. while(remove != stop) {
  175. remove->~T();
  176. remove++;
  177. elements--;
  178. }
  179. }
  180. void erase(T* start) {
  181. erase(start, start + 1);
  182. }
  183. T* as_array() {
  184. return begin();
  185. }
  186. const T* as_array() const {
  187. return begin();
  188. }
  189. void erase_by_swap(int index) {
  190. elements--;
  191. if(index != elements) {
  192. data[index] = std::move(data[elements]);
  193. }
  194. data[elements].~T();
  195. }
  196. int length() const {
  197. return capacity;
  198. }
  199. private:
  200. static T* allocate(int length) {
  201. return reinterpret_cast<T*>(new char[sizeof(T) * length]);
  202. }
  203. static void swap(Vector& a, Vector& b) {
  204. std::swap(a.data, b.data);
  205. std::swap(a.capacity, b.capacity);
  206. std::swap(a.elements, b.elements);
  207. }
  208. void ensureCapacity() {
  209. if(elements >= capacity) {
  210. // doubling the size amortizes costs
  211. reserve(capacity == 0 ? 1 : capacity * 2);
  212. }
  213. }
  214. };
  215. void printError(int number) {
  216. std::cout << "\033[0;31mError " << number << "\033[0m\n";
  217. }
  218. // V is either std::vector or Vector to test for complete same behaviour in both
  219. // implementations
  220. template<typename V>
  221. void test() {
  222. {
  223. const int elements = 2;
  224. V v;
  225. for(int i = 0; i < elements; i++) {
  226. v.push_back(A(i));
  227. }
  228. const V& cv = v;
  229. for(int i = 0; i < elements; i++) {
  230. if(v[i].a != i || cv[i].a != i || v.at(i).a != i ||
  231. cv.at(i).a != i) {
  232. printError(1);
  233. }
  234. }
  235. if(v.size() != elements) {
  236. printError(2);
  237. }
  238. }
  239. {
  240. V v1;
  241. v1.push_back(A(10));
  242. V v2 = v1;
  243. V v3;
  244. v3.push_back(A(20));
  245. v3 = v1;
  246. if(v1[0].a != 10 || v2[0].a != 10 || v3[0].a != 10) {
  247. printError(3);
  248. }
  249. V v4 = std::move(v1);
  250. V v5(std::move(v2));
  251. V v6;
  252. v6 = std::move(v3);
  253. if(v1.size() != 0 || v2.size() != 0 || v3.size() != 0 ||
  254. v4.size() != 1 || v5.size() != 1 || v6.size() != 1) {
  255. printError(4);
  256. }
  257. }
  258. {
  259. V v;
  260. v.resize(1, A(8));
  261. v.resize(3, A(9));
  262. if(v.size() != 3 || v[0].a != 8 || v[1].a != 9 || v[2].a != 9) {
  263. printError(5);
  264. }
  265. v.resize(1, A(10));
  266. if(v.size() != 1 || v[0].a != 8) {
  267. printError(6);
  268. }
  269. }
  270. {
  271. std::vector<A> v1;
  272. V v2;
  273. for(int i = 0; i < 200; i++) {
  274. v1.push_back(A(i));
  275. v2.push_back(A(i));
  276. }
  277. v1.erase(v1.begin());
  278. v1.erase(v1.begin() + 4, v1.begin() + 8);
  279. v1.erase(v1.begin() + 30, v1.begin() + 56);
  280. v2.erase(v2.begin());
  281. v2.erase(v2.begin() + 4, v2.begin() + 8);
  282. v2.erase(v2.begin() + 30, v2.begin() + 56);
  283. if(static_cast<int>(v1.size()) != static_cast<int>(v2.size())) {
  284. printError(100);
  285. } else {
  286. for(unsigned int i = 0; i < v1.size(); i++) {
  287. if(v1[i].a != v2[i].a) {
  288. printError(200);
  289. }
  290. }
  291. }
  292. }
  293. if(A::instances != 0) {
  294. std::cout << "object counter is not 0: " << A::instances << "\n";
  295. }
  296. }
  297. void test() {
  298. {
  299. Vector<A> v;
  300. v.push_back(1);
  301. v.push_back(2);
  302. v.push_back(3);
  303. v.erase_by_swap(1);
  304. if(v.size() != 2 || v[0].a != 1 || v[1].a != 3) {
  305. printError(9);
  306. }
  307. v.erase_by_swap(1);
  308. if(v.size() != 1 || v[0].a != 1) {
  309. printError(10);
  310. }
  311. v.erase_by_swap(0);
  312. if(v.size() != 0) {
  313. printError(11);
  314. }
  315. }
  316. if(A::instances != 0) {
  317. std::cout << "object counter is not 0: " << A::instances << "\n";
  318. }
  319. }
  320. int main() {
  321. test<Vector<A>>();
  322. test();
  323. std::cout << "--------------------------\n";
  324. test<std::vector<A>>();
  325. Vector<int> test(3);
  326. }