Arduino List Library  3.0.1
The Ultimate Collection of Lists
AbstractList.hpp
Go to the documentation of this file.
1 
26 #ifndef LIST_ABSTRACT_LIST_HPP
27 #define LIST_ABSTRACT_LIST_HPP
28 
29 #include <stddef.h>
30 
36 template<typename T>
37 class AbstractList {
38  size_t size = 0;
39  bool mutableList = false;
40 
41  protected:
44 #define extendedIsIndexOutOfBounds(index) \
45  ((index) != this->getSize() && this->isIndexOutOfBounds(index))
46 
50  class AbstractEntry {
51  T immutableValue;
52  T *mutableValue = nullptr;
53 
54  public:
62  T *getValue(const bool m) {
63  if (m) {
64  return mutableValue;
65  }
66  return &immutableValue;
67  }
68 
75  void setValue(T &val, const bool m) {
76  if (m) {
77  mutableValue = &val;
78  } else {
79  immutableValue = val;
80  }
81  }
82  };
83 
89  explicit AbstractList<T>(const bool mutableList) : mutableList(mutableList) {}
90 
103  virtual T *getPointer(int index) = 0;
104 
109  void increaseSize() { size++; }
110 
115  void decreaseSize() { size--; }
116 
120  void resetSize() { size = 0; }
121 
130  bool isIndexOutOfBounds(const int index) const { return index < 0 || index >= getSize(); }
131 
132  public:
139  void add(T &value) { addLast(value); }
140 
141 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
142 
146  void add(T &&value) { addLast(value); }
147 #endif
148 
160  virtual void addAtIndex(int index, T &value) = 0;
161 
162 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
163 
167  virtual void addAtIndex(const int index, T &&value) {
168  if (this->isMutable()) {
169  return;// Mutable lists cannot save rvalues!
170  }
171  addAtIndex(index, value);
172  }
173 #endif
174 
185  void addAll(int index, AbstractList<T> &list) {
186  for (int i = 0; i < list.getSize(); i++) {
187  addAtIndex(index++, list.get(i));
188  }
189  }
190 
198  void addAll(AbstractList<T> &list) { addAll(getSize(), list); }
199 
211  void addAll(int index, T *arr, const size_t arrSize) {
212  for (size_t i = 0; i < arrSize; ++i) {
213  addAtIndex(index++, arr[i]);
214  }
215  }
216 
224  void addAll(T *arr, const size_t arrSize) {
225  for (size_t i = 0; i < arrSize; ++i) {
226  add(arr[i]);
227  }
228  }
229 
236  void addFirst(T &value) { addAtIndex(0, value); }
237 
238 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
239 
243  void addFirst(T &&value) { addAtIndex(0, value); }
244 #endif
245 
252  void addLast(T &value) { addAtIndex(getSize(), value); }
253 
254 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
255 
259  void addLast(T &&value) { addAtIndex(getSize(), value); }
260 #endif
261 
275  T get(const int index) { return *this->getPointer(index); }
276 
289  T *getMutableValue(const int index) {
290  if (!this->isMutable()) {
291  return nullptr;
292  }
293 
294  return this->getPointer(index);
295  }
296 
300  virtual void clear() = 0;
301 
310  virtual void remove(int index) = 0;
311 
315  virtual void removeFirst() { remove(0); }
316 
320  virtual void removeLast() { remove(getSize() - 1); }
321 
327  void removeAll() { clear(); }
328 
334  int getSize() const { return size; }
335 
341  bool isMutable() const { return mutableList; }
342 
348  bool isEmpty() const { return getSize() == 0; }
349 
358  bool equals(AbstractList<T> &other) {
359  if (other.isMutable() != this->isMutable()) {
360  return false;
361  }
362 
363  if (other.getSize() != this->getSize()) {
364  return false;
365  }
366 
367  for (int i = 0; i < getSize(); i++) {
368  if (this->isMutable()) {
369  if (other.getMutableValue(i) != this->getMutableValue(i)) {
370  return false;
371  }
372  } else {
373  if (other.get(i) != this->get(i)) {
374  return false;
375  }
376  }
377  }
378  return true;
379  }
380 
385  T operator[](const int index) { return get(index); }
386 
391  bool operator==(AbstractList<T> &other) { return equals(other); }
392 
400  bool operator!=(AbstractList<T> &other) { return !this->equals(other); }
401 
407  void operator+(T &value) { this->add(value); }
408 
409 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
410 
415  void operator+(T &&value) { this->add(value); }
416 #endif
417 
423  void operator+(AbstractList<T> &list) { this->addAll(list); }
424 };
425 
426 #endif// LIST_ABSTRACT_LIST_HPP
void resetSize()
Reset the size to zero.
Definition: AbstractList.hpp:120
virtual T * getPointer(int index)=0
Get a pointer to the element, stored at specific index.
int getSize() const
Get the number how many elements are saved in the list.
Definition: AbstractList.hpp:334
bool isIndexOutOfBounds(const int index) const
Method to verify if the given index is out of the range of the list size.
Definition: AbstractList.hpp:130
void addAll(int index, T *arr, const size_t arrSize)
Add all entries from an array to this list at a specified index. The original entry at this index...
Definition: AbstractList.hpp:211
virtual void removeFirst()
Remove the first entry from the list.
Definition: AbstractList.hpp:315
void removeAll()
Remove all elements from the List.
Definition: AbstractList.hpp:327
void addAll(T *arr, const size_t arrSize)
Add all entries from an array.
Definition: AbstractList.hpp:224
void increaseSize()
Increase the size of the list by one. Should only be called after an insertion!
Definition: AbstractList.hpp:109
virtual void clear()=0
Remove all elements from the List.
void addAll(AbstractList< T > &list)
Add all entries from the given list at the end of the list.
Definition: AbstractList.hpp:198
void add(T &value)
Add a new entry at the end of the list.
Definition: AbstractList.hpp:139
T get(const int index)
Get the raw value at a specified index.
Definition: AbstractList.hpp:275
virtual void removeLast()
Remove the las entry from the list.
Definition: AbstractList.hpp:320
void setValue(T &val, const bool m)
Set the value.
Definition: AbstractList.hpp:75
bool operator==(AbstractList< T > &other)
Compare two lists whether their attributes and entries are equal.
Definition: AbstractList.hpp:391
Definition: AbstractList.hpp:50
bool operator!=(AbstractList< T > &other)
Opposite of &#39;==&#39;.
Definition: AbstractList.hpp:400
virtual void addAtIndex(int index, T &value)=0
Add the value to the list at the given index. The original entry at this index, and followings...
bool isEmpty() const
Check if the list is empty.
Definition: AbstractList.hpp:348
void addFirst(T &value)
Add a new entry at the beginning of the list.
Definition: AbstractList.hpp:236
T * getValue(const bool m)
A pointer to the raw value, assigned for mutable lists.
Definition: AbstractList.hpp:62
void decreaseSize()
Decrease the size of the list by one. Should only be called after an deletion!
Definition: AbstractList.hpp:115
bool isMutable() const
Check if the list is mutable.
Definition: AbstractList.hpp:341
T * getMutableValue(const int index)
Get the pointer to the mutable object at a specified index.
Definition: AbstractList.hpp:289
bool equals(AbstractList< T > &other)
Compare two lists whether their attributes and entries are equal.
Definition: AbstractList.hpp:358
void operator+(T &value)
Add a new entry at the end of the list.
Definition: AbstractList.hpp:407
void addAll(int index, AbstractList< T > &list)
Add all entries from the given list to this list at a specified index. The original entry at this ind...
Definition: AbstractList.hpp:185
T operator[](const int index)
Get the raw value at a specified index.
Definition: AbstractList.hpp:385
void addLast(T &value)
Add a new entry at the end of the list.
Definition: AbstractList.hpp:252
void operator+(AbstractList< T > &list)
Add all entries from the given list at the end of the list.
Definition: AbstractList.hpp:423
Abstract class from which all lists can be derived.
Definition: AbstractList.hpp:37