Skip to content

Commit 67201de

Browse files
committed
change default mutable state; small rework on getting values/pointers
1 parent 93db950 commit 67201de

File tree

9 files changed

+91
-67
lines changed

9 files changed

+91
-67
lines changed

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 = 1.0.1
41+
PROJECT_NUMBER = 2.0.0
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

examples/List/DifferenceMutableAndImmutable/DifferenceMutableAndImmutable.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <List.hpp>
22

33
// Create a mutable list
4-
List<int> mutableList;
4+
List<int> mutableList(true);
55
// Create an immutable list
6-
List<int> immutableList(false);
6+
List<int> immutableList;
77

88
void setup() {
99
Serial.begin(9600);

examples/List/ManageElements/ManageElements.ino

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <List.hpp>
22

3-
// Create a mutable list
3+
// Create an immutable list
44
List<int> list;
55

66
void setup() {
@@ -14,20 +14,21 @@ void setup() {
1414
Serial.println();
1515

1616
// Get the first element
17-
Serial.print("The value of the first element with the get() method and '*' is: ");
18-
int firstElement = *list.get(0); // Here, have to be the '*' to get the int Value, because otherwise a pointer (memory address) will be get.
19-
Serial.println(firstElement);
17+
Serial.print("The value of the first element with the getValue() method is: ");
18+
Serial.println(list.getValue(0)); // The most comfortable way to get the first elements value is to call the getValue() method. You cannot get the address of the element with the getValue() method!
2019

2120
// Get the first element (alternative)
2221
Serial.print("The value of the first element with the [] operator is: ");
23-
firstElement = *list[0]; // The '[]' Operator is a synonym for the get() method.
22+
int firstElement = list[0]; // The '[]' Operator is a synonym for the getValue() method.
2423
Serial.println(firstElement);
2524

26-
// Get only the value of the first element
27-
Serial.print("The value of the first element with the getValue() method is: ");
28-
Serial.println(list.getValue(0)); // The more comfortable way to get only the first elements value is to call the getValue() method. You cannot get the address of the element with the getValue() method!
25+
// Get the first element (alternative)
26+
Serial.print("The value of the first element with the getPointer() method and '*' is: ");
27+
int *firstElementPtr = list.getPointer(0); // Here, have to be the '*' to get the int Value, because otherwise a pointer (memory address) will be returned.
28+
Serial.println(*firstElementPtr);
29+
free(firstElementPtr); // free the pointer because it is an immutable list
2930

30-
Serial.println("As you can see, there are three possible ways to get the value. Choose your favorite one :)");
31+
Serial.println("As you can see, there are three possible ways to get the value. The last way is not for beginners because you have to handle pointers.");
3132
Serial.println();
3233

3334
// Get the size of the list

examples/List/UtilsAndHelper/UtilsAndHelper.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <List.hpp>
22

3-
// Create a mutable list
3+
// Create an immutable list
44
List<int> list;
55

66
void setup() {
@@ -45,6 +45,8 @@ void setup() {
4545
}
4646
}
4747
Serial.println("]");
48+
// free memory space generated by toArray(), because it is not used anymore
49+
free(array);
4850

4951
Serial.println();
5052

@@ -78,10 +80,10 @@ void setup() {
7880
Serial.println("Add all elements of the second list to the first list.");
7981
list.addAll(secondList);
8082

81-
// Print every element from the new array
82-
Serial.print("The list contains the following values which are get by converting the list to an array: [");
83+
// Print every element from the combined list
84+
Serial.print("The list contains the following values: [");
8385
for (int i = 0; i < list.getSize(); ++i) {
84-
Serial.print(*list[i]);
86+
Serial.print(list[i]);
8587
if (i != list.getSize() - 1) { // only add the ',' if the element is not the last; otherwise the output would be: [2,2,]; but now it is [2,2]
8688
Serial.print(",");
8789
}

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ SingleLinkedList KEYWORD1
1616

1717
addAtIndex KEYWORD2
1818
remove KEYWORD2
19-
get KEYWORD2
2019
getValue KEYWORD2
20+
getPointer KEYWORD2
2121
addLast KEYWORD2
2222
addFirst KEYWORD2
2323
add KEYWORD2

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=1.0.1
2+
version=2.0.0
33
author=Niklas Kaaf <[email protected]>
44
maintainer=Niklas Kaaf <[email protected]>
55
sentence=The Ultimate Collection of Lists

src/AbstractList.hpp

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#ifndef LIST_ABSTRACT_LIST_HPP
2727
#define LIST_ABSTRACT_LIST_HPP
2828

29+
#include <stdlib.h>
30+
2931
/*!
3032
* @brief Abstract class from which all lists can be derived.
3133
*
@@ -68,6 +70,17 @@ template <typename T> class AbstractList {
6870
return index < 0 || index >= this->getSize();
6971
}
7072

73+
/*!
74+
* @brief Get a pointer to the entry at the given index. If the given index
75+
* does not exists, null will be returned.
76+
* @note If the list is immutable, the returned pointer has to be free'd with
77+
* free() in order to prevent memory leaks.
78+
*
79+
* @param index Index of the element to get.
80+
* @return Pointer to the element.
81+
*/
82+
virtual T *get(int index) = 0;
83+
7184
public:
7285
/*!
7386
* @brief Add the value to the list at the given index. The original entry at
@@ -89,27 +102,31 @@ template <typename T> class AbstractList {
89102
virtual void remove(int index) = 0;
90103

91104
/*!
92-
* @brief Get a pointer to the entry at the given index. If the given index
93-
* does not exists in the list or the list is immutable, null will be
94-
* returned.
95-
* @note If you only want to get the value, use getValue().
105+
* @brief Get the value at the index.
106+
* @note Be safe, that the index exists otherwise the program will crash here!
96107
*
97-
* @param index Index of the element to get.
98-
* @return Pointer to the element.
108+
* @param index Index of element to get.
109+
* @return Value.
99110
*/
100-
virtual T *get(int index) = 0;
111+
T getValue(int index) {
112+
T* ptr = getPointer(index);
113+
T val = *ptr;
114+
if (!this->isMutable()) {
115+
free(ptr);
116+
}
117+
return val;
118+
}
101119

102120
/*!
103-
* @brief Get the plain value at the given index.
104-
* @note Be safe, that the given index exists and the list is mutable,
105-
* otherwise the program will crash here!
106-
* @see get()
107-
* @todo Rewrite this to let the program not crash!
121+
* @brief Get a pointer to the entry at the given index. If the given index
122+
* does not exists, null will be returned.
123+
* @note If the list is immutable, the returned pointer has to be free'd with
124+
* free() in order to prevent memory leaks.
108125
*
109126
* @param index Index of element to get.
110-
* @return Value.
127+
* @return Pointer to the element.
111128
*/
112-
T getValue(int index) { return *get(index); }
129+
T *getPointer(int index) { return get(index); }
113130

114131
/*!
115132
* @brief Add a new entry at the end of the list.
@@ -141,25 +158,26 @@ template <typename T> class AbstractList {
141158
* @param list Other list to copy from.
142159
*/
143160
void addAll(AbstractList<T> &list) {
144-
for (int i = 0; i < list.getSize(); ++i) {
145-
this->addLast(*list.get(i));
146-
}
161+
this->addAll(this->getSize(), list);
147162
}
148163

149164
/*!
150165
* @brief Add all entries from the given list to this list at the given index.
151166
* The original entry at this index, and followings, will be placed
152167
* directly after the entries of the given list.
153-
* @see addAtIndex()
154-
* @note Use this only if you know what you are doing. Otherwise use
155-
* addAll().
156168
*
157169
* @param index Index, at which the list should be added.
158170
* @param list List to add.
159171
*/
160172
void addAll(int index, AbstractList<T> &list) {
161173
for (int i = 0; i < list.getSize(); i++) {
162-
this->addAtIndex(index++, *list.get(i));
174+
T val = list.getValue(i);
175+
T *finalValue = (T *)malloc(sizeof(T));
176+
memcpy(finalValue, &val, sizeof(T));
177+
this->addAtIndex(index++, *finalValue);
178+
if (!this->isMutable()) {
179+
free(finalValue);
180+
}
163181
}
164182
}
165183

@@ -178,7 +196,7 @@ template <typename T> class AbstractList {
178196
T *arr = (T *)malloc(this->getSize() * sizeof(T));
179197

180198
for (int i = 0; i < this->getSize(); ++i) {
181-
arr[i] = *this->get(i);
199+
arr[i] = this->getValue(i);
182200
}
183201

184202
return arr;
@@ -223,22 +241,21 @@ template <typename T> class AbstractList {
223241
}
224242

225243
for (int i = 0; i < this->getSize(); i++) {
226-
if (*list.get(i) != *this->get(i)) {
244+
if (list.getValue(i) != this->getValue(i)) {
227245
return false;
228246
}
229247
}
230248
return true;
231249
}
232250

233251
/*!
234-
* @brief Get a pointer to the entry at the given index.
235-
* @see get()
252+
* @brief Get the value of the element at the index.
236253
* @see getValue()
237254
*
238255
* @param index Index of the element to get.
239-
* @return Pointer to the element.
256+
* @return Value of the element.
240257
*/
241-
T *operator[](int index) { return get(index); }
258+
T operator[](int index) { return getValue(index); }
242259

243260
/*!
244261
* @brief Compare two lists whether their attributes and entries are equal.

src/List.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ template <typename T> class List : public SingleLinkedList<T> {
5555
/*!
5656
* @brief Constructor of a List Object.
5757
*
58-
* @param mutableList true if the list should be mutable (default); false
59-
* otherwise.
58+
* @param mutableList true if the list should be mutable; false otherwise (default).
6059
*/
61-
explicit List<T>(bool mutableList = true)
60+
explicit List<T>(bool mutableList = false)
6261
: SingleLinkedList<T>(mutableList) {}
6362
};
6463

src/SingleLinkedList.hpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#ifndef LIST_SINGLE_LINKED_LIST_HPP
2727
#define LIST_SINGLE_LINKED_LIST_HPP
2828

29-
#include <stdlib.h>
3029
#include <string.h>
3130

3231
#include "AbstractList.hpp"
@@ -89,14 +88,35 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
8988
Entry *head = nullptr; /// The first entry of the list.
9089
Entry *tail = nullptr; /// The last entry of the list.
9190

91+
protected:
92+
T *get(int index) override {
93+
if (this->isIndexOutOfBounds(index)) {
94+
return nullptr;
95+
}
96+
97+
Entry *current = this->head;
98+
int i = 0;
99+
while (i != index) {
100+
current = current->getNext();
101+
i++;
102+
}
103+
104+
if (this->isMutable()) {
105+
return (T *) current->getValue();
106+
} else {
107+
T *finalValue = (T *)malloc(sizeof(T));
108+
memcpy(finalValue, current->getValue(), sizeof(T));
109+
return finalValue;
110+
}
111+
}
112+
92113
public:
93114
/*!
94115
* @brief Constructor of a SingleLinkedList Object.
95116
*
96-
* @param mutableList true if the list should be mutable (default); false
97-
* otherwise.
117+
* @param mutableList true if the list should be mutable; false otherwise (default).
98118
*/
99-
explicit SingleLinkedList<T>(bool mutableList = true)
119+
explicit SingleLinkedList<T>(bool mutableList = false)
100120
: AbstractList<T>(mutableList) {}
101121

102122
/*!
@@ -207,21 +227,6 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
207227
this->tail = nullptr;
208228
}
209229
}
210-
211-
T *get(int index) override {
212-
if (!this->isMutable() || this->isIndexOutOfBounds(index)) {
213-
return nullptr;
214-
}
215-
216-
Entry *current = this->head;
217-
int i = 0;
218-
while (i != index) {
219-
current = current->getNext();
220-
i++;
221-
}
222-
223-
return (T *)current->getValue();
224-
}
225230
};
226231

227232
#endif /* LIST_SINGLE_LINKED_LIST_HPP */

0 commit comments

Comments
 (0)