-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_array.h
More file actions
316 lines (292 loc) · 9.06 KB
/
dynamic_array.h
File metadata and controls
316 lines (292 loc) · 9.06 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
#ifndef DATA_STRUCTURES_DYNAMIC_ARRAY_H
#define DATA_STRUCTURES_DYNAMIC_ARRAY_H
#include <iostream>
#include <stdexcept>
namespace DataStructures {
/**
* @class DynamicArray
* @brief Dynamic array class with all related functionality.
* @tparam T Type of the implementation class.
*/
template <typename T>
class DynamicArray {
public:
/**
* @brief Default constructor for the DynamicArray class.
*
* Initializes an array of type T and size default initial capacity. The
* current size is set to zero and the capacity is initializd to the
* default initial capacity.
*/
DynamicArray()
{
data = new T[init_cap];
size = 0;
cap = init_cap;
}
/**
* @brief Constructor for DynamicArray class with specified initial
* capacity.
*
* Initalizes an array of type T and size specified initial capacity.
* The current size is set to zero and the capacity is initialized to
* the specified initial capacity.
*
* @param init_cap Desired initial capacity of the dynamic array.
* */
DynamicArray(int initial_capacity)
{
data = new T[initial_capacity];
size = 0;
cap = initial_capacity;
}
/**
* @brief Default destructor for the DynamicArray class.
*
* Frees the memory used by the array.
*/
~DynamicArray() { delete[] data; }
/** Add functions */
/**
* @brief Adds an element at the end of the array.
*
* This is achieved by first checking if the array is full, doubling its
* size if it is and then adding the element and incrementing the
* current size by unity.
*
* @param element The element to be inserted.
*/
void add_last(const T& element)
{
if (int(size) == cap) {
resize(2 * cap);
}
data[size] = element;
size++;
}
/**
* @brief Adds an element at a specified index.
*
* This is achieved by first checking if the position index is valid
* using the private @ref check_position_index() function. If the array
* is full then its size is doubled. The elements to the right of the
* desired position are shifted to the right, the new element added and
* the size incremented by unity.
*
* @param pos_index Position index where the new element is desired to
* be added.
* @param element The element to be added.
*/
void add(int pos_index, const T& element)
{
check_position_index(pos_index);
if (int(size) == cap) {
resize(2 * cap);
}
for (int i = int(size) - 1; i >= pos_index; i--) {
data[i + 1] = data[i];
}
data[pos_index] = element;
size++;
}
/**
* @brief Adds an element at the start of the array.
*
* Internally uses the @ref add() function with @c pos_index zero to add
* the element to the start of the array.
*
* @param element The element to be added.
*/
void add_first(const T& element) { add(0, element); }
/* Delete functions */
/**
* @brief Deletes the last element of the array and returns its value.
*
* This is achieved by first checking if the array is empty, if the
* current size is one-fourth the capacity alloted then the array is
* resized to half its size. The last element is cleared, the current
* size of the array updated and the last element's value is returned.
*
* @return The deleted last element.
*/
T del_last()
{
if (size == 0) {
throw std::out_of_range("The array is empty");
}
if (int(size) == cap / 4) {
resize(cap / 2);
}
T last_element = data[int(size) - 1];
data[int(size) - 1] = T();
size--;
return last_element;
}
/**
* @brief Deletes the element at a particular index.
*
* This is achieved by first checking if the index is valid, halfing the
* size of the array if its current size is one-fourth its capacity,
* then shifting all elements to the right of the index to the left,
* clearing its value, updating the size and finally returning the
* deleted element.
*
* @param index The index of the element to be erased.
* @return The deleted element.
*/
T del(int index)
{
check_element_index(index);
if (int(size) == cap / 4) {
resize(cap / 2);
}
T deleted_element = data[index];
for (int i = index + 1; i < int(size); i++) {
data[i - 1] = data[i];
}
data[int(size) - 1] = T();
size--;
return deleted_element;
}
/**
* @brief Deletes the first element in the array.
*
* Internally uses the @ref del() function with @c index zero to delete
* the first element in the array.
*
* @return The deleted first element.
*/
T del_first() { return del(0); }
/* Get functions */
/**
* @brief Get the element at a specified index.
*
* Checks if the requested element's index is valid using the private
* @ref check_element_index() function and returns the element if it is.
*
* @param index Index of the element.
* @return Element at the specified index.
*/
T get(int index) const
{
check_element_index(index);
return data[index];
}
/* Set functions */
/**
* @brief Sets the value of an element at a particular index.
*
* This is achieved by first checking if the index is valid using the
* private @ref check_element_index(), setting it's value and then
* returning the old value.
*
* @param index The index of the element whose value is to be set.
* @param val The new value to be set to.
* @return The old value.
*/
T set(int index, const T& val)
{
check_element_index(index);
T old_val = data[index];
data[index] = val;
return old_val;
}
/* Utility functions */
/**
* @brief Get the current size of the array.
* @return Size of the array.
*/
size_t get_size() const { return size; }
/**
* @brief Check if the array is empty.
* @return true if the array is empty, false otherwise.
*/
bool is_empty() const { return size == 0; }
/**
* @brief Resize the array to a new capacity.
*
* Creates an array with the new capacity, copies the old data to it and
* makes the old array pointer point to this new array. The capacity is
* updated accordingly.
*/
void resize(int new_cap)
{
T* temp = new T[new_cap];
for (int i = 0; i < int(size); i++) {
temp[i] = data[i];
}
data = temp;
cap = new_cap;
}
/**
* @brief Checks if the given element index is valid i.e in [0, size).
* @param index Index to be checked.
* @return true if the index is valid, false otherwise.
*/
bool is_element_index(int index) const
{
return index >= 0 && index < int(size);
}
/**
* @brief Checks if the given position index is valid i.e in [0, size].
*
* @param pos_index Position index to be checked.
* @return true if the position index is valid, false otherwise.
*/
bool is_position_index(int pos_index) const
{
return pos_index >= 0 && pos_index <= int(size);
}
/**
* @brief Checks the given element index and throws an exception if out
* of range.
* @param index Index to be checked.
* @throws std::out_of_range thrown when the index is out of the valid
* range i.e [0, size). The exception message states that the
* "Element index is out of bounds".
*/
void check_element_index(int index) const
{
if (!is_element_index(index)) {
throw std::out_of_range(
"Element index is out of bounds");
}
}
/**
* @brief Checks the given position index and throws an exception if out
* of range.
* @param pos_index Position index to be checked.
* @throws std::out_of_range thrown when the position index is out of
* the valid range i.e [0, size]. The exception message states
* that the "Position index is out of bounds".
*/
void check_position_index(int pos_index) const
{
if (!is_position_index(pos_index)) {
throw std::out_of_range(
"Position index is out of bounds");
}
}
/**
* @brief Report the current size and capacity of the array and display
* the values of its elements.
*/
void display()
{
std::cout << "Size = " << size << "Capacity = " << cap << '\n';
for (int i = 0; i < int(size); i++) {
std::cout << data[i] << " \n"[i == int(size) - 1];
}
}
private:
/** The underlying array that actually stores the data. */
T* data = nullptr;
/** Current number of elements. */
size_t size = 0;
/** Maximum element capacity. */
int cap = 0;
/** Default initial capacity. */
static constexpr int init_cap = 1;
};
} // namespace DataStructures
#endif // DATA_STRUCTURES_DYNAMIC_ARRAY_H