shushus

Спринт 8. Улучшаем собственный вектор.

Feb 2nd, 2023
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.13 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include "simple_vector.h"
  5.  
  6. #include <cassert>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. void TestReserveConstructor() {
  12.     cout << "TestReserveConstructor"s << endl;
  13.     SimpleVector<int> v(Reserve(5));
  14.     assert(v.GetCapacity() == 5);
  15.     assert(v.IsEmpty());
  16.     cout << "Done!"s << endl;
  17. }
  18.  
  19. void TestReserveMethod() {
  20.     cout << "TestReserveMethod"s << endl;
  21.     SimpleVector<int> v;
  22.  
  23.     v.Reserve(5);
  24.     assert(v.GetCapacity() == 5);
  25.     assert(v.IsEmpty());
  26.  
  27.     v.Reserve(1);
  28.  
  29.     assert(v.GetCapacity() == 5);
  30.  
  31.     for (int i = 0; i < 10; ++i) {
  32.         v.PushBack(i);
  33.     }
  34.     assert(v.GetSize() == 10);
  35.  
  36.     v.Reserve(100);
  37.  
  38.     assert(v.GetSize() == 10);
  39.     assert(v.GetCapacity() == 100);
  40.  
  41.     for (int i = 0; i < 10; ++i) {
  42.         assert(v[i] == i);
  43.     }
  44.     cout << "Done!"s << endl;
  45. }
  46.  
  47. int main() {
  48.     TestReserveConstructor();
  49.     TestReserveMethod();
  50. }
  51. -----------------------------------------------------------------------
  52. array_ptr.h
  53. -----------------------------------------------------------------------
  54. #pragma once
  55.  
  56. #include <stdexcept>
  57. #include <algorithm>
  58. #include <cassert>
  59. #include <cstdlib>
  60.  
  61. template <typename Type>
  62. class ArrayPtr {
  63. public:
  64.  
  65.     ArrayPtr() = default;
  66.  
  67.     explicit ArrayPtr(size_t size) {
  68.         if (size) {
  69.             simple_vector_ = new Type[size]();
  70.         } else{
  71.             simple_vector_= nullptr;
  72.         }
  73.     }
  74.  
  75.     ArrayPtr(const ArrayPtr&) = delete;
  76.  
  77.     ~ArrayPtr() {
  78.         delete[] simple_vector_;
  79.     }
  80.  
  81.     ArrayPtr(const ArrayPtr& other, size_t other_size, size_t new_size) {
  82.         simple_vector_ = new Type[new_size];
  83.         std::copy(other.simple_vector_, other.simple_vector_ + other_size, simple_vector_);
  84.     }
  85.  
  86.     Type& operator[](size_t index) noexcept {
  87.         return simple_vector_[index];
  88.     }
  89.  
  90.     const Type& operator[](size_t index) const noexcept {
  91.         return simple_vector_[index];
  92.     }
  93.  
  94.     explicit operator bool() const {
  95.         return simple_vector_;
  96.     }
  97.  
  98.     void swap(ArrayPtr& other) noexcept {
  99.         std::swap(simple_vector_, other.simple_vector_);
  100.     }
  101.  
  102. private:
  103.     Type* simple_vector_ = nullptr;
  104. };
  105. -----------------------------------------------------------------------
  106. simple_vector.h
  107. -----------------------------------------------------------------------
  108. #pragma once
  109.  
  110. #include <initializer_list>
  111. #include <algorithm>
  112. #include <stdexcept>
  113. #include "array_ptr.h"
  114.  
  115. class ReserveProxyObj {
  116. public:    
  117.     explicit ReserveProxyObj (size_t capacity_to_reserve) : capacity_(capacity_to_reserve){}
  118.     size_t Reserve_capacity() {return capacity_;}  
  119. private:    
  120.     size_t capacity_;
  121. };
  122.  
  123. ReserveProxyObj Reserve(size_t capacity_to_reserve) {
  124.     return ReserveProxyObj(capacity_to_reserve);
  125. }
  126.  
  127. template <typename Type>
  128. class SimpleVector {
  129. public:
  130.     using Iterator = Type*;
  131.     using ConstIterator = const Type*;
  132.  
  133.     SimpleVector() noexcept = default;
  134.  
  135.     explicit SimpleVector(size_t size) :simple_vector_(size), size_(size), capacity_(size){}
  136.  
  137.     SimpleVector(size_t size, const Type& value) :simple_vector_(size), size_(size), capacity_(size){
  138.         std::fill(begin(), end(), value);
  139.     }
  140.  
  141.     SimpleVector(std::initializer_list<Type> init) :simple_vector_(init.size()), size_(init.size()), capacity_(init.size()){
  142.         std::copy(init.begin(), init.end(), begin());
  143.     }
  144.    
  145.     SimpleVector(ReserveProxyObj capacity_to_reserve) {
  146.         Reserve(capacity_to_reserve.Reserve_capacity());
  147.     }
  148.  
  149.     size_t GetSize() const noexcept {
  150.         return size_;
  151.     }
  152.  
  153.     size_t GetCapacity() const noexcept {
  154.         return capacity_;
  155.     }
  156.  
  157.     bool IsEmpty() const noexcept {
  158.         return !size_;
  159.     }
  160.  
  161.     Type& operator[](size_t index) noexcept {
  162.         return simple_vector_[index];
  163.     }
  164.  
  165.     const Type& operator[](size_t index) const noexcept {
  166.         return simple_vector_[index];
  167.     }
  168.  
  169.     Type& At(size_t index) {
  170.         if (index < size_) {
  171.             return simple_vector_[index];
  172.         }else{
  173.             throw std::out_of_range("Non-existent vector element.");
  174.         }
  175.     }
  176.  
  177.     const Type& At(size_t index) const {
  178.         if (index < size_) {
  179.             return simple_vector_[index];
  180.         }else{
  181.             throw std::out_of_range("Non-existent vector element.");
  182.         }
  183.     }
  184.  
  185.     void Clear() noexcept {
  186.         size_ = 0;
  187.     }
  188.  
  189.     void Resize(size_t new_size) {
  190.         if (size_ >= new_size){
  191.             size_ = new_size;
  192.             return;
  193.         }else if(size_ < new_size && capacity_ > new_size){
  194.             std::fill(end(), begin() + new_size, Type());//
  195.             size_ = new_size;
  196.             return;
  197.         }else{
  198.             ArrayPtr<Type> helper(new_size);
  199.             std::copy(begin(), end(), &helper[0]);
  200.             simple_vector_.swap(helper);
  201.             size_ = new_size;
  202.             capacity_ = new_size*2;
  203.         }
  204.     }
  205.  
  206.     Iterator begin() noexcept {
  207.         return Iterator(&simple_vector_[0]);
  208.     }
  209.  
  210.     Iterator end() noexcept {
  211.         return Iterator(&simple_vector_[size_]);
  212.     }
  213.  
  214.     ConstIterator begin() const noexcept {
  215.         return cbegin();
  216.     }
  217.  
  218.     ConstIterator end() const noexcept {
  219.         return cend();
  220.     }
  221.  
  222.     ConstIterator cbegin() const noexcept {
  223.         return ConstIterator(&simple_vector_[0]);
  224.     }
  225.  
  226.     ConstIterator cend() const noexcept {
  227.         return ConstIterator(&simple_vector_[size_]);
  228.     }
  229.    
  230.     SimpleVector(const SimpleVector& other) {
  231.         ArrayPtr<Type> helper(other.size_);
  232.         size_ = other.size_;
  233.         capacity_= other.capacity_;
  234.         std::copy(other.begin(), other.end(), &helper[0]);
  235.         simple_vector_.swap(helper);
  236.     }
  237.  
  238.     SimpleVector& operator=(const SimpleVector& rhs) {
  239.         if (this !=&rhs){
  240.             SimpleVector helper(rhs);
  241.             swap(helper);
  242.         }
  243.         return *this;
  244.     }
  245.  
  246.     void PushBack(const Type& item) {
  247.         this->Resize(size_+1);
  248.         simple_vector_[size_-1] = item;
  249.     }
  250.  
  251.     Iterator Insert(ConstIterator pos, const Type& value) {
  252.         if(begin()<= pos && end()>= pos){
  253.             auto index = std::distance(cbegin(), pos);
  254.             if (size_== capacity_){
  255.                 if(size_){
  256.                     ArrayPtr<Type> helper(size_);
  257.                     std::copy(begin(), end(), &helper[0]);
  258.                     simple_vector_.swap(helper);
  259.                     size_ = size_;
  260.                     capacity_ = size_*2;
  261.                 }else{
  262.                     ArrayPtr<Type> helper(1);
  263.                     std::copy(begin(), end(), &helper[0]);
  264.                     simple_vector_.swap(helper);
  265.                     capacity_ =1;
  266.                 }
  267.             }
  268.             for (size_t i = size_; i >(size_t)index; --i) {
  269.                 simple_vector_[i] = simple_vector_[i-1];
  270.             }
  271.             ++size_;
  272.             simple_vector_[index] = value;
  273.             return const_cast<Iterator>(index +begin());
  274.         }else{
  275.             throw std::out_of_range("Non-existent vector pos.");
  276.         }
  277.     }
  278.  
  279.     void PopBack() noexcept {
  280.         if(size_) --size_;
  281.     }
  282.  
  283.     Iterator Erase(ConstIterator pos) {
  284.         auto index = std::distance(cbegin(), pos);
  285.         std::copy(++pos, cend(), &simple_vector_[index]);
  286.         --size_;
  287.         return Iterator(&simple_vector_[index]);
  288.     }
  289.  
  290.     void swap(SimpleVector& other) noexcept {
  291.         simple_vector_.swap(other.simple_vector_);
  292.         std::swap(size_, other.size_);
  293.         std::swap(capacity_, other.capacity_);
  294.     }
  295.    
  296.     void Reserve(size_t new_capacity){
  297.         if (new_capacity> capacity_) {
  298.            ArrayPtr<Type> helper(new_capacity);
  299.             for (size_t i=0; i< size_; ++i){
  300.                 helper[i] = simple_vector_[i];
  301.             }
  302.             simple_vector_.swap(helper);
  303.             capacity_ = new_capacity;
  304.         }else{
  305.             return;
  306.         }
  307.     }
  308.    
  309. private:
  310.     ArrayPtr<Type> simple_vector_;
  311.     size_t size_ = 0;
  312.     size_t capacity_ = 0;
  313. };
  314.  
  315. template <typename Type>
  316. inline bool operator==(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  317.     return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  318. }
  319.  
  320. template <typename Type>
  321. inline bool operator!=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  322.     return !(lhs == rhs);
  323. }
  324.  
  325. template <typename Type>
  326. inline bool operator<(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  327.     return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  328. }
  329.  
  330. template <typename Type>
  331. inline bool operator<=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  332.     return lhs < rhs || lhs == rhs;
  333. }
  334.  
  335. template <typename Type>
  336. inline bool operator>(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  337.     return rhs < lhs;
  338. }
  339.  
  340. template <typename Type>
  341. inline bool operator>=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  342.     return !(lhs < rhs);
  343. }
Advertisement
Add Comment
Please, Sign In to add comment