reverse_iterator.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_DETAIL_REVERSE_ITERATOR_HPP
  13. #define BOOST_INTRUSIVE_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/intrusive/detail/config_begin.hpp>
  21. #include <boost/intrusive/detail/iterator.hpp>
  22. #include <boost/intrusive/detail/mpl.hpp>
  23. namespace boost {
  24. namespace intrusive {
  25. template<class It>
  26. class reverse_iterator
  27. {
  28. public:
  29. typedef typename boost::intrusive::iterator_traits<It>::pointer pointer;
  30. typedef typename boost::intrusive::iterator_traits<It>::reference reference;
  31. typedef typename boost::intrusive::iterator_traits<It>::difference_type difference_type;
  32. typedef typename boost::intrusive::iterator_traits<It>::iterator_category iterator_category;
  33. typedef typename boost::intrusive::iterator_traits<It>::value_type value_type;
  34. typedef It iterator_type;
  35. reverse_iterator()
  36. : m_current() //Value initialization to achieve "null iterators" (N3644)
  37. {}
  38. explicit reverse_iterator(It r)
  39. : m_current(r)
  40. {}
  41. reverse_iterator(const reverse_iterator& r)
  42. : m_current(r.base())
  43. {}
  44. template<class OtherIt>
  45. reverse_iterator( const reverse_iterator<OtherIt>& r
  46. , typename boost::intrusive::detail::enable_if_convertible<OtherIt, It>::type* =0
  47. )
  48. : m_current(r.base())
  49. {}
  50. reverse_iterator & operator=( const reverse_iterator& r)
  51. { m_current = r.base(); return *this; }
  52. template<class OtherIt>
  53. typename boost::intrusive::detail::enable_if_convertible<OtherIt, It, reverse_iterator &>::type
  54. operator=( const reverse_iterator<OtherIt>& r)
  55. { m_current = r.base(); return *this; }
  56. It base() const
  57. { return m_current; }
  58. reference operator*() const
  59. {
  60. It temp(m_current);
  61. --temp;
  62. reference r = *temp;
  63. return r;
  64. }
  65. pointer operator->() const
  66. {
  67. It temp(m_current);
  68. --temp;
  69. return iterator_arrow_result(temp);
  70. }
  71. reference operator[](difference_type off) const
  72. {
  73. return this->m_current[-off - 1];
  74. }
  75. reverse_iterator& operator++()
  76. {
  77. --m_current;
  78. return *this;
  79. }
  80. reverse_iterator operator++(int)
  81. {
  82. reverse_iterator temp((*this));
  83. --m_current;
  84. return temp;
  85. }
  86. reverse_iterator& operator--()
  87. {
  88. ++m_current;
  89. return *this;
  90. }
  91. reverse_iterator operator--(int)
  92. {
  93. reverse_iterator temp((*this));
  94. ++m_current;
  95. return temp;
  96. }
  97. friend bool operator==(const reverse_iterator& l, const reverse_iterator& r)
  98. { return l.m_current == r.m_current; }
  99. friend bool operator!=(const reverse_iterator& l, const reverse_iterator& r)
  100. { return l.m_current != r.m_current; }
  101. friend bool operator<(const reverse_iterator& l, const reverse_iterator& r)
  102. { return l.m_current > r.m_current; }
  103. friend bool operator<=(const reverse_iterator& l, const reverse_iterator& r)
  104. { return l.m_current >= r.m_current; }
  105. friend bool operator>(const reverse_iterator& l, const reverse_iterator& r)
  106. { return l.m_current < r.m_current; }
  107. friend bool operator>=(const reverse_iterator& l, const reverse_iterator& r)
  108. { return l.m_current <= r.m_current; }
  109. reverse_iterator& operator+=(difference_type off)
  110. { m_current -= off; return *this; }
  111. reverse_iterator& operator-=(difference_type off)
  112. { m_current += off; return *this; }
  113. friend reverse_iterator operator+(reverse_iterator l, difference_type off)
  114. { return (l += off); }
  115. friend reverse_iterator operator+(difference_type off, reverse_iterator r)
  116. { return (r += off); }
  117. friend reverse_iterator operator-(reverse_iterator l, difference_type off)
  118. { return (l-= off); }
  119. friend difference_type operator-(const reverse_iterator& l, const reverse_iterator& r)
  120. { return r.m_current - l.m_current; }
  121. private:
  122. It m_current; // the wrapped iterator
  123. };
  124. } //namespace intrusive {
  125. } //namespace boost {
  126. #include <boost/intrusive/detail/config_end.hpp>
  127. #endif //BOOST_INTRUSIVE_DETAIL_REVERSE_ITERATOR_HPP