-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_array.cpp
236 lines (203 loc) · 6.35 KB
/
dynamic_array.cpp
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
#include "stdio.h" // printf
#include "stdlib.h" // malloc, realloc, free
template <typename T>
class DynamicArray {
private:
#pragma region Private Variables
// Array
T *array;
// Interal values
int startingSize, currentIndex, currentSize;
#pragma endregion Private Variables
#pragma region Interal Methods
/**
* @brief Validates the array size, and expands or shrinks the array as needed.
*
*/
void validateArraySize() {
// Increase the size of the array if the current index is equal to the current size
if (this->currentIndex == this->currentSize) {
this->currentSize *= 2;
this->array = (T *) realloc(this->array, sizeof(T) * this->currentSize);
printf("Reallocated array to size %d\n", this->currentSize);
}
// Decrease the size of the array if it's not more than 50% full. (Also ensures that the array never gets smaller than the starting size.)
if (this->currentSize > this->startingSize && this->currentSize / 2 > this->currentIndex) {
this->currentSize /= 2;
this->array = (T *) realloc(this->array, sizeof(T) * this->currentSize);
printf("Reallocated array to size %d\n", this->currentSize);
}
}
#pragma endregion Interal Methods
public:
#pragma region Constructors
/**
* @brief Construct a new Dynamic Array object
*
*/
DynamicArray() {
this->currentIndex = 0;
this->currentSize = 1;
this->startingSize = 1;
this->array = (T *) malloc(sizeof(T) * this->currentSize);
}
/**
* @brief Construct a new Dynamic Array object
*
* @param startingSize The starting size of the array
*/
DynamicArray(int startingSize) {
this->currentIndex = 0;
this->currentSize = startingSize;
this->startingSize = startingSize;
this->array = (T *) malloc(sizeof(T) * startingSize);
}
/**
* @brief Construct a new Dynamic Array object
*
* @param other The DynamicArray object to copy
*/
DynamicArray(const DynamicArray<T> &toCopy) {
this->currentIndex = toCopy.currentIndex;
this->currentSize = toCopy.currentSize;
this->startingSize = toCopy.startingSize;
this->array = (T *) malloc(sizeof(T) * this->currentSize);
for (int i = 0; i < this->getSize(); i++) { this->array[i] = toCopy.array[i]; }
}
#pragma endregion Constructors
#pragma region Destructor
/**
* @brief Destroy the Dynamic Array object
*
*/
~DynamicArray() {
free(this->array);
}
#pragma endregion Destructor
#pragma region Public Methods
/**
* @brief Adds an item to the end of the array
*
* @param toAdd The item to add
*/
void add(T toAdd) {
this->validateArraySize();
this->array[this->currentIndex] = toAdd;
this->currentIndex++;
}
/**
* @brief Removes an item from the array at the specified index
*
* @param index The index of the item to remove
*/
void remove(int index) {
if (index < 0 || index >= this->currentIndex) { printf("Error: Index out of bounds.\n"); return; }
for (int i = index; i < this->currentIndex - 1; i++) { this->array[i] = this->array[i + 1]; }
this->currentIndex--;
this->validateArraySize();
}
/**
* @brief Push an item to the end of the array
*
* @param toAdd The item to add
*/
void push(T toAdd) {
this->validateArraySize();
this->array[this->currentIndex] = toAdd;
this->currentIndex++;
}
/**
* @brief Pops the last item from the array
*
*/
void pop() {
if (this->currentIndex == 0) { printf("Error: Array is empty.\n"); return; }
this->currentIndex--;
this->validateArraySize();
}
/**
* @brief Push an item to the front of the array
*
* @param toAdd The item to add
*/
void push_front(T toAdd) {
this->validateArraySize();
for (int i = this->currentIndex; i > 0; i--) { this->array[i] = this->array[i - 1]; }
this->array[0] = toAdd;
this->currentIndex++;
}
/**
* @brief Pops the first item from the array
*
*/
void pop_front() {
if (this->currentIndex == 0) { printf("Error: Array is empty.\n"); return; }
for (int i = 0; i < this->currentIndex - 1; i++) { this->array[i] = this->array[i + 1]; }
this->currentIndex--;
this->validateArraySize();
}
/**
* @brief Clears the array
*
*/
void clear() { // Unsure if this method will even work, but I'll figure it out later.
this->currentIndex = 0;
this->validateArraySize();
}
#pragma endregion Public Methods
#pragma region Getters
/**
* @brief Gets the item at the specified index
*
* @param index The index of the item to get
* @return const T The item at the specified index
*/
const T get(int index) {
if (index < 0 || index >= this->currentIndex) { printf("Error: Index out of bounds.\n"); return 0; }
return this->array[index];
}
/**
* @brief Get the starting size of the object
*
* @return const int The starting size of the array
*/
const int getStartingSize() {
return this->startingSize;
}
/**
* @brief Get the current "size" of the object (How many items are in the array)
*
* @return const int The current "size" of the array
*/
const int getSize() {
return this->currentIndex;
}
/**
* @brief Get the current index object
*
* @return const int The current index
*/
const int getCurrentIndex() {
return this->currentIndex;
}
/**
* @brief Get the actual size of the array (How many items the array can hold)
*
* @return const int The actual size of the array
*/
const int getActualSize() {
return this->currentSize;
}
#pragma endregion Getters
#pragma region Setters
/**
* @brief Sets the item at the specified index
*
* @param index The index of the item to set
* @param toSet The value to set the item to
*/
void set(int index, T toSet) {
this->array[index] = toSet;
}
#pragma endregion Setters
};