55 * easy-to-use list implementations. They are specially designed and optimized
66 * for different purposes.
77 *
8- * Copyright (C) 2021-2022 Niklas Kaaf
8+ * Copyright (C) 2021-2023 Niklas Kaaf
99 *
1010 * This library is free software; you can redistribute it and/or
1111 * modify it under the terms of the GNU Lesser General Public
@@ -138,6 +138,11 @@ template <typename T> class AbstractList {
138138 */
139139 void add (T &value) { addLast (value); }
140140
141+ /* !
142+ * @copydoc AbstractList::add()
143+ */
144+ void add (T &&value) { addLast (value); }
145+
141146 /* !
142147 * @brief Add the value to the list at the given index. The original entry at
143148 * this index, and followings, will be placed directly after the new
@@ -150,6 +155,14 @@ template <typename T> class AbstractList {
150155 */
151156 virtual void addAtIndex (int index, T &value) = 0;
152157
158+ /* !
159+ * @copydoc AbstractList::addAtIndex()
160+ */
161+ virtual void addAtIndex (int index, T &&value) {
162+ T val = value;
163+ addAtIndex (index, val);
164+ }
165+
153166 /* !
154167 * @brief Add all entries from the given list to this list at the given index.
155168 * The original entry at this index, and followings, will be placed
@@ -177,20 +190,42 @@ template <typename T> class AbstractList {
177190 */
178191 void addAll (AbstractList<T> &list) { addAll (getSize (), list); }
179192
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+
180205 /* !
181206 * @brief Add a new entry at the beginning of the list.
182207 *
183208 * @param value Value to add.
184209 */
185210 void addFirst (T &value) { addAtIndex (0 , value); }
186211
212+ /* !
213+ * @copydoc AbstractList::addFirst()
214+ */
215+ void addFirst (T value) { addAtIndex (0 , value); }
216+
187217 /* !
188218 * @brief Add a new entry at the end of the list.
189219 *
190220 * @param value Value to add.
191221 */
192222 void addLast (T &value) { addAtIndex (getSize (), value); }
193223
224+ /* !
225+ * @copydoc AbstractList::addLast()
226+ */
227+ void addLast (T &&value) { addAtIndex (getSize (), value); }
228+
194229 /* !
195230 * @copydoc AbstractList::get()
196231 */
@@ -284,25 +319,51 @@ template <typename T> class AbstractList {
284319 return arr;
285320 }
286321
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+
287348 /* !
288349 * @brief Compare two lists whether their attributes and entries are equal.
289350 * @note If you use this list for non-primitive data types, check if the
290351 * data type implements the != operator!
291352 *
292- * @param list Second list to compare.
353+ * @param other Second list to compare.
293354 * @return true if the lists are equal; false otherwise.
294355 */
295- bool equals (AbstractList<T> &list ) {
296- if (list .isMutable () != isMutable ()) {
356+ bool equals (AbstractList<T> &other ) {
357+ if (other .isMutable () != isMutable ()) {
297358 return false ;
298359 }
299360
300- if (list .getSize () != getSize ()) {
361+ if (other .getSize () != getSize ()) {
301362 return false ;
302363 }
303364
304365 for (int i = 0 ; i < getSize (); i++) {
305- if (list .getValue (i) != getValue (i)) {
366+ if (other .getValue (i) != getValue (i)) {
306367 return false ;
307368 }
308369 }
@@ -319,14 +380,29 @@ template <typename T> class AbstractList {
319380 * @copydoc AbstractList::equals()
320381 * @see equals()
321382 */
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); }
323393
324394 /* !
325395 * @copydoc AbstractList::add()
326396 * @see add()
327397 */
328398 void operator +(T &value) { this ->add (value); }
329399
400+ /* !
401+ * @copydoc AbstractList::add()
402+ * @see add()
403+ */
404+ void operator +(T &&value) { this ->add (value); }
405+
330406 /* !
331407 * @copydoc AbstractList::addAll(AbstractList<T>&)
332408 * @see addAll(AbstractList<T>&)
0 commit comments