reversed.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_ADAPTOR_REVERSED_HPP
  11. #define BOOST_RANGE_ADAPTOR_REVERSED_HPP
  12. #include <boost/range/iterator_range.hpp>
  13. #include <boost/iterator/reverse_iterator.hpp>
  14. namespace boost
  15. {
  16. namespace range_detail
  17. {
  18. template< class R >
  19. struct reversed_range :
  20. public boost::iterator_range<
  21. boost::reverse_iterator<
  22. BOOST_DEDUCED_TYPENAME range_iterator<R>::type
  23. >
  24. >
  25. {
  26. private:
  27. typedef boost::iterator_range<
  28. boost::reverse_iterator<
  29. BOOST_DEDUCED_TYPENAME range_iterator<R>::type
  30. >
  31. >
  32. base;
  33. public:
  34. typedef boost::reverse_iterator<BOOST_DEDUCED_TYPENAME range_iterator<R>::type> iterator;
  35. explicit reversed_range( R& r )
  36. : base( iterator(boost::end(r)), iterator(boost::begin(r)) )
  37. { }
  38. };
  39. struct reverse_forwarder {};
  40. template< class BidirectionalRng >
  41. inline reversed_range<BidirectionalRng>
  42. operator|( BidirectionalRng& r, reverse_forwarder )
  43. {
  44. return reversed_range<BidirectionalRng>( r );
  45. }
  46. template< class BidirectionalRng >
  47. inline reversed_range<const BidirectionalRng>
  48. operator|( const BidirectionalRng& r, reverse_forwarder )
  49. {
  50. return reversed_range<const BidirectionalRng>( r );
  51. }
  52. } // 'range_detail'
  53. using range_detail::reversed_range;
  54. namespace adaptors
  55. {
  56. namespace
  57. {
  58. const range_detail::reverse_forwarder reversed =
  59. range_detail::reverse_forwarder();
  60. }
  61. template<class BidirectionalRange>
  62. inline reversed_range<BidirectionalRange>
  63. reverse(BidirectionalRange& rng)
  64. {
  65. return reversed_range<BidirectionalRange>(rng);
  66. }
  67. template<class BidirectionalRange>
  68. inline reversed_range<const BidirectionalRange>
  69. reverse(const BidirectionalRange& rng)
  70. {
  71. return reversed_range<const BidirectionalRange>(rng);
  72. }
  73. } // 'adaptors'
  74. } // 'boost'
  75. #endif