shizaero

Untitled

Aug 17th, 2023
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.43 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.         if (size == capacity) {
  62.             // Увеличиваем вместимость вдвое
  63.             size_t new_capacity = capacity == 0 ? 1 : capacity * 2;
  64.             Resize(new_capacity);
  65.             size += 1;
  66.         }
  67.         if (size < capacity)
  68.         {
  69.             data[size++] = item;
  70.         }
  71.         data[size - 1] = item;
  72.     }
  73.  
  74.  
  75.     // Вставляет значение value в позицию pos.
  76.     // Возвращает итератор на вставленное значение
  77.     // Если перед вставкой значения вектор был заполнен полностью,
  78.     // вместимость вектора должна увеличиться вдвое, а для вектора вместимостью 0 стать равной 1
  79.     Iterator Insert(ConstIterator pos, const Type& value) {
  80.         size_t index = pos - data;
  81.  
  82.         if (size == capacity) {
  83.             size_t new_capacity = capacity == 0 ? 1 : capacity * 2;
  84.             Resize(new_capacity);
  85.             size += 1;
  86.         }
  87.  
  88.         for (size_t i = size; i > index; --i) {
  89.             data[i] = data[i - 1];
  90.         }
  91.  
  92.         data[index] = value;
  93.  
  94.         return data + index;
  95.     }
  96.  
  97.     // "Удаляет" последний элемент вектора. Вектор не должен быть пустым
  98.     void PopBack() noexcept {
  99.         if (size > 0) {
  100.             --size;
  101.         }
  102.     }
  103.  
  104.     // Удаляет элемент вектора в указанной позиции
  105.     Iterator Erase(ConstIterator pos) {
  106.         size_t index = pos - data;
  107.  
  108.         if (index >= size) {
  109.             return end();
  110.         }
  111.  
  112.         for (size_t i = index; i < size - 1; ++i) {
  113.             data[i] = data[i + 1];
  114.         }
  115.  
  116.         --size;
  117.  
  118.         return data + index;
  119.     }
  120.  
  121.  
  122.     void swap(SimpleVector& other) noexcept {
  123.         std::swap(data, other.data);
  124.         std::swap(size, other.size);
  125.         std::swap(capacity, other.capacity);
  126.     }
  127.  
  128.     size_t GetSize() const noexcept {
  129.         return size;
  130.     }
  131.  
  132.     size_t GetCapacity() const noexcept {
  133.         return capacity;
  134.     }
  135.  
  136.     bool IsEmpty() const noexcept {
  137.         return size == 0;
  138.     }
  139.  
  140.     Type& operator[](size_t index) noexcept {
  141.         if (index >= size) {
  142.             //throw std::out_of_range("Index out of range");
  143.         }
  144.         return data[index];
  145.     }
  146.  
  147.     const Type& operator[](size_t index) const noexcept {
  148.         if (index < size) {
  149.             // throw std::out_of_range("Index out of range");
  150.         }
  151.         return data[index];
  152.     }
  153.  
  154.     Type& At(size_t index) {
  155.         if (index >= size) throw std::out_of_range("Index is too large!");
  156.         return (*this)[index];
  157.     }
  158.  
  159.     const Type& At(size_t index) const {
  160.         if (index >= size) throw std::out_of_range("Index is too large!");
  161.         return (*this)[index];
  162.     }
  163.  
  164.     void Clear() noexcept {
  165.         size = 0;
  166.     }
  167.  
  168.     void Resize(size_t new_size) {
  169.         if (new_size > capacity) {
  170.             Type* new_data = new Type[new_size];
  171.             for (size_t i = 0; i < size; ++i) {
  172.                 new_data[i] = data[i];
  173.             }
  174.             for (size_t i = size; i < new_size; ++i) {
  175.                 new_data[i] = Type();  // Initialize new elements with default value
  176.             }
  177.             delete[] data;
  178.             data = new_data;
  179.             capacity = new_size;
  180.         }
  181.         if(new_size < size) fill(data+new_size, data + size, 0);
  182.         size = new_size;
  183.     }
  184.  
  185.     Iterator begin() noexcept {
  186.         return data;
  187.     }
  188.  
  189.     Iterator end() noexcept {
  190.         return data + size;
  191.     }
  192.  
  193.     ConstIterator begin() const noexcept {
  194.         return data;
  195.     }
  196.  
  197.     ConstIterator end() const noexcept {
  198.         return data + size;
  199.     }
  200.  
  201.     ConstIterator cbegin() const noexcept {
  202.         return data;
  203.     }
  204.  
  205.     ConstIterator cend() const noexcept {
  206.         return data + size;
  207.     }
  208.  
  209.     ~SimpleVector() {
  210.         delete[] data;
  211.     }
  212.  
  213. private:
  214.     Type* data;
  215.     size_t size;
  216.     size_t capacity;
  217. };
  218.  
  219. template <typename Type>
  220. inline bool operator==(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  221.     // Заглушка. Напишите тело самостоятельно
  222.     if (lhs.GetSize() != rhs.GetSize())
  223.     {
  224.         return false;
  225.     }
  226.  
  227.     for(size_t i = 0; i < lhs.GetSize(); ++i)
  228.     {
  229.         if(lhs[i] != rhs[i])
  230.         {
  231.             return false;
  232.         }
  233.     }
  234.     return true;
  235. }
  236.  
  237. template <typename Type>
  238. inline bool operator!=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  239.     // Заглушка. Напишите тело самостоятельно
  240.     return !(lhs == rhs);
  241. }
  242.  
  243. template <typename Type>
  244. inline bool operator<(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  245.     // Заглушка. Напишите тело самостоятельно
  246.     return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  247. }
  248.  
  249. template <typename Type>
  250. inline bool operator<=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  251.     // Заглушка. Напишите тело самостоятельно
  252.     return !(lhs > rhs);
  253. }
  254.  
  255. template <typename Type>
  256. inline bool operator>(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  257.     // Заглушка. Напишите тело самостоятельно
  258.     return rhs < lhs;
  259. }
  260.  
  261. template <typename Type>
  262. inline bool operator>=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
  263.     // Заглушка. Напишите тело самостоятельно
  264.     return !(lhs < rhs);
  265. }
Add Comment
Please, Sign In to add comment