shizaero

Untitled

Aug 17th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.11 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cassert>
  4. #include <initializer_list>
  5. #include <stdexcept>
  6.  
  7. using namespace std;
  8.  
  9. template <typename Type>
  10. class SimpleVector {
  11. public:
  12.     using Iterator = Type*;
  13.     using ConstIterator = const Type*;
  14.  
  15.     SimpleVector() noexcept : data(nullptr), size(0), capacity(0) {}
  16.  
  17.     explicit SimpleVector(size_t size) : size(size), capacity(size) {
  18.         data = size > 0 ? new Type[size] : nullptr;
  19.         for (size_t i = 0; i < size; ++i) {
  20.             data[i] = Type();  // Initialize with default value
  21.         }
  22.     }
  23.  
  24.     SimpleVector(size_t size, const Type& value) : size(size), capacity(size) {
  25.         data = size > 0 ? new Type[size] : nullptr;
  26.         for (size_t i = 0; i < size; ++i) {
  27.             data[i] = value;
  28.         }
  29.     }
  30.  
  31.     SimpleVector(std::initializer_list<Type> init) : size(init.size()), capacity(init.size()) {
  32.         data = size > 0 ? new Type[size] : nullptr;
  33.         size_t i = 0;
  34.         for (const auto& item : init) {
  35.             data[i++] = item;
  36.         }
  37.     }
  38.  
  39.     SimpleVector(const SimpleVector& other)
  40.         : size(other.size), capacity(other.size)
  41.     { //fuck this place
  42.         data = size > 0 ? new Type[size] : nullptr;
  43.         for (size_t i = 0; i < size; ++i) {
  44.             data[i] = other.data[i];
  45.         }
  46.     }
  47.  
  48.     SimpleVector& operator=(const SimpleVector& rhs) {
  49.         if (this == &rhs) {
  50.             return *this;
  51.         }
  52.         SimpleVector temp(rhs);
  53.         swap(temp);
  54.  
  55.         return *this;
  56.     }
  57.  
  58.     // Добавляет элемент в конец вектора
  59.     // При нехватке места увеличивает вдвое вместимость вектора
  60.     void PushBack(const Type& item) {
  61.         data[++size] = item;
  62.     }
  63.  
  64.  
  65.     // Вставляет значение value в позицию pos.
  66.     // Возвращает итератор на вставленное значение
  67.     // Если перед вставкой значения вектор был заполнен полностью,
  68.     // вместимость вектора должна увеличиться вдвое, а для вектора вместимостью 0 стать равной 1
  69.     Iterator Insert(ConstIterator pos, const Type& value) {
  70.         size_t index = pos - data;
  71.  
  72.         if (size == capacity) {
  73.             size_t new_capacity = capacity == 0 ? 1 : capacity * 2;
  74.             Resize(new_capacity);
  75.             size += 1;
  76.         }
  77.  
  78.         for (size_t i = size; i > index; --i) {
  79.             data[i] = data[i - 1];
  80.         }
  81.  
  82.         data[index] = value;
  83.  
  84.         return data + index;
  85.     }
  86.  
  87.     // "Удаляет" последний элемент вектора. Вектор не должен быть пустым
  88.     void PopBack() noexcept {
  89.         if (size > 0) {
  90.             --size;
  91.         }
  92.     }
  93.  
  94.     // Удаляет элемент вектора в указанной позиции
  95.     Iterator Erase(ConstIterator pos) {
  96.         size_t index = pos - data;
  97.  
  98.         if (index >= size) {
  99.             return end();
  100.         }
  101.  
  102.         for (size_t i = index; i < size - 1; ++i) {
  103.             data[i] = data[i + 1];
  104.         }
  105.  
  106.         --size;
  107.  
  108.         return data + index;
  109.     }
  110.  
  111.  
  112.     void swap(SimpleVector& other) noexcept {
  113.         std::swap(data, other.data);
  114.         std::swap(size, other.size);
  115.         std::swap(capacity, other.capacity);
  116.     }
  117.  
  118.     size_t GetSize() const noexcept {
  119.         return size;
  120.     }
  121.  
  122.     size_t GetCapacity() const noexcept {
  123.         return capacity;
  124.     }
  125.  
  126.     bool IsEmpty() const noexcept {
  127.         return size == 0;
  128.     }
  129.  
  130.     Type& operator[](size_t index) noexcept {
  131.         if (index >= size) {
  132.             //throw std::out_of_range("Index out of range");
  133.         }
  134.         return data[index];
  135.     }
  136.  
  137.     const Type& operator[](size_t index) const noexcept {
  138.         if (index < size) {
  139.             // throw std::out_of_range("Index out of range");
  140.         }
  141.         return data[index];
  142.     }
  143.  
  144.     Type& At(size_t index) {
  145.         if (index >= size) throw std::out_of_range("Index is too large!");
  146.         return (*this)[index];
  147.     }
  148.  
  149.     const Type& At(size_t index) const {
  150.         if (index >= size) throw std::out_of_range("Index is too large!");
  151.         return (*this)[index];
  152.     }
  153.  
  154.     void Clear() noexcept {
  155.         size = 0;
  156.     }
  157.  
  158.     void Resize(size_t new_size) {
  159.         if (new_size > capacity) {
  160.             Type* new_data = new Type[new_size];
  161.             for (size_t i = 0; i < size; ++i) {
  162.                 new_data[i] = data[i];
  163.             }
  164.             for (size_t i = size; i < new_size; ++i) {
  165.                 new_data[i] = Type();  // Initialize new elements with default value
  166.             }
  167.             delete[] data;
  168.             data = new_data;
  169.             capacity = new_size;
  170.         }
  171.         if (new_size < size) fill(data + new_size, data + size, 0);
  172.         size = new_size;
  173.     }
  174.  
  175.     Iterator begin() noexcept {
  176.         return data;
  177.     }
  178.  
  179.     Iterator end() noexcept {
  180.         return data + size;
  181.     }
  182.  
  183.     ConstIterator begin() const noexcept {
  184.         return data;
  185.     }
  186.  
  187.     ConstIterator end() const noexcept {
  188.         return data + size;
  189.     }
  190.  
  191.     ConstIterator cbegin() const noexcept {
  192.         return data;
  193.     }
  194.  
  195.     ConstIterator cend() const noexcept {
  196.         return data + size;
  197.     }
  198.  
  199.     ~SimpleVector() {
  200.         delete[] data;
  201.     }
  202.  
  203. private:
  204.     Type* data;
  205.     size_t size;
  206.     size_t capacity;
  207. };
  208.  
  209. template <typename Type>
  210. inline bool operator==(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  211.     // Заглушка. Напишите тело самостоятельно
  212.     if (lhs.GetSize() != rhs.GetSize())
  213.     {
  214.         return false;
  215.     }
  216.  
  217.     for (size_t i = 0; i < lhs.GetSize(); ++i)
  218.     {
  219.         if (lhs[i] != rhs[i])
  220.         {
  221.             return false;
  222.         }
  223.     }
  224.     return true;
  225. }
  226.  
  227. template <typename Type>
  228. inline bool operator!=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  229.     // Заглушка. Напишите тело самостоятельно
  230.     return !(lhs == rhs);
  231. }
  232.  
  233. template <typename Type>
  234. inline bool operator<(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  235.     // Заглушка. Напишите тело самостоятельно
  236.     return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  237. }
  238.  
  239. template <typename Type>
  240. inline bool operator<=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  241.     // Заглушка. Напишите тело самостоятельно
  242.     return !(lhs > rhs);
  243. }
  244.  
  245. template <typename Type>
  246. inline bool operator>(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  247.     // Заглушка. Напишите тело самостоятельно
  248.     return rhs < lhs;
  249. }
  250.  
  251. template <typename Type>
  252. inline bool operator>=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  253.     // Заглушка. Напишите тело самостоятельно
  254.     return !(lhs < rhs);
  255. }
Add Comment
Please, Sign In to add comment