reverse_iterator.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // (C) Copyright David Abrahams 2002.
  2. // (C) Copyright Jeremy Siek 2002.
  3. // (C) Copyright Thomas Witt 2002.
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_REVERSE_ITERATOR_23022003THW_HPP
  8. #define BOOST_REVERSE_ITERATOR_23022003THW_HPP
  9. #include <boost/iterator/iterator_adaptor.hpp>
  10. namespace boost {
  11. namespace iterators {
  12. //
  13. //
  14. //
  15. template <class Iterator>
  16. class reverse_iterator
  17. : public iterator_adaptor< reverse_iterator<Iterator>, Iterator >
  18. {
  19. typedef iterator_adaptor< reverse_iterator<Iterator>, Iterator > super_t;
  20. friend class iterator_core_access;
  21. public:
  22. reverse_iterator() {}
  23. explicit reverse_iterator(Iterator x)
  24. : super_t(x) {}
  25. template<class OtherIterator>
  26. reverse_iterator(
  27. reverse_iterator<OtherIterator> const& r
  28. , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
  29. )
  30. : super_t(r.base())
  31. {}
  32. private:
  33. typename super_t::reference dereference() const
  34. {
  35. Iterator it = this->base_reference();
  36. --it;
  37. return *it;
  38. }
  39. void increment() { --this->base_reference(); }
  40. void decrement() { ++this->base_reference(); }
  41. void advance(typename super_t::difference_type n)
  42. {
  43. this->base_reference() -= n;
  44. }
  45. template <class OtherIterator>
  46. typename super_t::difference_type
  47. distance_to(reverse_iterator<OtherIterator> const& y) const
  48. {
  49. return this->base_reference() - y.base();
  50. }
  51. };
  52. template <class BidirectionalIterator>
  53. inline reverse_iterator<BidirectionalIterator> make_reverse_iterator(BidirectionalIterator x)
  54. {
  55. return reverse_iterator<BidirectionalIterator>(x);
  56. }
  57. } // namespace iterators
  58. using iterators::reverse_iterator;
  59. using iterators::make_reverse_iterator;
  60. } // namespace boost
  61. #endif // BOOST_REVERSE_ITERATOR_23022003THW_HPP