fuad_cs22

Dynamic Array

Jan 13th, 2021
662
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class Vector{
  5. public:
  6.     Vector(){
  7.         /// allocate 2 elements
  8.         ReAlloc(2);
  9.     }
  10.     ~Vector(){
  11.         delete[] m_Data;
  12.     }
  13.     void PopBack(){
  14.         if(m_size > 0){
  15.             m_size--;
  16.             m_Data[m_size].~T();
  17.         }
  18.     }
  19.     void Clear(){
  20.         for (int i = 0; i < m_size; ++i) {
  21.             m_Data[i].~T();
  22.         }
  23.         m_size = 0;
  24.     }
  25.     void PushBack(const T& value){
  26.         if(m_size >= m_Capacity){
  27.             ReAlloc(m_Capacity + m_Capacity / 2);
  28.         }
  29.         m_Data[m_size] = value;
  30.         m_size++;
  31.     }
  32.     void PushBack(T&& value){
  33.         if(m_size >= m_Capacity){
  34.             ReAlloc(m_Capacity + m_Capacity / 2);
  35.         }
  36.         m_Data[m_size] = std::move(value);
  37.         m_size++;
  38.     }
  39.  
  40.     template<typename... Args>
  41.     void  EmplaceBack(Args&&... args){
  42.         if(m_size >= m_Capacity){
  43.             ReAlloc(m_Capacity + m_Capacity / 2);
  44.         }
  45.         new(&m_Data[m_size]) T(std::forward<Args>(args)...);
  46.         m_size++;
  47.     }
  48.  
  49.     [[nodiscard]] size_t size() const { return m_size; }
  50.     const T& operator[] (size_t index) const{
  51.         if (index >= m_size || index < 0){
  52.             std::cout << "Out of bounds" << std::endl;
  53.         }
  54.         return m_Data[index];
  55.     }
  56.     T& operator[] (size_t index){
  57.         if (index >= m_size || index < 0){
  58.             std::cout << "Out of bounds" << std::endl;
  59.         }
  60.         return m_Data[index];
  61.     }
  62.     [[nodiscard]] const T* begin() const{
  63.         return m_Data;
  64.     }
  65.     T* begin(){
  66.         return m_Data;
  67.     }
  68.     [[nodiscard]] const T* end() const{
  69.         return (m_Data + m_size);
  70.     }
  71.     const T* end(){
  72.         return (m_Data + m_size);
  73.     }
  74. private:
  75.     void ReAlloc(size_t newCapacity){
  76.         /// 1. allocate a new block of memory
  77.         /// 2. copy/move old elements into new block
  78.         /// 3. Delete
  79.  
  80.         T* newBlock = new T[newCapacity];
  81.  
  82.         if (newCapacity < m_size){
  83.             m_size = newCapacity;
  84.         }
  85.         for (size_t i = 0; i < m_size; ++i) {
  86.             newBlock[i] = std::move(m_Data[i]);
  87.         }
  88.         delete[] m_Data;
  89.         m_Data = newBlock;
  90.         m_Capacity = newCapacity;
  91.     }
  92. private:
  93.     T * m_Data = nullptr;
  94.     size_t m_size = 0;
  95.     size_t m_Capacity = 0;
  96. };
  97.  
  98. class Vector3{
  99. public:
  100.     float x = 0.0f, y = 0.0f, z = 0.0f;
  101.     Vector3()= default;
  102.     explicit Vector3(float scaler)
  103.     :x(scaler), y(scaler), z(scaler){}
  104.     Vector3(float x, float y, float z)
  105.     :x(x),y(y),z(z){}
  106.  
  107.     Vector3(const Vector3& other) = default;
  108.     Vector3(Vector3&& other)noexcept
  109.     :x(other.x),y(other.y),z(other.z){
  110.  
  111.     }
  112.     ~Vector3() = default;
  113.     Vector3& operator=(const Vector3& other) = default;
  114.     Vector3& operator=(Vector3&& other) noexcept {
  115.         x = other.x;
  116.         y = other.y;
  117.         z = other.z;
  118.         return *this;
  119.     }
  120. };
  121. template<typename T>
  122. void print(const Vector<T>& vector){
  123.     for (auto& v : vector) {
  124.         std::cout << v << std::endl;
  125.     }
  126.     std::cout << "---------------------------" << std::endl;
  127. }
  128. template<>
  129. void print(const Vector<Vector3>& vector){
  130.     for (auto& v : vector) {
  131.         std::cout << v.x << ", " << v.y << ", " << v.z << std::endl;
  132.     }
  133.     std::cout << "---------------------------" << std::endl;
  134. }
  135. int main() {
  136.     Vector<Vector3> vector;
  137.     vector.PushBack(Vector3(1.0f));
  138.     vector.PushBack(Vector3(2,3,4));
  139.     vector.PushBack(Vector3());
  140.     print(vector);
  141.     vector.EmplaceBack(2.9f);
  142.     vector.EmplaceBack(2,6.01,5.8);
  143.     vector.EmplaceBack();
  144.     print(vector);
  145.     vector.PopBack();
  146.     print(vector);
  147.     vector.Clear();
  148.     print(vector);
  149. }
RAW Paste Data