counted.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_COUNTED_H
  10. #define _LIBCPP___RANGES_COUNTED_H
  11. #include <__concepts/convertible_to.h>
  12. #include <__config>
  13. #include <__iterator/concepts.h>
  14. #include <__iterator/counted_iterator.h>
  15. #include <__iterator/default_sentinel.h>
  16. #include <__iterator/incrementable_traits.h>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__memory/pointer_traits.h>
  19. #include <__ranges/subrange.h>
  20. #include <__type_traits/decay.h>
  21. #include <__utility/forward.h>
  22. #include <__utility/move.h>
  23. #include <cstddef>
  24. #include <span>
  25. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  26. # pragma GCC system_header
  27. #endif
  28. _LIBCPP_PUSH_MACROS
  29. #include <__undef_macros>
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. #if _LIBCPP_STD_VER >= 20
  32. namespace ranges::views {
  33. namespace __counted {
  34. struct __fn {
  35. template <contiguous_iterator _It>
  36. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  37. __go(_It __it,
  38. iter_difference_t<_It> __count) noexcept(noexcept(span(std::to_address(__it), static_cast<size_t>(__count))))
  39. // Deliberately omit return-type SFINAE, because to_address is not SFINAE-friendly
  40. {
  41. return span(std::to_address(__it), static_cast<size_t>(__count));
  42. }
  43. template <random_access_iterator _It>
  44. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  45. __go(_It __it, iter_difference_t<_It> __count) noexcept(noexcept(subrange(__it, __it + __count)))
  46. -> decltype(subrange(__it, __it + __count)) {
  47. return subrange(__it, __it + __count);
  48. }
  49. template <class _It>
  50. _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_It __it, iter_difference_t<_It> __count) noexcept(
  51. noexcept(subrange(counted_iterator(std::move(__it), __count), default_sentinel)))
  52. -> decltype(subrange(counted_iterator(std::move(__it), __count), default_sentinel)) {
  53. return subrange(counted_iterator(std::move(__it), __count), default_sentinel);
  54. }
  55. template <class _It, convertible_to<iter_difference_t<_It>> _Diff>
  56. requires input_or_output_iterator<decay_t<_It>>
  57. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_It&& __it, _Diff&& __count) const
  58. noexcept(noexcept(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))))
  59. -> decltype(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))) {
  60. return __go(std::forward<_It>(__it), std::forward<_Diff>(__count));
  61. }
  62. };
  63. } // namespace __counted
  64. inline namespace __cpo {
  65. inline constexpr auto counted = __counted::__fn{};
  66. } // namespace __cpo
  67. } // namespace ranges::views
  68. #endif // _LIBCPP_STD_VER >= 20
  69. _LIBCPP_END_NAMESPACE_STD
  70. _LIBCPP_POP_MACROS
  71. #endif // _LIBCPP___RANGES_COUNTED_H