nochnoikoshmar1

simple_vector.h

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