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 <__config> 13 #include <__algorithm/iter_swap.h> 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_PUSH_MACROS 21 #include <__undef_macros> 22 23 _LIBCPP_BEGIN_NAMESPACE_STD 24 25 template <class _BidirectionalIterator> 26 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 27 void 28 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) 29 { 30 while (__first != __last) 31 { 32 if (__first == --__last) 33 break; 34 _VSTD::iter_swap(__first, __last); 35 ++__first; 36 } 37 } 38 39 template <class _RandomAccessIterator> 40 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 41 void 42 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) 43 { 44 if (__first != __last) 45 for (; __first < --__last; ++__first) 46 _VSTD::iter_swap(__first, __last); 47 } 48 49 template <class _BidirectionalIterator> 50 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 51 void 52 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) 53 { 54 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category()); 55 } 56 57 _LIBCPP_END_NAMESPACE_STD 58 59 _LIBCPP_POP_MACROS 60 61 #endif // _LIBCPP___ALGORITHM_REVERSE_H 62