From ec22b86e00c264a98583b44f63e1bf4ee54833fd Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 20 Mar 2023 13:15:38 +0200 Subject: [PATCH] BAN: Add implementation for basic iterator for contiguous memory --- BAN/include/BAN/Iterators.h | 159 ++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 BAN/include/BAN/Iterators.h diff --git a/BAN/include/BAN/Iterators.h b/BAN/include/BAN/Iterators.h new file mode 100644 index 0000000000..37ea338605 --- /dev/null +++ b/BAN/include/BAN/Iterators.h @@ -0,0 +1,159 @@ +#pragma once + +#include + +namespace BAN +{ + + template + class IteratorSimple + { + public: + IteratorSimple() = default; + + const T& operator*() const + { + ASSERT(m_pointer); + return *m_pointer; + } + T& operator*() + { + ASSERT(m_pointer); + return *m_pointer; + } + + const T* operator->() const + { + ASSERT(m_pointer); + return m_pointer; + } + T* operator->() + { + ASSERT(m_pointer); + return m_pointer; + } + + IteratorSimple& operator++() + { + ASSERT(m_pointer); + ++m_pointer; + return *this; + } + IteratorSimple operator++(int) + { + auto temp = *this; + ++(*this); + return temp; + } + + IteratorSimple& operator--() + { + ASSERT(m_pointer); + return --m_pointer; + } + IteratorSimple operator--(int) + { + auto temp = *this; + --(*this); + return temp; + } + + bool operator==(const IteratorSimple& other) const + { + return m_pointer == other.m_pointer; + } + bool operator!=(const IteratorSimple& other) const + { + return !(*this == other); + } + + operator bool() const + { + return m_pointer; + } + + private: + IteratorSimple(T* pointer) + : m_pointer(pointer) + { + } + + private: + T* m_pointer = nullptr; + + friend Container; + }; + + template + class ConstIteratorSimple + { + public: + ConstIteratorSimple() = default; + ConstIteratorSimple(IteratorSimple other) + : m_pointer(other.m_pointer) + { + } + + const T& operator*() const + { + ASSERT(m_pointer); + return *m_pointer; + } + + const T* operator->() const + { + ASSERT(m_pointer); + return m_pointer; + } + + ConstIteratorSimple& operator++() + { + ASSERT(m_pointer); + ++m_pointer; + return *this; + } + ConstIteratorSimple operator++(int) + { + auto temp = *this; + ++(*this); + return temp; + } + + ConstIteratorSimple& operator--() + { + ASSERT(m_pointer); + return --m_pointer; + } + ConstIteratorSimple operator--(int) + { + auto temp = *this; + --(*this); + return temp; + } + + bool operator==(const ConstIteratorSimple& other) const + { + return m_pointer == other.m_pointer; + } + bool operator!=(const ConstIteratorSimple& other) const + { + return !(*this == other); + } + + operator bool() const + { + return !!m_pointer; + } + + private: + ConstIteratorSimple(const T* pointer) + : m_pointer(pointer) + { + } + + private: + const T* m_pointer = nullptr; + + friend Container; + }; +} \ No newline at end of file