2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
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)
|
|
|
|
=============================================================================*/
|
2011-09-30 15:04:03 +00:00
|
|
|
#ifndef SPROUT_ITERATOR_REVERSE_ITERATOR_HPP
|
|
|
|
#define SPROUT_ITERATOR_REVERSE_ITERATOR_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <utility>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/next.hpp>
|
|
|
|
#include <sprout/iterator/prev.hpp>
|
2012-04-30 01:55:33 +00:00
|
|
|
#include <sprout/iterator/distance.hpp>
|
2014-01-05 13:57:41 +00:00
|
|
|
#include <sprout/iterator/const_iterator_cast.hpp>
|
2012-10-05 15:58:56 +00:00
|
|
|
#include <sprout/utility/swap.hpp>
|
2014-01-05 13:57:41 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2011-09-30 15:04:03 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// reverse_iterator
|
|
|
|
//
|
|
|
|
template<typename Iterator>
|
|
|
|
class reverse_iterator
|
|
|
|
: public std::iterator<
|
|
|
|
typename std::iterator_traits<Iterator>::iterator_category,
|
|
|
|
typename std::iterator_traits<Iterator>::value_type,
|
|
|
|
typename std::iterator_traits<Iterator>::difference_type,
|
|
|
|
typename std::iterator_traits<Iterator>::pointer,
|
|
|
|
typename std::iterator_traits<Iterator>::reference
|
|
|
|
>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Iterator iterator_type;
|
|
|
|
typedef typename std::iterator_traits<iterator_type>::iterator_category iterator_category;
|
|
|
|
typedef typename std::iterator_traits<iterator_type>::value_type value_type;
|
|
|
|
typedef typename std::iterator_traits<iterator_type>::difference_type difference_type;
|
|
|
|
typedef typename std::iterator_traits<iterator_type>::pointer pointer;
|
|
|
|
typedef typename std::iterator_traits<iterator_type>::reference reference;
|
|
|
|
protected:
|
|
|
|
iterator_type current;
|
|
|
|
public:
|
2014-01-13 15:18:06 +00:00
|
|
|
SPROUT_CONSTEXPR reverse_iterator() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
|
2012-05-14 06:42:54 +00:00
|
|
|
SPROUT_CONSTEXPR reverse_iterator(reverse_iterator const& other)
|
|
|
|
: current(other.current)
|
|
|
|
{}
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR reverse_iterator(iterator_type it)
|
2011-09-30 15:04:03 +00:00
|
|
|
: current(it)
|
|
|
|
{}
|
|
|
|
template<typename U>
|
|
|
|
SPROUT_CONSTEXPR reverse_iterator(reverse_iterator<U> const& it)
|
2012-11-09 13:33:11 +00:00
|
|
|
: current(it.base())
|
2011-09-30 15:04:03 +00:00
|
|
|
{}
|
|
|
|
template<typename U>
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator& operator=(reverse_iterator<U> const& it) {
|
2011-09-30 15:04:03 +00:00
|
|
|
reverse_iterator temp(it);
|
2012-03-23 14:31:47 +00:00
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR iterator_type base() const {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator*() const {
|
2013-01-19 23:53:20 +00:00
|
|
|
return *sprout::prev(current);
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR pointer operator->() const {
|
2013-01-19 23:53:20 +00:00
|
|
|
return &*(*this);
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator& operator++() {
|
2011-09-30 15:04:03 +00:00
|
|
|
--current;
|
2011-10-02 08:18:35 +00:00
|
|
|
return *this;
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator operator++(int) {
|
2011-10-02 08:18:35 +00:00
|
|
|
reverse_iterator result(*this);
|
2011-09-30 15:04:03 +00:00
|
|
|
--current;
|
|
|
|
return result;
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator& operator--() {
|
2011-09-30 15:04:03 +00:00
|
|
|
++current;
|
2011-10-02 08:18:35 +00:00
|
|
|
return *this;
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator operator--(int) {
|
2011-10-02 08:18:35 +00:00
|
|
|
reverse_iterator temp(*this);
|
2011-09-30 15:04:03 +00:00
|
|
|
++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);
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator& operator+=(difference_type n) {
|
2011-09-30 15:04:03 +00:00
|
|
|
reverse_iterator temp(current - n);
|
2012-03-23 14:31:47 +00:00
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR reverse_iterator& operator-=(difference_type n) {
|
2011-09-30 15:04:03 +00:00
|
|
|
reverse_iterator temp(current + n);
|
2012-03-23 14:31:47 +00:00
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
|
2013-01-19 23:53:20 +00:00
|
|
|
return *(current - (n + 1));
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
SPROUT_CONSTEXPR reverse_iterator next() const {
|
|
|
|
return reverse_iterator(sprout::prev(current));
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reverse_iterator prev() const {
|
|
|
|
return reverse_iterator(sprout::next(current));
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void swap(reverse_iterator& other)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF(
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_NOEXCEPT_EXPR(swap(current, other.current))
|
|
|
|
)
|
|
|
|
{
|
2011-09-30 15:04:03 +00:00
|
|
|
swap(current, other.current);
|
|
|
|
}
|
|
|
|
};
|
2012-04-30 10:41:24 +00:00
|
|
|
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator1, typename Iterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator==(sprout::reverse_iterator<Iterator1> const& lhs, sprout::reverse_iterator<Iterator2> const& rhs) {
|
2011-09-30 15:04:03 +00:00
|
|
|
return lhs.base() == rhs.base();
|
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator1, typename Iterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator!=(sprout::reverse_iterator<Iterator1> const& lhs, sprout::reverse_iterator<Iterator2> const& rhs) {
|
2011-09-30 15:04:03 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator1, typename Iterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<(sprout::reverse_iterator<Iterator1> const& lhs, sprout::reverse_iterator<Iterator2> const& rhs) {
|
2011-09-30 15:04:03 +00:00
|
|
|
return lhs.base() < rhs.base();
|
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator1, typename Iterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>(sprout::reverse_iterator<Iterator1> const& lhs, sprout::reverse_iterator<Iterator2> const& rhs) {
|
2011-09-30 15:04:03 +00:00
|
|
|
return rhs < lhs;
|
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator1, typename Iterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<=(sprout::reverse_iterator<Iterator1> const& lhs, sprout::reverse_iterator<Iterator2> const& rhs) {
|
2011-09-30 15:04:03 +00:00
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator1, typename Iterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>=(sprout::reverse_iterator<Iterator1> const& lhs, sprout::reverse_iterator<Iterator2> const& rhs) {
|
2011-09-30 15:04:03 +00:00
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator1, typename Iterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR decltype(std::declval<Iterator1>() - std::declval<Iterator2>())
|
|
|
|
operator-(sprout::reverse_iterator<Iterator1> const& lhs, sprout::reverse_iterator<Iterator2> const& rhs) {
|
2013-01-19 23:53:20 +00:00
|
|
|
return rhs.base() - lhs.base();
|
2011-09-30 15:04:03 +00:00
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
template<typename Iterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::reverse_iterator<Iterator>
|
|
|
|
operator+(
|
2011-09-30 15:04:03 +00:00
|
|
|
typename sprout::reverse_iterator<Iterator>::difference_type n,
|
|
|
|
sprout::reverse_iterator<Iterator> const& it
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return it + n;
|
|
|
|
}
|
|
|
|
|
2012-09-21 06:43:30 +00:00
|
|
|
//
|
|
|
|
// make_reverse_iterator
|
|
|
|
//
|
|
|
|
template<typename Iterator>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::reverse_iterator<Iterator>
|
|
|
|
make_reverse_iterator(Iterator it) {
|
|
|
|
return sprout::reverse_iterator<Iterator>(it);
|
|
|
|
}
|
|
|
|
|
2011-09-30 15:04:03 +00:00
|
|
|
//
|
|
|
|
// swap
|
|
|
|
//
|
|
|
|
template<typename Iterator>
|
2013-10-09 14:44:25 +00:00
|
|
|
inline SPROUT_CXX14_CONSTEXPR void
|
2012-10-05 15:58:56 +00:00
|
|
|
swap(sprout::reverse_iterator<Iterator>& lhs, sprout::reverse_iterator<Iterator>& rhs)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
|
2012-10-05 15:58:56 +00:00
|
|
|
{
|
2011-09-30 15:04:03 +00:00
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
2012-04-30 01:55:33 +00:00
|
|
|
|
2012-09-26 07:31:11 +00:00
|
|
|
//
|
|
|
|
// iterator_next
|
2012-04-30 01:55:33 +00:00
|
|
|
//
|
|
|
|
template<typename Iterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::reverse_iterator<Iterator>
|
|
|
|
iterator_next(sprout::reverse_iterator<Iterator> const& it) {
|
2012-04-30 01:55:33 +00:00
|
|
|
return it.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2012-09-26 07:31:11 +00:00
|
|
|
// iterator_prev
|
2012-04-30 01:55:33 +00:00
|
|
|
//
|
|
|
|
template<typename Iterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::reverse_iterator<Iterator>
|
|
|
|
iterator_prev(sprout::reverse_iterator<Iterator> const& it) {
|
2012-04-30 01:55:33 +00:00
|
|
|
return it.prev();
|
|
|
|
}
|
2014-01-05 13:57:41 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// is_const_iterator_cast_convertible
|
|
|
|
//
|
|
|
|
template<typename FromIterator, typename ToIterator>
|
|
|
|
struct is_const_iterator_cast_convertible<sprout::reverse_iterator<FromIterator>, sprout::reverse_iterator<ToIterator> >
|
|
|
|
: public sprout::is_const_iterator_cast_convertible<FromIterator, ToIterator>
|
|
|
|
{};
|
|
|
|
//
|
|
|
|
// const_iterator_conversion
|
|
|
|
//
|
|
|
|
template<
|
|
|
|
typename T,
|
|
|
|
typename Iterator,
|
|
|
|
typename sprout::enabler_if<sprout::is_const_iterator_cast_convertible<sprout::reverse_iterator<Iterator>, T>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
const_iterator_conversion(sprout::reverse_iterator<Iterator> const& it) {
|
|
|
|
return T(sprout::const_iterator_cast<typename T::iterator_type>(it.base()));
|
|
|
|
}
|
2011-09-30 15:04:03 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ITERATOR_REVERSE_ITERATOR_HPP
|