From 8edabaea55d4407d5b77513ab28ede77a520bf9f Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 20 Mar 2023 13:26:42 +0200 Subject: [PATCH] BAN: Add iterators to all containers with contiguous memory --- BAN/include/BAN/Array.h | 8 +++++ BAN/include/BAN/Queue.h | 8 +++++ BAN/include/BAN/String.h | 8 +++++ BAN/include/BAN/StringView.h | 5 +++ BAN/include/BAN/Vector.h | 63 +++++------------------------------- 5 files changed, 37 insertions(+), 55 deletions(-) diff --git a/BAN/include/BAN/Array.h b/BAN/include/BAN/Array.h index 2aa78b2c7b..f19c680f23 100644 --- a/BAN/include/BAN/Array.h +++ b/BAN/include/BAN/Array.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include @@ -13,11 +14,18 @@ namespace BAN public: using size_type = decltype(S); using value_type = T; + using iterator = IteratorSimple; + using const_iterator = ConstIteratorSimple; public: Array(); Array(const T&); + iterator begin() { return iterator(m_data); } + iterator end() { return iterator(m_data + size()); } + const_iterator begin() const { return const_iterator(m_data); } + const_iterator end() const { return const_iterator(m_data + size()); } + const T& operator[](size_type) const; T& operator[](size_type); diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index 9ec8b14c7d..49cd72f351 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -14,6 +15,8 @@ namespace BAN public: using size_type = size_t; using value_type = T; + using iterator = IteratorSimple; + using const_iterator = ConstIteratorSimple; public: Queue() = default; @@ -32,6 +35,11 @@ namespace BAN ErrorOr reserve(size_type); ErrorOr shrink_to_fit(); + iterator begin() { return iterator(m_data); } + iterator end() { return iterator(m_data + m_size); } + const_iterator begin() const { return const_iterator(m_data); } + const_iterator end() const { return const_iterator(m_data + m_size); } + void pop(); void clear(); diff --git a/BAN/include/BAN/String.h b/BAN/include/BAN/String.h index 4518e6bae3..843f9a437f 100644 --- a/BAN/include/BAN/String.h +++ b/BAN/include/BAN/String.h @@ -3,6 +3,7 @@ #include #include #include +#include namespace BAN { @@ -11,6 +12,8 @@ namespace BAN { public: using size_type = size_t; + using iterator = IteratorSimple; + using const_iterator = ConstIteratorSimple; public: String(); @@ -38,6 +41,11 @@ namespace BAN void clear(); + const_iterator begin() const { return const_iterator(m_data); } + iterator begin() { return iterator(m_data); } + const_iterator end() const { return const_iterator(m_data + m_size); } + iterator end() { return iterator(m_data + m_size); } + char operator[](size_type) const; char& operator[](size_type); diff --git a/BAN/include/BAN/StringView.h b/BAN/include/BAN/StringView.h index 89cf2f3aac..651fb85732 100644 --- a/BAN/include/BAN/StringView.h +++ b/BAN/include/BAN/StringView.h @@ -2,6 +2,7 @@ #include #include +#include namespace BAN { @@ -10,12 +11,16 @@ namespace BAN { public: using size_type = size_t; + using const_iterator = ConstIteratorSimple; public: StringView(); StringView(const String&); StringView(const char*, size_type = -1); + const_iterator begin() const { return const_iterator(m_data); } + const_iterator end() const { return const_iterator(m_data + m_size); } + char operator[](size_type) const; bool operator==(const String&) const; diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 004ffe59c9..3cf9868530 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -8,9 +9,6 @@ namespace BAN { - template - class VectorIterator; - // T must be move assignable, move constructable (and copy constructable for some functions) template class Vector @@ -18,8 +16,8 @@ namespace BAN public: using size_type = size_t; using value_type = T; - using iterator = VectorIterator; - using const_iterator = VectorIterator; + using iterator = IteratorSimple; + using const_iterator = ConstIteratorSimple; public: Vector() = default; @@ -40,10 +38,10 @@ namespace BAN ErrorOr insert(size_type, T&&); ErrorOr insert(size_type, const T&); - iterator begin() { return iterator (m_data); } - const_iterator begin() const { return const_iterator(m_data); } - iterator end() { return iterator (m_data + m_size); } - const_iterator end() const { return const_iterator(m_data + m_size); } + iterator begin() { return iterator(m_data); } + iterator end() { return iterator(m_data + m_size); } + const_iterator begin() const { return const_iterator(m_data); } + const_iterator end() const { return const_iterator(m_data + m_size); } void pop_back(); void remove(size_type); @@ -76,54 +74,9 @@ namespace BAN private: T* m_data = nullptr; size_type m_capacity = 0; - size_type m_size = 0; + size_type m_size = 0; }; - template - class VectorIterator - { - public: - using value_type = T; - using data_type = maybe_const_t; - - public: - VectorIterator() = default; - template - VectorIterator(const VectorIterator& other, enable_if_t) - : m_data(other.m_data) - { - } - - VectorIterator& operator++() { m_data++; return *this; } - VectorIterator& operator--() { m_data--; return *this; } - VectorIterator operator++(int) { auto temp = *this; ++(*this); return temp; } - VectorIterator operator--(int) { auto temp = *this; --(*this); return temp; } - - template - enable_if_t operator*() { ASSERT(m_data); return *m_data; } - const T& operator*() const { ASSERT(m_data); return *m_data; } - - template - enable_if_t operator->() { ASSERT(m_data); return m_data; } - const T* operator->() const { ASSERT(m_data); return m_data; } - - bool operator==(const VectorIterator& other) const { return m_data == other.m_data; } - bool operator!=(const VectorIterator& other) const { return !(*this == other); } - - operator bool() const { return m_data; } - - private: - VectorIterator(data_type* data) : m_data(data) { } - - private: - data_type* m_data = nullptr; - - friend class Vector; - friend class VectorIterator; - }; - - - template Vector::Vector(Vector&& other) {