From c62d512d68127b78b0aa9704ddcfe3d077d57278 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 13 Jan 2023 17:48:19 +0200 Subject: [PATCH] BAN: Add Iterator and ConstIterator to Vector --- BAN/include/BAN/Vector.h | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index ac5faa11..d4d698bf 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -7,6 +7,49 @@ namespace BAN { + template + class Vector; + + template + class VectorIterator + { + public: + VectorIterator() = default; + VectorIterator(const VectorIterator& other) : m_data(other.m_data) { } + VectorIterator& operator=(const VectorIterator& other) { m_data = other.m_data; return *this; } + VectorIterator& operator++() { m_data++; return *this; } + T& operator*() { return *m_data; } + const T& operator*() const { return *m_data; } + T* operator->() { return m_data; } + const T* operator->() const { return m_data; } + bool operator==(const VectorIterator& other) const { return !(*this != other); } + bool operator!=(const VectorIterator& other) const { return m_data != other.m_data; } + private: + VectorIterator(T* data) : m_data(data) { } + private: + T* m_data = nullptr; + friend class Vector; + }; + + template + class VectorConstIterator + { + public: + VectorConstIterator() = default; + VectorConstIterator(const VectorConstIterator& other) : m_data(other.m_data) { } + VectorConstIterator& operator=(const VectorConstIterator& other) { m_data = other.m_data; return *this; } + VectorConstIterator& operator++() { m_data++; return *this; } + const T& operator*() const { return *m_data; } + const T* operator->() const { return m_data; } + bool operator==(const VectorConstIterator& other) const { return !(*this != other); } + bool operator!=(const VectorConstIterator& other) const { return m_data != other.m_data; } + private: + VectorConstIterator(T* data) : m_data(data) { } + private: + const T* m_data = nullptr; + friend class Vector; + }; + // T must be move assignable, move constructable (and copy constructable for some functions) template class Vector @@ -14,6 +57,8 @@ namespace BAN public: using size_type = size_t; using value_type = T; + using iterator = VectorIterator; + using const_iterator = VectorConstIterator; public: Vector() = default; @@ -29,6 +74,11 @@ namespace BAN [[nodiscard]] ErrorOr Insert(T&&, size_type); [[nodiscard]] ErrorOr Insert(const T&, size_type); + iterator begin(); + iterator end(); + const_iterator begin() const; + const_iterator end() const; + void PopBack(); void Remove(size_type); void Clear(); @@ -149,6 +199,30 @@ namespace BAN return Insert(Move(T(value)), index); } + template + typename Vector::iterator Vector::begin() + { + return VectorIterator(Address(0)); + } + + template + typename Vector::iterator Vector::end() + { + return VectorIterator(Address(m_size)); + } + + template + typename Vector::const_iterator Vector::begin() const + { + return VectorConstIterator(Address(0)); + } + + template + typename Vector::const_iterator Vector::end() const + { + return VectorConstIterator(Address(m_size)); + } + template void Vector::PopBack() {