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_RANGES_REVERSE_H 10 #define _LIBCPP___ALGORITHM_RANGES_REVERSE_H 11 12 #include <__config> 13 #include <__iterator/concepts.h> 14 #include <__iterator/iter_swap.h> 15 #include <__iterator/next.h> 16 #include <__iterator/permutable.h> 17 #include <__ranges/access.h> 18 #include <__ranges/concepts.h> 19 #include <__ranges/dangling.h> 20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22 # pragma GCC system_header 23 #endif 24 25 #if _LIBCPP_STD_VER >= 20 26 27 _LIBCPP_BEGIN_NAMESPACE_STD 28 29 namespace ranges { 30 struct __reverse { 31 template <bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent> 32 requires permutable<_Iter> 33 _LIBCPP_HIDE_FROM_ABI constexpr _Iter operator()(_Iter __first, _Sent __last) const { 34 if constexpr (random_access_iterator<_Iter>) { 35 if (__first == __last) 36 return __first; 37 38 auto __end = ranges::next(__first, __last); 39 auto __ret = __end; 40 41 while (__first < --__end) { 42 ranges::iter_swap(__first, __end); 43 ++__first; 44 } 45 return __ret; 46 } else { 47 auto __end = ranges::next(__first, __last); 48 auto __ret = __end; 49 50 while (__first != __end) { 51 if (__first == --__end) 52 break; 53 54 ranges::iter_swap(__first, __end); 55 ++__first; 56 } 57 return __ret; 58 } 59 } 60 61 template <bidirectional_range _Range> 62 requires permutable<iterator_t<_Range>> 63 _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Range> operator()(_Range&& __range) const { 64 return (*this)(ranges::begin(__range), ranges::end(__range)); 65 } 66 }; 67 68 inline namespace __cpo { 69 inline constexpr auto reverse = __reverse{}; 70 } // namespace __cpo 71 } // namespace ranges 72 73 _LIBCPP_END_NAMESPACE_STD 74 75 #endif // _LIBCPP_STD_VER >= 20 76 77 #endif // _LIBCPP___ALGORITHM_RANGES_REVERSE_H 78