blob: 9937765cf6d6d248bad6a549c52a625b9769afd1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#pragma once
#include<iostream>
const size_t DEFAULT_CAPACITY = 4;
template <typename T>
class Vector {
private:
T* data;
size_t size;
size_t capacity;
public:
Vector();
Vector(const Vector<T>&);
Vector(Vector<T>&&);
Vector<T>& operator=(const Vector<T>&);
Vector<T>& operator=(Vector<T>&&);
~Vector();
size_t getSize() const;
size_t getCapacity() const;
void pushBack(const T&);
void pushBack(T&& n);
void pushAt(const T&, const size_t);
void pushAt(T&&, const size_t);
T& popBack();
T& popAt(const size_t);
T& operator[](const size_t);
const T& operator[](const size_t) const;
bool empty() const;
void clear();
void swap(const Vector<T>&);
private:
void copy(const Vector&);
void free();
void move(Vector&&);
size_t calculateCapacity(const size_t) const;
void resize(const size_t);
};
template <typename T>
Vector<T>::Vector() {
data = nullptr;
capacity = size = 0;
}
template <typename T>
Vector<T>::Vector(const Vector<T>& other) {
copy(other);
}
template <typename T>
Vector<T>::Vector(Vector<T>&& other) {
move(std::move(other));
}
template <typename T>
Vector<T>& Vector<T>::operator=(const Vector<T>& other) {
if (this != &other)
{
free();
copy(other);
}
return *this;
}
template <typename T>
Vector<T>& Vector<T>::operator=(Vector<T>&& other) {
if (this != &other)
{
free();
move(std::move(other));
}
return *this;
}
template <typename T>
Vector<T>::~Vector() {
free();
}
template <typename T>
size_t Vector<T>::getSize() const {
return size;
}
template <typename T>
size_t Vector<T>::getCapacity() const {
return capacity;
}
template <typename T>
void Vector<T>::pushBack(const T& element) {
if (size + 1 > capacity)
resize(size + 1);
data[size++] = element;
}
template <typename T>
void Vector<T>::pushBack(T&& element) {
if (size + 1 > capacity)
resize(size + 1);
data[size++] = element;
}
template <typename T>
void Vector<T>::pushAt(const T& element, const size_t index) {
if (size + 1 > capacity)
resize(size + 1);
for (size_t i = size; i > index; i--)
data[i] = data[i - 1];
data[index] = element;
size++;
}
template <typename T>
void Vector<T>::pushAt(T&& element, const size_t index) {
if (size + 1 > capacity)
resize(size + 1);
for (size_t i = size; i > index; i--)
data[i] = data[i - 1];
data[index] = element;
size++;
}
template <typename T>
T& Vector<T>::popBack() {
if (empty())
throw "Vector is empty";
return data[--size];
}
template <typename T>
T& Vector<T>::popAt(size_t index) {
if (index < 0 || index >= size)
throw "Invalid index";
T temp = data[index];
for (size_t i = index; i < size - 1; i++)
data[i] = data[i + 1];
size--;
return temp;
}
template <typename T>
T& Vector<T>::operator[](const size_t index) {
if (index < 0 || index >= size)
throw "Invalid index";
return data[index];
}
template <typename T>
const T& Vector<T>::operator[](const size_t index) const {
if (index < 0 || index >= size)
throw "Invalid index";
return data[index];
}
template <typename T>
bool Vector<T>::empty() const {
return size == 0;
}
template <typename T>
void Vector<T>::clear() {
delete[] data;
capacity = DEFAULT_CAPACITY;
data = new T[capacity];
size = 0;
}
template <typename T>
void Vector<T>::swap(const Vector<T>& other) {
Vector<T> tempVector(std::move(*this));
*this = std::move(other);
other = std::move(tempVector);
}
template <typename T>
void Vector<T>::copy(const Vector<T>& other) {
size = other.size;
capacity = other.capacity;
data = new T[capacity];
for (size_t i = 0; i < size; i++)
data[i] = other[i];
}
template <typename T>
void Vector<T>::free() {
delete[] data;
data = nullptr;
capacity = size = 0;
}
template <typename T>
void Vector<T>::move(Vector&& other)
{
data = other.data;
size = other.size;
capacity = other.capacity;
other.data = nullptr;
other.capacity = other.size = 0;
}
template <typename T>
size_t Vector<T>::calculateCapacity(const size_t number) const {
size_t result = DEFAULT_CAPACITY;
while (number > result)
result *= 2;
return result;
}
template <typename T>
void Vector<T>::resize(const size_t expectedCapacityToFit) {
capacity = calculateCapacity(expectedCapacityToFit);
T* temp = new T[capacity];
for (size_t i = 0; i < size; i++)
temp[i] = data[i];
delete[] data;
data = temp;
}
|