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 <__iterator/iterator_traits.h> 14 #include <type_traits> 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 // reverse 26 27 template <class _BidirectionalIterator> 28 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 29 void 30 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) 31 { 32 while (__first != __last) 33 { 34 if (__first == --__last) 35 break; 36 _VSTD::iter_swap(__first, __last); 37 ++__first; 38 } 39 } 40 41 template <class _RandomAccessIterator> 42 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 43 void 44 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) 45 { 46 if (__first != __last) 47 for (; __first < --__last; ++__first) 48 _VSTD::iter_swap(__first, __last); 49 } 50 51 template <class _BidirectionalIterator> 52 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 53 void 54 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) 55 { 56 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category()); 57 } 58 59 // reverse_copy 60 61 template <class _BidirectionalIterator, class _OutputIterator> 62 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 63 _OutputIterator 64 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) 65 { 66 for (; __first != __last; ++__result) 67 *__result = *--__last; 68 return __result; 69 } 70 71 _LIBCPP_END_NAMESPACE_STD 72 73 _LIBCPP_POP_MACROS 74 75 #endif // _LIBCPP___ALGORITHM_REVERSE_H 76