ScapeGoatTree
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1#ifndef VECTOR_HPP
2#define VECTOR_HPP
3#include <stdexcept>
4#include "vector"
5template <typename T>
6class Vector {
7
8 unsigned int _size = 50;
9 int nElements = 0;
10 T* data = new T[_size]{};
11
12
13public:
14 template<typename>
15 friend class Stack;
19 ~Vector() { delete[] data; }
20
24 Vector()=default;
25
29 [[nodiscard]] unsigned int size() const { return nElements; }
30
34 void push_back(const T& value) {
35 if (nElements >= _size) {
36 _size *= 2;
37 T* newData = new T[_size]{};
38 for (unsigned int i = 0; i < nElements; ++i)
39 newData[i] = data[i];
40 delete[] data;
41 data = newData;
42 }
43 data[nElements++] = value;
44 }
51 if (nElements == 0) {
52 throw std::out_of_range("pop_back on empty Vector");
53 }
54 --nElements;
55 T value = data[nElements];
56
57 // Shrink when underutilized, keep minimum capacity of 50
58 if (nElements > 0 && static_cast<unsigned int>(nElements) <= _size / 4 && _size > 50) {
59 _size = 50u > _size ? 50u : _size;
60 T* newData = new T[_size]{};
61 for (unsigned int i = 0; i < nElements; ++i)
62 newData[i] = data[i];
63 delete[] data;
64 data = newData;
65 }
66 return value;
67 }
68 T* begin() { return data; }
69 T* end() { return data + _size; }
70
74 T& operator[](unsigned int index) { return data[index]; }
75
79 const T& operator[](unsigned int index) const { return data[index]; }
80 template <typename>
81 friend class ScapeGoatTree;
82
84 nElements = other.nElements;
85 _size = other._size;
86 data = new T[_size];
87 for (int i = 0; i < nElements; i++) {
88 data[i] = other.data[i];
89 }
90 }
91
92 // Copy assignment operator
94 if (this == &other) return *this; // self-assignment check
95
96 delete[] data; // free old memory
97
98 nElements = other.nElements;
99 _size = other._size;
100 data = new T[_size];
101 for (int i = 0; i < nElements; i++) {
102 data[i] = other.data[i];
103 }
104 return *this;
105 }
106 // Move constructor
107 Vector(Vector&& other) noexcept {
108 data = other.data;
109 nElements = other.nElements;
110 _size = other._size;
111
112 other.data = nullptr;
113 other.nElements = 0;
114 other._size = 0;
115 }
117 if (this != &other) {
118 delete[] data; // free old memory
119
120 data = other.data;
121 nElements = other.nElements;
122 _size = other._size;
123
124 other.data = nullptr;
125 other.nElements = 0;
126 other._size = 0;
127 }
128 return *this;
129 }
130
131};
132#endif
Definition queue.hpp:9
Definition scapegoat_tree.hpp:53
Definition stack.hpp:11
Definition vector.hpp:6
Vector & operator=(const Vector &other)
Definition vector.hpp:93
const T & operator[](unsigned int index) const
Definition vector.hpp:79
void push_back(const T &value)
Definition vector.hpp:34
T * begin()
Definition vector.hpp:68
Vector()=default
unsigned int size() const
Definition vector.hpp:29
T * data
Definition vector.hpp:10
int nElements
Definition vector.hpp:9
Vector(Vector &&other) noexcept
Definition vector.hpp:107
T & operator[](unsigned int index)
Definition vector.hpp:74
Vector(const Vector &other)
Definition vector.hpp:83
T * end()
Definition vector.hpp:69
T pop_back()
Definition vector.hpp:50
Vector & operator=(Vector &&other) noexcept
Definition vector.hpp:116
unsigned int _size
Definition vector.hpp:8
~Vector()
Definition vector.hpp:19