Horizon
reverse.hpp
Go to the documentation of this file.
1 // 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 
16 #include <range/v3/range_fwd.hpp>
17 
25 #include <range/v3/utility/static_const.hpp>
27 
28 #include <range/v3/detail/prologue.hpp>
29 
30 namespace ranges
31 {
34 
36  namespace detail
37  {
38  template<typename I>
39  constexpr void reverse_impl(I first, I last, std::bidirectional_iterator_tag)
40  {
41  while(first != last)
42  {
43  if(first == --last)
44  break;
45  ranges::iter_swap(first, last);
46  ++first;
47  }
48  }
49 
50  template<typename I>
51  constexpr void reverse_impl(I first, I last, std::random_access_iterator_tag)
52  {
53  if(first != last)
54  for(; first < --last; ++first)
55  ranges::iter_swap(first, last);
56  }
57  } // namespace detail
59 
60  RANGES_FUNC_BEGIN(reverse)
61 
62 
63  template(typename I, typename S)(
64  requires bidirectional_iterator<I> AND sentinel_for<S, I> AND permutable<I>)
65  constexpr I RANGES_FUNC(reverse)(I first, S end_)
66  {
67  I last = ranges::next(first, end_);
68  detail::reverse_impl(first, last, iterator_tag_of<I>{});
69  return last;
70  }
71 
73  template(typename Rng, typename I = iterator_t<Rng>)(
74  requires bidirectional_range<Rng> AND permutable<I>)
75  constexpr borrowed_iterator_t<Rng> RANGES_FUNC(reverse)(Rng && rng) //
76  {
77  return (*this)(begin(rng), end(rng));
78  }
79 
80  RANGES_FUNC_END(reverse)
81 
82  namespace cpp20
83  {
84  using ranges::reverse;
85  }
87 } // namespace ranges
88 
89 #include <range/v3/detail/epilogue.hpp>
90 
91 #endif
template(typename Rng, typename I=iterator_t< Rng >)(requires bidirectional_range< Rng > AND permutable< I >) const expr borrowed_iterator_t< Rng > RANGES_FUNC(reverse)(Rng &&rng)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: reverse.hpp:73
CPP_concept permutable
\concept permutable
Definition: concepts.hpp:840
CPP_concept sentinel_for
\concept sentinel_for
Definition: concepts.hpp:306
CPP_concept bidirectional_iterator
\concept bidirectional_iterator
Definition: concepts.hpp:390
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
front< Pair > first
Retrieve the first element of the pair Pair.
Definition: meta.hpp:2251