Advertisement
AyratT

Untitled

Jan 31st, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.03 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <cassert>
  4. #include <initializer_list>
  5. #include <algorithm>
  6. #include "array_ptr.h"
  7. #include <numeric>
  8. #include <iterator>
  9. #include <stdexcept>
  10. #include <cmath>
  11. #include <utility>
  12.  
  13. struct ReserveProxyObj {
  14.     size_t capacity_to_reserve_;
  15.     ReserveProxyObj(size_t capacity_to_reserve)
  16.         : capacity_to_reserve_(capacity_to_reserve) {}
  17. };
  18.  
  19. ReserveProxyObj Reserve(size_t capacity_to_reserve) {
  20.     return ReserveProxyObj(capacity_to_reserve);
  21. }
  22.  
  23. template <typename Type>
  24. class SimpleVector {
  25. public:
  26.     using Iterator = Type*;
  27.     using ConstIterator = const Type*;
  28.  
  29.     SimpleVector() noexcept = default;
  30.  
  31.     SimpleVector(ReserveProxyObj obj) {
  32.         Reserve(obj.capacity_to_reserve_);
  33.     }
  34.  
  35.  
  36.     // Создаёт вектор из size элементов, инициализированных значением по умолчанию
  37.     explicit SimpleVector(size_t size) {
  38.         ArrayPtr<Type> simple_vector_copy(size);
  39.         simple_vector_copy.swap(simple_vector_);
  40.         size_ = size;
  41.         capacity_ = size;
  42.         //std::fill(begin(), end(), Type{});
  43.     }
  44.  
  45.     // Создаёт вектор из size элементов, инициализированных значением value
  46.     SimpleVector(size_t size, const Type& value) {
  47.         ArrayPtr<Type> simple_vector_copy(size);
  48.         std::fill(simple_vector_copy.Get(), simple_vector_copy.Get() + size, value);
  49.         simple_vector_.swap(simple_vector_copy);
  50.         size_ = size;
  51.         capacity_ = size;
  52.     }
  53.  
  54.     // Создаёт вектор из std::initializer_list
  55.     SimpleVector(std::initializer_list<Type> init) {
  56.         ArrayPtr<Type> simple_vector_copy(init.size());
  57.         std::copy(std::make_move_iterator(init.begin()), std::make_move_iterator(init.end()),
  58.             simple_vector_copy.Get());
  59.         simple_vector_.swap(simple_vector_copy);
  60.         size_ = init.size();
  61.         capacity_ = init.size();
  62.     }
  63.  
  64.     // Конструктор копирования
  65.     SimpleVector(const SimpleVector& other) {
  66.         SimpleVector<Type> simple_vector_copy(other.size_);
  67.         std::copy(other.begin(), other.end(),
  68.             simple_vector_copy.begin());
  69.         simple_vector_copy.size_ = other.size_;
  70.         simple_vector_copy.capacity_ = other.capacity_;
  71.         swap(simple_vector_copy);
  72.     }
  73.  
  74.     // конструктор перемещения
  75.     SimpleVector(SimpleVector&& other) {
  76.         simple_vector_ = std::move(other.simple_vector_);
  77.         size_ = std::move(other.size_);
  78.         capacity_ = std::move(other.capacity_);
  79.         other.size_ = 0;
  80.         other.capacity_ = 0;
  81.     }
  82.  
  83.     // Оператор присваивания
  84.     SimpleVector& operator=(const SimpleVector& rhs) {
  85.         if (simple_vector_.Get() != rhs.simple_vector_.Get()) {
  86.             ArrayPtr<Type> simple_vector_copy(rhs.size_);
  87.             std::copy(std::make_move_iterator(rhs.simple_vector_.Get()), std::make_move_iterator(rhs.simple_vector_.Get() + rhs.size_),
  88.                 simple_vector_copy.Get());
  89.             simple_vector_.swap(simple_vector_copy);
  90.             size_ = std::move(rhs.size_);
  91.             capacity_ = std::move(rhs.capacity_);
  92.         }
  93.         return *this;
  94.     }
  95.  
  96.     // Добавляет элемент в конец вектора
  97.     // При нехватке места увеличивает вдвое вместимость вектора
  98.     void PushBack(Type item) {
  99.         if (size_ < capacity_) {
  100.             simple_vector_[size_] = std::move(item);
  101.             ++size_;
  102.         }
  103.         else {
  104.             SimpleVector<Type> simple_vector_copy(std::max(size_ * 2, size_t(1)));
  105.             std::copy(std::make_move_iterator(begin()), std::make_move_iterator(end()),
  106.                 simple_vector_copy.begin());
  107.             simple_vector_copy[size_] = std::move(item);
  108.             simple_vector_copy.size_ = size_ + 1;
  109.             swap(simple_vector_copy);
  110.         }
  111.     }
  112.  
  113.     // Вставляет значение value в позицию pos.
  114.     // Возвращает итератор на вставленное значение
  115.     // Если перед вставкой значения вектор был заполнен полностью,
  116.     // вместимость вектора должна увеличиться вдвое, а для вектора вместимостью 0 стать равной 1
  117.     Iterator Insert(ConstIterator pos, Type value) {
  118.         size_t index = size_t(pos - begin());
  119.         if (size_ < capacity_) {
  120.             std::copy_backward(std::make_move_iterator(begin() + index), std::make_move_iterator(end()), end() + 1);
  121.             simple_vector_[index] = std::move(value);
  122.             ++size_;
  123.         }
  124.         else {
  125.             SimpleVector<Type> simple_vector_copy(std::max(size_ * 2, size_t(1)));
  126.             std::copy(std::make_move_iterator(begin()), std::make_move_iterator(begin() + index), simple_vector_copy.begin());
  127.             simple_vector_copy[index] = std::move(value);
  128.             std::copy(std::make_move_iterator(begin() + index), std::make_move_iterator(end()), simple_vector_copy.begin() + index + 1);
  129.             simple_vector_copy.size_ = size_ + 1;
  130.             swap(simple_vector_copy);
  131.         }
  132.         return begin() + index;
  133.     }
  134.  
  135.     // "Удаляет" последний элемент вектора. Вектор не должен быть пустым
  136.     void PopBack() noexcept {
  137.         if (size_ > 0) {
  138.             --size_;
  139.         }
  140.     }
  141.  
  142.     // Удаляет элемент вектора в указанной позиции
  143.     Iterator Erase(ConstIterator pos) {
  144.         size_t index = size_t(pos - begin());
  145.         std::copy(std::make_move_iterator(begin() + index + 1), std::make_move_iterator(end()), begin() + index);
  146.         --size_;
  147.         return begin() + index;
  148.     }
  149.  
  150.     // Обменивает значение с другим вектором
  151.     void swap(SimpleVector& other) noexcept {
  152.         simple_vector_.swap(other.simple_vector_);
  153.         std::swap(size_, other.size_);
  154.         std::swap(capacity_, other.capacity_);
  155.     }
  156.  
  157.     // Возвращает количество элементов в массиве
  158.     size_t GetSize() const noexcept {
  159.         return size_;
  160.     }
  161.  
  162.     // Возвращает вместимость массива
  163.     size_t GetCapacity() const noexcept {
  164.         return capacity_;
  165.     }
  166.  
  167.     // Сообщает, пустой ли массив
  168.     bool IsEmpty() const noexcept {
  169.         return size_ == 0;
  170.     }
  171.  
  172.     // Возвращает ссылку на элемент с индексом index
  173.     Type& operator[](size_t index) noexcept {
  174.         assert(index < size_);
  175.         return simple_vector_[index];
  176.     }
  177.  
  178.     // Возвращает константную ссылку на элемент с индексом index
  179.     const Type& operator[](size_t index) const noexcept {
  180.         assert(index < size_);
  181.         return simple_vector_[index];
  182.     }
  183.  
  184.     // Возвращает константную ссылку на элемент с индексом index
  185.     // Выбрасывает исключение std::out_of_range, если index >= size
  186.     Type& At(size_t index) {
  187.         if (index >= size_) {
  188.             throw std::out_of_range("out_of_range");
  189.         }
  190.         else {
  191.             return simple_vector_[index];
  192.         }
  193.     }
  194.  
  195.     // Возвращает константную ссылку на элемент с индексом index
  196.     // Выбрасывает исключение std::out_of_range, если index >= size
  197.     const Type& At(size_t index) const {
  198.         if (index >= size_) {
  199.             throw std::out_of_range("out_of_range");
  200.         }
  201.         else {
  202.             return simple_vector_[index];
  203.         }
  204.     }
  205.  
  206.     // Обнуляет размер массива, не изменяя его вместимость
  207.     void Clear() noexcept {
  208.         size_ = 0;
  209.     }
  210.  
  211.     void Reserve(size_t new_capacity) {
  212.         if (new_capacity > capacity_) {
  213.             SimpleVector<Type> simple_vector_copy(new_capacity);
  214.             std::copy(begin(), end(), simple_vector_copy.begin());
  215.             simple_vector_copy.size_ = size_;
  216.             swap(simple_vector_copy);
  217.         }
  218.     }
  219.  
  220.     // Изменяет размер массива.
  221.     // При увеличении размера новые элементы получают значение по умолчанию для типа Type
  222.     void Resize(size_t new_size) {
  223.         if (new_size <= size_) {
  224.             size_ = new_size;
  225.         }
  226.         else if (new_size <= capacity_) {
  227.             std::fill(simple_vector_.Get(), simple_vector_.Get() + new_size, Type{});
  228.             /*auto first = simple_vector_.Get();
  229.             auto last = simple_vector_.Get() + new_size;
  230.             for (; first != last; ++first)
  231.                 *first = Type{};*/
  232.             size_ = new_size;
  233.         }
  234.         else {
  235.             SimpleVector<Type> simple_vector_copy_(std::max(capacity_ * 2, new_size));
  236.             std::copy(begin(), end(), simple_vector_copy_.begin());
  237.             swap(simple_vector_copy_);
  238.         }
  239.     }
  240.  
  241.     // Возвращает итератор на начало массива
  242.     // Для пустого массива может быть равен (или не равен) nullptr
  243.     Iterator begin() noexcept {
  244.         return simple_vector_.Get();
  245.     }
  246.  
  247.     // Возвращает итератор на элемент, следующий за последним
  248.     // Для пустого массива может быть равен (или не равен) nullptr
  249.     Iterator end() noexcept {
  250.         return simple_vector_.Get() + size_;
  251.     }
  252.  
  253.     // Возвращает константный итератор на начало массива
  254.     // Для пустого массива может быть равен (или не равен) nullptr
  255.     ConstIterator begin() const noexcept {
  256.         return simple_vector_.Get();
  257.     }
  258.  
  259.     // Возвращает итератор на элемент, следующий за последним
  260.     // Для пустого массива может быть равен (или не равен) nullptr
  261.     ConstIterator end() const noexcept {
  262.         return simple_vector_.Get() + size_;
  263.     }
  264.  
  265.     // Возвращает константный итератор на начало массива
  266.     // Для пустого массива может быть равен (или не равен) nullptr
  267.     ConstIterator cbegin() const noexcept {
  268.         return simple_vector_.Get();
  269.     }
  270.  
  271.     // Возвращает итератор на элемент, следующий за последним
  272.     // Для пустого массива может быть равен (или не равен) nullptr
  273.     ConstIterator cend() const noexcept {
  274.         return simple_vector_.Get() + size_;
  275.     }
  276.  
  277. private:
  278.     ArrayPtr<Type> simple_vector_;
  279.     size_t size_ = 0;
  280.     size_t capacity_ = 0;
  281.  
  282. };
  283.  
  284. template <typename Type>
  285. inline bool operator==(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  286.     return std::equal(lhs.begin(), lhs.end(), rhs.begin());
  287. }
  288.  
  289. template <typename Type>
  290. inline bool operator!=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  291.  
  292.     return !(lhs == rhs);
  293. }
  294.  
  295. template <typename Type>
  296. inline bool operator<(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  297.     return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  298. }
  299.  
  300. template <typename Type>
  301. inline bool operator<=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  302.     return !(rhs < lhs);
  303. }
  304.  
  305. template <typename Type>
  306. inline bool operator>(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  307.     return rhs < lhs;
  308. }
  309.  
  310. template <typename Type>
  311. inline bool operator>=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  312.     return !(lhs < rhs);
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement