1fe6060f1SDimitry Andric // -*- C++ -*- 2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 3fe6060f1SDimitry Andric // 4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7fe6060f1SDimitry Andric // 8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 9fe6060f1SDimitry Andric 10fe6060f1SDimitry Andric #ifndef _LIBCPP___ITERATOR_PREV_H 11fe6060f1SDimitry Andric #define _LIBCPP___ITERATOR_PREV_H 12fe6060f1SDimitry Andric 1381ad6265SDimitry Andric #include <__assert> 14fe6060f1SDimitry Andric #include <__config> 15fe6060f1SDimitry Andric #include <__iterator/advance.h> 16fe6060f1SDimitry Andric #include <__iterator/concepts.h> 17fe6060f1SDimitry Andric #include <__iterator/incrementable_traits.h> 18fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h> 19bdd1243dSDimitry Andric #include <__type_traits/enable_if.h> 20fe6060f1SDimitry Andric 21fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22fe6060f1SDimitry Andric # pragma GCC system_header 23fe6060f1SDimitry Andric #endif 24fe6060f1SDimitry Andric 25fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 26fe6060f1SDimitry Andric 275f757f3fSDimitry Andric template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0> 28cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter 29fe6060f1SDimitry Andric prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) { 30*1db9f3b2SDimitry Andric // Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation. 31*1db9f3b2SDimitry Andric // Note that this check duplicates the similar check in `std::advance`. 32*1db9f3b2SDimitry Andric _LIBCPP_ASSERT_PEDANTIC(__n <= 0 || __has_bidirectional_iterator_category<_InputIter>::value, 33fe6060f1SDimitry Andric "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator"); 345f757f3fSDimitry Andric std::advance(__x, -__n); 35fe6060f1SDimitry Andric return __x; 36fe6060f1SDimitry Andric } 37fe6060f1SDimitry Andric 3806c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 39fe6060f1SDimitry Andric 4004eeddc0SDimitry Andric // [range.iter.op.prev] 41fe6060f1SDimitry Andric 4204eeddc0SDimitry Andric namespace ranges { 4304eeddc0SDimitry Andric namespace __prev { 4404eeddc0SDimitry Andric 4504eeddc0SDimitry Andric struct __fn { 46fe6060f1SDimitry Andric template <bidirectional_iterator _Ip> 47cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const { 48fe6060f1SDimitry Andric --__x; 49fe6060f1SDimitry Andric return __x; 50fe6060f1SDimitry Andric } 51fe6060f1SDimitry Andric 52fe6060f1SDimitry Andric template <bidirectional_iterator _Ip> 53cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const { 54fe6060f1SDimitry Andric ranges::advance(__x, -__n); 55fe6060f1SDimitry Andric return __x; 56fe6060f1SDimitry Andric } 57fe6060f1SDimitry Andric 58fe6060f1SDimitry Andric template <bidirectional_iterator _Ip> 5981ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const { 6081ad6265SDimitry Andric ranges::advance(__x, -__n, __bound_iter); 61fe6060f1SDimitry Andric return __x; 62fe6060f1SDimitry Andric } 63fe6060f1SDimitry Andric }; 64fe6060f1SDimitry Andric 6504eeddc0SDimitry Andric } // namespace __prev 6604eeddc0SDimitry Andric 6704eeddc0SDimitry Andric inline namespace __cpo { 6804eeddc0SDimitry Andric inline constexpr auto prev = __prev::__fn{}; 6904eeddc0SDimitry Andric } // namespace __cpo 70fe6060f1SDimitry Andric } // namespace ranges 71fe6060f1SDimitry Andric 7206c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 73fe6060f1SDimitry Andric 74fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 75fe6060f1SDimitry Andric 76fe6060f1SDimitry Andric #endif // _LIBCPP___ITERATOR_PREV_H 77