xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/reverse.h (revision d8096b2df282d7a50e56eddba523bcdda1676106)
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 <__config>
14 #include <__iterator/iterator_traits.h>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #pragma GCC system_header
18 #endif
19 
20 _LIBCPP_BEGIN_NAMESPACE_STD
21 
22 template <class _BidirectionalIterator>
23 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
24 void
25 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
26 {
27     while (__first != __last)
28     {
29         if (__first == --__last)
30             break;
31         _VSTD::iter_swap(__first, __last);
32         ++__first;
33     }
34 }
35 
36 template <class _RandomAccessIterator>
37 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
38 void
39 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
40 {
41     if (__first != __last)
42         for (; __first < --__last; ++__first)
43             _VSTD::iter_swap(__first, __last);
44 }
45 
46 template <class _BidirectionalIterator>
47 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
48 void
49 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
50 {
51     _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
52 }
53 
54 _LIBCPP_END_NAMESPACE_STD
55 
56 #endif // _LIBCPP___ALGORITHM_REVERSE_H
57