-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVector.h
More file actions
298 lines (271 loc) · 7.16 KB
/
Vector.h
File metadata and controls
298 lines (271 loc) · 7.16 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* This class represents a dynamic array (vector) that can grow in size as needed.
* It supports basic operations such as resizing, accessing elements, and displaying the contents.
*/
#pragma once
#include <iostream>
#include <stdexcept>
template <typename T>
class Vector
{
T *data; // Pointer to the array of elements
size_t cap; // Capacity of the vector (maximum number of elements it can hold)
size_t len; // Current size of the vector (number of elements currently stored)
public:
/**a
* @brief Constructor to initialize vector with a specified size.
*
* If an initial size is provided, the vector is created with that many elements, each default-initialized.
* If no size is provided, an empty vector is created.
*
* @param initial_size The initial size of the vector (default is 0).
*/
Vector(size_t initial_size = 0)
: data(initial_size ? new T[initial_size] : nullptr), cap(initial_size), len(initial_size) {}
/**
* @brief Constructor to initialize a vector with a list of values.
*
* This constructor creates a vector with the specified elements from an initializer list.
*
* @param list An initializer list of elements.
*/
Vector(std::initializer_list<T> list)
: data(new T[list.size()]), cap(list.size()), len(list.size())
{
size_t i = 0;
for (const T &elem : list)
{
data[i++] = elem;
}
}
/**
* @brief Destructor to clean up memory allocated for the vector.
*
* The destructor frees the dynamically allocated memory for the vector's elements.
*/
~Vector()
{
delete[] data;
}
/**
* @brief Copy constructor.
*
* Creates a deep copy of another vector.
*
* @param other The vector to copy from.
*/
Vector(const Vector &other)
: data(other.len ? new T[other.len] : nullptr), cap(other.cap), len(other.len)
{
for (size_t i = 0; i < len; ++i)
{
data[i] = other.data[i];
}
}
/**
* @brief Copy assignment operator.
*
* Assigns a deep copy of another vector to this vector.
*
* @param other The vector to copy from.
* @return A reference to this vector.
*/
Vector &operator=(const Vector &other)
{
if (this != &other)
{
// Clean up existing data
delete[] data;
// Copy new data
cap = other.cap;
len = other.len;
data = other.len ? new T[other.len] : nullptr;
for (size_t i = 0; i < len; ++i)
{
data[i] = other.data[i];
}
}
return *this;
}
/**
* @brief Move constructor.
*
* Transfers ownership of resources from another vector.
*
* @param other The vector to move from.
*/
Vector(Vector &&other) noexcept
: data(other.data), cap(other.cap), len(other.len)
{
other.data = nullptr;
other.cap = 0;
other.len = 0;
}
/**
* @brief Move assignment operator.
*
* Transfers ownership of resources from another vector.
*
* @param other The vector to move from.
* @return A reference to this vector.
*/
Vector &operator=(Vector &&other) noexcept
{
if (this != &other)
{
// Clean up existing data
delete[] data;
// Transfer ownership
data = other.data;
cap = other.cap;
len = other.len;
// Reset other
other.data = nullptr;
other.cap = 0;
other.len = 0;
}
return *this;
}
/**
* @brief Access element at the specified index.
*
* This function returns a reference to the element at the specified index. If the index is out of bounds,
* an exception is thrown.
*
* @param index The index of the element to access.
* @return A reference to the element at the specified index.
* @throws std::out_of_range If the index is out of bounds.
*/
T &operator[](size_t index) const
{
if (index >= len)
{
throw std::out_of_range("Index out of bounds in Vector::operator[]");
}
return data[index];
}
/**
* @brief Resize the vector to a new size.
*
* This function resizes the vector to the specified size. If the new size is greater than the current capacity,
* the capacity is increased. New elements are default-initialized if the new size is larger than the current size.
*
* @param new_size The new size for the vector.
*/
void resize(size_t new_size)
{
if (new_size > cap)
{
// Allocate new memory with some growth factor
size_t new_cap = (new_size > cap * 2) ? new_size : cap * 2;
if (new_cap == 0)
new_cap = 1;
T *new_data = new T[new_cap];
for (size_t i = 0; i < len; ++i)
{
new_data[i] = data[i]; // Copy existing elements
}
delete[] data;
data = new_data;
cap = new_cap;
}
// Default-initialize new elements if resizing to a larger size
for (size_t i = len; i < new_size; ++i)
{
data[i] = T();
}
len = new_size;
}
/**
* @brief Get the current size of the vector.
*
* This function returns the current size of the vector (the number of elements stored in the vector).
*
* @return The number of elements currently stored in the vector.
*/
size_t size() const
{
return len;
}
/**
* @brief Display the elements of the vector.
*
* This function prints all the elements of the vector to the console.
*/
void display(bool printFirstElement = true, std::string sep = " ") const
{
if (len == 0)
{
std::cout << std::endl;
return;
}
size_t start = printFirstElement ? 0 : 1;
if (start < len)
{
std::cout << data[start];
for (size_t i = start + 1; i < len; ++i)
std::cout << sep << data[i];
}
std::cout << std::endl;
}
/**
* @brief Add a new element to the end of the vector.
*
* If the current capacity is not sufficient, the vector's capacity is doubled before adding the new element.
*
* @param value The value to be added to the vector.
*/
void push_back(const T &value)
{
if (len >= cap)
{
// Need to grow capacity
size_t new_cap = (cap == 0) ? 1 : cap * 2;
T *new_data = new T[new_cap];
for (size_t i = 0; i < len; ++i)
{
new_data[i] = data[i];
}
delete[] data;
data = new_data;
cap = new_cap;
}
data[len++] = value;
}
/**
* @brief Find the first occurrence of a value.
*
* @param key The value to search for.
* @return The index of the first occurrence, or -1 if not found.
*/
int findOne(const T &key) const
{
for (size_t i = 0; i < len; ++i)
{
if (data[i] == key)
return static_cast<int>(i);
}
return -1;
}
/**
* @brief Remove an element by its index.
*
* This function removes the element at the specified index and shifts the subsequent elements
* to fill the gap. If the index is invalid, an exception is thrown.
*
* @param index The index of the element to remove.
* @throws std::out_of_range If the index is out of bounds.
*/
void removeByIndex(size_t index)
{
if (index >= len)
{
throw std::out_of_range("Index out of bounds in Vector::removeByIndex");
}
for (size_t i = index; i < len - 1; ++i)
{
data[i] = data[i + 1];
}
--len;
}
};