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