shizaero

Untitled

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