/*============================================================================= 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 TESTSPR_ITERATOR_HPP #define TESTSPR_ITERATOR_HPP #include #include #include #include #include #include #include #include #include namespace testspr { // // reduce_iterator // template::iterator_category> class reduce_iterator : public std::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 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 reduce_iterator() : current() {} reduce_iterator(reduce_iterator const& other) = default; explicit SPROUT_CONSTEXPR reduce_iterator(iterator_type it) : current(it) {} template::value>::type = sprout::enabler> SPROUT_CONSTEXPR reduce_iterator(reduce_iterator const& it) : current(it.base()) {} template::value>::type = sprout::enabler> SPROUT_CXX14_CONSTEXPR reduce_iterator& operator=(reduce_iterator const& it) { reduce_iterator temp(it); temp.swap(*this); return *this; } SPROUT_CONSTEXPR iterator_type base() const { return current; } SPROUT_CONSTEXPR reference operator*() const { return *current; } SPROUT_CONSTEXPR pointer operator->() const { return &*current; } SPROUT_CXX14_CONSTEXPR reduce_iterator& operator++() { ++current; return *this; } SPROUT_CXX14_CONSTEXPR reduce_iterator operator++(int) { reduce_iterator result(*this); ++current; return result; } SPROUT_CXX14_CONSTEXPR reduce_iterator& operator--() { --current; return *this; } SPROUT_CXX14_CONSTEXPR reduce_iterator operator--(int) { reduce_iterator temp(*this); --current; return temp; } SPROUT_CONSTEXPR reduce_iterator operator+(difference_type n) const { return reduce_iterator(current + n); } SPROUT_CONSTEXPR reduce_iterator operator-(difference_type n) const { return reduce_iterator(current - n); } SPROUT_CXX14_CONSTEXPR reduce_iterator& operator+=(difference_type n) { reduce_iterator temp(current + n); temp.swap(*this); return *this; } SPROUT_CXX14_CONSTEXPR reduce_iterator& operator-=(difference_type n) { reduce_iterator temp(current - n); temp.swap(*this); return *this; } SPROUT_CONSTEXPR reference operator[](difference_type n) const { return current[n]; } SPROUT_CONSTEXPR reduce_iterator next() const { return reduce_iterator(sprout::next(current)); } SPROUT_CONSTEXPR reduce_iterator prev() const { return reduce_iterator(sprout::prev(current)); } SPROUT_CXX14_CONSTEXPR void swap(reduce_iterator& other) SPROUT_NOEXCEPT_IF( SPROUT_NOEXCEPT_EXPR(swap(current, other.current)) ) { using std::swap; swap(current, other.current); } }; template inline SPROUT_CONSTEXPR bool operator==(testspr::reduce_iterator const& lhs, testspr::reduce_iterator const& rhs) { return lhs.base() == rhs.base(); } template inline SPROUT_CONSTEXPR bool operator!=(testspr::reduce_iterator const& lhs, testspr::reduce_iterator const& rhs) { return !(lhs == rhs); } template inline SPROUT_CONSTEXPR bool operator<(testspr::reduce_iterator const& lhs, testspr::reduce_iterator const& rhs) { return lhs.base() < rhs.base(); } template inline SPROUT_CONSTEXPR bool operator>(testspr::reduce_iterator const& lhs, testspr::reduce_iterator const& rhs) { return rhs < lhs; } template inline SPROUT_CONSTEXPR bool operator<=(testspr::reduce_iterator const& lhs, testspr::reduce_iterator const& rhs) { return !(rhs < lhs); } template inline SPROUT_CONSTEXPR bool operator>=(testspr::reduce_iterator const& lhs, testspr::reduce_iterator const& rhs) { return !(lhs < rhs); } template inline SPROUT_CONSTEXPR decltype(std::declval() - std::declval()) operator-(testspr::reduce_iterator const& lhs, testspr::reduce_iterator const& rhs) { return lhs.base() - rhs.base(); } template inline SPROUT_CONSTEXPR testspr::reduce_iterator operator+( typename testspr::reduce_iterator::difference_type n, testspr::reduce_iterator const& it ) { return it + n; } // // make_reduce_iterator // template inline SPROUT_CONSTEXPR testspr::reduce_iterator make_reduce_iterator(Iterator it) { return testspr::reduce_iterator(it); } template inline SPROUT_CONSTEXPR testspr::reduce_iterator make_reduce_iterator(Iterator it) { return testspr::reduce_iterator(it); } // // reduce_input // reduce_forward // reduce_bidirectional // reduce_random_access // template inline SPROUT_CONSTEXPR testspr::reduce_iterator reduce_input(Iterator it) { return testspr::reduce_iterator(it); } template inline SPROUT_CONSTEXPR testspr::reduce_iterator reduce_forward(Iterator it) { return testspr::reduce_iterator(it); } template inline SPROUT_CONSTEXPR testspr::reduce_iterator reduce_bidirectional(Iterator it) { return testspr::reduce_iterator(it); } template inline SPROUT_CONSTEXPR testspr::reduce_iterator reduce_random_access(Iterator it) { return testspr::reduce_iterator(it); } // // swap // template inline SPROUT_CXX14_CONSTEXPR void swap(testspr::reduce_iterator& lhs, testspr::reduce_iterator& rhs) SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs)) { lhs.swap(rhs); } // // iterator_distance // template inline SPROUT_CONSTEXPR typename std::iterator_traits >::difference_type iterator_distance(testspr::reduce_iterator first, testspr::reduce_iterator last) { return sprout::distance(first.base(), last.base()); } // // iterator_next // template inline SPROUT_CONSTEXPR testspr::reduce_iterator iterator_next(testspr::reduce_iterator const& it) { return it.next(); } template inline SPROUT_CONSTEXPR testspr::reduce_iterator iterator_next( testspr::reduce_iterator const& it, typename testspr::reduce_iterator::difference_type n ) { return testspr::reduce_iterator(sprout::next(it.base(), n)); } // // iterator_prev // template inline SPROUT_CONSTEXPR testspr::reduce_iterator iterator_prev(testspr::reduce_iterator const& it) { return it.prev(); } template inline SPROUT_CONSTEXPR testspr::reduce_iterator iterator_prev( testspr::reduce_iterator const& it, typename testspr::reduce_iterator::difference_type n ) { return testspr::reduce_iterator(sprout::next(it.base(), n)); } } // namespace testspr #endif // #ifndef TESTSPR_ITERATOR_HPP