reverse_iterator.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/move for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_MOVE_DETAIL_REVERSE_ITERATOR_HPP
  13. #define BOOST_MOVE_DETAIL_REVERSE_ITERATOR_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. #include <boost/move/detail/config_begin.hpp>
  21. #include <boost/move/detail/iterator_traits.hpp>
  22. #include <boost/move/detail/meta_utils.hpp>
  23. namespace boost {
  24. namespace movelib {
  25. template<class I>
  26. inline typename iterator_traits<I>::pointer iterator_arrow_result(const I &i)
  27. { return i.operator->(); }
  28. template<class T>
  29. BOOST_MOVE_FORCEINLINE T * iterator_arrow_result(T *p)
  30. { return p; }
  31. template<class It>
  32. class reverse_iterator
  33. {
  34. public:
  35. typedef typename boost::movelib::iterator_traits<It>::pointer pointer;
  36. typedef typename boost::movelib::iterator_traits<It>::reference reference;
  37. typedef typename boost::movelib::iterator_traits<It>::difference_type difference_type;
  38. typedef typename boost::movelib::iterator_traits<It>::iterator_category iterator_category;
  39. typedef typename boost::movelib::iterator_traits<It>::value_type value_type;
  40. typedef It iterator_type;
  41. inline reverse_iterator()
  42. : m_current() //Value initialization to achieve "null iterators" (N3644)
  43. {}
  44. inline explicit reverse_iterator(It r)
  45. : m_current(r)
  46. {}
  47. inline reverse_iterator(const reverse_iterator& r)
  48. : m_current(r.base())
  49. {}
  50. template<class OtherIt>
  51. inline
  52. reverse_iterator( const reverse_iterator<OtherIt>& r
  53. , typename boost::move_detail::enable_if_convertible<OtherIt, It>::type* =0
  54. )
  55. : m_current(r.base())
  56. {}
  57. inline reverse_iterator & operator=( const reverse_iterator& r)
  58. { m_current = r.base(); return *this; }
  59. template<class OtherIt>
  60. inline typename boost::move_detail::enable_if_convertible<OtherIt, It, reverse_iterator &>::type
  61. operator=( const reverse_iterator<OtherIt>& r)
  62. { m_current = r.base(); return *this; }
  63. inline It base() const
  64. { return m_current; }
  65. inline reference operator*() const
  66. {
  67. It temp(m_current);
  68. --temp;
  69. reference r = *temp;
  70. return r;
  71. }
  72. inline pointer operator->() const
  73. {
  74. It temp(m_current);
  75. --temp;
  76. return (iterator_arrow_result)(temp);
  77. }
  78. inline reference operator[](difference_type off) const
  79. {
  80. return this->m_current[difference_type(-off - 1)];
  81. }
  82. inline reverse_iterator& operator++()
  83. {
  84. --m_current;
  85. return *this;
  86. }
  87. inline reverse_iterator operator++(int)
  88. {
  89. reverse_iterator temp((*this));
  90. --m_current;
  91. return temp;
  92. }
  93. inline reverse_iterator& operator--()
  94. {
  95. ++m_current;
  96. return *this;
  97. }
  98. inline reverse_iterator operator--(int)
  99. {
  100. reverse_iterator temp((*this));
  101. ++m_current;
  102. return temp;
  103. }
  104. inline friend bool operator==(const reverse_iterator& l, const reverse_iterator& r)
  105. { return l.m_current == r.m_current; }
  106. inline friend bool operator!=(const reverse_iterator& l, const reverse_iterator& r)
  107. { return l.m_current != r.m_current; }
  108. inline friend bool operator<(const reverse_iterator& l, const reverse_iterator& r)
  109. { return l.m_current > r.m_current; }
  110. inline friend bool operator<=(const reverse_iterator& l, const reverse_iterator& r)
  111. { return l.m_current >= r.m_current; }
  112. inline friend bool operator>(const reverse_iterator& l, const reverse_iterator& r)
  113. { return l.m_current < r.m_current; }
  114. inline friend bool operator>=(const reverse_iterator& l, const reverse_iterator& r)
  115. { return l.m_current <= r.m_current; }
  116. inline reverse_iterator& operator+=(difference_type off)
  117. { m_current -= off; return *this; }
  118. inline reverse_iterator& operator-=(difference_type off)
  119. { m_current += off; return *this; }
  120. inline friend reverse_iterator operator+(reverse_iterator l, difference_type off)
  121. { return (l += off); }
  122. inline friend reverse_iterator operator+(difference_type off, reverse_iterator r)
  123. { return (r += off); }
  124. inline friend reverse_iterator operator-(reverse_iterator l, difference_type off)
  125. { return (l-= off); }
  126. inline friend difference_type operator-(const reverse_iterator& l, const reverse_iterator& r)
  127. { return r.m_current - l.m_current; }
  128. private:
  129. It m_current; // the wrapped iterator
  130. };
  131. template< class Iterator >
  132. inline reverse_iterator<Iterator> make_reverse_iterator( Iterator i )
  133. { return reverse_iterator<Iterator>(i); }
  134. } //namespace movelib {
  135. } //namespace boost {
  136. #include <boost/move/detail/config_end.hpp>
  137. #endif //BOOST_MOVE_DETAIL_REVERSE_ITERATOR_HPP