AlexSSH

Untitled

Jan 27th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.56 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <bits/stdc++.h>
  4. #include "array_ptr.h"
  5.  
  6. template<typename Type>
  7. class SimpleVector {
  8. public:
  9.     using Iterator = Type *;
  10.     using ConstIterator = const Type *;
  11.  
  12.     SimpleVector(const SimpleVector &other) {
  13.         SimpleVector tmp;
  14.         if (!other.IsEmpty()) {
  15.             tmp.size_ = other.size_;
  16.             tmp.capacity_ = other.capacity_;
  17.             tmp.data = other.data;
  18.         }
  19.         swap(tmp);
  20.     }
  21.  
  22.     SimpleVector &operator=(const SimpleVector &rhs) {
  23.         if (this != &rhs) {
  24.             auto rhs_copy(rhs);
  25.             swap(rhs_copy);
  26.         }
  27.         return *this;
  28.     }
  29.  
  30.     SimpleVector() noexcept = default;
  31.  
  32.     // Создаёт вектор из size элементов, инициализированных значением по умолчанию
  33.     explicit SimpleVector(size_t size) {
  34.         if (size > 0) {
  35.             size_ = size;
  36.             capacity_ = size;
  37.             // сейчас: data.raw_ptr = nullptrr
  38.             // надо: data.raw_ptr = new Type[size]
  39.             std::fill(begin(), end(), Type());
  40.         }
  41.     }
  42.  
  43.     // Создаёт вектор из size элементов, инициализированных значением value
  44.     SimpleVector(size_t size, const Type &value) {
  45.  
  46.     }
  47.  
  48.     // Создаёт вектор из std::initializer_list
  49.     SimpleVector(std::initializer_list<Type> init) {
  50.  
  51.     }
  52.  
  53.     // Возвращает количество элементов в массиве
  54.     [[nodiscard]] size_t GetSize() const noexcept {
  55.         return size_;
  56.     }
  57.  
  58.     // Возвращает вместимость массива
  59.     [[nodiscard]] size_t GetCapacity() const noexcept {
  60.         return capacity_;
  61.     }
  62.  
  63.     // Сообщает, пустой ли массив
  64.     [[nodiscard]] bool IsEmpty() const noexcept {
  65.         return size_ == 0;
  66.     }
  67.  
  68.     // Возвращает ссылку на элемент с индексом index
  69.     Type &operator[](size_t index) noexcept {
  70.         return data[index];
  71.     }
  72.  
  73.     // Возвращает константную ссылку на элемент с индексом index
  74.     const Type &operator[](size_t index) const noexcept {
  75.         return data[index];
  76.     }
  77.  
  78.     // Возвращает константную ссылку на элемент с индексом index
  79.     // Выбрасывает исключение std::out_of_range, если index >= size
  80.     Type &At(size_t index) {
  81.         if (index >= size_) throw std::out_of_range("");
  82.         return data[index];
  83.     }
  84.  
  85.     // Возвращает константную ссылку на элемент с индексом index
  86.     // Выбрасывает исключение std::out_of_range, если index >= size
  87.     const Type &At(size_t index) const {
  88.         if (index >= size_) throw std::out_of_range("");
  89.         return data[index];
  90.     }
  91.  
  92.     // Обнуляет размер массива, не изменяя его вместимость
  93.     void Clear() noexcept {
  94.         size_ = 0;
  95.     }
  96.  
  97.     // Изменяет размер массива.
  98.     // При увеличении размера новые элементы получают значение по умолчанию для типа Type
  99.     void Resize(size_t new_size) {
  100.  
  101.     }
  102.  
  103.     // Добавляет элемент в конец вектора
  104.     // При нехватке места увеличивает вдвое вместимость вектора
  105.     void PushBack(const Type &item) {
  106.  
  107.     }
  108.  
  109.     // Вставляет значение value в позицию pos.
  110.     // Возвращает итератор на вставленное значение
  111.     // Если перед вставкой значения вектор был заполнен полностью,
  112.     // вместимость вектора должна увеличиться вдвое, а для вектора вместимостью 0 стать равной 1
  113.     Iterator Insert(Iterator pos, const Type &value) {
  114.  
  115.     }
  116.  
  117.     // "Удаляет" последний элемент вектора. Вектор не должен быть пустым
  118.     void PopBack() noexcept {
  119.         if (!IsEmpty()) --size_;
  120.     }
  121.  
  122.     // Удаляет элемент вектора в указанной позиции
  123.     Iterator Erase(ConstIterator pos) {
  124.  
  125.     }
  126.  
  127.     // Обменивает значение с другим вектором
  128.     void swap(SimpleVector &other) noexcept {
  129.         std::swap(size_, other.size_);
  130.         std::swap(capacity_, other.capacity_);
  131.         std::swap(data, other.data);
  132.     }
  133.  
  134.     // Возвращает итератор на начало массива
  135.     // Для пустого массива может быть равен (или не равен) nullptr
  136.     Type* begin() noexcept {
  137.         return data.Get();
  138.     }
  139.  
  140.     // Возвращает итератор на элемент, следующий за последним
  141.     // Для пустого массива может быть равен (или не равен) nullptr
  142.     Iterator end() noexcept {
  143.         return data.Get()+size_;
  144.     }
  145.  
  146.     // Возвращает константный итератор на начало массива
  147.     // Для пустого массива может быть равен (или не равен) nullptr
  148.     ConstIterator begin() const noexcept {
  149.         return data.Get();
  150.     }
  151.  
  152.     // Возвращает итератор на элемент, следующий за последним
  153.     // Для пустого массива может быть равен (или не равен) nullptr
  154.     ConstIterator end() const noexcept {
  155.         return data.Get() + size_;
  156.     }
  157.  
  158.     // Возвращает константный итератор на начало массива
  159.     // Для пустого массива может быть равен (или не равен) nullptr
  160.     ConstIterator cbegin() const noexcept {
  161.         return data.Get();
  162.     }
  163.  
  164.     // Возвращает итератор на элемент, следующий за последним
  165.     // Для пустого массива может быть равен (или не равен) nullptr
  166.     ConstIterator cend() const noexcept {
  167.         return data.Get() + size_;
  168.     }
  169.  
  170. private:
  171.     ArrayPtr<Type> data;
  172.     size_t size_ = 0;
  173.     size_t capacity_ = 0;
  174. };
  175.  
  176. template<typename Type>
  177. inline bool operator==(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs) {
  178.     return lhs.GetSize() == rhs.GetSize() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
  179. }
  180.  
  181. template<typename Type>
  182. inline bool operator!=(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs) {
  183.     return !(operator==(lhs, rhs));
  184. }
  185.  
  186. template<typename Type>
  187. inline bool operator<(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs) {
  188.     return std::lexicographical_compare(lhs.begin(), lhs.end(),
  189.                                         rhs.begin(), rhs.end());
  190. }
  191.  
  192. template<typename Type>
  193. inline bool operator<=(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs) {
  194.     return (operator==(lhs, rhs)) || (operator<(lhs, rhs));
  195. }
  196.  
  197. template<typename Type>
  198. inline bool operator>(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs) {
  199.     return !(operator<=(lhs, rhs));
  200. }
  201.  
  202. template<typename Type>
  203. inline bool operator>=(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs) {
  204.     return (operator==(lhs, rhs)) || (operator>(lhs, rhs));
  205. }
Advertisement
Add Comment
Please, Sign In to add comment