-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDynamicArray_SR.hpp
More file actions
384 lines (328 loc) · 8.81 KB
/
DynamicArray_SR.hpp
File metadata and controls
384 lines (328 loc) · 8.81 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//=================================================================================================================
/**
* Example of implementation of a class that defines dynamic arrays.
*/
//=================================================================================================================
#ifndef DYNAMIC_ARRAY_HPP
#define DYNAMIC_ARRAY_HPP
// Includes
#include <stdexcept> // For std::out_of_range
#include <iostream>
/**
* Class that defines a dynamic array.
*
* @tparam T Type of the elements in the array.
*/
template <typename T>
class DynamicArray {
public:
/**
* Default constructor. The array is empty.
*/
DynamicArray() = default;
/**
* Constructor that initializes all elements with a constant value.
*/
DynamicArray(const T& value, unsigned int size) : size_(size), capacity_(size*2)
{
data_ = new T[capacity_];
for (unsigned int i = 0; i < size_; ++i)
data_[i] = value;
}
/**
* Copy constructor.
*/
DynamicArray(const DynamicArray& other)
{
size_ = other.size_;
capacity_ = other.capacity_;
data_ = new T[capacity_];
for (unsigned int i = 0; i < size_; ++i)
data_[i] = other.data_[i];
}
/**
* Destructor. Deallocates the memory.
*/
~DynamicArray()
{
if (data_ != nullptr)
delete[] data_;
}
/**
* Copy assignment operator.
*/
DynamicArray& operator=(const DynamicArray& other)
{
if (this != &other) {
if (data_ != nullptr)
delete[] data_;
size_ = other.size_;
capacity_ = other.capacity_;
data_ = new T[capacity_];
for (unsigned int i = 0; i < size_; ++i)
data_[i] = other.data_[i];
}
return *this;
}
/**
* Returns the size of the array.
*/
unsigned int size() const
{
return size_;
}
/**
* Checks if the array is empty.
*/
bool empty() const
{
return size_ == 0;
}
/**
* Access an element of the array (without bound checking).
*
* @param[in] index The index of the element to access.
*
* @return The element at the given index.
*/
T& operator[](unsigned int index)
{
return data_[index];
}
/**
* Access an element of the array (without bound checking).
*
* @param[in] index The index of the element to access.
*
* @return The element at the given index.
*/
const T& operator[](unsigned int index) const
{
return data_[index];
}
/**
* Access an element of the array (with bound checking).
*
* @param[in] index The index of the element to access.
*
* @return The element at the given index.
*/
T& at(unsigned int index)
{
if (index >= size_) {
throw std::out_of_range("Index out of range");
}
return data_[index];
}
/**
* Access an element of the array (with bound checking).
*
* @param[in] index The index of the element to access.
*
* @return The element at the given index.
*/
const T& at(unsigned int index) const
{
if (index >= size_) {
throw std::out_of_range("Index out of range");
}
return data_[index];
}
/**
* Access the first element of the array.
*/
T& front()
{
return data_[0];
}
/**
* Access the first element of the array.
*/
const T& front() const
{
return data_[0];
}
/**
* Access the last element of the array.
*/
T& back()
{
return data_[size_ - 1];
}
/**
* Access the last element of the array.
*/
const T& back() const
{
return data_[size_ - 1];
}
/**
* Fills the array with a value.
*
* @param[in] value The value to fill the array with.
*/
void fill(const T& value)
{
for (unsigned int i = 0; i < size_; ++i) {
data_[i] = value;
}
}
/**
* Finds the index of a value in the array.
*
* @param[in] value The value to be found.
*
* @return The index of the value in the array, or -1 if the value is not found.
*/
int find(const T& value)
{
int index = -1;
for (int i = 0; i < (int)size_; ++i) {
if (data_[i] == value) {
index = i;
break;
}
}
return index;
}
/**
* Clears the array and deallocates the memory.
*/
void clear()
{
if (data_ != nullptr)
delete[] data_;
data_ = nullptr;
size_ = 0;
}
/**
* Resizes the array and fills the new elements with a value.
*
* @param newSize The new size of the array.
* @param value The value to fill the new elements with.
*/
void resize(unsigned int newCapacity)
{
if (newCapacity > capacity_) {
T* newData = new T[newCapacity];
for (unsigned int i = 0; i < size_; ++i)
newData[i] = data_[i];
delete[] data_;
data_ = newData;
capacity_ = newCapacity;
}
}
/**
* Resizes the array and fills the new elements with a value.
*
* @param newSize The new size of the array.
* @param value The value to fill the new elements with.
*/
void resize(unsigned int newSize, const T& value)
{
if (newSize > capacity_) {
// Duplicar capacidad o establecerla a newSize, lo que sea mayor
unsigned int newCapacity = (newSize > capacity_ * 2) ? newSize : capacity_ * 2;
T* newData = new T[newCapacity];
// Copiar los elementos al nuevo arreglo
unsigned int copySize = (newSize < size_) ? newSize : size_;
for (unsigned int i = 0; i < copySize; ++i)
newData[i] = data_[i];
// Llenar los nuevos elementos con el valor proporcionado
for (unsigned int i = copySize; i < newSize; ++i)
newData[i] = value;
// Liberar el arreglo anterior y actualizar punteros
delete[] data_;
data_ = newData;
capacity_ = newCapacity;
}
// Actualizar el tamaño del arreglo
size_ = newSize;
}
/**
* Inserts a new element at a specific position.
*
* @param index The position where the element will be inserted.
*/
void insert(unsigned int index, const T& value)
{
if (index > size_) {
throw std::out_of_range("Index out of range");
}
resize(size_ + 1);
for (unsigned int i = size_ - 1; i > index; --i) {
data_[i] = data_[i - 1];
}
data_[index] = value;
}
/**
* Erases an element at a specific position.
*
* @param[in] index The position of the element to erase.
*/
void erase(unsigned int index)
{
if (index >= size_) {
throw std::out_of_range("Index out of range");
}
for (unsigned int i = index; i < size_ - 1; ++i) {
data_[i] = data_[i + 1];
}
resize(size_ - 1);
}
/**
* Inserts a new element at the end of the array.
*
* @param[in] value The value to insert.
*/
void push_back(const T& value)
{
if (size_ == capacity_) {
resize(capacity_ > 0 ? capacity_ * 2 : 1); // Duplicar capacidad o inicializar en 1 si es 0
}
data_[size_] = value;
++size_;
}
/**
* Removes the last element of the array.
*/
void pop_back()
{
if (size_ > 0) {
resize(size_ - 1);
}
}
/**
* Inserts a new element at the front of the array.
*
* @param[in] value The value to insert.
*/
void push_front(const T& value)
{
insert(0, value);
}
/**
* Removes the first element of the array.
*/
void pop_front()
{
erase(0);
}
unsigned int capacity() const {
return capacity_;
}
void print()const{
for (unsigned int i = 0; i < size_; ++i) {
std::cout << data_[i]<<" ";
}
std::cout << std::endl<< std::endl;
}
private:
T* data_{nullptr}; // Pointer to the dynamic array
unsigned int size_{0}; // Current size of the array
unsigned int capacity_{0}; //Current capacity of the array
};
#endif
//=================================================================================================================
// END OF FILE
//=================================================================================================================