daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <memory>
  2. #include <cstddef>
  3. #include <utility>
  4.  
  5. template <typename T>
  6. class Vector {
  7. public:
  8.     Vector() = default;
  9.  
  10.     Vector(size_t alloc_number)
  11.         : _capacity(alloc_number), _size(alloc_number) {
  12.         _data = reinterpret_cast<T*>(new unsigned char[alloc_number * sizeof(T)]);
  13.         for (size_t i = 0; i != alloc_number; ++i)
  14.             new (_data + i) T();
  15.     }
  16.  
  17.     Vector(const Vector<T>& other)
  18.         : _capacity(other._capacity), _size(other._size) {
  19.         _data = reinterpret_cast<T*>(new unsigned char[other._size * sizeof(T)]);
  20.         for (size_t i = 0; i != other._size; ++i)
  21.             new (_data + i) T(other._data[i]);
  22.     }
  23.  
  24.     Vector<T>& operator=(const Vector<T>& other) {
  25.         if (other._size <= _capacity) {
  26.             for (size_t i = 0; i != other._size; ++i)
  27.                 _data[i] = other._data[i];
  28.             for (size_t i = other._size; i < _size; ++i)
  29.                 (_data + i)->~T();
  30.             _size = other._size;
  31.         } else {
  32.             T* new_data = reinterpret_cast<T*>(new unsigned char[other._size * sizeof(T)]);
  33.             for (size_t i = 0; i != other._size; ++i)
  34.                 new (new_data + i) T(other._data[i]);
  35.             std::swap(_data, new_data);
  36.             _size = other._size;
  37.             _capacity = other._capacity;
  38.             for (size_t i = 0; i != _size; ++i)
  39.                 (new_data + i)->~T();
  40.             delete[] reinterpret_cast<unsigned char*>(new_data);
  41.         }
  42.         return *this;
  43.     }
  44.  
  45.     T* begin() {
  46.         return _data;
  47.     }
  48.  
  49.     const T* begin() const {
  50.         return _data;
  51.     }
  52.  
  53.     T* end() {
  54.         return _data + _size;
  55.     }
  56.  
  57.     const T* end() const {
  58.         return _data + _size;
  59.     }
  60.  
  61.     size_t size() const {
  62.         return _size;
  63.     }
  64.  
  65.     size_t capacity() const {
  66.         return _capacity;
  67.     }
  68.  
  69.     T& operator[](size_t index) {
  70.         return _data[index];
  71.     }
  72.  
  73.     const T& operator[](size_t index) const {
  74.         return _data[index];
  75.     }
  76.  
  77.     void reserve(size_t new_capacity) {
  78.         if (new_capacity > _capacity) {
  79.             T* new_data = reinterpret_cast<T*>(new unsigned char[new_capacity * sizeof(T)]);
  80.             for (size_t i = 0; i != _size; ++i) {
  81.                 new (new_data + i) T(_data[i]);
  82.             }
  83.             std::swap(_data, new_data);
  84.             for (size_t i = 0; i != _size; ++i)
  85.                 (new_data + i)->~T();
  86.             delete[] reinterpret_cast<unsigned char*>(new_data);
  87.             _capacity = new_capacity;
  88.         }
  89.     }
  90.  
  91.     void resize(size_t new_size) {
  92.         if (new_size >= _size) {
  93.             if (new_size > _capacity) {
  94.                 T* new_data = reinterpret_cast<T*>(new unsigned char[new_size * sizeof(T)]);
  95.                 for (size_t i = 0; i != _size; ++i)
  96.                     new (new_data + i) T(_data[i]);
  97.                 for (size_t i = _size; i != new_size; ++i)
  98.                     new (new_data + i) T();
  99.                 std::swap(_data, new_data);
  100.                 _capacity = new_size;
  101.                 for (size_t i = 0; i != _size; ++i)
  102.                     (new_data + i)->~T();
  103.                 delete[] reinterpret_cast<unsigned char*>(new_data);
  104.             } else {
  105.                 for (size_t i = _size; i != new_size; ++i)
  106.                     new (_data + i) T();
  107.             }
  108.         } else {
  109.             for (size_t cnt = 0; cnt != _size - new_size; ++cnt)
  110.                 (_data + _size - 1 - cnt)->~T();
  111.         }
  112.         _size = new_size;
  113.     }
  114.  
  115.     void clear() {
  116.         for (size_t i = 0; i != _size; ++i)
  117.             (_data + _size - 1 - i)->~T();
  118.         _data = nullptr;
  119.         _size = 0;
  120.     }
  121.  
  122.     void push_back(const T& value) {
  123.         if (!_capacity) {
  124.             this->reserve(1);
  125.         } else if (_capacity == _size) {
  126.             this->reserve(_capacity * 2);
  127.         }
  128.         new (_data + _size) T(value);
  129.         _size++;
  130.     }
  131.  
  132.     void push_back(T&& value) {
  133.         if (!_capacity) {
  134.             this->reserve(1);
  135.         } else if (_capacity == _size) {
  136.             this->reserve(_capacity * 2);
  137.         }
  138.         new (_data + _size) T(std::move(value));
  139.         _size++;
  140.     }
  141.  
  142.     void pop_back() {
  143.         --_size;
  144.         (_data + _size)->~T();
  145.     }
  146.  
  147.     void swap(Vector<T>& other) noexcept {
  148.         std::swap(_data, other._data);
  149.         std::swap(_size, other._size);
  150.         std::swap(_capacity, other._capacity);
  151.     }
  152.  
  153.     ~Vector() {
  154.         for (size_t i = 0; i != _size; ++i)
  155.             (_data + i)->~T();
  156.         delete[] reinterpret_cast<unsigned char*>(_data);
  157.     }
  158.  
  159. private:
  160.     T * _data = nullptr;
  161.     size_t _size = 0;
  162.     size_t _capacity = 0;
  163. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top