2727#define LIST_ABSTRACT_LIST_HPP
2828
2929#include < stdlib.h>
30+ #include < string.h>
3031
3132/* !
3233 * @brief Abstract class from which all lists can be derived.
3536 */
3637template <typename T> class AbstractList {
3738private:
38- int size = 0 ; // / Size of the list
39- bool mutableList = true ; // / Is the list mutable or immutable
39+ int size = 0 ; // / Size of the list.
40+ bool mutableList = true ; // / Is the list mutable or immutable.
4041
4142protected:
43+ // / Sometimes it is allowed, that index == this->getSize() to insert it behind
44+ // / the last entry
45+ #define extendedIsIndexOutOfBounds (index ) \
46+ ((index) != this ->getSize () && this->isIndexOutOfBounds(index))
47+
48+ // / Create a final Value from the given Value
49+ #define createFinalValue (value, finalValue, T ) \
50+ finalValue = (T *)malloc(sizeof (T)); \
51+ memcpy (finalValue, &(value), sizeof (T));
52+
53+ /* *
54+ * Class representing an abstract entry in the list.
55+ */
56+ class AbstractEntry {
57+ private:
58+ T *value = nullptr ; // / Pointer to the value.
59+
60+ public:
61+ /* !
62+ * @brief Constructor of an AbstractEntry Object.
63+ *
64+ * @param value Value of the entry.
65+ */
66+ explicit AbstractEntry (T *value) : value(value) {}
67+
68+ /* !
69+ * @brief Free the memory of the value to prevent memory leaks.
70+ */
71+ void freeValue () { free (value); }
72+
73+ /* !
74+ * @brief Get the value of the entry.
75+ *
76+ * @return Pointer to the value of the entry.
77+ */
78+ T *getValue () { return value; };
79+ };
80+
4281 /* !
4382 * @brief Constructor of an AbstractList Object.
4483 *
@@ -50,13 +89,13 @@ template <typename T> class AbstractList {
5089 * @brief Increase the size of the list by one. Should only be called after an
5190 * insertion!
5291 */
53- void increaseSize () { this -> size ++; }
92+ void increaseSize () { size++; }
5493
5594 /* !
5695 * @brief Decrease the size of the list by one. Should only be called after an
5796 * deletion!
5897 */
59- void decreaseSize () { this -> size --; }
98+ void decreaseSize () { size--; }
6099
61100 /* !
62101 * @brief Method to verify if the given index is out of the range of the list
@@ -66,9 +105,7 @@ template <typename T> class AbstractList {
66105 * @return true if the given index is in the range of the list; false
67106 * otherwise
68107 */
69- bool isIndexOutOfBounds (int index) {
70- return index < 0 || index >= this ->getSize ();
71- }
108+ bool isIndexOutOfBounds (int index) { return index < 0 || index >= getSize (); }
72109
73110 /* !
74111 * @brief Get a pointer to the entry at the given index. If the given index
@@ -82,6 +119,14 @@ template <typename T> class AbstractList {
82119 virtual T *get (int index) = 0;
83120
84121public:
122+ /* !
123+ * @brief Add a new entry at the end of the list.
124+ * @see addLast()
125+ *
126+ * @param value Value to add.
127+ */
128+ void add (T &value) { addLast (value); }
129+
85130 /* !
86131 * @brief Add the value to the list at the given index. The original entry at
87132 * this index, and followings, will be placed directly after the new
@@ -95,28 +140,47 @@ template <typename T> class AbstractList {
95140 virtual void addAtIndex (int index, T &value) = 0;
96141
97142 /* !
98- * @brief Remove the entry at the given index.
143+ * @brief Add all entries from the given list to this list at the given index.
144+ * The original entry at this index, and followings, will be placed
145+ * directly after the entries of the given list.
99146 *
100- * @param index Index of element to remove.
147+ * @param index Index, at which the list should be added.
148+ * @param list List to add.
101149 */
102- virtual void remove (int index) = 0;
150+ void addAll (int index, AbstractList<T> &list) {
151+ for (int i = 0 ; i < list.getSize (); i++) {
152+ T val = list.getValue (i);
153+ T *finalValue;
154+ createFinalValue (val, finalValue, T);
155+ addAtIndex (index++, *finalValue);
156+ if (!this ->isMutable ()) {
157+ free (finalValue);
158+ }
159+ }
160+ }
103161
104162 /* !
105- * @brief Get the value at the index.
106- * @note Be safe, that the index exists otherwise the program will crash
107- * here!
163+ * @brief Add all entries from the given list at the end of the list.
164+ * @see addLast()
108165 *
109- * @param index Index of element to get.
110- * @return Value.
166+ * @param list Other list to copy from.
111167 */
112- T getValue (int index) {
113- T *ptr = getPointer (index);
114- T val = *ptr;
115- if (!this ->isMutable ()) {
116- free (ptr);
117- }
118- return val;
119- }
168+ void addAll (AbstractList<T> &list) { addAll (getSize (), list); }
169+
170+ /* !
171+ * @brief Add a new entry at the beginning of the list.
172+ *
173+ * @param value Value to add.
174+ */
175+ void addFirst (T &value) { addAtIndex (0 , value); }
176+
177+ /* !
178+ * @brief Add a new entry at the end of the list.
179+ * @see add()
180+ *
181+ * @param value Value to add.
182+ */
183+ void addLast (T &value) { addAtIndex (getSize (), value); }
120184
121185 /* !
122186 * @brief Get a pointer to the entry at the given index. If the given index
@@ -130,55 +194,49 @@ template <typename T> class AbstractList {
130194 T *getPointer (int index) { return get (index); }
131195
132196 /* !
133- * @brief Add a new entry at the end of the list.
134- * @see add()
197+ * @brief Get the value at the index.
198+ * @note Be safe, that the index exists otherwise the program will crash
199+ * here!
135200 *
136- * @param value Value to add.
201+ * @param index Index of element to get.
202+ * @return Value.
137203 */
138- void addLast (T &value) { this ->addAtIndex (this ->getSize (), value); }
204+ T getValue (int index) {
205+ T *ptr = getPointer (index);
206+ T val = *ptr;
207+ if (!isMutable ()) {
208+ free (ptr);
209+ }
210+ return val;
211+ }
139212
140213 /* !
141- * @brief Add a new entry at the beginning of the list .
214+ * @brief Remove the entry at the given index .
142215 *
143- * @param value Value to add .
216+ * @param index Index of element to remove .
144217 */
145- void addFirst (T &value) { this -> addAtIndex ( 0 , value); }
218+ virtual void remove ( int index) = 0;
146219
147220 /* !
148- * @brief Add a new entry at the end of the list.
149- * @see addLast()
221+ * @brief Get the number how many elements are saved in the list.
150222 *
151- * @param value Value to add .
223+ * @return Size of the list .
152224 */
153- void add (T &value ) { this -> addLast (value) ; }
225+ int getSize ( ) { return size ; }
154226
155227 /* !
156- * @brief Add all entries from the given list at the end of the list.
157- * @see addLast()
228+ * @brief Check if the list is mutable.
158229 *
159- * @param list Other list to copy from .
230+ * @return true if the list is mutable; false otherwise .
160231 */
161- void addAll (AbstractList<T> &list ) { this -> addAll ( this -> getSize (), list) ; }
232+ bool isMutable ( ) { return mutableList ; }
162233
163234 /* !
164- * @brief Add all entries from the given list to this list at the given index.
165- * The original entry at this index, and followings, will be placed
166- * directly after the entries of the given list.
235+ * @brief Check if the list is empty.
167236 *
168- * @param index Index, at which the list should be added.
169- * @param list List to add.
237+ * @return true if the list is empty; false otherwise
170238 */
171- void addAll (int index, AbstractList<T> &list) {
172- for (int i = 0 ; i < list.getSize (); i++) {
173- T val = list.getValue (i);
174- T *finalValue = (T *)malloc (sizeof (T));
175- memcpy (finalValue, &val, sizeof (T));
176- this ->addAtIndex (index++, *finalValue);
177- if (!this ->isMutable ()) {
178- free (finalValue);
179- }
180- }
181- }
239+ bool isEmpty () { return size == 0 ; }
182240
183241 /* !
184242 * @brief Get an array which represent the list.
@@ -188,40 +246,19 @@ template <typename T> class AbstractList {
188246 * @return Array representation of the list or null if the list is empty.
189247 */
190248 T *toArray () {
191- if (this -> getSize () == 0 ) {
249+ if (getSize () == 0 ) {
192250 return nullptr ;
193251 }
194252
195- T *arr = (T *)malloc (this -> getSize () * sizeof (T));
253+ T *arr = (T *)malloc (getSize () * sizeof (T));
196254
197- for (int i = 0 ; i < this -> getSize (); ++i) {
198- arr[i] = this -> getValue (i);
255+ for (int i = 0 ; i < getSize (); ++i) {
256+ arr[i] = getValue (i);
199257 }
200258
201259 return arr;
202260 }
203261
204- /* !
205- * @brief Get the number how many elements are saved in the list.
206- *
207- * @return Size of the list.
208- */
209- int getSize () { return this ->size ; }
210-
211- /* !
212- * @brief Check if the list is mutable.
213- *
214- * @return true if the list is mutable; false otherwise.
215- */
216- bool isMutable () { return mutableList; }
217-
218- /* !
219- * @brief Check if the list is empty.
220- *
221- * @return true if the list is empty; false otherwise
222- */
223- bool isEmpty () { return this ->size == 0 ; }
224-
225262 /* !
226263 * @brief Compare two lists whether their attributes and entries are equal.
227264 * @note If you use this list for non-primitive data types, check if the
@@ -231,16 +268,16 @@ template <typename T> class AbstractList {
231268 * @return true if the lists are equal; false otherwise.
232269 */
233270 bool equals (AbstractList<T> &list) {
234- if (list.isMutable () != this -> isMutable ()) {
271+ if (list.isMutable () != isMutable ()) {
235272 return false ;
236273 }
237274
238- if (list.getSize () != this -> getSize ()) {
275+ if (list.getSize () != getSize ()) {
239276 return false ;
240277 }
241278
242- for (int i = 0 ; i < this -> getSize (); i++) {
243- if (list.getValue (i) != this -> getValue (i)) {
279+ for (int i = 0 ; i < getSize (); i++) {
280+ if (list.getValue (i) != getValue (i)) {
244281 return false ;
245282 }
246283 }
@@ -272,7 +309,7 @@ template <typename T> class AbstractList {
272309 *
273310 * @param value Value to add.
274311 */
275- void operator +(T &value) { this ->addLast (value); }
312+ void operator +(T &value) { this ->add (value); }
276313
277314 /* !
278315 * @brief Add all entries from the given list at the end of the list.
@@ -283,4 +320,4 @@ template <typename T> class AbstractList {
283320 void operator +(AbstractList<T> &list) { this ->addAll (list); }
284321};
285322
286- #endif /* LIST_ABSTRACT_LIST_HPP */
323+ #endif // LIST_ABSTRACT_LIST_HPP
0 commit comments