1

const iterator + separate iterator from list.h

This commit is contained in:
2022-07-18 21:55:06 +02:00
parent b9fe5d7d53
commit 2e4c0339a3
4 changed files with 52 additions and 41 deletions

40
c_os/user/lib/Iterator.h Normal file
View File

@ -0,0 +1,40 @@
#ifndef __Iterator_Include_H_
#define __Iterator_Include_H_
// This iterator works for structures where the elements are adjacent in memory.
// For things like LinkedList, the operator++ has to be overriden to implement the traversal.
template<typename T>
class Iterator {
public:
using Type = T;
protected:
Type* ptr;
public:
Iterator(Type* ptr) : ptr(ptr) {}
// I only implement the least necessary operators
virtual Iterator& operator++() {
this->ptr = this->ptr + 1;
return *this;
}
Type* operator->() {
return this->ptr;
}
Type& operator*() {
return *this->ptr;
}
bool operator==(const Iterator& other) const {
return this->ptr == other.ptr;
}
bool operator!=(const Iterator& other) const {
return !(*this == other); // Use our == implementation
}
};
#endif