Advertisement
Guest User

Untitled

a guest
May 26th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <cstddef>
  2. #include <memory>
  3. #include <cstring>
  4. #include <utility>
  5. #include <stdexcept>
  6.  
  7. namespace my {
  8. template <class T>
  9. class vector {
  10. public:
  11.     explicit vector(size_t count = 0)
  12.         : _data((T*)new char[(count ? count : 1)*sizeof(T)])
  13.         , _size(count)
  14.         , _capacity(count ? count : 1)
  15.     {}
  16.  
  17.     vector(const vector<T>& other)
  18.         : _data((T*)new char[(other._size ? other._size : 1)*sizeof(T)])
  19.         , _size(0)
  20.         , _capacity(other._size ? other._size : 1)
  21.     {
  22.         for(int i = 0; i < other._size; ++i) {
  23.             push_back(other._data[i]);
  24.         }
  25.     }
  26.  
  27.     vector(vector<T>&& other) = default;
  28.  
  29.     ~vector() {
  30.         for(T* p = _data.release(), *end = p + _size; p != end; ++p) {
  31.             p->~T();
  32.         }
  33.     }
  34.  
  35.     void swap(vector<T>& other) {
  36.         std::swap(_size, other._size);
  37.         std::swap(_capacity, other._capacity); std::swap(_data, other._data);
  38.     }
  39.  
  40.     T& at(size_t index) {
  41.         if(index >= _size) {
  42.             throw std::out_of_range("out of range in method \"at\" in my::vector");
  43.         }
  44.         return operator[](index);
  45.     }
  46.    
  47.     T at(size_t index) const {
  48.         if(index >= _size) {
  49.             throw std::out_of_range("out of range in method \"at\" in my::vector");
  50.         }
  51.         return operator[](index);
  52.     }
  53.  
  54.     T& operator[](size_t index) {
  55.         return _data[index];
  56.     }
  57.  
  58.     T operator[](size_t index) const {
  59.         return _data[index];
  60.     }
  61.  
  62.     bool empty() const {
  63.         return !_size;
  64.     }
  65.  
  66.     size_t size() const {
  67.         return _size;
  68.     }
  69.  
  70.     size_t capacity() const {
  71.         return _capacity;
  72.     }
  73.  
  74.     void push_back(const T& e) {
  75.         if(_size == _capacity) {
  76.             reserve(_capacity * 2);
  77.         }
  78.        
  79.         new(_data.get() + _size) T(e);
  80.         _size++;
  81.     }
  82.  
  83.     void push_back(T&& e) {
  84.         if(_size == _capacity) {
  85.             reserve(_capacity * 2);
  86.         }
  87.         new(_data.get() + _size) T(std::move(e));
  88.         _size++;
  89.     }
  90.  
  91.     void reserve(size_t new_capacity) {
  92.         if(new_capacity <= _capacity) return;
  93.  
  94.         std::unique_ptr<T[]> new_data((T*)new char[new_capacity*sizeof(T)]);
  95.         for(int i = 0; i < _size; ++i) {
  96.             new(new_data.get() + i) T(std::move(_data[i]));
  97.         }
  98.         _data.swap(new_data);
  99.         _capacity = new_capacity;
  100.     }
  101.  
  102. private:
  103.     std::unique_ptr<T[]> _data;
  104.     size_t _size;
  105.     size_t _capacity;
  106. };
  107.  
  108. template <class T>
  109. void swap(vector<T>& lhs, vector<T>& rhs) {
  110.     lhs.swap(rhs);
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement