Repository navigation
Expand file tree
/
Copy pathDynamicArray.h
More file actions
343 lines (279 loc) · 13.3 KB
/
Copy pathDynamicArray.h
File metadata and controls
343 lines (279 loc) · 13.3 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
/**
* @file DynamicArray.h
* @brief Lightweight container with a fixed maximum capacity.
*
* This header defines a very small dynamic array implementation that avoids
* dynamic memory allocation. The array stores up to `MaxCount` elements of
* `DataType` and provides basic append, insert and remove operations along with
* a simple random access iterator.
* @todo Add @copyright line once project copyright wording is finalised.
*/
#ifndef HF_UTILS_GENERAL_DYNAMICARRAY_H_
#define HF_UTILS_GENERAL_DYNAMICARRAY_H_
#include <array>
#include <initializer_list>
#include <algorithm>
#include <iterator>
#include <cstddef>
#include <functional>
template <typename DataType, uint8_t MaxCount>
class DynamicArray
{
public:
DynamicArray() : data(), count(0) {}
DynamicArray(const std::initializer_list<DataType> targets) noexcept :
data(),
count(0)
{
count = std::min(static_cast<uint8_t>(targets.size()), MaxCount); /// Only copy as many elements as will fit
std::copy(targets.begin(), targets.begin() + count, data.begin());
}
/// Copy constructor
DynamicArray(const DynamicArray& other) noexcept :
data(),
count(other.count)
{
std::copy(other.data.begin(), other.data.begin() + count, data.begin());
}
/// Copy assignment operator
DynamicArray& operator=(const DynamicArray& other) noexcept
{
if (this != &other) {
count = other.count;
std::copy(other.data.begin(), other.data.begin() + count, data.begin());
}
return *this;
}
bool Append(const DataType& item) {
if (count < MaxCount) {
data[count++] = item;
return true;
}
return false;
}
bool Remove(std::function<bool(const DataType&)>& condition) {
auto it = std::find_if(begin(), end(), condition);
if (it != end()) {
std::swap(*it, data[--count]);
return true;
}
return false;
}
bool Insert(size_t index, const DataType& item) {
if (index <= count && count < MaxCount) {
std::rotate(data.begin() + index, data.begin() + count, data.end());
data[index] = item;
++count;
return true;
}
return false;
}
void ClearAll() {
count = 0;
}
DataType& operator[](const size_t& index) {
return data[index];
}
const DataType& operator[](const size_t& index) const {
return data[index];
}
//=============================================================//
/// RANDOM ACCESS ITERATOR.
//=============================================================//
/**
* @brief Random access iterator for the MultiRail class.
*
* This iterator allows direct access to any element in the readings array.
* It supports all the operations required for a random access iterator,
* including moving forwards and backwards, direct access to any element,
* and comparisons between iterators.
*/
struct iterator
{
/// The category of the iterator. Random access iterators support a superset of the operations of bidirectional iterators and forward iterators.
using iterator_category = std::random_access_iterator_tag;
/// A type that provides the difference between two iterators.
using difference_type = std::ptrdiff_t;
/// The type of the elements pointed to by the iterator.
using value_type = DataType;
/// The type of a pointer to an element pointed to by the iterator.
using pointer = value_type*;
/// The type of a reference to an element pointed to by the iterator.
using reference = value_type&;
/**
* @brief Construct a new iterator object.
*
* @param ptr Pointer to the current reading.
*/
iterator(pointer ptr) noexcept : currentReading(ptr) {}
/// Copy constructor. Constructs the iterator with the copy of the contents of other.
iterator(const iterator& copy ) noexcept = default;
/// Copy assignment operator. Replaces the contents with a copy of the contents of other.
iterator& operator = (const iterator& copy ) noexcept = default;
/// Destructor. Destroys the iterator.
~iterator() = default;
/// Dereference operator. Gives the value pointed to by the iterator.
reference operator*() const { return *currentReading; }
/// Arrow operator. Gives a pointer to the value pointed to by the iterator.
pointer operator->() const { return currentReading; }
/// Prefix increment operator. Advances the iterator to the next element and returns an iterator to the new current element.
iterator& operator++() { currentReading++; return *this; }
/// Postfix increment operator. Advances the iterator to the next element and returns an iterator to the element before the increment.
iterator operator++(int) { iterator tmp = *this; ++(*this); return tmp; }
/// Prefix decrement operator. Moves the iterator to the previous element and returns an iterator to the new current element.
iterator& operator--() { currentReading--; return *this; }
/// Postfix decrement operator. Moves the iterator to the previous element and returns an iterator to the element before the decrement.
iterator operator--(int) { iterator tmp = *this; --(*this); return tmp; }
/// Addition operator. Returns an iterator that is n positions ahead of the current iterator.
iterator operator+(difference_type n) const { return iterator(currentReading + n); }
/// Subtraction operator. Returns an iterator that is n positions behind the current iterator.
iterator operator-(difference_type n) const { return iterator(currentReading - n); }
/// Difference operator. Returns the distance between two iterators.
difference_type operator-(const iterator& other) const { return currentReading - other.currentReading; }
/// Array subscript operator. Returns the n-th element from the current position.
reference operator[](difference_type n) const { return *(currentReading + n); }
/// Less than operator. Checks if the current iterator points to an element before the other iterator.
bool operator<(const iterator& other) const { return currentReading < other.currentReading; }
/// Less than or equal to operator. Checks if the current iterator points to an element before or at the same position as the other iterator.
bool operator<=(const iterator& other) const { return currentReading <= other.currentReading; }
/// Greater than operator. Checks if the current iterator points to an element after the other iterator.
bool operator>(const iterator& other) const { return currentReading > other.currentReading; }
/// Greater than or equal to operator. Checks if the current iterator points to an element after or at the same position as the other iterator.
bool operator>=(const iterator& other) const { return currentReading >= other.currentReading; }
/// Equality operator. Checks if both iterators point to the same element.
bool operator==(const iterator& other) const { return currentReading == other.currentReading; }
/// Inequality operator. Checks if the iterators point to different elements.
bool operator!=(const iterator& other) const { return !operator==(other); }
/**
* @brief Compound addition (plus-equal) operator.
*
* Advances the iterator by n positions.
*
* @param n The number of positions to advance the iterator by.
* @return A reference to the updated iterator.
*/
iterator& operator+=(difference_type n) { currentReading += n; return *this; }
/**
* @brief Compound subtraction (minus-equal) operator.
*
* Moves the iterator back by n positions.
*
* @param n The number of positions to move the iterator back by.
* @return A reference to the updated iterator.
*/
iterator& operator-=(difference_type n) { currentReading -= n; return *this; }
private:
/// Pointer to the current reading.
pointer currentReading;
};
// Random access iterator for const objects
struct const_iterator {
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = const DataType;
using pointer = const value_type*;
using reference = const value_type&;
const_iterator(pointer ptr) noexcept : currentReading(ptr) {}
const_iterator(const const_iterator& copy) noexcept = default;
const_iterator& operator=(const const_iterator& copy) noexcept = default;
~const_iterator() = default;
reference operator*() const { return *currentReading; }
pointer operator->() const { return currentReading; }
const_iterator& operator++() { currentReading++; return *this; }
const_iterator operator++(int) { const_iterator tmp = *this; ++(*this); return tmp; }
const_iterator& operator--() { currentReading--; return *this; }
const_iterator operator--(int) { const_iterator tmp = *this; --(*this); return tmp; }
const_iterator operator+(difference_type n) const { return const_iterator(currentReading + n); }
const_iterator operator-(difference_type n) const { return const_iterator(currentReading - n); }
difference_type operator-(const const_iterator& other) const { return currentReading - other.currentReading; }
reference operator[](difference_type n) const { return *(currentReading + n); }
bool operator<(const const_iterator& other) const { return currentReading < other.currentReading; }
bool operator<=(const const_iterator& other) const { return currentReading <= other.currentReading; }
bool operator>(const const_iterator& other) const { return currentReading > other.currentReading; }
bool operator>=(const const_iterator& other) const { return currentReading >= other.currentReading; }
bool operator==(const const_iterator& other) const { return currentReading == other.currentReading; }
bool operator!=(const const_iterator& other) const { return !operator==(other); }
const_iterator& operator+=(difference_type n) { currentReading += n; return *this; }
const_iterator& operator-=(difference_type n) { currentReading -= n; return *this; }
private:
pointer currentReading;
};
/**
* @brief Returns an iterator to the beginning of the array.
*
* This function provides a non-const iterator to the first element of the array.
*
* @return iterator to the first element.
*/
iterator begin() noexcept { return iterator(data.data()); }
/**
* @brief Returns an iterator to the end of the array.
*
* This function provides a non-const iterator to the element following the last element of the array.
*
* @return iterator to the element following the last element.
*/
iterator end() noexcept { return iterator(data.data() + count); }
/**
* @brief Returns a const iterator to the beginning of the array.
*
* This function provides a const iterator to the first element of the array.
*
* @return const_iterator to the first element.
*/
const_iterator begin() const noexcept { return const_iterator(data.data()); }
/**
* @brief Returns a const iterator to the end of the array.
*
* This function provides a const iterator to the element following the last element of the array.
*
* @return const_iterator to the element following the last element.
*/
const_iterator end() const noexcept { return const_iterator(data.data() + count); }
/**
* @brief Returns a const iterator to the beginning of the array.
*
* This function provides a const iterator to the first element of the array.
* It is equivalent to the const version of begin().
*
* @return const_iterator to the first element.
*/
const_iterator cbegin() const noexcept { return const_iterator(data.data()); }
/**
* @brief Returns a const iterator to the end of the array.
*
* This function provides a const iterator to the element following the last element of the array.
* It is equivalent to the const version of end().
*
* @return const_iterator to the element following the last element.
*/
const_iterator cend() const noexcept { return const_iterator(data.data() + count); }
/**
* @brief Returns the number of elements in the array.
*
* This function returns the current number of elements stored in the array.
*
* @return The number of elements in the array.
*/
size_t size() const noexcept { return count; }
/**
* @brief Returns the maximum capacity of the array.
*
* This function returns the maximum number of elements that the array can hold.
*
* @return The maximum capacity of the array.
*/
size_t capacity() const noexcept { return MaxCount; }
/**
* @brief Checks if the array is empty.
*
* This function checks whether the array contains any elements.
*
* @return True if the array is empty, false otherwise.
*/
bool empty() const noexcept { return count == 0; }
private:
std::array<DataType, MaxCount> data;
uint8_t count;
};
#endif /* HF_UTILS_GENERAL_DYNAMICARRAY_H_ */