|
| 1 | +/*! |
| 2 | + * @file AbstractList.hpp |
| 3 | + * |
| 4 | + * This file is part of the List library. It extends the arduino ecosystem with |
| 5 | + * easy-to-use list implementations. They are specially designed and optimized |
| 6 | + * for different purposes. |
| 7 | + * |
| 8 | + * Copyright (C) 2021 Niklas Kaaf |
| 9 | + * |
| 10 | + * This library is free software; you can redistribute it and/or |
| 11 | + * modify it under the terms of the GNU Lesser General Public |
| 12 | + * License as published by the Free Software Foundation; either |
| 13 | + * version 2.1 of the License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This library is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + * Lesser General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Lesser General Public |
| 21 | + * License along with this library; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 23 | + * USA |
| 24 | + */ |
| 25 | + |
| 26 | +#ifndef LIST_ABSTRACT_LIST_HPP |
| 27 | +#define LIST_ABSTRACT_LIST_HPP |
| 28 | + |
| 29 | +/*! |
| 30 | + * @brief Abstract class from which all lists can be derived. |
| 31 | + * |
| 32 | + * @tparam T Data Type of entries, that should be stored in the list. |
| 33 | + */ |
| 34 | +template <typename T> class AbstractList { |
| 35 | +private: |
| 36 | + int size = 0; /// Size of the list |
| 37 | + bool mutableList = true; /// Is the list mutable or immutable |
| 38 | + |
| 39 | +protected: |
| 40 | + /*! |
| 41 | + * @brief Constructor of an AbstractList Object. |
| 42 | + * |
| 43 | + * @param mutableList true if the list should be mutable; false otherwise. |
| 44 | + */ |
| 45 | + explicit AbstractList<T>(bool mutableList) : mutableList(mutableList) {} |
| 46 | + |
| 47 | + /*! |
| 48 | + * @brief Increase the size of the list by one. Should only be called after an |
| 49 | + * insertion! |
| 50 | + */ |
| 51 | + void increaseSize() { this->size++; } |
| 52 | + |
| 53 | + /*! |
| 54 | + * @brief Decrease the size of the list by one. Should only be called after an |
| 55 | + * deletion! |
| 56 | + */ |
| 57 | + void decreaseSize() { this->size--; } |
| 58 | + |
| 59 | + /*! |
| 60 | + * @brief Method to verify if the given index is out of the range of the list |
| 61 | + * size. |
| 62 | + * |
| 63 | + * @param index Index to check. |
| 64 | + * @return true if the given index is in the range of the list; false |
| 65 | + * otherwise |
| 66 | + */ |
| 67 | + bool isIndexOutOfBounds(int index) { |
| 68 | + return index < 0 || index >= this->getSize(); |
| 69 | + } |
| 70 | + |
| 71 | +public: |
| 72 | + /*! |
| 73 | + * @brief Add the value to the list at the given index. The original entry at |
| 74 | + * this index, and followings, will be placed directly after the new |
| 75 | + * entry. |
| 76 | + * @note Use this only if you know what you are doing. Otherwise use add(), |
| 77 | + * addFirst() or addLast(). |
| 78 | + * |
| 79 | + * @param index Index of the entry, where the value should be added. |
| 80 | + * @param value Value of the new entry. |
| 81 | + */ |
| 82 | + virtual void addAtIndex(int index, T &value) = 0; |
| 83 | + |
| 84 | + /*! |
| 85 | + * @brief Remove the entry at the given index. |
| 86 | + * |
| 87 | + * @param index Index of element to remove. |
| 88 | + */ |
| 89 | + virtual void remove(int index) = 0; |
| 90 | + |
| 91 | + /*! |
| 92 | + * @brief Get a pointer to the entry at the given index. |
| 93 | + * @note If you only want to get the value, use getValue(). |
| 94 | + * |
| 95 | + * @param index Index of the element to get. |
| 96 | + * @return Pointer to the element. |
| 97 | + */ |
| 98 | + virtual T *get(int index) = 0; |
| 99 | + |
| 100 | + /*! |
| 101 | + * @brief Get the plain value at the given index. |
| 102 | + * @see get() |
| 103 | + * |
| 104 | + * @param index Index of element to get. |
| 105 | + * @return Value. |
| 106 | + */ |
| 107 | + T getValue(int index) { return *get(index); } |
| 108 | + |
| 109 | + /*! |
| 110 | + * @brief Add a new entry at the end of the list. |
| 111 | + * @see add() |
| 112 | + * |
| 113 | + * @param value Value to add. |
| 114 | + */ |
| 115 | + void addLast(T &value) { this->addAtIndex(this->getSize(), value); } |
| 116 | + |
| 117 | + /*! |
| 118 | + * @brief Add a new entry at the beginning of the list. |
| 119 | + * |
| 120 | + * @param value Value to add. |
| 121 | + */ |
| 122 | + void addFirst(T &value) { this->addAtIndex(0, value); } |
| 123 | + |
| 124 | + /*! |
| 125 | + * @brief Add a new entry at the end of the list. |
| 126 | + * @see addLast() |
| 127 | + * |
| 128 | + * @param value Value to add. |
| 129 | + */ |
| 130 | + void add(T &value) { this->addLast(value); } |
| 131 | + |
| 132 | + /*! |
| 133 | + * @brief Add all entries from the given list at the end of the list. |
| 134 | + * @see addLast() |
| 135 | + * |
| 136 | + * @param list Other list to copy from. |
| 137 | + */ |
| 138 | + void addAll(AbstractList<T> &list) { |
| 139 | + for (int i = 0; i < list.getSize(); ++i) { |
| 140 | + this->addLast(*list.get(i)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /*! |
| 145 | + * @brief Add all entries from the given list to this list at the given index. |
| 146 | + * The original entry at this index, and followings, will be placed |
| 147 | + * directly after the entries of the given list. |
| 148 | + * @see addAtIndex() |
| 149 | + * @note Use this only if you know what you are doing. Otherwise use |
| 150 | + * addAll(). |
| 151 | + * |
| 152 | + * @param index Index, at which the list should be added. |
| 153 | + * @param list List to add. |
| 154 | + */ |
| 155 | + void addAll(int index, AbstractList<T> &list) { |
| 156 | + for (int i = 0; i < list.getSize(); i++) { |
| 157 | + this->addAtIndex(index++, *list.get(i)); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /*! |
| 162 | + * @brief Get an array which represent the list. |
| 163 | + * @note The returned pointer has to be free'd with free() in order to |
| 164 | + * prevent memory leaks. |
| 165 | + * |
| 166 | + * @return Array representation of the list or null if the list is empty. |
| 167 | + */ |
| 168 | + T *toArray() { |
| 169 | + if (this->getSize() == 0) { |
| 170 | + return nullptr; |
| 171 | + } |
| 172 | + |
| 173 | + T *arr = (T *)malloc(this->getSize() * sizeof(T)); |
| 174 | + |
| 175 | + for (int i = 0; i < this->getSize(); ++i) { |
| 176 | + arr[i] = *this->get(i); |
| 177 | + } |
| 178 | + |
| 179 | + return arr; |
| 180 | + } |
| 181 | + |
| 182 | + /*! |
| 183 | + * @brief Get the number how many elements are saved in the list. |
| 184 | + * |
| 185 | + * @return Size of the list. |
| 186 | + */ |
| 187 | + int getSize() { return this->size; } |
| 188 | + |
| 189 | + /*! |
| 190 | + * @brief Check if the list is mutable. |
| 191 | + * |
| 192 | + * @return true if the list is mutable; false otherwise. |
| 193 | + */ |
| 194 | + bool isMutable() { return mutableList; } |
| 195 | + |
| 196 | + /*! |
| 197 | + * @brief Check if the list is empty. |
| 198 | + * |
| 199 | + * @return true if the list is empty; false otherwise |
| 200 | + */ |
| 201 | + bool isEmpty() { return this->size == 0; } |
| 202 | + |
| 203 | + /*! |
| 204 | + * @brief Compare two lists whether their attributes and entries are equal. |
| 205 | + * @note If you use this list for non-primitive data types, check if the |
| 206 | + * data type implements the != operator! |
| 207 | + * |
| 208 | + * @param list Second list to compare. |
| 209 | + * @return true if the lists are equal; false otherwise. |
| 210 | + */ |
| 211 | + bool equals(AbstractList<T> &list) { |
| 212 | + if (list.isMutable() != this->isMutable()) { |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + if (list.getSize() != this->getSize()) { |
| 217 | + return false; |
| 218 | + } |
| 219 | + |
| 220 | + for (int i = 0; i < this->getSize(); i++) { |
| 221 | + if (*list.get(i) != *this->get(i)) { |
| 222 | + return false; |
| 223 | + } |
| 224 | + } |
| 225 | + return true; |
| 226 | + } |
| 227 | + |
| 228 | + /*! |
| 229 | + * @brief Get a pointer to the entry at the given index. |
| 230 | + * @see get() |
| 231 | + * @see getValue() |
| 232 | + * |
| 233 | + * @param index Index of the element to get. |
| 234 | + * @return Pointer to the element. |
| 235 | + */ |
| 236 | + T *operator[](int index) { return get(index); } |
| 237 | + |
| 238 | + /*! |
| 239 | + * @brief Compare two lists whether their attributes and entries are equal. |
| 240 | + * @see equals() |
| 241 | + * |
| 242 | + * @param list Second list to compare. |
| 243 | + * @return true if the lists are equal; false otherwise. |
| 244 | + */ |
| 245 | + bool operator==(AbstractList<T> &list) { return equals(list); } |
| 246 | + |
| 247 | + /*! |
| 248 | + * @brief Add a new entry at the end of the list. |
| 249 | + * @see add() |
| 250 | + * @see addLast() |
| 251 | + * |
| 252 | + * @param value Value to add. |
| 253 | + */ |
| 254 | + void operator+(T &value) { this->addLast(value); } |
| 255 | + |
| 256 | + /*! |
| 257 | + * @brief Add all entries from the given list at the end of the list. |
| 258 | + * @see addAll() |
| 259 | + * |
| 260 | + * @param list Other list to copy from. |
| 261 | + */ |
| 262 | + void operator+(AbstractList<T> &list) { this->addAll(list); } |
| 263 | +}; |
| 264 | + |
| 265 | +#endif /* LIST_ABSTRACT_LIST_HPP */ |
0 commit comments