nochnoikoshmar1

Untitled

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