reverse_iterator.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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