#pragma once #include <iterator> #include "../../common/specialized.hpp" #include "../../common/compat.hpp" namespace sprawl { template<typename ValueType, typename ParentType> class VectorIterator; } template<typename ValueType, typename ParentType> class sprawl::VectorIterator : public std::iterator<std::random_access_iterator_tag, ValueType, std::ptrdiff_t, ValueType*, ValueType&> { public: VectorIterator(ValueType* item, ParentType const* parent) : m_currentItem(item) , m_parent(parent) { // } ValueType& operator*() { return *m_currentItem; } ValueType* operator->() { return m_currentItem; } ValueType const& operator*() const { return *m_currentItem; } ValueType const* operator->() const { return m_currentItem; } ValueType& Value() { return *m_currentItem; } ValueType const& Value() const { return *m_currentItem; } ssize_t Index() const { return m_currentItem - m_parent->begin().m_currentItem; } VectorIterator<ValueType, ParentType>& operator++() { ++m_currentItem; return *this; } VectorIterator<ValueType, ParentType> operator++(int) { return VectorIterator<ValueType, ParentType>(m_currentItem++, m_parent); } VectorIterator<ValueType, ParentType> const& operator++() const { ++m_currentItem; return *this; } VectorIterator<ValueType, ParentType> const operator++(int) const { return VectorIterator<ValueType, ParentType>(m_currentItem++, m_parent); } VectorIterator<ValueType, ParentType> operator+(int steps) { return VectorIterator<ValueType, ParentType>(m_currentItem+steps, m_parent); } VectorIterator<ValueType, ParentType> const operator+(int steps) const { return VectorIterator<ValueType, ParentType>(m_currentItem+steps, m_parent); } VectorIterator<ValueType, ParentType>& operator--() { --m_currentItem; return *this; } VectorIterator<ValueType, ParentType> operator--(int) { return VectorIterator<ValueType, ParentType>(m_currentItem--, m_parent); } VectorIterator<ValueType, ParentType> const& operator--() const { --m_currentItem; return *this; } VectorIterator<ValueType, ParentType> const operator--(int) const { return VectorIterator<ValueType, ParentType>(m_currentItem--, m_parent); } VectorIterator<ValueType, ParentType> operator-(int steps) { return VectorIterator<ValueType, ParentType>(m_currentItem - steps, m_parent); } VectorIterator<ValueType, ParentType> const operator-(int steps) const { return VectorIterator<ValueType, ParentType>(m_currentItem - steps, m_parent); } bool operator==(VectorIterator<ValueType, ParentType> const& rhs) const { return m_currentItem == rhs.m_currentItem; } bool operator!=(VectorIterator<ValueType, ParentType> const& rhs) const { return !this->operator==(rhs); } operator bool() const { return Valid(); } bool operator!() const { return !Valid(); } bool Valid() const { return operator>=(m_parent->begin()) && operator<(m_parent->end()); } bool More() const { return Valid() && Next().Valid(); } VectorIterator<ValueType, ParentType> Next() { return VectorIterator<ValueType, ParentType>(m_currentItem + 1, m_parent); } VectorIterator<ValueType, ParentType> const Next() const { return VectorIterator<ValueType, ParentType>(m_currentItem + 1, m_parent); } size_t operator-(VectorIterator<ValueType, ParentType> const& other) { return m_currentItem - other.m_currentItem; } ValueType& operator[](size_t index) { return m_currentItem[index]; } ValueType const& operator[](size_t index) const { return m_currentItem[index]; } bool operator<(VectorIterator<ValueType, ParentType> const& rhs) const { return m_currentItem < rhs.m_currentItem; } bool operator>(VectorIterator<ValueType, ParentType> const& rhs) const { return m_currentItem > rhs.m_currentItem; } bool operator<=(VectorIterator<ValueType, ParentType> const& rhs) const { return m_currentItem <= rhs.m_currentItem; } bool operator>=(VectorIterator<ValueType, ParentType> const& rhs) const { return m_currentItem >= rhs.m_currentItem; } protected: mutable ValueType* m_currentItem; ParentType const* m_parent; };
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 18645 | brandon_m_bare | Integrated latest version of libsprawl. | ||
#1 | 15089 | brandon_m_bare | First integration of sprawl. | ||
//guest/ShadauxCat/Sprawl/Mainline/collections/iterator/VectorIterator.hpp | |||||
#1 | 14091 | ShadauxCat |
-Created Vector class, iterator, and unit test -Made list iterator a bidirectional iterator when created from a doubly-linked list. (It still has operator-- when created from singly-linked list, but it won't compile if it's used.) -Changed front() and back() in list classes to Front() and Back() #review-14083 |