master
 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
27template <class _AlgPolicy, class _BidirectionalIterator>
28inline _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
38template <class _AlgPolicy, class _RandomAccessIterator>
39inline _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
46template <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
52template <class _BidirectionalIterator>
53inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
54reverse(_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