shizaero

Untitled

Aug 17th, 2023
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 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.     size_t GetSize() const noexcept {
  40.         return size;
  41.     }
  42.  
  43.     size_t GetCapacity() const noexcept {
  44.         return capacity;
  45.     }
  46.  
  47.     bool IsEmpty() const noexcept {
  48.         return size == 0;
  49.     }
  50.  
  51.     Type& operator[](size_t index) noexcept {
  52.         if (index >= size) {
  53.             //throw std::out_of_range("Index out of range");
  54.         }
  55.         return data[index];
  56.     }
  57.  
  58.     const Type& operator[](size_t index) const noexcept {
  59.         if (index < size) {
  60.            // throw std::out_of_range("Index out of range");
  61.         }
  62.         return data[index];
  63.     }
  64.  
  65.     Type& At(size_t index) noexcept {
  66.        if (index >= size) throw std::out_of_range("Index is too large!");
  67.         return (*this)[index];
  68.     }
  69.  
  70.     const Type& At(size_t index) const noexcept {
  71.        if (index >= size) throw std::out_of_range("Index is too large!");
  72.         return (*this)[index];
  73.     }
  74.  
  75.     void Clear() noexcept {
  76.         size = 0;
  77.     }
  78.  
  79.     void Resize(size_t new_size) {
  80.         if (new_size > capacity) {
  81.             Type* new_data = new Type[new_size];
  82.             for (size_t i = 0; i < size; ++i) {
  83.                 new_data[i] = data[i];
  84.             }
  85.             for (size_t i = size; i < new_size; ++i) {
  86.                 new_data[i] = Type();  // Initialize new elements with default value
  87.             }
  88.             delete[] data;
  89.             data = new_data;
  90.             capacity = new_size;
  91.         }
  92.         size = new_size;
  93.     }
  94.  
  95.     Iterator begin() noexcept {
  96.         return data;
  97.     }
  98.  
  99.     Iterator end() noexcept {
  100.         return data + size;
  101.     }
  102.  
  103.     ConstIterator begin() const noexcept {
  104.         return data;
  105.     }
  106.  
  107.     ConstIterator end() const noexcept {
  108.         return data + size;
  109.     }
  110.  
  111.     ConstIterator cbegin() const noexcept {
  112.         return data;
  113.     }
  114.  
  115.     ConstIterator cend() const noexcept {
  116.         return data + size;
  117.     }
  118.  
  119.     ~SimpleVector() {
  120.         delete[] data;
  121.     }
  122.  
  123. private:
  124.     Type* data;
  125.     size_t size;
  126.     size_t capacity;
  127. };
Advertisement
Add Comment
Please, Sign In to add comment