-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.h
More file actions
188 lines (150 loc) · 4.74 KB
/
DynamicArray.h
File metadata and controls
188 lines (150 loc) · 4.74 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
#pragma once
#include <assert.h>
#include <cstddef>
/**
* DynArray - A dynamic array container with manual memory management
*
* Example:
* DynArray<int> v;
* v.PushBack(10);
* v.PushBack(20);
* for (int elem : v) std::cout << elem << "\n";
* DynArray<int> v2;
* v2 = v;
*/
template <typename T>
/**
* This generic class is templated to support any data type T. Internally, it manages a
* dynamically allocated array to store the elements, which grows in capacity as needed.
*/
class DynArray {
using SizeType = std::size_t;
/**
* A pointer to data of the dynamic array.
*
* This pointer is dynamically allocated and managed by the class to store elements
* of the array.
*
* The memory for this pointer is reallocated when the array's capacity grows
*/
T* Data;
SizeType CurrentSize;
SizeType CurrentCapacity;
/**
* Doubles the capacity of the dynamic array when it is full and reallocates the "data".
*
* If the current capacity is zero, the method initializes the capacity to 1 and allocates memory for the array.
* Otherwise, the capacity is doubled, and the method allocates new memory for the array.
*
*/
void Grow() {
if (CurrentCapacity == 0)
{
CurrentCapacity = 1;
Data = new T[CurrentCapacity];
}
else
{
CurrentCapacity*=2;
T* OldData = Data;
Data = new T[CurrentCapacity];
for (SizeType i = 0; i < CurrentSize; ++i)
Data[i] = OldData[i];
delete[] OldData;
}
}
public:
DynArray() : Data(nullptr), CurrentSize(0), CurrentCapacity(0) {
} ;
~DynArray() {
delete[] Data;
};
// Copy Constructor
DynArray(const DynArray& other) :
Data(nullptr), CurrentSize(0), CurrentCapacity(0)
{
if (other.CurrentSize > 0) {
CurrentCapacity = other.CurrentSize;
Data = new T[CurrentCapacity];
for (SizeType i = 0; i < CurrentCapacity; ++i) {
Data[i] = other.Data[i];
}
CurrentSize = other.CurrentSize;
}
}
// Copy Assignment
DynArray& operator=(const DynArray& other)
{
if (this == &other)
return *this;
delete[] Data;
Data = nullptr;
CurrentSize = other.CurrentSize;
CurrentCapacity = other.CurrentCapacity;
Data = new T[CurrentCapacity];
for (SizeType i = 0; i < CurrentSize; ++i)
{
Data[i] = other.Data[i];
}
return *this;
}
// Move constructor transfers ownership of resources from another DynArray instance.
DynArray(DynArray&& other) noexcept :
Data(other.Data), CurrentSize(other.CurrentSize), CurrentCapacity(other.CurrentCapacity)
{
other.Data = nullptr;
other.CurrentSize = 0;
other.CurrentCapacity = 0;
}
// Move Assigment
DynArray& operator=(DynArray&& other) noexcept
{
if (this == &other)
return *this;
delete[] Data;
Data = other.Data;
CurrentSize = other.CurrentSize;
CurrentCapacity = other.CurrentCapacity;
other.Data = nullptr;
other.CurrentSize = 0;
other.CurrentCapacity = 0;
return *this;
}
[[nodiscard]] SizeType Size() const { return CurrentSize; };
[[nodiscard]] SizeType Capacity() const { return CurrentCapacity; };
void PushBack(const T& value)
{
if (CurrentSize >= CurrentCapacity)
Grow();
Data[CurrentSize++] = value;
}
/**
* Operator overloading to read and write our data:
* const DynArray<int> v;
* v[0] = 5; <- Writing
* int x = v[0]; <- Reading
* The non-const function gives a reference allowing to write and read data.
* The const function gives a const reference allowing only to read data.
*/
T& operator[](SizeType index)
{
assert(index < CurrentSize);
return Data[index];
}
// Provides read-only access to an element at the specified index in the dynamic array.
const T& operator[](SizeType index) const
{
assert(index < CurrentSize);
return Data[index];
}
/** iterator logic **/
// Returns a pointer to the beginning of the dynamic array.
T* begin() { return Data; }
// Returns a pointer to one past the last element in the dynamic array.
T* end() { return Data + CurrentSize; }
// This method provides a read-only iterator to the first element of the data.
const T* begin() const { return Data; }
// A const pointer to the position one past the last element of the array.
const T* end() const { return Data + CurrentSize; }
};