Advertisement
Petrovi4

SimpleVector

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