shizaero

Untitled

Aug 18th, 2023
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cassert>
  4. #include <initializer_list>
  5. #include <stdexcept>
  6. #include "array_ptr.h"
  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.         ArrayPtr<Type> new_data(size);
  20.         data.swap(new_data);
  21.         delete[] new_data.Get();
  22.     }
  23.  
  24.     SimpleVector(size_t size, const Type& value) : size(size), capacity(size) {
  25.         ArrayPtr<Type> new_data(size);
  26.         data.swap(new_data);
  27.          delete[] new_data.Get();
  28.         for (size_t i = 0; i < size; ++i) {
  29.             data[i] = value;
  30.         }
  31.     }
  32.  
  33.     SimpleVector(std::initializer_list<Type> init) : size(init.size()), capacity(init.size()) {
  34.          ArrayPtr<Type> new_data(size);
  35.         data.swap(new_data);
  36.         delete[] new_data.Get();
  37.         size_t i = 0;
  38.         for (const auto& item : init) {
  39.             data[i++] = item;
  40.         }
  41.     }
  42.  
  43.     size_t GetSize() const noexcept {
  44.         return size;
  45.     }
  46.  
  47.     size_t GetCapacity() const noexcept {
  48.         return capacity;
  49.     }
  50.  
  51.     bool IsEmpty() const noexcept {
  52.         return size == 0;
  53.     }
  54.  
  55.     Type& operator[](size_t index) noexcept {
  56.         if (index >= size) {
  57.             //throw std::out_of_range("Index out of range");
  58.         }
  59.         return data[index];
  60.     }
  61.  
  62.     const Type& operator[](size_t index) const noexcept {
  63.         if (index < size) {
  64.            // throw std::out_of_range("Index out of range");
  65.         }
  66.         return data[index];
  67.     }
  68.  
  69.     Type& At(size_t index){
  70.        if (index >= size) throw std::out_of_range("Index is too large!");
  71.         return (*this)[index];
  72.     }
  73.  
  74.     const Type& At(size_t index) const {
  75.        if (index >= size) throw std::out_of_range("Index is too large!");
  76.         return (*this)[index];
  77.     }
  78.  
  79.     void Clear() noexcept {
  80.         size = 0;
  81.     }
  82.  
  83.     void Resize(size_t new_size) {
  84.         if (new_size > capacity) {
  85.             ArrayPtr<Type> new_data(new_size);
  86.             data.swap(new_data);
  87.             for (size_t i = size; i < new_size; ++i) {
  88.                 new_data[i] = Type();  // Initialize new elements with default value
  89.             }
  90.             capacity = new_size;
  91.         }
  92.         size = new_size;
  93.     }
  94.  
  95.     Iterator begin() noexcept {
  96.         return data.Get();
  97.     }
  98.  
  99.     Iterator end() noexcept {
  100.         return data.Get() + size;
  101.     }
  102.  
  103.     ConstIterator begin() const noexcept {
  104.         return data.Get();
  105.     }
  106.  
  107.     ConstIterator end() const noexcept {
  108.         return data.Get() + size;
  109.     }
  110.  
  111.     ConstIterator cbegin() const noexcept {
  112.         return data.Get();
  113.     }
  114.  
  115.     ConstIterator cend() const noexcept {
  116.         return data.Get() + size;
  117.     }
  118.  
  119.     ~SimpleVector() {
  120.         delete[] data.Get();
  121.     }
  122.  
  123. private:
  124.     ArrayPtr<Type> data;
  125.     size_t size;
  126.     size_t capacity;
  127. };
Advertisement
Add Comment
Please, Sign In to add comment