/*============================================================================= Copyright (c) 2011-2019 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #ifndef SPROUT_ITERATOR_INDEXED_ITERATOR_HPP #define SPROUT_ITERATOR_INDEXED_ITERATOR_HPP #include #include #include #include #include #include #include namespace sprout { // // indexed_iterator // template class indexed_iterator : public sprout::detail::iterator_base::type { private: typedef typename sprout::detail::iterator_base::type base_type; public: typedef Iterator iterator_type; typedef typename base_type::iterator_category iterator_category; typedef typename base_type::value_type value_type; typedef typename base_type::difference_type difference_type; typedef typename base_type::pointer pointer; typedef typename base_type::reference reference; typedef difference_type index_type; protected: iterator_type current; index_type idx; public: SPROUT_CONSTEXPR indexed_iterator() : current(), idx() {} SPROUT_CONSTEXPR indexed_iterator(indexed_iterator const& other) : current(other.current) , idx(other.idx) {} explicit SPROUT_CONSTEXPR indexed_iterator(iterator_type it, index_type idx = 0) : current(it) , idx(idx) {} template SPROUT_CONSTEXPR indexed_iterator(indexed_iterator const& it) : current(it.base()) , idx(it.index()) {} template SPROUT_CXX14_CONSTEXPR indexed_iterator& operator=(indexed_iterator const& it) { indexed_iterator temp(it); temp.swap(*this); return *this; } SPROUT_CONSTEXPR iterator_type base() const { return current; } SPROUT_CONSTEXPR index_type index() const { return idx; } SPROUT_CONSTEXPR reference operator*() const { return *current; } SPROUT_CONSTEXPR pointer operator->() const { return &*current; } SPROUT_CXX14_CONSTEXPR indexed_iterator& operator++() { ++current; ++idx; return *this; } SPROUT_CXX14_CONSTEXPR indexed_iterator operator++(int) { indexed_iterator result(*this); ++current; ++idx; return result; } SPROUT_CXX14_CONSTEXPR indexed_iterator& operator--() { --current; --idx; return *this; } SPROUT_CXX14_CONSTEXPR indexed_iterator operator--(int) { indexed_iterator temp(*this); --current; --idx; return temp; } SPROUT_CONSTEXPR indexed_iterator operator+(difference_type n) const { return indexed_iterator(current + n, idx + n); } SPROUT_CONSTEXPR indexed_iterator operator-(difference_type n) const { return indexed_iterator(current - n, idx - n); } SPROUT_CXX14_CONSTEXPR indexed_iterator& operator+=(difference_type n) { indexed_iterator temp(current + n, idx + n); temp.swap(*this); return *this; } SPROUT_CXX14_CONSTEXPR indexed_iterator& operator-=(difference_type n) { indexed_iterator temp(current - n, idx - n); temp.swap(*this); return *this; } SPROUT_CONSTEXPR reference operator[](difference_type n) const { return *(current + n); } SPROUT_CONSTEXPR indexed_iterator next() const { return indexed_iterator(sprout::next(current), idx + 1); } SPROUT_CONSTEXPR indexed_iterator prev() const { return indexed_iterator(sprout::prev(current), idx - 1); } SPROUT_CXX14_CONSTEXPR void swap(indexed_iterator& other) SPROUT_NOEXCEPT_IF( SPROUT_NOEXCEPT_EXPR(swap(current, other.current)) && SPROUT_NOEXCEPT_EXPR(swap(idx, other.idx)) ) { swap(current, other.current); swap(idx, other.idx); } }; template inline SPROUT_CONSTEXPR bool operator==(sprout::indexed_iterator const& lhs, sprout::indexed_iterator const& rhs) { return lhs.base() == rhs.base(); } template inline SPROUT_CONSTEXPR bool operator!=(sprout::indexed_iterator const& lhs, sprout::indexed_iterator const& rhs) { return !(lhs == rhs); } template inline SPROUT_CONSTEXPR bool operator<(sprout::indexed_iterator const& lhs, sprout::indexed_iterator const& rhs) { return lhs.base() < rhs.base(); } template inline SPROUT_CONSTEXPR bool operator>(sprout::indexed_iterator const& lhs, sprout::indexed_iterator const& rhs) { return rhs < lhs; } template inline SPROUT_CONSTEXPR bool operator<=(sprout::indexed_iterator const& lhs, sprout::indexed_iterator const& rhs) { return !(rhs < lhs); } template inline SPROUT_CONSTEXPR bool operator>=(sprout::indexed_iterator const& lhs, sprout::indexed_iterator const& rhs) { return !(lhs < rhs); } template inline SPROUT_CONSTEXPR decltype(std::declval() - std::declval()) operator-(sprout::indexed_iterator const& lhs, sprout::indexed_iterator const& rhs) { return lhs.base() - rhs.base(); } template inline SPROUT_CONSTEXPR sprout::indexed_iterator operator+( typename sprout::indexed_iterator::difference_type n, sprout::indexed_iterator const& it ) { return it + n; } // // make_indexed_iterator // template inline SPROUT_CONSTEXPR sprout::indexed_iterator make_indexed_iterator(Iterator it, typename sprout::indexed_iterator::index_type idx = 0) { return sprout::indexed_iterator(it, idx); } // // swap // template inline SPROUT_CXX14_CONSTEXPR void swap(sprout::indexed_iterator& lhs, sprout::indexed_iterator& rhs) SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) { lhs.swap(rhs); } // // iterator_next // template inline SPROUT_CONSTEXPR sprout::indexed_iterator iterator_next(sprout::indexed_iterator const& it) { return it.next(); } // // iterator_prev // template inline SPROUT_CONSTEXPR sprout::indexed_iterator iterator_prev(sprout::indexed_iterator const& it) { return it.prev(); } } // namespace sprout #endif // #ifndef SPROUT_ITERATOR_INDEXED_ITERATOR_HPP