Skip to content

Commit

Permalink
Improvements and additions for 2.1.4 (#36)
Browse files Browse the repository at this point in the history
- improve "add" functions to handle rvalues
- add sort (quicksort)
- add fromArray
- add != operator
- add + operator for rvalues
  • Loading branch information
nkaaf committed Jun 10, 2023
1 parent e754124 commit 5cf846b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 13 deletions.
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
# CLion
.idea

# CMake
CMakeLists.txt
cmake-build-*

# gh_pages files
doxygen_sqlite3.db
html
html

# VSCode
.vscode

# PlatformIO for testing
.pio
test
lib
src/main.cpp
include
platformio.ini
CMakeListsPrivate.txt
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "Arduino List Library"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 2.1.3
PROJECT_NUMBER = 2.1.4

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ https://semver.org/ compliant

* https://docs.arduino.cc/learn/contributions/arduino-writing-style-guide
* https://docs.arduino.cc/learn/contributions/arduino-library-style-guide
* https://arduino.github.io/arduino-cli/0.29/library-specification/
* https://arduino.github.io/arduino-cli/0.29/sketch-specification/
* https://arduino.github.io/arduino-cli/0.33/library-specification/
* https://arduino.github.io/arduino-cli/0.33/sketch-specification/
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ getSize KEYWORD2
isMutable KEYWORD2
isEmpty KEYWORD2
toArray KEYWORD2
fromArray KEYWORD2
sort KEYWORD2
equals KEYWORD2
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "List",
"version": "2.1.3",
"version": "2.1.4",
"description": "The Ultimate Collection of Lists. This library extends the Arduino ecosystem with the functionality of several easy-to-use lists for numerous purposes.",
"keywords": [
"arduino",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=List
version=2.1.3
version=2.1.4
author=Niklas Kaaf <[email protected]>
maintainer=Niklas Kaaf <[email protected]>
sentence=The Ultimate Collection of Lists
Expand Down
90 changes: 83 additions & 7 deletions src/AbstractList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* easy-to-use list implementations. They are specially designed and optimized
* for different purposes.
*
* Copyright (C) 2021-2022 Niklas Kaaf
* Copyright (C) 2021-2023 Niklas Kaaf
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -138,6 +138,11 @@ template <typename T> class AbstractList {
*/
void add(T &value) { addLast(value); }

/*!
* @copydoc AbstractList::add()
*/
void add(T &&value) { addLast(value); }

/*!
* @brief Add the value to the list at the given index. The original entry at
* this index, and followings, will be placed directly after the new
Expand All @@ -150,6 +155,14 @@ template <typename T> class AbstractList {
*/
virtual void addAtIndex(int index, T &value) = 0;

/*!
* @copydoc AbstractList::addAtIndex()
*/
virtual void addAtIndex(int index, T &&value) {
T val = value;
addAtIndex(index, val);
}

/*!
* @brief Add all entries from the given list to this list at the given index.
* The original entry at this index, and followings, will be placed
Expand Down Expand Up @@ -177,20 +190,42 @@ template <typename T> class AbstractList {
*/
void addAll(AbstractList<T> &list) { addAll(getSize(), list); }

/*!
* @brief Add all entries from the given array
*
* @param arr Array
* @param size Size of array
*/
void addAll(T *arr, size_t size) {
for (size_t i = 0; i < size; ++i) {
add(arr[i]);
}
}

/*!
* @brief Add a new entry at the beginning of the list.
*
* @param value Value to add.
*/
void addFirst(T &value) { addAtIndex(0, value); }

/*!
* @copydoc AbstractList::addFirst()
*/
void addFirst(T value) { addAtIndex(0, value); }

/*!
* @brief Add a new entry at the end of the list.
*
* @param value Value to add.
*/
void addLast(T &value) { addAtIndex(getSize(), value); }

/*!
* @copydoc AbstractList::addLast()
*/
void addLast(T &&value) { addAtIndex(getSize(), value); }

/*!
* @copydoc AbstractList::get()
*/
Expand Down Expand Up @@ -284,25 +319,51 @@ template <typename T> class AbstractList {
return arr;
}

/*!
* @brief Create the list from given array.
* @note Removes all entries in current list.
*
* @param arr Array
* @param size Size of Array
*/
void fromArray(T *arr, size_t size) {
this->clear();
addAll(arr, size);
}

/*!
* @brief Sort the entries in the list with Quicksort.
*
* @param compFunc Comparator Method
*/
void sort(int (*compFunc)(const void *, const void *)) {
T *arr = this->toArray();

qsort(arr, getSize(), sizeof(*arr), compFunc);

this->fromArray(arr, getSize());
free(arr);
}

/*!
* @brief Compare two lists whether their attributes and entries are equal.
* @note If you use this list for non-primitive data types, check if the
* data type implements the != operator!
*
* @param list Second list to compare.
* @param other Second list to compare.
* @return true if the lists are equal; false otherwise.
*/
bool equals(AbstractList<T> &list) {
if (list.isMutable() != isMutable()) {
bool equals(AbstractList<T> &other) {
if (other.isMutable() != isMutable()) {
return false;
}

if (list.getSize() != getSize()) {
if (other.getSize() != getSize()) {
return false;
}

for (int i = 0; i < getSize(); i++) {
if (list.getValue(i) != getValue(i)) {
if (other.getValue(i) != getValue(i)) {
return false;
}
}
Expand All @@ -319,14 +380,29 @@ template <typename T> class AbstractList {
* @copydoc AbstractList::equals()
* @see equals()
*/
bool operator==(AbstractList<T> &list) { return equals(list); }
bool operator==(AbstractList<T> &other) { return equals(other); }

/*!
* @brief Opposite of '=='
* @see equals()
*
* @param other Other list to compare
* @return true if the lists are not equal; false otherwise.
*/
bool operator!=(AbstractList<T> &other) { return !(*this == other); }

/*!
* @copydoc AbstractList::add()
* @see add()
*/
void operator+(T &value) { this->add(value); }

/*!
* @copydoc AbstractList::add()
* @see add()
*/
void operator+(T &&value) { this->add(value); }

/*!
* @copydoc AbstractList::addAll(AbstractList<T>&)
* @see addAll(AbstractList<T>&)
Expand Down

0 comments on commit 5cf846b

Please sign in to comment.