reversed.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/range/concepts.hpp>
  14. #include <boost/iterator/reverse_iterator.hpp>
  15. namespace boost
  16. {
  17. namespace range_detail
  18. {
  19. template< class R >
  20. struct reversed_range :
  21. public boost::iterator_range<
  22. boost::reverse_iterator<
  23. BOOST_DEDUCED_TYPENAME range_iterator<R>::type
  24. >
  25. >
  26. {
  27. private:
  28. typedef boost::iterator_range<
  29. boost::reverse_iterator<
  30. BOOST_DEDUCED_TYPENAME range_iterator<R>::type
  31. >
  32. >
  33. base;
  34. public:
  35. typedef boost::reverse_iterator<BOOST_DEDUCED_TYPENAME range_iterator<R>::type> iterator;
  36. explicit reversed_range( R& r )
  37. : base( iterator(boost::end(r)), iterator(boost::begin(r)) )
  38. { }
  39. };
  40. struct reverse_forwarder {};
  41. template< class BidirectionalRange >
  42. inline reversed_range<BidirectionalRange>
  43. operator|( BidirectionalRange& r, reverse_forwarder )
  44. {
  45. BOOST_RANGE_CONCEPT_ASSERT((
  46. BidirectionalRangeConcept<BidirectionalRange>));
  47. return reversed_range<BidirectionalRange>( r );
  48. }
  49. template< class BidirectionalRange >
  50. inline reversed_range<const BidirectionalRange>
  51. operator|( const BidirectionalRange& r, reverse_forwarder )
  52. {
  53. BOOST_RANGE_CONCEPT_ASSERT((
  54. BidirectionalRangeConcept<const BidirectionalRange>));
  55. return reversed_range<const BidirectionalRange>( r );
  56. }
  57. } // 'range_detail'
  58. using range_detail::reversed_range;
  59. namespace adaptors
  60. {
  61. namespace
  62. {
  63. const range_detail::reverse_forwarder reversed =
  64. range_detail::reverse_forwarder();
  65. }
  66. template<class BidirectionalRange>
  67. inline reversed_range<BidirectionalRange>
  68. reverse(BidirectionalRange& rng)
  69. {
  70. BOOST_RANGE_CONCEPT_ASSERT((
  71. BidirectionalRangeConcept<BidirectionalRange>));
  72. return reversed_range<BidirectionalRange>(rng);
  73. }
  74. template<class BidirectionalRange>
  75. inline reversed_range<const BidirectionalRange>
  76. reverse(const BidirectionalRange& rng)
  77. {
  78. BOOST_RANGE_CONCEPT_ASSERT((
  79. BidirectionalRangeConcept<const BidirectionalRange>));
  80. return reversed_range<const BidirectionalRange>(rng);
  81. }
  82. } // 'adaptors'
  83. } // 'boost'
  84. #endif