5
5
* easy-to-use list implementations. They are specially designed and optimized
6
6
* for different purposes.
7
7
*
8
- * Copyright (C) 2021-2022 Niklas Kaaf
8
+ * Copyright (C) 2021-2023 Niklas Kaaf
9
9
*
10
10
* This library is free software; you can redistribute it and/or
11
11
* modify it under the terms of the GNU Lesser General Public
@@ -138,6 +138,11 @@ template <typename T> class AbstractList {
138
138
*/
139
139
void add (T &value) { addLast (value); }
140
140
141
+ /* !
142
+ * @copydoc AbstractList::add()
143
+ */
144
+ void add (T &&value) { addLast (value); }
145
+
141
146
/* !
142
147
* @brief Add the value to the list at the given index. The original entry at
143
148
* this index, and followings, will be placed directly after the new
@@ -150,6 +155,14 @@ template <typename T> class AbstractList {
150
155
*/
151
156
virtual void addAtIndex (int index, T &value) = 0;
152
157
158
+ /* !
159
+ * @copydoc AbstractList::addAtIndex()
160
+ */
161
+ virtual void addAtIndex (int index, T &&value) {
162
+ T val = value;
163
+ addAtIndex (index, val);
164
+ }
165
+
153
166
/* !
154
167
* @brief Add all entries from the given list to this list at the given index.
155
168
* The original entry at this index, and followings, will be placed
@@ -177,20 +190,42 @@ template <typename T> class AbstractList {
177
190
*/
178
191
void addAll (AbstractList<T> &list) { addAll (getSize (), list); }
179
192
193
+ /* !
194
+ * @brief Add all entries from the given array
195
+ *
196
+ * @param arr Array
197
+ * @param size Size of array
198
+ */
199
+ void addAll (T *arr, size_t size) {
200
+ for (size_t i = 0 ; i < size; ++i) {
201
+ add (arr[i]);
202
+ }
203
+ }
204
+
180
205
/* !
181
206
* @brief Add a new entry at the beginning of the list.
182
207
*
183
208
* @param value Value to add.
184
209
*/
185
210
void addFirst (T &value) { addAtIndex (0 , value); }
186
211
212
+ /* !
213
+ * @copydoc AbstractList::addFirst()
214
+ */
215
+ void addFirst (T value) { addAtIndex (0 , value); }
216
+
187
217
/* !
188
218
* @brief Add a new entry at the end of the list.
189
219
*
190
220
* @param value Value to add.
191
221
*/
192
222
void addLast (T &value) { addAtIndex (getSize (), value); }
193
223
224
+ /* !
225
+ * @copydoc AbstractList::addLast()
226
+ */
227
+ void addLast (T &&value) { addAtIndex (getSize (), value); }
228
+
194
229
/* !
195
230
* @copydoc AbstractList::get()
196
231
*/
@@ -284,25 +319,51 @@ template <typename T> class AbstractList {
284
319
return arr;
285
320
}
286
321
322
+ /* !
323
+ * @brief Create the list from given array.
324
+ * @note Removes all entries in current list.
325
+ *
326
+ * @param arr Array
327
+ * @param size Size of Array
328
+ */
329
+ void fromArray (T *arr, size_t size) {
330
+ this ->clear ();
331
+ addAll (arr, size);
332
+ }
333
+
334
+ /* !
335
+ * @brief Sort the entries in the list with Quicksort.
336
+ *
337
+ * @param compFunc Comparator Method
338
+ */
339
+ void sort (int (*compFunc)(const void *, const void *)) {
340
+ T *arr = this ->toArray ();
341
+
342
+ qsort (arr, getSize (), sizeof (*arr), compFunc);
343
+
344
+ this ->fromArray (arr, getSize ());
345
+ free (arr);
346
+ }
347
+
287
348
/* !
288
349
* @brief Compare two lists whether their attributes and entries are equal.
289
350
* @note If you use this list for non-primitive data types, check if the
290
351
* data type implements the != operator!
291
352
*
292
- * @param list Second list to compare.
353
+ * @param other Second list to compare.
293
354
* @return true if the lists are equal; false otherwise.
294
355
*/
295
- bool equals (AbstractList<T> &list ) {
296
- if (list .isMutable () != isMutable ()) {
356
+ bool equals (AbstractList<T> &other ) {
357
+ if (other .isMutable () != isMutable ()) {
297
358
return false ;
298
359
}
299
360
300
- if (list .getSize () != getSize ()) {
361
+ if (other .getSize () != getSize ()) {
301
362
return false ;
302
363
}
303
364
304
365
for (int i = 0 ; i < getSize (); i++) {
305
- if (list .getValue (i) != getValue (i)) {
366
+ if (other .getValue (i) != getValue (i)) {
306
367
return false ;
307
368
}
308
369
}
@@ -319,14 +380,29 @@ template <typename T> class AbstractList {
319
380
* @copydoc AbstractList::equals()
320
381
* @see equals()
321
382
*/
322
- bool operator ==(AbstractList<T> &list) { return equals (list); }
383
+ bool operator ==(AbstractList<T> &other) { return equals (other); }
384
+
385
+ /* !
386
+ * @brief Opposite of '=='
387
+ * @see equals()
388
+ *
389
+ * @param other Other list to compare
390
+ * @return true if the lists are not equal; false otherwise.
391
+ */
392
+ bool operator !=(AbstractList<T> &other) { return !(*this == other); }
323
393
324
394
/* !
325
395
* @copydoc AbstractList::add()
326
396
* @see add()
327
397
*/
328
398
void operator +(T &value) { this ->add (value); }
329
399
400
+ /* !
401
+ * @copydoc AbstractList::add()
402
+ * @see add()
403
+ */
404
+ void operator +(T &&value) { this ->add (value); }
405
+
330
406
/* !
331
407
* @copydoc AbstractList::addAll(AbstractList<T>&)
332
408
* @see addAll(AbstractList<T>&)
0 commit comments