counted.h 2.9 KB

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