You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reverse.hpp 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ALGORITHM_REVERSE_HPP
  14. #define RANGES_V3_ALGORITHM_REVERSE_HPP
  15. #include <range/v3/range_fwd.hpp>
  16. #include <range/v3/iterator/concepts.hpp>
  17. #include <range/v3/iterator/operations.hpp>
  18. #include <range/v3/iterator/traits.hpp>
  19. #include <range/v3/range/access.hpp>
  20. #include <range/v3/range/concepts.hpp>
  21. #include <range/v3/range/dangling.hpp>
  22. #include <range/v3/range/traits.hpp>
  23. #include <range/v3/utility/static_const.hpp>
  24. #include <range/v3/utility/swap.hpp>
  25. namespace ranges
  26. {
  27. /// \addtogroup group-algorithms
  28. /// @{
  29. /// \cond
  30. namespace detail
  31. {
  32. template<typename I>
  33. void reverse_impl(I first, I last, detail::bidirectional_iterator_tag_)
  34. {
  35. while(first != last)
  36. {
  37. if(first == --last)
  38. break;
  39. ranges::iter_swap(first, last);
  40. ++first;
  41. }
  42. }
  43. template<typename I>
  44. void reverse_impl(I first, I last, detail::random_access_iterator_tag_)
  45. {
  46. if(first != last)
  47. for(; first < --last; ++first)
  48. ranges::iter_swap(first, last);
  49. }
  50. } // namespace detail
  51. /// \endcond
  52. RANGES_BEGIN_NIEBLOID(reverse)
  53. /// \brief function template \c reverse
  54. template<typename I, typename S>
  55. auto RANGES_FUN_NIEBLOID(reverse)(I first, S end_) //
  56. ->CPP_ret(I)( //
  57. requires bidirectional_iterator<I> && sentinel_for<S, I> && permutable<I>)
  58. {
  59. I last = ranges::next(first, end_);
  60. detail::reverse_impl(first, last, iterator_tag_of<I>{});
  61. return last;
  62. }
  63. /// \overload
  64. template<typename Rng, typename I = iterator_t<Rng>>
  65. auto RANGES_FUN_NIEBLOID(reverse)(Rng && rng) //
  66. ->CPP_ret(safe_iterator_t<Rng>)( //
  67. requires bidirectional_range<Rng> && permutable<I>)
  68. {
  69. return (*this)(begin(rng), end(rng));
  70. }
  71. RANGES_END_NIEBLOID(reverse)
  72. namespace cpp20
  73. {
  74. using ranges::reverse;
  75. }
  76. /// @}
  77. } // namespace ranges
  78. #endif // include guard