fsfw/container/HybridIterator.h

91 lines
1.9 KiB
C
Raw Normal View History

#ifndef HYBRIDITERATOR_H_
#define HYBRIDITERATOR_H_
#include <framework/container/ArrayList.h>
#include <framework/container/SinglyLinkedList.h>
template<typename T, typename count_t = uint8_t>
class HybridIterator: public LinkedElement<T>::Iterator,
public ArrayList<T, count_t>::Iterator {
public:
2020-06-17 20:57:35 +02:00
HybridIterator() {}
HybridIterator(typename LinkedElement<T>::Iterator *iter) :
2020-06-17 20:57:35 +02:00
LinkedElement<T>::Iterator(*iter), value(iter->value),
linked(true) {
}
HybridIterator(LinkedElement<T> *start) :
2020-06-24 11:35:07 +02:00
LinkedElement<T>::Iterator(start), value(start->value),
linked(true) {
}
HybridIterator(typename ArrayList<T, count_t>::Iterator start,
typename ArrayList<T, count_t>::Iterator end) :
ArrayList<T, count_t>::Iterator(start), value(start.value), linked(
false), end(end.value) {
if (value == this->end) {
value = NULL;
}
}
HybridIterator(T *firstElement, T *lastElement) :
ArrayList<T, count_t>::Iterator(firstElement), value(firstElement), linked(
false), end(++lastElement) {
if (value == end) {
value = NULL;
}
}
HybridIterator& operator++() {
if (linked) {
LinkedElement<T>::Iterator::operator++();
if (LinkedElement<T>::Iterator::value != NULL) {
value = LinkedElement<T>::Iterator::value->value;
} else {
value = NULL;
}
} else {
ArrayList<T, count_t>::Iterator::operator++();
value = ArrayList<T, count_t>::Iterator::value;
if (value == end) {
value = NULL;
}
}
return *this;
}
HybridIterator operator++(int) {
HybridIterator tmp(*this);
operator++();
return tmp;
}
2020-06-24 11:35:07 +02:00
bool operator==(const HybridIterator& other) const {
2020-06-17 20:57:35 +02:00
return value == other.value;
}
2020-06-24 11:35:07 +02:00
bool operator!=(const HybridIterator& other) const {
return !(*this == other);
}
T operator*() {
return *value;
}
T *operator->() {
return value;
}
2020-06-17 20:57:35 +02:00
T* value = nullptr;
private:
2020-06-17 20:57:35 +02:00
bool linked = false;
T *end = nullptr;
};
#endif /* HYBRIDITERATOR_H_ */