|
| 1 | +// DynamicArray.h |
| 2 | + |
| 3 | +/* |
| 4 | + * @file DynamicArray.h |
| 5 | + * @brief This library is a dynamic array demo for Arduino boards using C++. |
| 6 | + * @note I must warn you about errors, since Arduino doesn't react to exceptions. |
| 7 | + * If a user tries to reach an index that isn't available, the output will be a bunch of useless data. |
| 8 | + * This can harm your board if used too much or used wrong, so make sure to check your code at least twice. |
| 9 | + */ |
| 10 | + |
| 11 | +/* |
| 12 | + * @brief A dynamic array class that allows you to add and/or remove elements and resize the array dynamically. |
| 13 | + * @tparam T The type that will be stored in the dynamic array. |
| 14 | + */ |
| 15 | +template <typename T> |
| 16 | +class DynamicArray { |
| 17 | +private: |
| 18 | + T* data; ///< Pointer to the dynamic array data. |
| 19 | + size_t size; ///< Current number of elements in the array. |
| 20 | + size_t capacity; ///< Current capacity of the array. |
| 21 | + |
| 22 | +public: |
| 23 | + /* |
| 24 | + * @brief Default constructor for the dynamic array. |
| 25 | + */ |
| 26 | + DynamicArray() : data(nullptr), size(0), capacity(0) {} |
| 27 | + |
| 28 | + /* |
| 29 | + * @brief Destructor for the dynamic array. |
| 30 | + */ |
| 31 | + ~DynamicArray() { |
| 32 | + delete[] data; |
| 33 | + } |
| 34 | + |
| 35 | + /* |
| 36 | + * @brief Adds an element to the end of the dynamic array. |
| 37 | + * @param value The value to be added. |
| 38 | + */ |
| 39 | + void push_back(const T& value) { |
| 40 | + if (size == capacity) { |
| 41 | + resize(); |
| 42 | + } |
| 43 | + data[size] = value; |
| 44 | + size++; |
| 45 | + } |
| 46 | + |
| 47 | + /* |
| 48 | + * @brief Removes the last element from the end of the dynamic array. |
| 49 | + */ |
| 50 | + void pop_back() { |
| 51 | + if (size > 0) { |
| 52 | + size--; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /* |
| 57 | + * @brief Removes an element from the dynamic array at the specified index. |
| 58 | + * @param index The index of the element to be removed. |
| 59 | + */ |
| 60 | + void remove(size_t index) { |
| 61 | + if (index < size) { |
| 62 | + for (size_t i = index; i < size - 1; i++) { |
| 63 | + data[i] = data[i + 1]; |
| 64 | + } |
| 65 | + size--; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + * @brief Overloaded operator to access elements in the dynamic array. |
| 71 | + * @param index The index of the element to access. |
| 72 | + * @return A reference to the element at the specified index. |
| 73 | + */ |
| 74 | + T& operator[](size_t index) { |
| 75 | + if (index < size) { |
| 76 | + return data[index]; |
| 77 | + } |
| 78 | + // Handle out of range index |
| 79 | + } |
| 80 | + |
| 81 | + /* |
| 82 | + * @brief Gets the current number of elements in the dynamic array. |
| 83 | + * @return The size of the dynamic array. |
| 84 | + */ |
| 85 | + size_t getSize() const { |
| 86 | + return size; |
| 87 | + } |
| 88 | + |
| 89 | +private: |
| 90 | + /* |
| 91 | + * @brief Resizes the dynamic array by creating a new array with increased capacity and copying elements. |
| 92 | + */ |
| 93 | + void resize() { |
| 94 | + size_t newCapacity = (capacity == 0) ? 1 : capacity * 2; |
| 95 | + T* newData = new T[newCapacity]; |
| 96 | + for (size_t i = 0; i < size; i++) { |
| 97 | + newData[i] = data[i]; |
| 98 | + } |
| 99 | + delete[] data; |
| 100 | + data = newData; |
| 101 | + capacity = newCapacity; |
| 102 | + } |
| 103 | +}; |
0 commit comments