only implement one iterator class, no inheritance
This commit is contained in:
@ -5,39 +5,16 @@
|
|||||||
|
|
||||||
namespace bse {
|
namespace bse {
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class AbstractIterator {
|
|
||||||
public:
|
|
||||||
T* ptr;
|
|
||||||
|
|
||||||
// *this is always <= other
|
|
||||||
virtual std::size_t dist(const AbstractIterator& other) const = 0;
|
|
||||||
|
|
||||||
AbstractIterator(T* ptr) : ptr(ptr) {}
|
|
||||||
|
|
||||||
virtual AbstractIterator& operator++() = 0;
|
|
||||||
|
|
||||||
T* operator->() { return this->ptr; }
|
|
||||||
T& operator*() { return *this->ptr; }
|
|
||||||
bool operator==(const AbstractIterator& other) const { return this->ptr == other.ptr; }
|
|
||||||
bool operator!=(const AbstractIterator& other) const { return !(*this == other); }
|
|
||||||
|
|
||||||
friend std::size_t distance(const AbstractIterator& first, const AbstractIterator& last) {
|
|
||||||
return first.dist(last);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// This iterator works for structures where the elements are adjacent in memory.
|
// This iterator works for structures where the elements are adjacent in memory.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class ContinuousIterator : public AbstractIterator<T> {
|
class ContinuousIterator {
|
||||||
|
private:
|
||||||
|
T* ptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::size_t dist(const AbstractIterator<T>& other) const override {
|
ContinuousIterator(T* ptr) : ptr(ptr) {}
|
||||||
return other.ptr - this->ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
ContinuousIterator(T* ptr) : AbstractIterator<T>(ptr) {}
|
ContinuousIterator& operator++() {
|
||||||
|
|
||||||
ContinuousIterator& operator++() override {
|
|
||||||
++this->ptr;
|
++this->ptr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -51,8 +28,29 @@ namespace bse {
|
|||||||
this->ptr -= sub;
|
this->ptr -= sub;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convenience
|
||||||
|
T* operator->() { return this->ptr; }
|
||||||
|
const T* operator->() const { return this->ptr; }
|
||||||
|
T& operator*() { return *this->ptr; }
|
||||||
|
const T& operator*() const { return *this->ptr; }
|
||||||
|
|
||||||
|
bool operator<(const ContinuousIterator& other) const { return this->ptr < other.ptr; }
|
||||||
|
bool operator<=(const ContinuousIterator& other) const { return this->ptr <= other.ptr; }
|
||||||
|
bool operator>(const ContinuousIterator& other) const { return this->ptr > other.ptr; }
|
||||||
|
bool operator>=(const ContinuousIterator& other) const { return this->ptr >= other.ptr; }
|
||||||
|
bool operator==(const ContinuousIterator& other) const { return this->ptr == other.ptr; }
|
||||||
|
bool operator!=(const ContinuousIterator& other) const { return !(*this == other); }
|
||||||
|
|
||||||
|
template<typename t>
|
||||||
|
friend std::size_t distance(const ContinuousIterator<t>& first, const ContinuousIterator<t>& last);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::size_t distance(const ContinuousIterator<T>& first, const ContinuousIterator<T>& last) {
|
||||||
|
return last.ptr - first.ptr;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace bse
|
} // namespace bse
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user