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 <__iterator/advance.h>
15#include <__iterator/concepts.h>
16#include <__iterator/incrementable_traits.h>
17#include <__iterator/iterator_traits.h>
18#include <__type_traits/enable_if.h>
19#include <__utility/move.h>
20
21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23#endif
24
25_LIBCPP_PUSH_MACROS
26#include <__undef_macros>
27
28_LIBCPP_BEGIN_NAMESPACE_STD
29
30template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
31[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter
32prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n) {
33 std::advance(__x, -__n);
34 return __x;
35}
36
37// LWG 3197
38// It is unclear what the implications of "BidirectionalIterator" in the standard are.
39// However, calling std::prev(non-bidi-iterator) is obviously an error and we should catch it at compile time.
40template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
41[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter prev(_InputIter __it) {
42 static_assert(__has_bidirectional_iterator_category<_InputIter>::value,
43 "Attempt to prev(it) with a non-bidirectional iterator");
44 return std::prev(std::move(__it), 1);
45}
46
47#if _LIBCPP_STD_VER >= 20
48
49// [range.iter.op.prev]
50
51namespace ranges {
52struct __prev {
53 template <bidirectional_iterator _Ip>
54 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {
55 --__x;
56 return __x;
57 }
58
59 template <bidirectional_iterator _Ip>
60 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
61 ranges::advance(__x, -__n);
62 return __x;
63 }
64
65 template <bidirectional_iterator _Ip>
66 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip
67 operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const {
68 ranges::advance(__x, -__n, __bound_iter);
69 return __x;
70 }
71};
72
73inline namespace __cpo {
74inline constexpr auto prev = __prev{};
75} // namespace __cpo
76} // namespace ranges
77
78#endif // _LIBCPP_STD_VER >= 20
79
80_LIBCPP_END_NAMESPACE_STD
81
82_LIBCPP_POP_MACROS
83
84#endif // _LIBCPP___ITERATOR_PREV_H
85