Advertisement
oster_man

vector by []

Dec 25th, 2023
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #pragma once
  2. #include <cassert>
  3. #include <cstdlib>
  4. #include <new>
  5. #include <utility>
  6. #include <cstring>
  7.  
  8. template <typename T>
  9. class Vector {
  10. public:
  11.     Vector() = default;
  12.    
  13.     Vector(size_t size)
  14.     :data_(Allocate(size)){
  15.         data_ = new T[size];
  16.         capacity_ = size;
  17.         size_= size;
  18.     }
  19.    
  20.     Vector(const Vector<T>& other)
  21.         :data_(Allocate(other.size_)){
  22.         for(size_t i=0;i<other.size_;++i)
  23.             data_[i] = T(other.data_[i]);
  24.         size_ = other.size_;
  25.         capacity_=other.capacity_;
  26.     }
  27.    
  28.     /*
  29.     Vector(Vector<T>&& other)
  30.     {
  31.         T* new_data_=Allocate(other.size);
  32.         new_data_ = new T[other.size_];
  33.         std::memmove(other.data_,new_data_,other.size_);
  34.         other.~Vector();
  35.     }*/
  36.    
  37.     static void CopyConstruct(T* from, T* to, size_t len) {
  38.         for (size_t i = 0; i<len; ++i){
  39.             to[i]=from[i];
  40.         }
  41.     }
  42.    
  43.     size_t Size() const noexcept {
  44.         return size_;
  45.     }
  46.  
  47.     size_t Capacity() const noexcept {
  48.         return capacity_;
  49.     }
  50.  
  51.     const T& operator[](size_t index) const noexcept {
  52.         return const_cast<Vector&>(*this)[index];
  53.     }
  54.  
  55.     T& operator[](size_t index) noexcept {
  56.         assert(index < size_);
  57.         return data_[index];
  58.     }
  59.    
  60.     static T* Allocate(size_t n){
  61.         return n!=0? static_cast<T*>(operator new(n*sizeof(T))):nullptr;
  62.     }
  63.    
  64.     static void Deallocate(T* buf, size_t size) noexcept{
  65.         operator delete[](buf,size);
  66.     }
  67.    
  68.     ~Vector(){
  69.         DestroyN(data_,size_);
  70.         Deallocate(data_,size_);
  71.     }
  72.    
  73.     static void DestroyN(T* buf, size_t n) noexcept {
  74.         for (size_t i = 0; i != n; ++i) {
  75.             Destroy(buf + i);
  76.         }
  77.     }
  78.  
  79.     // Вызывает деструктор объекта по адресу buf
  80.     static void Destroy(T* buf) noexcept {
  81.         buf->~T();
  82.     }
  83.    
  84.     void Reserve(size_t new_capacity){
  85.         if (new_capacity <= capacity_) {
  86.             return;
  87.         }
  88.         T* new_data = Allocate(new_capacity);
  89.         CopyConstruct(data_, new_data,size_);
  90.         DestroyN(data_, size_);
  91.         Deallocate(data_,size_);
  92.  
  93.         data_ = new_data;
  94.         capacity_ = new_capacity;
  95.     }
  96.  
  97. private:
  98.     T* data_ = nullptr;
  99.     size_t capacity_ = 0;
  100.     size_t size_ = 0;
  101. };
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement