Line data Source code
1 : /* ✔ */ 2 : 3 : #pragma once 4 : 5 : #include <Helpers/Error.hpp> 6 : #include <stddef.h> 7 : 8 : /// @addtogroup Containers 9 : /// @{ 10 : 11 : /** 12 : * @brief An array wrapper for easy copying, comparing, and iterating. 13 : * 14 : * @tparam T 15 : * The type of the elements in the array. 16 : * @tparam N 17 : * The number of elements in the array. 18 : */ 19 : template <class T, size_t N> 20 496 : struct Array { 21 : T data[N]; 22 : using type = T; 23 : constexpr static size_t length = N; 24 : 25 : /** 26 : * @brief Get the element at the given index. 27 : * 28 : * @note Bounds checking is performed. If fatal errors are disabled, the 29 : * last element is returned if the index is out of bounds. 30 : * 31 : * @param index 32 : * The (zero-based) index of the element to return. 33 : */ 34 303 : T &operator[](size_t index) { 35 303 : if (index >= N) { // TODO 36 1 : ERROR(F("Index out of bounds: ") << index << F(" ≥ ") << N, 0xEDED); 37 0 : index = N - 1; 38 0 : } 39 302 : return data[index]; 40 1 : } 41 : 42 : /** 43 : * @brief Get the element at the given index. 44 : * 45 : * @note Bounds checking is performed. If fatal errors are disabled, the 46 : * last element is returned if the index is out of bounds. 47 : * 48 : * @param index 49 : * The (zero-based) index of the element to return. 50 : */ 51 669 : const T &operator[](size_t index) const { 52 669 : if (index >= N) { // TODO 53 1 : ERROR(F("Index out of bounds: ") << index << F(" ≥ ") << N, 0xEDED); 54 0 : index = N - 1; 55 0 : } 56 668 : return data[index]; 57 1 : } 58 : 59 : /** 60 : * @brief Get a pointer to the first element. 61 : */ 62 34 : T *begin() { return &data[0]; } 63 : 64 : /** 65 : * @brief Get a pointer to the first element. 66 0 : */ 67 24 : const T *begin() const { return &data[0]; } 68 : 69 : /** 70 : * @brief Get a pointer to the memory beyond the array. 71 : */ 72 34 : T *end() { return &data[N]; } 73 : 74 : /** 75 : * @brief Get a pointer to the memory beyond the array. 76 0 : */ 77 24 : const T *end() const { return &data[N]; } 78 : 79 : /** 80 : * @brief Check the equality of all elements in two arrays. 81 : * 82 : * @param rhs 83 : * The array to compare this array to. 84 : */ 85 13 : bool operator==(const Array<T, N> &rhs) const { 86 13 : if (this == &rhs) 87 3 : return true; 88 53 : for (size_t i = 0; i < N; i++) 89 46 : if ((*this)[i] != rhs[i]) 90 3 : return false; 91 7 : return true; 92 13 : } 93 : 94 : /** 95 : * @brief Check the inequality of all elements in two arrays. 96 : * 97 : * @param rhs 98 : * The array to compare this array to. 99 : */ 100 4 : bool operator!=(const Array<T, N> &rhs) const { return !(*this == rhs); } 101 : }; 102 : 103 : /// @}