1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_REVERSE_H
10 #define _LIBCPP___ALGORITHM_REVERSE_H
12 #include <__algorithm/iter_swap.h>
13 #include <__algorithm/iterator_operations.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__utility/move.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
22 _LIBCPP_BEGIN_NAMESPACE_STD
24 template <class _AlgPolicy
, class _BidirectionalIterator
>
25 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
27 __reverse_impl(_BidirectionalIterator __first
, _BidirectionalIterator __last
, bidirectional_iterator_tag
)
29 while (__first
!= __last
)
31 if (__first
== --__last
)
33 _IterOps
<_AlgPolicy
>::iter_swap(__first
, __last
);
38 template <class _AlgPolicy
, class _RandomAccessIterator
>
39 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
41 __reverse_impl(_RandomAccessIterator __first
, _RandomAccessIterator __last
, random_access_iterator_tag
)
43 if (__first
!= __last
)
44 for (; __first
< --__last
; ++__first
)
45 _IterOps
<_AlgPolicy
>::iter_swap(__first
, __last
);
48 template <class _AlgPolicy
, class _BidirectionalIterator
, class _Sentinel
>
49 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
50 void __reverse(_BidirectionalIterator __first
, _Sentinel __last
) {
51 using _IterCategory
= typename _IterOps
<_AlgPolicy
>::template __iterator_category
<_BidirectionalIterator
>;
52 std::__reverse_impl
<_AlgPolicy
>(std::move(__first
), std::move(__last
), _IterCategory());
55 template <class _BidirectionalIterator
>
56 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
58 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
60 std::__reverse
<_ClassicAlgPolicy
>(std::move(__first
), std::move(__last
));
63 _LIBCPP_END_NAMESPACE_STD
65 #endif // _LIBCPP___ALGORITHM_REVERSE_H