Skip to content

Commit 5cf846b

Browse files
authored
Improvements and additions for 2.1.4 (#36)
- improve "add" functions to handle rvalues - add sort (quicksort) - add fromArray - add != operator - add + operator for rvalues
1 parent e754124 commit 5cf846b

File tree

7 files changed

+107
-13
lines changed

7 files changed

+107
-13
lines changed

.gitignore

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
# CLion
12
.idea
3+
4+
# CMake
5+
CMakeLists.txt
26
cmake-build-*
37

48
# gh_pages files
59
doxygen_sqlite3.db
6-
html
10+
html
11+
12+
# VSCode
13+
.vscode
14+
15+
# PlatformIO for testing
16+
.pio
17+
test
18+
lib
19+
src/main.cpp
20+
include
21+
platformio.ini
22+
CMakeListsPrivate.txt

Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_NAME = "Arduino List Library"
3838
# could be handy for archiving the generated documentation or if some version
3939
# control system is used.
4040

41-
PROJECT_NUMBER = 2.1.3
41+
PROJECT_NUMBER = 2.1.4
4242

4343
# Using the PROJECT_BRIEF tag one can provide an optional one line description
4444
# for a project that appears at the top of each page and should give viewer a

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ https://semver.org/ compliant
1313

1414
* https://docs.arduino.cc/learn/contributions/arduino-writing-style-guide
1515
* https://docs.arduino.cc/learn/contributions/arduino-library-style-guide
16-
* https://arduino.github.io/arduino-cli/0.29/library-specification/
17-
* https://arduino.github.io/arduino-cli/0.29/sketch-specification/
16+
* https://arduino.github.io/arduino-cli/0.33/library-specification/
17+
* https://arduino.github.io/arduino-cli/0.33/sketch-specification/

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ getSize KEYWORD2
3131
isMutable KEYWORD2
3232
isEmpty KEYWORD2
3333
toArray KEYWORD2
34+
fromArray KEYWORD2
35+
sort KEYWORD2
3436
equals KEYWORD2

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "List",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"description": "The Ultimate Collection of Lists. This library extends the Arduino ecosystem with the functionality of several easy-to-use lists for numerous purposes.",
55
"keywords": [
66
"arduino",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=List
2-
version=2.1.3
2+
version=2.1.4
33
author=Niklas Kaaf <nkaaf@protonmail.com>
44
maintainer=Niklas Kaaf <nkaaf@protonmail.com>
55
sentence=The Ultimate Collection of Lists

src/AbstractList.hpp

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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

Comments
 (0)