shushus

Спринт 8. Разработка контейнера SimpleVector. Часть первая.

Feb 2nd, 2023
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include "simple_vector.h"
  5. #include "tests.h"
  6.  
  7. int main() {
  8.     Test1();
  9.     return 0;
  10. }
  11. -----------------------------------------------------------------------
  12. tests.h
  13. -----------------------------------------------------------------------
  14. #pragma once
  15.  
  16. #include <cassert>
  17. #include <stdexcept>
  18.  
  19. inline void Test1() {
  20.  
  21.     {
  22.         SimpleVector<int> v;
  23.         assert(v.GetSize() == 0u);
  24.         assert(v.IsEmpty());
  25.         assert(v.GetCapacity() == 0u);
  26.     }
  27.  
  28.     {
  29.         SimpleVector<int> v(5);
  30.         assert(v.GetSize() == 5u);
  31.         assert(v.GetCapacity() == 5u);
  32.         assert(!v.IsEmpty());
  33.         for (size_t i = 0; i < v.GetSize(); ++i) {
  34.             assert(v[i] == 0);
  35.         }
  36.     }
  37.  
  38.     {
  39.         SimpleVector<int> v(3, 42);
  40.         assert(v.GetSize() == 3);
  41.         assert(v.GetCapacity() == 3);
  42.         for (size_t i = 0; i < v.GetSize(); ++i) {
  43.             assert(v[i] == 42);
  44.         }
  45.     }
  46.  
  47.     {
  48.         SimpleVector<int> v{1, 2, 3};
  49.         assert(v.GetSize() == 3);
  50.         assert(v.GetCapacity() == 3);
  51.         assert(v[2] == 3);
  52.     }
  53.  
  54.     {
  55.         SimpleVector<int> v(3);
  56.         assert(&v.At(2) == &v[2]);
  57.         try {
  58.             v.At(3);
  59.             assert(false);
  60.         } catch (const std::out_of_range&) {
  61.         } catch (...) {
  62.             assert(false);
  63.         }
  64.     }
  65.  
  66.     {
  67.         SimpleVector<int> v(10);
  68.         const size_t old_capacity = v.GetCapacity();
  69.         v.Clear();
  70.         assert(v.GetSize() == 0);
  71.         assert(v.GetCapacity() == old_capacity);
  72.     }
  73.  
  74.     {
  75.         SimpleVector<int> v(3);
  76.         v[2] = 17;
  77.         v.Resize(7);
  78.         assert(v.GetSize() == 7);
  79.         assert(v.GetCapacity() >= v.GetSize());
  80.         assert(v[2] == 17);
  81.         assert(v[3] == 0);
  82.     }
  83.     {
  84.         SimpleVector<int> v(3);
  85.         v[0] = 42;
  86.         v[1] = 55;
  87.         const size_t old_capacity = v.GetCapacity();
  88.         v.Resize(2);
  89.         assert(v.GetSize() == 2);
  90.         assert(v.GetCapacity() == old_capacity);
  91.         assert(v[0] == 42);
  92.         assert(v[1] == 55);
  93.     }
  94.     {
  95.         const size_t old_size = 3;
  96.         SimpleVector<int> v(3);
  97.         v.Resize(old_size + 5);
  98.         v[3] = 42;
  99.         v.Resize(old_size);
  100.         v.Resize(old_size + 2);
  101.         assert(v[3] == 0);
  102.     }
  103.  
  104.     {
  105.  
  106.         {
  107.             SimpleVector<int> v;
  108.             assert(v.begin() == nullptr);
  109.             assert(v.end() == nullptr);
  110.         }
  111.  
  112.         {
  113.             SimpleVector<int> v(10, 42);
  114.             assert(v.begin());
  115.             assert(*v.begin() == 42);
  116.             assert(v.end() == v.begin() + v.GetSize());
  117.         }
  118.     }
  119. }
  120. -----------------------------------------------------------------------
  121. simple_vector.h
  122. -----------------------------------------------------------------------
  123. #pragma once
  124.  
  125. #include <cassert>
  126. #include <initializer_list>
  127. #include <algorithm>
  128. #include <stdexcept>
  129. #include "array_ptr.h"
  130.  
  131. template <typename Type>
  132. class SimpleVector {
  133. public:
  134.     using Iterator = Type*;
  135.     using ConstIterator = const Type*;
  136.  
  137.     SimpleVector() noexcept = default;
  138.  
  139.     explicit SimpleVector(size_t size) :simple_vector_(size), size_(size), capacity_(size){}
  140.  
  141.     SimpleVector(size_t size, const Type& value) :simple_vector_(size), size_(size), capacity_(size){
  142.         std::fill(begin(), end(), value);
  143.     }
  144.  
  145.     SimpleVector(std::initializer_list<Type> init) :simple_vector_(init.size()), size_(init.size()), capacity_(init.size()){
  146.         std::copy(init.begin(), init.end(), begin());
  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. private:
  230.     ArrayPtr<Type> simple_vector_;
  231.     size_t size_ = 0;
  232.     size_t capacity_ = 0;
  233. };
  234. -----------------------------------------------------------------------
  235. array_ptr.h
  236. -----------------------------------------------------------------------
  237. #pragma once
  238.  
  239. #include <stdexcept>
  240. #include <algorithm>
  241. #include <cassert>
  242. #include <cstdlib>
  243.  
  244. template <typename Type>
  245. class ArrayPtr {
  246. public:
  247.  
  248.     ArrayPtr() = default;
  249.  
  250.     explicit ArrayPtr(size_t size) {
  251.         if (size) {
  252.             simple_vector_ = new Type[size]();
  253.         } else{
  254.             simple_vector_= nullptr;
  255.         }
  256.     }
  257.  
  258.     ArrayPtr(const ArrayPtr&) = delete;
  259.  
  260.     ~ArrayPtr() {
  261.         delete[] simple_vector_;
  262.     }
  263.  
  264.     ArrayPtr& operator=(const ArrayPtr&) = delete;
  265.  
  266.     Type& operator[](size_t index) noexcept {
  267.         return simple_vector_[index];
  268.     }
  269.  
  270.     const Type& operator[](size_t index) const noexcept {
  271.         return simple_vector_[index];
  272.     }
  273.  
  274.     explicit operator bool() const {
  275.         return simple_vector_;
  276.     }
  277.  
  278.     void swap(ArrayPtr& other) noexcept {
  279.         std::swap(simple_vector_, other.simple_vector_);
  280.     }
  281.  
  282. private:
  283.     Type* simple_vector_ = nullptr;
  284. };
Advertisement
Add Comment
Please, Sign In to add comment