Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <cstddef>
  4. #include <initializer_list>
  5. #include <utility>
  6. #include <cstring>
  7.  
  8. template<typename T>
  9. class Vector {
  10.  private:
  11.     T* data = nullptr;
  12.     size_t length = 0;
  13.     size_t real_length = 0;
  14.  
  15.  public:
  16.     Vector() {}
  17.  
  18.     Vector(const Vector& other) {
  19.         reserve(other.real_length);
  20.         length = other.length;
  21.         for (size_t i = 0; i < length; i++)
  22.             data[i] = other.data[i];
  23.     }
  24.  
  25.     Vector(std::initializer_list<T> list) {
  26.         for (auto x : list)
  27.             push_back(x);
  28.     }
  29.  
  30.     ~Vector() {
  31.         for (size_t i = 0; i < length; i++)
  32.             data[i].~T();
  33.         if (data != nullptr) delete[] reinterpret_cast<char*>(data);
  34.     }
  35.  
  36.     Vector& operator= (const Vector& other) {
  37.         clear();
  38.         reserve(other.length);
  39.         length = other.length;
  40.         for (size_t i = 0; i < length * sizeof(T); i++)
  41.             new (data + i) T(other.data[i]);
  42.         return *this;
  43.     }
  44.  
  45.     void reserve(size_t n, bool call_desctructors = true) {
  46.         if (n <= real_length) return;
  47.         T* new_data = reinterpret_cast<T*>(new char[n * sizeof(T)]);
  48.         try {
  49.             for (size_t i = 0; i < length; i++)
  50.                 new (new_data + i) T(data[i]);
  51.         } catch (...) {
  52.             delete[] reinterpret_cast<char*>(data);
  53.             throw;
  54.         }
  55.         real_length = n;
  56.         if (call_desctructors) {
  57.             for (size_t i = 0; i < length; i++)
  58.                 data[i].~T();
  59.         }
  60.         if (data != nullptr) delete[] reinterpret_cast<char*>(data);
  61.         data = new_data;
  62.     }
  63.  
  64.     void resize(size_t n) {
  65.         bool need_to_call_destructors = false;
  66.         if (n > real_length) {
  67.             reserve(n, false);
  68.             need_to_call_destructors = true;
  69.         }
  70.  
  71.         if (n > length) {
  72.             for (size_t i = length; i < n; i++)
  73.                 new (data + i) T();
  74.             if (need_to_call_destructors) {
  75.                 for (size_t i = 0; i < length; i++)
  76.                     data[i].~T();
  77.             }
  78.         } else if (n < length) {
  79.             for (size_t i = n; i < length; i++)
  80.                 data[i].~T();
  81.         }
  82.         length = n;
  83.     }
  84.  
  85.     size_t size() const {
  86.         return length;
  87.     }
  88.  
  89.     size_t capacity() const {
  90.         return real_length;
  91.     }
  92.  
  93.     T* begin() {
  94.         return data;
  95.     }
  96.  
  97.     T* end() {
  98.         return data + length;
  99.     }
  100.  
  101.     void push_back(const T& x) {
  102.         if (length == 0) {
  103.             reserve(1);
  104.         } else if (length == real_length) {
  105.             reserve(real_length * 2);
  106.         }
  107.         ++length;
  108.         try {
  109.             new (data + (length - 1)) T(x);
  110.         } catch (...) {
  111.             --length;
  112.         }
  113.     }
  114.  
  115.     void push_back(T&& x) {
  116.         if (length == 0) {
  117.             reserve(1);
  118.         } else if (length == real_length) {
  119.             reserve(real_length * 2);
  120.         }
  121.         ++length;
  122.         try {
  123.             new (data + (length - 1)) T(std::move(x));
  124.         } catch (...) {
  125.             --length;
  126.         }
  127.     }
  128.  
  129.     void pop_back() {
  130.         data[length-1].~T();
  131.         --length;
  132.     }
  133.  
  134.     void clear() {
  135.         for (size_t i = 0; i < length; i++)
  136.             data[i].~T();
  137.         length = 0;
  138.     }
  139.  
  140.     void swap(Vector& other) {
  141.         std::swap(length, other.length);
  142.         std::swap(real_length, other.real_length);
  143.         std::swap(data, other.data);
  144.     }
  145.  
  146.     T& operator[] (size_t i) {
  147.         return data[i];
  148.     }
  149. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement