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_DISTANCE_H
11#define _LIBCPP___ITERATOR_DISTANCE_H
12
13#include <__algorithm/for_each_segment.h>
14#include <__concepts/same_as.h>
15#include <__config>
16#include <__iterator/concepts.h>
17#include <__iterator/incrementable_traits.h>
18#include <__iterator/iterator_traits.h>
19#include <__iterator/segmented_iterator.h>
20#include <__ranges/access.h>
21#include <__ranges/concepts.h>
22#include <__ranges/size.h>
23#include <__type_traits/decay.h>
24#include <__type_traits/enable_if.h>
25#include <__type_traits/is_array.h>
26#include <__type_traits/remove_reference.h>
27#include <__utility/move.h>
28
29#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30# pragma GCC system_header
31#endif
32
33_LIBCPP_PUSH_MACROS
34#include <__undef_macros>
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37
38#if _LIBCPP_STD_VER >= 20
39template <class _Iter>
40using __iter_distance_t _LIBCPP_NODEBUG = std::iter_difference_t<_Iter>;
41#else
42template <class _Iter>
43using __iter_distance_t _LIBCPP_NODEBUG = typename iterator_traits<_Iter>::difference_type;
44#endif
45
46template <class _RandIter, __enable_if_t<__has_random_access_iterator_category<_RandIter>::value, int> = 0>
47inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 __iter_distance_t<_RandIter>
48__distance(_RandIter __first, _RandIter __last) {
49 return __last - __first;
50}
51
52template <class _InputIter, class _Sent>
53inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 __iter_distance_t<_InputIter>
54__distance(_InputIter __first, _Sent __last) {
55 __iter_distance_t<_InputIter> __r(0);
56#if _LIBCPP_STD_VER >= 20
57 if constexpr (same_as<_InputIter, _Sent> && __is_segmented_iterator_v<_InputIter>) {
58 std::__for_each_segment(__first, __last, [&__r](auto __lfirst, auto __llast) {
59 __r += std::__distance(__lfirst, __llast);
60 });
61 } else
62#endif
63 {
64 for (; __first != __last; ++__first)
65 ++__r;
66 }
67 return __r;
68}
69
70template <class _InputIter>
71[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
72typename iterator_traits<_InputIter>::difference_type
73distance(_InputIter __first, _InputIter __last) {
74 return std::__distance(__first, __last);
75}
76
77#if _LIBCPP_STD_VER >= 20
78
79// [range.iter.op.distance]
80
81namespace ranges {
82struct __distance {
83 template <class _Ip, sentinel_for<_Ip> _Sp>
84 requires(!sized_sentinel_for<_Sp, _Ip>)
85 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<_Ip> operator()(_Ip __first, _Sp __last) const {
86 return std::__distance(std::move(__first), std::move(__last));
87 }
88
89 template <class _Ip, sized_sentinel_for<decay_t<_Ip>> _Sp>
90 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iter_difference_t<decay_t<_Ip>>
91 operator()(_Ip&& __first, _Sp __last) const {
92 if constexpr (!is_array_v<remove_reference_t<_Ip>>) {
93 return __last - __first;
94 } else {
95 return __last - static_cast<decay_t<_Ip>>(__first);
96 }
97 }
98
99 template <range _Rp>
100 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr range_difference_t<_Rp> operator()(_Rp&& __r) const {
101 if constexpr (sized_range<_Rp>) {
102 return static_cast<range_difference_t<_Rp>>(ranges::size(__r));
103 } else {
104 return operator()(ranges::begin(__r), ranges::end(__r));
105 }
106 }
107};
108
109inline namespace __cpo {
110inline constexpr auto distance = __distance{};
111} // namespace __cpo
112} // namespace ranges
113
114#endif // _LIBCPP_STD_VER >= 20
115
116_LIBCPP_END_NAMESPACE_STD
117
118_LIBCPP_POP_MACROS
119
120#endif // _LIBCPP___ITERATOR_DISTANCE_H
121