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_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void 26 __reverse_impl(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) { 27 while (__first != __last) { 28 if (__first == --__last) 29 break; 30 _IterOps<_AlgPolicy>::iter_swap(__first, __last); 31 ++__first; 32 } 33 } 34 35 template <class _AlgPolicy, class _RandomAccessIterator> 36 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void 37 __reverse_impl(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) { 38 if (__first != __last) 39 for (; __first < --__last; ++__first) 40 _IterOps<_AlgPolicy>::iter_swap(__first, __last); 41 } 42 43 template <class _AlgPolicy, class _BidirectionalIterator, class _Sentinel> 44 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reverse(_BidirectionalIterator __first, _Sentinel __last) { 45 using _IterCategory = typename _IterOps<_AlgPolicy>::template __iterator_category<_BidirectionalIterator>; 46 std::__reverse_impl<_AlgPolicy>(std::move(__first), std::move(__last), _IterCategory()); 47 } 48 49 template <class _BidirectionalIterator> 50 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void 51 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) { 52 std::__reverse<_ClassicAlgPolicy>(std::move(__first), std::move(__last)); 53 } 54 55 _LIBCPP_END_NAMESPACE_STD 56 57 #endif // _LIBCPP___ALGORITHM_REVERSE_H 58