counted.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_BEGIN_NAMESPACE_STD
  29. #if _LIBCPP_STD_VER >= 20
  30. namespace ranges::views {
  31. namespace __counted {
  32. struct __fn {
  33. template<contiguous_iterator _It>
  34. _LIBCPP_HIDE_FROM_ABI
  35. static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
  36. noexcept(noexcept(span(std::to_address(__it), static_cast<size_t>(__count))))
  37. // Deliberately omit return-type SFINAE, because to_address is not SFINAE-friendly
  38. { return span(std::to_address(__it), static_cast<size_t>(__count)); }
  39. template<random_access_iterator _It>
  40. _LIBCPP_HIDE_FROM_ABI
  41. static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
  42. noexcept(noexcept(subrange(__it, __it + __count)))
  43. -> decltype( subrange(__it, __it + __count))
  44. { return subrange(__it, __it + __count); }
  45. template<class _It>
  46. _LIBCPP_HIDE_FROM_ABI
  47. static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
  48. noexcept(noexcept(subrange(counted_iterator(std::move(__it), __count), default_sentinel)))
  49. -> decltype( subrange(counted_iterator(std::move(__it), __count), default_sentinel))
  50. { return subrange(counted_iterator(std::move(__it), __count), default_sentinel); }
  51. template<class _It, convertible_to<iter_difference_t<_It>> _Diff>
  52. requires input_or_output_iterator<decay_t<_It>>
  53. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  54. constexpr auto operator()(_It&& __it, _Diff&& __count) const
  55. noexcept(noexcept(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))))
  56. -> decltype( __go(std::forward<_It>(__it), std::forward<_Diff>(__count)))
  57. { return __go(std::forward<_It>(__it), std::forward<_Diff>(__count)); }
  58. };
  59. } // namespace __counted
  60. inline namespace __cpo {
  61. inline constexpr auto counted = __counted::__fn{};
  62. } // namespace __cpo
  63. } // namespace ranges::views
  64. #endif // _LIBCPP_STD_VER >= 20
  65. _LIBCPP_END_NAMESPACE_STD
  66. #endif // _LIBCPP___RANGES_COUNTED_H