Debian Codegraph @

3rd_party/range-v3/range/v3/algorithm/reverse.hpp

raw
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
16 #include <range/v3/range_fwd.hpp>
17
18 #include <range/v3/iterator/concepts.hpp>
19 #include <range/v3/iterator/operations.hpp>
20 #include <range/v3/iterator/traits.hpp>
21 #include <range/v3/range/access.hpp>
22 #include <range/v3/range/concepts.hpp>
23 #include <range/v3/range/dangling.hpp>
24 #include <range/v3/range/traits.hpp>
25 #include <range/v3/utility/static_const.hpp>
26 #include <range/v3/utility/swap.hpp>
27
28 #include <range/v3/detail/prologue.hpp>
29
30 namespace ranges
31 {
32 /// \addtogroup group-algorithms
33 /// @{
34
35 /// \cond
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
58 /// \endcond
59
60 RANGES_FUNC_BEGIN(reverse)
61
62 /// \brief function template \c reverse
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
72 /// \overload
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 }
86 /// @}
87 } // namespace ranges
88
89 #include <range/v3/detail/epilogue.hpp>
90
91 #endif