27
27
#define LIST_ABSTRACT_LIST_HPP
28
28
29
29
#include < stdlib.h>
30
+ #include < string.h>
30
31
31
32
/* !
32
33
* @brief Abstract class from which all lists can be derived.
35
36
*/
36
37
template <typename T> class AbstractList {
37
38
private:
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.
40
41
41
42
protected:
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
+
42
81
/* !
43
82
* @brief Constructor of an AbstractList Object.
44
83
*
@@ -50,13 +89,13 @@ template <typename T> class AbstractList {
50
89
* @brief Increase the size of the list by one. Should only be called after an
51
90
* insertion!
52
91
*/
53
- void increaseSize () { this -> size ++; }
92
+ void increaseSize () { size++; }
54
93
55
94
/* !
56
95
* @brief Decrease the size of the list by one. Should only be called after an
57
96
* deletion!
58
97
*/
59
- void decreaseSize () { this -> size --; }
98
+ void decreaseSize () { size--; }
60
99
61
100
/* !
62
101
* @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 {
66
105
* @return true if the given index is in the range of the list; false
67
106
* otherwise
68
107
*/
69
- bool isIndexOutOfBounds (int index) {
70
- return index < 0 || index >= this ->getSize ();
71
- }
108
+ bool isIndexOutOfBounds (int index) { return index < 0 || index >= getSize (); }
72
109
73
110
/* !
74
111
* @brief Get a pointer to the entry at the given index. If the given index
@@ -82,6 +119,14 @@ template <typename T> class AbstractList {
82
119
virtual T *get (int index) = 0;
83
120
84
121
public:
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
+
85
130
/* !
86
131
* @brief Add the value to the list at the given index. The original entry at
87
132
* this index, and followings, will be placed directly after the new
@@ -95,28 +140,47 @@ template <typename T> class AbstractList {
95
140
virtual void addAtIndex (int index, T &value) = 0;
96
141
97
142
/* !
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.
99
146
*
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.
101
149
*/
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
+ }
103
161
104
162
/* !
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()
108
165
*
109
- * @param index Index of element to get.
110
- * @return Value.
166
+ * @param list Other list to copy from.
111
167
*/
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); }
120
184
121
185
/* !
122
186
* @brief Get a pointer to the entry at the given index. If the given index
@@ -130,55 +194,49 @@ template <typename T> class AbstractList {
130
194
T *getPointer (int index) { return get (index); }
131
195
132
196
/* !
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!
135
200
*
136
- * @param value Value to add.
201
+ * @param index Index of element to get.
202
+ * @return Value.
137
203
*/
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
+ }
139
212
140
213
/* !
141
- * @brief Add a new entry at the beginning of the list .
214
+ * @brief Remove the entry at the given index .
142
215
*
143
- * @param value Value to add .
216
+ * @param index Index of element to remove .
144
217
*/
145
- void addFirst (T &value) { this -> addAtIndex ( 0 , value); }
218
+ virtual void remove ( int index) = 0;
146
219
147
220
/* !
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.
150
222
*
151
- * @param value Value to add .
223
+ * @return Size of the list .
152
224
*/
153
- void add (T &value ) { this -> addLast (value) ; }
225
+ int getSize ( ) { return size ; }
154
226
155
227
/* !
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.
158
229
*
159
- * @param list Other list to copy from .
230
+ * @return true if the list is mutable; false otherwise .
160
231
*/
161
- void addAll (AbstractList<T> &list ) { this -> addAll ( this -> getSize (), list) ; }
232
+ bool isMutable ( ) { return mutableList ; }
162
233
163
234
/* !
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.
167
236
*
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
170
238
*/
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 ; }
182
240
183
241
/* !
184
242
* @brief Get an array which represent the list.
@@ -188,40 +246,19 @@ template <typename T> class AbstractList {
188
246
* @return Array representation of the list or null if the list is empty.
189
247
*/
190
248
T *toArray () {
191
- if (this -> getSize () == 0 ) {
249
+ if (getSize () == 0 ) {
192
250
return nullptr ;
193
251
}
194
252
195
- T *arr = (T *)malloc (this -> getSize () * sizeof (T));
253
+ T *arr = (T *)malloc (getSize () * sizeof (T));
196
254
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);
199
257
}
200
258
201
259
return arr;
202
260
}
203
261
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
-
225
262
/* !
226
263
* @brief Compare two lists whether their attributes and entries are equal.
227
264
* @note If you use this list for non-primitive data types, check if the
@@ -231,16 +268,16 @@ template <typename T> class AbstractList {
231
268
* @return true if the lists are equal; false otherwise.
232
269
*/
233
270
bool equals (AbstractList<T> &list) {
234
- if (list.isMutable () != this -> isMutable ()) {
271
+ if (list.isMutable () != isMutable ()) {
235
272
return false ;
236
273
}
237
274
238
- if (list.getSize () != this -> getSize ()) {
275
+ if (list.getSize () != getSize ()) {
239
276
return false ;
240
277
}
241
278
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)) {
244
281
return false ;
245
282
}
246
283
}
@@ -272,7 +309,7 @@ template <typename T> class AbstractList {
272
309
*
273
310
* @param value Value to add.
274
311
*/
275
- void operator +(T &value) { this ->addLast (value); }
312
+ void operator +(T &value) { this ->add (value); }
276
313
277
314
/* !
278
315
* @brief Add all entries from the given list at the end of the list.
@@ -283,4 +320,4 @@ template <typename T> class AbstractList {
283
320
void operator +(AbstractList<T> &list) { this ->addAll (list); }
284
321
};
285
322
286
- #endif /* LIST_ABSTRACT_LIST_HPP */
323
+ #endif // LIST_ABSTRACT_LIST_HPP
0 commit comments