xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/reverse.h (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_REVERSE_H
10 #define _LIBCPP___ALGORITHM_REVERSE_H
11 
12 #include <__algorithm/iter_swap.h>
13 #include <__algorithm/iterator_operations.h>
14 #include <__config>
15 #include <__iterator/iterator_traits.h>
16 #include <__utility/move.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template <class _AlgPolicy, class _BidirectionalIterator>
28 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
29 __reverse_impl(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) {
30   while (__first != __last) {
31     if (__first == --__last)
32       break;
33     _IterOps<_AlgPolicy>::iter_swap(__first, __last);
34     ++__first;
35   }
36 }
37 
38 template <class _AlgPolicy, class _RandomAccessIterator>
39 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
40 __reverse_impl(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) {
41   if (__first != __last)
42     for (; __first < --__last; ++__first)
43       _IterOps<_AlgPolicy>::iter_swap(__first, __last);
44 }
45 
46 template <class _AlgPolicy, class _BidirectionalIterator, class _Sentinel>
47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reverse(_BidirectionalIterator __first, _Sentinel __last) {
48   using _IterCategory = typename _IterOps<_AlgPolicy>::template __iterator_category<_BidirectionalIterator>;
49   std::__reverse_impl<_AlgPolicy>(std::move(__first), std::move(__last), _IterCategory());
50 }
51 
52 template <class _BidirectionalIterator>
53 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
54 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) {
55   std::__reverse<_ClassicAlgPolicy>(std::move(__first), std::move(__last));
56 }
57 
58 _LIBCPP_END_NAMESPACE_STD
59 
60 _LIBCPP_POP_MACROS
61 
62 #endif // _LIBCPP___ALGORITHM_REVERSE_H
63