nochnoikoshmar1

simple_vector.h

May 28th, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | None | 0 0
  1. #pragma once
  2. #include "array_ptr.h"
  3.  
  4. struct ReserveProxyObj{
  5.     size_t capacity_to_reserve_;
  6.     explicit ReserveProxyObj(size_t capacity_to_reserve)
  7.         : capacity_to_reserve_(capacity_to_reserve){}
  8. };
  9.  
  10. ReserveProxyObj Reserve(size_t capacity_to_reserve){
  11.     return ReserveProxyObj(capacity_to_reserve);
  12. }
  13.  
  14. template <typename Type>
  15. class SimpleVector{
  16. private:
  17.     ArrayPtr<Type> raw_vector_;
  18.     size_t size_ = 0u;
  19.     size_t capacity_ = 0u;
  20.  
  21. public:
  22.     using Iterator = Type *;
  23.     using ConstIterator = const Type *;
  24.  
  25.     SimpleVector() noexcept = default;
  26.  
  27.     explicit SimpleVector(size_t size)
  28.     :raw_vector_(size), size_(size), capacity_(size){
  29.         ArrayPtr<Type> array_copy(size);
  30.         array_copy.swap(raw_vector_);
  31.     }
  32.  
  33.     SimpleVector(size_t size, const std::initializer_list<Type&> &value)
  34.     //SimpleVector(size_t size, const Type &value)
  35.     :raw_vector_(size), size_(size), capacity_(size){
  36.         ArrayPtr<Type> array_copy(size);
  37.         std::fill(array_copy.Get(), array_copy.Get() + size, value);
  38.         array_copy.swap(raw_vector_);
  39.     }
  40.  
  41.     SimpleVector(std::initializer_list<Type> init)
  42.     :raw_vector_(init.size()), size_(init.size()), capacity_(init.size()){
  43.         ArrayPtr<Type> array_copy(init.size());
  44.         std::copy(std::make_move_iterator(init.begin()), std::make_move_iterator(init.end()), array_copy.Get());
  45.         array_copy.swap(raw_vector_);
  46.     }    
  47.  
  48.     SimpleVector(const SimpleVector &other)
  49.     /*:raw_vector_(other.capacity_), size_(other.size_), capacity_(other.capacity_)*/{
  50.         SimpleVector temp(other.capacity_);
  51.         //SimpleVector<Type> temp(other.size_);
  52.         temp.size_ = other.size_;
  53.         //temp.capacity_ = other.capacity_;
  54.         std::copy(other.begin(), other.end(), temp.begin());
  55.         swap(temp);
  56.     }    
  57.  
  58.     SimpleVector(SimpleVector &&other){
  59.         raw_vector_.swap(other.raw_vector_);
  60.         size_ = std::exchange(other.size_, 0u);
  61.         capacity_ = std::exchange(other.capacity_, 0u);
  62.     }
  63.  
  64.     SimpleVector(ReserveProxyObj obj){
  65.         Reserve(obj.capacity_to_reserve_);
  66.     }
  67.  
  68.     SimpleVector &operator=(SimpleVector rhs){
  69.         SimpleVector obj(rhs);
  70.         swap(obj);
  71.         //swap(rhs);
  72.         return *this;
  73.     }
  74.    
  75.     Type &operator[](size_t index)noexcept{
  76.         assert(index < size_);
  77.         return raw_vector_[index];
  78.     }
  79.    
  80.     const Type &operator[](size_t index)const noexcept{
  81.         assert(index < size_);
  82.         return raw_vector_[index];
  83.     }
  84.  
  85.     void swap(SimpleVector<Type> &other) noexcept{
  86.         raw_vector_.swap(other.raw_vector_);
  87.         std::swap(size_, other.size_);
  88.         std::swap(capacity_, other.capacity_);
  89.     }
  90.  
  91.     size_t GetSize() const noexcept{
  92.         return size_;
  93.     }
  94.  
  95.     size_t GetCapacity() const noexcept{
  96.         return capacity_;
  97.     }
  98.  
  99.     bool IsEmpty() const noexcept{
  100.         return size_ == 0;
  101.     }
  102.  
  103.     Type &At(size_t index){
  104.         if(index >= size_){
  105.             throw std::out_of_range("Index is out of range");
  106.         }
  107.         return raw_vector_[index];
  108.     }
  109.  
  110.     const Type &At(size_t index) const{
  111.         if(index >= size_){
  112.             throw std::out_of_range("Index is out of range");
  113.         }
  114.         return raw_vector_[index];
  115.     }
  116.  
  117.     void Clear() noexcept{
  118.         size_ = 0;
  119.     }
  120.  
  121.     bool Reserve(size_t new_capacity){
  122.         if(new_capacity <= capacity_){
  123.             return false;
  124.         }
  125.  
  126.         ArrayPtr<Type> new_data(new_capacity);
  127.         std::move(raw_vector_.Get(), raw_vector_.Get() + size_, new_data.Get());
  128.         raw_vector_.swap(new_data);
  129.         capacity_ = new_capacity;
  130.  
  131.         return true;
  132.     }
  133.  
  134.     void Resize(size_t new_size){
  135.         if(new_size <= size_){
  136.             size_ = new_size;
  137.  
  138.         } else{
  139.             if(new_size <= capacity_){
  140.                 for(auto i = size_; i < new_size; i++){
  141.                     raw_vector_[i] = std::move(Type{});
  142.                 }
  143.                 size_ = new_size;
  144.  
  145.             } else{
  146.                 if(new_size > capacity_){
  147.                     ArrayPtr<Type> new_items(new_size);
  148.                     std::move(this->begin(), this->end(), new_items.Get());
  149.  
  150.                     for(auto i = size_; i < new_size; i++){
  151.                         new_items[i] = std::move(Type{});
  152.                     }
  153.                     new_items.swap(raw_vector_);
  154.                     size_ = new_size;
  155.                     capacity_ = new_size;
  156.                 }
  157.             }
  158.         }
  159.     }
  160.  
  161.     void PushBack(Type item){
  162.         if(size_ < capacity_){
  163.             raw_vector_[size_++] = std::move(item);
  164.         } else{
  165.             SimpleVector<Type> temp(std::max(size_ * 2, static_cast<size_t>(1)));
  166.             temp.size_ = size_ + 1;
  167.             std::copy(std::make_move_iterator(begin()), std::make_move_iterator(end()), temp.begin());
  168.             temp[size_] = std::move(item);
  169.             swap(temp);
  170.         }
  171.     }  
  172.  
  173.     void PopBack() noexcept{
  174.         assert(!IsEmpty());
  175.         --size_;
  176.     }
  177.  
  178.     Iterator Insert(ConstIterator pos, Type value){
  179.         assert((pos >= begin()) && (pos <= end()));
  180.         size_t index = static_cast<size_t>(pos - begin());
  181.         if(size_ < capacity_){
  182.             std::copy_backward(std::make_move_iterator(begin() + index), std::make_move_iterator(end()), end() + 1);
  183.             raw_vector_[index] = std::move(value);
  184.             ++size_;
  185.         } else{
  186.             SimpleVector<Type> temp(std::max(size_ * 2, static_cast<size_t>(1)));
  187.             temp.size_ = size_ + 1;
  188.             std::copy(std::make_move_iterator(begin()), std::make_move_iterator(begin() + index), temp.begin());
  189.             temp[index] = std::move(value);
  190.             std::copy(std::make_move_iterator(begin() + index), std::make_move_iterator(end()), temp.begin() + index + 1);
  191.             swap(temp);
  192.         }
  193.         return begin() + index;
  194.     }    
  195.  
  196.     Iterator Erase(ConstIterator pos){
  197.         assert((pos >= begin()) && (pos < end()));
  198.         size_t index = static_cast<size_t>(pos - begin());
  199.         std::copy(std::make_move_iterator(begin() + index + 1), std::make_move_iterator(end()), begin() + index);
  200.         --size_;
  201.         return begin() + index;
  202.     }
  203.  
  204.     Iterator begin() noexcept{
  205.         return raw_vector_.Get();
  206.     }
  207.  
  208.     Iterator end() noexcept{
  209.         return raw_vector_.Get() + size_;
  210.     }
  211.  
  212.     ConstIterator begin() const noexcept{
  213.         return raw_vector_.Get();
  214.     }
  215.  
  216.     ConstIterator end() const noexcept{
  217.         return raw_vector_.Get() + size_;
  218.     }
  219.  
  220.     ConstIterator cbegin() const noexcept{
  221.         return raw_vector_.Get();
  222.     }
  223.  
  224.     ConstIterator cend() const noexcept{
  225.         return raw_vector_.Get() + size_;
  226.     }    
  227. };
  228.  
  229. template <typename Type>
  230. inline bool operator==(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs){
  231.     if(lhs.GetSize() != rhs.GetSize()){
  232.         return false;
  233.     }
  234.     return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  235. }
  236.  
  237. template <typename Type>
  238. inline bool operator!=(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs){
  239.     return !(lhs == rhs);
  240. }
  241.  
  242. template <typename Type>
  243. inline bool operator<(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs){
  244.     return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  245. }
  246.  
  247. template <typename Type>
  248. inline bool operator<=(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs){
  249.     return !(rhs < lhs);
  250. }
  251.  
  252. template <typename Type>
  253. inline bool operator>(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs){
  254.     return (rhs < lhs);
  255. }
  256.  
  257. template <typename Type>
  258. inline bool operator>=(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs){
  259.     return !(lhs < rhs);
  260. }
Advertisement
Add Comment
Please, Sign In to add comment