diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 3c3a2b63d..287fb10ea 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -8,48 +8,8 @@ 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; - }; + template + class VectorIterator; // T must be move assignable, move constructable (and copy constructable for some functions) template @@ -58,8 +18,8 @@ namespace BAN public: using size_type = size_t; using value_type = T; - using iterator = VectorIterator; - using const_iterator = VectorConstIterator; + using iterator = VectorIterator; + using const_iterator = VectorIterator; public: Vector() = default; @@ -79,16 +39,16 @@ namespace BAN [[nodiscard]] ErrorOr insert(size_type, T&&); [[nodiscard]] ErrorOr insert(size_type, const T&); - iterator begin(); - iterator end(); - const_iterator begin() const; - const_iterator end() const; + iterator begin() { return iterator(address_of(0)); } + const_iterator begin() const { return const_iterator(address_of(0)); } + iterator end() { return iterator(address_of(m_size)); } + const_iterator end() const { return const_iterator(address_of(m_size)); } void pop_back(); void remove(size_type); void clear(); - bool has(const T&) const; + bool contains(const T&) const; const T& operator[](size_type) const; T& operator[](size_type); @@ -116,6 +76,48 @@ namespace BAN size_type m_size = 0; }; + template + class VectorIterator + { + public: + using value_type = 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); } + + private: + VectorIterator(T* data) : m_data(data) { } + + private: + T* m_data = nullptr; + + friend class Vector; + friend class VectorIterator; + }; + + + template Vector::Vector(Vector&& other) { @@ -242,30 +244,6 @@ namespace BAN return insert(move(T(value)), index); } - template - typename Vector::iterator Vector::begin() - { - return VectorIterator(address_of(0)); - } - - template - typename Vector::iterator Vector::end() - { - return VectorIterator(address_of(m_size)); - } - - template - typename Vector::const_iterator Vector::begin() const - { - return VectorConstIterator(address_of(0)); - } - - template - typename Vector::const_iterator Vector::end() const - { - return VectorConstIterator(address_of(m_size)); - } - template void Vector::pop_back() { @@ -296,7 +274,7 @@ namespace BAN } template - bool Vector::has(const T& other) const + bool Vector::contains(const T& other) const { for (size_type i = 0; i < m_size; i++) if (*address_of(i) == other)