2020-06-24 11:36:10 +02:00
|
|
|
#ifndef FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_
|
|
|
|
#define FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "ArrayList.h"
|
|
|
|
#include "SinglyLinkedList.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
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() {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
HybridIterator(typename LinkedElement<T>::Iterator *iter) :
|
2020-06-17 20:57:35 +02:00
|
|
|
LinkedElement<T>::Iterator(*iter), value(iter->value),
|
|
|
|
linked(true) {
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
HybridIterator(LinkedElement<T> *start) :
|
2020-06-24 11:35:07 +02:00
|
|
|
LinkedElement<T>::Iterator(start), value(start->value),
|
|
|
|
linked(true) {
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
HybridIterator(typename ArrayList<T, count_t>::Iterator start,
|
|
|
|
typename ArrayList<T, count_t>::Iterator end) :
|
2020-06-24 11:36:10 +02:00
|
|
|
ArrayList<T, count_t>::Iterator(start), value(start.value),
|
|
|
|
linked(false), end(end.value) {
|
2016-06-15 23:48:41 +02:00
|
|
|
if (value == this->end) {
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HybridIterator(T *firstElement, T *lastElement) :
|
2020-06-24 11:36:10 +02:00
|
|
|
ArrayList<T, count_t>::Iterator(firstElement), value(firstElement),
|
|
|
|
linked(false), end(++lastElement) {
|
2016-06-15 23:48:41 +02:00
|
|
|
if (value == end) {
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HybridIterator& operator++() {
|
|
|
|
if (linked) {
|
|
|
|
LinkedElement<T>::Iterator::operator++();
|
2020-06-24 11:36:10 +02:00
|
|
|
if (LinkedElement<T>::Iterator::value != nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
value = LinkedElement<T>::Iterator::value->value;
|
|
|
|
} else {
|
2020-06-24 11:36:10 +02:00
|
|
|
value = nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ArrayList<T, count_t>::Iterator::operator++();
|
|
|
|
value = ArrayList<T, count_t>::Iterator::value;
|
|
|
|
|
|
|
|
if (value == end) {
|
2020-06-24 11:36:10 +02:00
|
|
|
value = nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:35:07 +02:00
|
|
|
bool operator!=(const HybridIterator& other) const {
|
2016-06-15 23:48:41 +02:00
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
T operator*() {
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *operator->() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-06-17 20:57:35 +02:00
|
|
|
T* value = nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
private:
|
2020-06-17 20:57:35 +02:00
|
|
|
bool linked = false;
|
|
|
|
T *end = nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
2020-06-26 12:52:03 +02:00
|
|
|
#endif /* FRAMEWORK_CONTAINER_HYBRIDITERATOR_H_ */
|