distance.h 3.0 KB

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