Advertisement
chevengur

СПРИНТ № 8 | Эффективные линейные контейнеры | Урок 7: Проще и быстрее: std::array

Jun 14th, 2024
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. stack_vector.h
  2.  
  3. #pragma once
  4.  
  5. #include <array>
  6. #include <stdexcept>
  7. #include <algorithm>
  8.  
  9. template <typename T, size_t N>
  10. class StackVector {
  11. public:
  12.     explicit StackVector(size_t a_size = 0){
  13.         if (a_size <= Capacity())
  14.         {
  15.             size_ = a_size;
  16.         }
  17.         else
  18.         {
  19.             throw std::invalid_argument("size > capacity");
  20.         }
  21.     }
  22.  
  23.     T& operator[](size_t index)
  24.     {
  25.         return my_array_[index];
  26.     }
  27.  
  28.     const T& operator[](size_t index) const
  29.     {
  30.         return my_array_[index];
  31.     }
  32.  
  33.     typename std::array<T, N>::iterator begin()
  34.     {
  35.         return my_array_.begin();
  36.     }
  37.  
  38.     typename std::array<T, N>::iterator end()
  39.     {
  40.         return next(my_array_.begin(), size_);
  41.     }
  42.  
  43.     typename std::array<T, N>::const_iterator begin() const
  44.     {
  45.         return my_array_.begin();
  46.     }
  47.  
  48.     typename std::array<T, N>::const_iterator end() const
  49.     {
  50.         return next(my_array_.begin(), size_);
  51.     }
  52.  
  53.     size_t Size() const
  54.     {
  55.         return size_;
  56.     }
  57.  
  58.     size_t Capacity() const
  59.     {
  60.         return my_array_.size();
  61.     }
  62.  
  63.     void PushBack(const T& value)
  64.     {
  65.         if (size_ != Capacity())
  66.         {
  67.             my_array_[size_++] = value;
  68.         }
  69.         else
  70.         {
  71.             throw std::overflow_error("stack is full");
  72.         }
  73.     }
  74.  
  75.     T PopBack()
  76.     {
  77.         if (size_ > 0)
  78.         {
  79.             --size_;
  80.             return my_array_[size_];
  81.         }
  82.         else
  83.         {
  84.             throw std::underflow_error("stack is empty");
  85.         }
  86.     }
  87.  
  88. private:
  89.     T size_ = 0;
  90.     std::array<T, N> my_array_;
  91. };
  92.  
  93. =======================================================================================================================================
  94. main.cpp
  95.  
  96. #include "log_duration.h"
  97. #include "my_assert.h"
  98. #include "stack_vector.h"
  99.  
  100. #include <iostream>
  101. #include <random>
  102. #include <stdexcept>
  103.  
  104. using namespace std;
  105.  
  106. void TestConstruction() {
  107.     StackVector<int, 10> v;
  108.     assert(v.Size() == 0u);
  109.     assert(v.Capacity() == 10u);
  110.  
  111.     StackVector<int, 8> u(5);
  112.     assert(u.Size() == 5u);
  113.     assert(u.Capacity() == 8u);
  114.  
  115.     try {
  116.         StackVector<int, 10> u(50);
  117.         cout << "Expect invalid_argument for too large size"s << endl;
  118.         assert(false);
  119.     }
  120.     catch (invalid_argument&) {
  121.     }
  122.     catch (...) {
  123.         cout << "Expect invalid_argument for too large size"s << endl;
  124.         assert(false);
  125.     }
  126. }
  127.  
  128. void TestPushBack() {
  129.     StackVector<int, 5> v;
  130.     for (size_t i = 0; i < v.Capacity(); ++i) {
  131.         v.PushBack(i);
  132.     }
  133.  
  134.     try {
  135.         v.PushBack(0);
  136.         cout << "Expect overflow_error for PushBack in full vector"s << endl;
  137.         assert(false);
  138.     }
  139.     catch (overflow_error&) {
  140.     }
  141.     catch (...) {
  142.         cout << "Unexpected exception for PushBack in full vector"s << endl;
  143.         assert(false);
  144.     }
  145. }
  146.  
  147. void TestPopBack() {
  148.     StackVector<int, 5> v;
  149.     for (size_t i = 1; i <= v.Capacity(); ++i) {
  150.         v.PushBack(i);
  151.     }
  152.     for (int i = v.Size(); i > 0; --i) {
  153.         assert(v.PopBack() == i);
  154.     }
  155.  
  156.     try {
  157.         v.PopBack();
  158.         cout << "Expect underflow_error for PopBack from empty vector"s << endl;
  159.         assert(false);
  160.     }
  161.     catch (underflow_error&) {
  162.     }
  163.     catch (...) {
  164.         cout << "Unexpected exception for PopBack from empty vector"s << endl;
  165.         assert(false);
  166.     }
  167. }
  168.  
  169. int main() {
  170.     TestConstruction();
  171.     TestPushBack();
  172.     TestPopBack();
  173.  
  174.     cerr << "Running benchmark..."s << endl;
  175.     const size_t max_size = 2500;
  176.  
  177.     default_random_engine re;
  178.     uniform_int_distribution<int> value_gen(1, max_size);
  179.  
  180.     vector<vector<int>> test_data(50000);
  181.     for (auto& cur_vec : test_data) {
  182.         cur_vec.resize(value_gen(re));
  183.         for (int& x : cur_vec) {
  184.             x = value_gen(re);
  185.         }
  186.     }
  187.  
  188.     {
  189.         LOG_DURATION("vector w/o reserve");
  190.         for (auto& cur_vec : test_data) {
  191.             vector<int> v;
  192.             for (int x : cur_vec) {
  193.                 v.push_back(x);
  194.             }
  195.         }
  196.     }
  197.     {
  198.         LOG_DURATION("vector with reserve");
  199.         for (auto& cur_vec : test_data) {
  200.             vector<int> v;
  201.             v.reserve(cur_vec.size());
  202.             for (int x : cur_vec) {
  203.                 v.push_back(x);
  204.             }
  205.         }
  206.     }
  207.     {
  208.         LOG_DURATION("StackVector");
  209.         for (auto& cur_vec : test_data) {
  210.             StackVector<int, max_size> v;
  211.             for (int x : cur_vec) {
  212.                 v.PushBack(x);
  213.             }
  214.         }
  215.     }
  216.     cerr << "Done"s << endl;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement