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_BEGIN_NAMESPACE_STD 23 24 template <class _AlgPolicy, class _BidirectionalIterator> 25 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 26 void 27 __reverse_impl(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) 28 { 29 while (__first != __last) 30 { 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_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 40 void 41 __reverse_impl(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) 42 { 43 if (__first != __last) 44 for (; __first < --__last; ++__first) 45 _IterOps<_AlgPolicy>::iter_swap(__first, __last); 46 } 47 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()); 53 } 54 55 template <class _BidirectionalIterator> 56 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 57 void 58 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) 59 { 60 std::__reverse<_ClassicAlgPolicy>(std::move(__first), std::move(__last)); 61 } 62 63 _LIBCPP_END_NAMESPACE_STD 64 65 #endif // _LIBCPP___ALGORITHM_REVERSE_H 66