/*============================================================================= Copyright (c) 2011-2016 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_REVERSE_ITERATOR_HPP #define SPROUT_ITERATOR_REVERSE_ITERATOR_HPP #include #include #include #include #include #include #include #include #include namespace sprout { // // reverse_iterator // template class reverse_iterator : public std::iterator< typename std::iterator_traits::iterator_category, typename std::iterator_traits::value_type, typename std::iterator_traits::difference_type, typename std::iterator_traits::pointer, typename std::iterator_traits::reference > { public: typedef Iterator iterator_type; typedef typename std::iterator_traits::iterator_category iterator_category; typedef typename std::iterator_traits::value_type value_type; typedef typename std::iterator_traits::difference_type difference_type; typedef typename std::iterator_traits::pointer pointer; typedef typename std::iterator_traits::reference reference; protected: iterator_type current; public: SPROUT_CONSTEXPR reverse_iterator() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL SPROUT_CONSTEXPR reverse_iterator(reverse_iterator const& other) : current(other.current) {} explicit SPROUT_CONSTEXPR reverse_iterator(iterator_type it) : current(it) {} template SPROUT_CONSTEXPR reverse_iterator(reverse_iterator const& it) : current(it.base()) {} template SPROUT_CXX14_CONSTEXPR reverse_iterator& operator=(reverse_iterator const& it) { reverse_iterator temp(it); temp.swap(*this); return *this; } SPROUT_CONSTEXPR iterator_type base() const { return current; } SPROUT_CONSTEXPR reference operator*() const { return *sprout::prev(current); } SPROUT_CONSTEXPR pointer operator->() const { return &*(*this); } SPROUT_CXX14_CONSTEXPR reverse_iterator& operator++() { --current; return *this; } SPROUT_CXX14_CONSTEXPR reverse_iterator operator++(int) { reverse_iterator result(*this); --current; return result; } SPROUT_CXX14_CONSTEXPR reverse_iterator& operator--() { ++current; return *this; } SPROUT_CXX14_CONSTEXPR reverse_iterator operator--(int) { reverse_iterator temp(*this); ++current; return temp; } SPROUT_CONSTEXPR reverse_iterator operator+(difference_type n) const { return reverse_iterator(current - n); } SPROUT_CONSTEXPR reverse_iterator operator-(difference_type n) const { return reverse_iterator(current + n); } SPROUT_CXX14_CONSTEXPR reverse_iterator& operator+=(difference_type n) { reverse_iterator temp(current - n); temp.swap(*this); return *this; } SPROUT_CXX14_CONSTEXPR reverse_iterator& operator-=(difference_type n) { reverse_iterator temp(current + n); temp.swap(*this); return *this; } SPROUT_CONSTEXPR reference operator[](difference_type n) const { return *(current - (n + 1)); } SPROUT_CONSTEXPR reverse_iterator next() const { return reverse_iterator(sprout::prev(current)); } SPROUT_CONSTEXPR reverse_iterator prev() const { return reverse_iterator(sprout::next(current)); } SPROUT_CXX14_CONSTEXPR void swap(reverse_iterator& other) SPROUT_NOEXCEPT_IF( SPROUT_NOEXCEPT_EXPR(swap(current, other.current)) ) { swap(current, other.current); } }; template inline SPROUT_CONSTEXPR bool operator==(sprout::reverse_iterator const& lhs, sprout::reverse_iterator const& rhs) { return lhs.base() == rhs.base(); } template inline SPROUT_CONSTEXPR bool operator!=(sprout::reverse_iterator const& lhs, sprout::reverse_iterator const& rhs) { return !(lhs == rhs); } template inline SPROUT_CONSTEXPR bool operator<(sprout::reverse_iterator const& lhs, sprout::reverse_iterator const& rhs) { return lhs.base() < rhs.base(); } template inline SPROUT_CONSTEXPR bool operator>(sprout::reverse_iterator const& lhs, sprout::reverse_iterator const& rhs) { return rhs < lhs; } template inline SPROUT_CONSTEXPR bool operator<=(sprout::reverse_iterator const& lhs, sprout::reverse_iterator const& rhs) { return !(rhs < lhs); } template inline SPROUT_CONSTEXPR bool operator>=(sprout::reverse_iterator const& lhs, sprout::reverse_iterator const& rhs) { return !(lhs < rhs); } template inline SPROUT_CONSTEXPR decltype(std::declval() - std::declval()) operator-(sprout::reverse_iterator const& lhs, sprout::reverse_iterator const& rhs) { return rhs.base() - lhs.base(); } template inline SPROUT_CONSTEXPR sprout::reverse_iterator operator+( typename sprout::reverse_iterator::difference_type n, sprout::reverse_iterator const& it ) { return it + n; } // // make_reverse_iterator // template inline SPROUT_CONSTEXPR sprout::reverse_iterator make_reverse_iterator(Iterator it) { return sprout::reverse_iterator(it); } // // swap // template inline SPROUT_CXX14_CONSTEXPR void swap(sprout::reverse_iterator& lhs, sprout::reverse_iterator& rhs) SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) { lhs.swap(rhs); } // // iterator_next // template inline SPROUT_CONSTEXPR sprout::reverse_iterator iterator_next(sprout::reverse_iterator const& it) { return it.next(); } // // iterator_prev // template inline SPROUT_CONSTEXPR sprout::reverse_iterator iterator_prev(sprout::reverse_iterator const& it) { return it.prev(); } // // is_const_iterator_cast_convertible // template struct is_const_iterator_cast_convertible, sprout::reverse_iterator > : public sprout::is_const_iterator_cast_convertible {}; // // const_iterator_conversion // template< typename T, typename Iterator, typename sprout::enabler_if, T>::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR T const_iterator_conversion(sprout::reverse_iterator const& it) { return T(sprout::const_iterator_cast(it.base())); } } // namespace sprout #endif // #ifndef SPROUT_ITERATOR_REVERSE_ITERATOR_HPP