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 <__config> 14 #include <__iterator/concepts.h> 15 #include <__iterator/incrementable_traits.h> 16 #include <__iterator/iterator_traits.h> 17 #include <__ranges/access.h> 18 #include <__ranges/concepts.h> 19 #include <__ranges/size.h> 20 #include <type_traits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 # pragma GCC system_header 24 #endif 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 28 template <class _InputIter> 29 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 30 typename iterator_traits<_InputIter>::difference_type 31 __distance(_InputIter __first, _InputIter __last, input_iterator_tag) 32 { 33 typename iterator_traits<_InputIter>::difference_type __r(0); 34 for (; __first != __last; ++__first) 35 ++__r; 36 return __r; 37 } 38 39 template <class _RandIter> 40 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 41 typename iterator_traits<_RandIter>::difference_type 42 __distance(_RandIter __first, _RandIter __last, random_access_iterator_tag) 43 { 44 return __last - __first; 45 } 46 47 template <class _InputIter> 48 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 49 typename iterator_traits<_InputIter>::difference_type 50 distance(_InputIter __first, _InputIter __last) 51 { 52 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category()); 53 } 54 55 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 56 57 // [range.iter.op.distance] 58 59 namespace ranges { 60 namespace __distance { 61 62 struct __fn { 63 template<class _Ip, sentinel_for<_Ip> _Sp> 64 requires (!sized_sentinel_for<_Sp, _Ip>) 65 _LIBCPP_HIDE_FROM_ABI 66 constexpr iter_difference_t<_Ip> operator()(_Ip __first, _Sp __last) const { 67 iter_difference_t<_Ip> __n = 0; 68 while (__first != __last) { 69 ++__first; 70 ++__n; 71 } 72 return __n; 73 } 74 75 template<class _Ip, sized_sentinel_for<decay_t<_Ip>> _Sp> 76 _LIBCPP_HIDE_FROM_ABI 77 constexpr iter_difference_t<_Ip> operator()(_Ip&& __first, _Sp __last) const { 78 if constexpr (sized_sentinel_for<_Sp, __uncvref_t<_Ip>>) { 79 return __last - __first; 80 } else { 81 return __last - decay_t<_Ip>(__first); 82 } 83 } 84 85 template<range _Rp> 86 _LIBCPP_HIDE_FROM_ABI 87 constexpr range_difference_t<_Rp> operator()(_Rp&& __r) const { 88 if constexpr (sized_range<_Rp>) { 89 return static_cast<range_difference_t<_Rp>>(ranges::size(__r)); 90 } else { 91 return operator()(ranges::begin(__r), ranges::end(__r)); 92 } 93 } 94 }; 95 96 } // namespace __distance 97 98 inline namespace __cpo { 99 inline constexpr auto distance = __distance::__fn{}; 100 } // namespace __cpo 101 } // namespace ranges 102 103 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) 104 105 _LIBCPP_END_NAMESPACE_STD 106 107 #endif // _LIBCPP___ITERATOR_DISTANCE_H 108