size.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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___RANGES_SIZE_H
  10. #define _LIBCPP___RANGES_SIZE_H
  11. #include <__concepts/arithmetic.h>
  12. #include <__concepts/class_or_enum.h>
  13. #include <__config>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__ranges/access.h>
  17. #include <__type_traits/decay.h>
  18. #include <__type_traits/make_signed.h>
  19. #include <__type_traits/make_unsigned.h>
  20. #include <__type_traits/remove_cvref.h>
  21. #include <__utility/auto_cast.h>
  22. #include <__utility/declval.h>
  23. #include <cstddef>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. #if _LIBCPP_STD_VER >= 20
  29. namespace ranges {
  30. template <class>
  31. inline constexpr bool disable_sized_range = false;
  32. } // namespace ranges
  33. // [range.prim.size]
  34. namespace ranges {
  35. namespace __size {
  36. void size() = delete;
  37. template <class _Tp>
  38. concept __size_enabled = !disable_sized_range<remove_cvref_t<_Tp>>;
  39. template <class _Tp>
  40. concept __member_size = __size_enabled<_Tp> && requires(_Tp&& __t) {
  41. { _LIBCPP_AUTO_CAST(__t.size()) } -> __integer_like;
  42. };
  43. template <class _Tp>
  44. concept __unqualified_size =
  45. __size_enabled<_Tp> && !__member_size<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
  46. { _LIBCPP_AUTO_CAST(size(__t)) } -> __integer_like;
  47. };
  48. template <class _Tp>
  49. concept __difference =
  50. !__member_size<_Tp> && !__unqualified_size<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
  51. { ranges::begin(__t) } -> forward_iterator;
  52. { ranges::end(__t) } -> sized_sentinel_for<decltype(ranges::begin(std::declval<_Tp>()))>;
  53. };
  54. struct __fn {
  55. // `[range.prim.size]`: the array case (for rvalues).
  56. template <class _Tp, size_t _Sz>
  57. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&&)[_Sz]) const noexcept {
  58. return _Sz;
  59. }
  60. // `[range.prim.size]`: the array case (for lvalues).
  61. template <class _Tp, size_t _Sz>
  62. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&)[_Sz]) const noexcept {
  63. return _Sz;
  64. }
  65. // `[range.prim.size]`: `auto(t.size())` is a valid expression.
  66. template <__member_size _Tp>
  67. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
  68. noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.size()))) {
  69. return _LIBCPP_AUTO_CAST(__t.size());
  70. }
  71. // `[range.prim.size]`: `auto(size(t))` is a valid expression.
  72. template <__unqualified_size _Tp>
  73. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
  74. noexcept(noexcept(_LIBCPP_AUTO_CAST(size(__t)))) {
  75. return _LIBCPP_AUTO_CAST(size(__t));
  76. }
  77. // [range.prim.size]: the `to-unsigned-like` case.
  78. template <__difference _Tp>
  79. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  80. noexcept(noexcept(std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t))))
  81. -> decltype(std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t))) {
  82. return std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t));
  83. }
  84. };
  85. } // namespace __size
  86. inline namespace __cpo {
  87. inline constexpr auto size = __size::__fn{};
  88. } // namespace __cpo
  89. } // namespace ranges
  90. // [range.prim.ssize]
  91. namespace ranges {
  92. namespace __ssize {
  93. struct __fn {
  94. template <class _Tp>
  95. requires requires(_Tp&& __t) { ranges::size(__t); }
  96. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr integral auto operator()(_Tp&& __t) const
  97. noexcept(noexcept(ranges::size(__t))) {
  98. using _Signed = make_signed_t<decltype(ranges::size(__t))>;
  99. if constexpr (sizeof(ptrdiff_t) > sizeof(_Signed))
  100. return static_cast<ptrdiff_t>(ranges::size(__t));
  101. else
  102. return static_cast<_Signed>(ranges::size(__t));
  103. }
  104. };
  105. } // namespace __ssize
  106. inline namespace __cpo {
  107. inline constexpr auto ssize = __ssize::__fn{};
  108. } // namespace __cpo
  109. } // namespace ranges
  110. #endif // _LIBCPP_STD_VER >= 20
  111. _LIBCPP_END_NAMESPACE_STD
  112. #endif // _LIBCPP___RANGES_SIZE_H