Added generic iterator
This commit is contained in:
parent
77e1ba5915
commit
34e5a2cf56
31
src/container/iterator.hpp
Normal file
31
src/container/iterator.hpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace gz {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A generic iterator that satisfies std::forward_iterator
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
struct Iterator {
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
Iterator() : ptr(nullptr) {};
|
||||||
|
Iterator(T* ptr) : ptr(ptr) {};
|
||||||
|
T& operator*() const { return *ptr; };
|
||||||
|
Iterator& operator=(const Iterator& other) {
|
||||||
|
ptr = other.ptr;
|
||||||
|
return *this;
|
||||||
|
};
|
||||||
|
Iterator& operator++() { ptr += sizeof(T); return *this; };
|
||||||
|
Iterator operator++(int) { auto copy = *this; ptr += sizeof(T); return copy; };
|
||||||
|
friend int operator-(Iterator lhs, Iterator rhs) {
|
||||||
|
return lhs.ptr - rhs.ptr;
|
||||||
|
};
|
||||||
|
bool operator==(const Iterator& other) const { return ptr == other.ptr; };
|
||||||
|
// bool operator!=(const Iterator& other) const { return ptr != other.ptr; };
|
||||||
|
private:
|
||||||
|
T* ptr;
|
||||||
|
}; // Iterator
|
||||||
|
|
||||||
|
/* static_assert(std::forward_iterator<Iterator<int>>, "Iterator not a forward range."); */
|
||||||
|
} // namespace gz
|
Loading…
Reference in New Issue
Block a user