1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___ITERATOR_PREV_H 11 #define _LIBCPP___ITERATOR_PREV_H 12 13 #include <__config> 14 #include <__debug> 15 #include <__function_like.h> 16 #include <__iterator/advance.h> 17 #include <__iterator/concepts.h> 18 #include <__iterator/incrementable_traits.h> 19 #include <__iterator/iterator_traits.h> 20 #include <type_traits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 #pragma GCC system_header 24 #endif 25 26 _LIBCPP_PUSH_MACROS 27 #include <__undef_macros> 28 29 _LIBCPP_BEGIN_NAMESPACE_STD 30 31 template <class _InputIter> 32 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 33 typename enable_if<__is_cpp17_input_iterator<_InputIter>::value, _InputIter>::type 34 prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) { 35 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value, 36 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator"); 37 _VSTD::advance(__x, -__n); 38 return __x; 39 } 40 41 #if !defined(_LIBCPP_HAS_NO_RANGES) 42 43 namespace ranges { 44 struct __prev_fn final : private __function_like { 45 _LIBCPP_HIDE_FROM_ABI 46 constexpr explicit __prev_fn(__tag __x) noexcept : __function_like(__x) {} 47 48 template <bidirectional_iterator _Ip> 49 _LIBCPP_HIDE_FROM_ABI 50 constexpr _Ip operator()(_Ip __x) const { 51 --__x; 52 return __x; 53 } 54 55 template <bidirectional_iterator _Ip> 56 _LIBCPP_HIDE_FROM_ABI 57 constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const { 58 ranges::advance(__x, -__n); 59 return __x; 60 } 61 62 template <bidirectional_iterator _Ip> 63 _LIBCPP_HIDE_FROM_ABI 64 constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound) const { 65 ranges::advance(__x, -__n, __bound); 66 return __x; 67 } 68 }; 69 70 inline constexpr auto prev = __prev_fn(__function_like::__tag()); 71 } // namespace ranges 72 73 #endif // !defined(_LIBCPP_HAS_NO_RANGES) 74 75 _LIBCPP_END_NAMESPACE_STD 76 77 _LIBCPP_POP_MACROS 78 79 #endif // _LIBCPP___ITERATOR_PREV_H 80