Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:53

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___ITERATOR_PREV_H
0011 #define _LIBCPP___ITERATOR_PREV_H
0012 
0013 #include <__assert>
0014 #include <__config>
0015 #include <__iterator/advance.h>
0016 #include <__iterator/concepts.h>
0017 #include <__iterator/incrementable_traits.h>
0018 #include <__iterator/iterator_traits.h>
0019 #include <__type_traits/enable_if.h>
0020 #include <__utility/move.h>
0021 
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 #  pragma GCC system_header
0024 #endif
0025 
0026 _LIBCPP_PUSH_MACROS
0027 #include <__undef_macros>
0028 
0029 _LIBCPP_BEGIN_NAMESPACE_STD
0030 
0031 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
0032 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter
0033 prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n) {
0034   // Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.
0035   // Note that this check duplicates the similar check in `std::advance`.
0036   _LIBCPP_ASSERT_PEDANTIC(__n <= 0 || __has_bidirectional_iterator_category<_InputIter>::value,
0037                           "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
0038   std::advance(__x, -__n);
0039   return __x;
0040 }
0041 
0042 // LWG 3197
0043 // It is unclear what the implications of "BidirectionalIterator" in the standard are.
0044 // However, calling std::prev(non-bidi-iterator) is obviously an error and we should catch it at compile time.
0045 template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
0046 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter prev(_InputIter __it) {
0047   static_assert(__has_bidirectional_iterator_category<_InputIter>::value,
0048                 "Attempt to prev(it) with a non-bidirectional iterator");
0049   return std::prev(std::move(__it), 1);
0050 }
0051 
0052 #if _LIBCPP_STD_VER >= 20
0053 
0054 // [range.iter.op.prev]
0055 
0056 namespace ranges {
0057 struct __prev {
0058   template <bidirectional_iterator _Ip>
0059   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {
0060     --__x;
0061     return __x;
0062   }
0063 
0064   template <bidirectional_iterator _Ip>
0065   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
0066     ranges::advance(__x, -__n);
0067     return __x;
0068   }
0069 
0070   template <bidirectional_iterator _Ip>
0071   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip
0072   operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const {
0073     ranges::advance(__x, -__n, __bound_iter);
0074     return __x;
0075   }
0076 };
0077 
0078 inline namespace __cpo {
0079 inline constexpr auto prev = __prev{};
0080 } // namespace __cpo
0081 } // namespace ranges
0082 
0083 #endif // _LIBCPP_STD_VER >= 20
0084 
0085 _LIBCPP_END_NAMESPACE_STD
0086 
0087 _LIBCPP_POP_MACROS
0088 
0089 #endif // _LIBCPP___ITERATOR_PREV_H