take_view.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_TAKE_VIEW_H
  10. #define _LIBCPP___RANGES_TAKE_VIEW_H
  11. #include <__algorithm/min.h>
  12. #include <__config>
  13. #include <__iterator/concepts.h>
  14. #include <__iterator/counted_iterator.h>
  15. #include <__iterator/default_sentinel.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__ranges/access.h>
  18. #include <__ranges/all.h>
  19. #include <__ranges/concepts.h>
  20. #include <__ranges/enable_borrowed_range.h>
  21. #include <__ranges/size.h>
  22. #include <__ranges/view_interface.h>
  23. #include <__utility/move.h>
  24. #include <concepts>
  25. #include <type_traits>
  26. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  27. # pragma GCC system_header
  28. #endif
  29. _LIBCPP_PUSH_MACROS
  30. #include <__undef_macros>
  31. _LIBCPP_BEGIN_NAMESPACE_STD
  32. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  33. namespace ranges {
  34. template<view _View>
  35. class take_view : public view_interface<take_view<_View>> {
  36. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  37. range_difference_t<_View> __count_ = 0;
  38. template<bool> class __sentinel;
  39. public:
  40. _LIBCPP_HIDE_FROM_ABI
  41. take_view() requires default_initializable<_View> = default;
  42. _LIBCPP_HIDE_FROM_ABI
  43. constexpr take_view(_View __base, range_difference_t<_View> __count)
  44. : __base_(std::move(__base)), __count_(__count) {}
  45. _LIBCPP_HIDE_FROM_ABI
  46. constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
  47. _LIBCPP_HIDE_FROM_ABI
  48. constexpr _View base() && { return std::move(__base_); }
  49. _LIBCPP_HIDE_FROM_ABI
  50. constexpr auto begin() requires (!__simple_view<_View>) {
  51. if constexpr (sized_range<_View>) {
  52. if constexpr (random_access_range<_View>) {
  53. return ranges::begin(__base_);
  54. } else {
  55. using _DifferenceT = range_difference_t<_View>;
  56. auto __size = size();
  57. return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size));
  58. }
  59. } else {
  60. return counted_iterator(ranges::begin(__base_), __count_);
  61. }
  62. }
  63. _LIBCPP_HIDE_FROM_ABI
  64. constexpr auto begin() const requires range<const _View> {
  65. if constexpr (sized_range<const _View>) {
  66. if constexpr (random_access_range<const _View>) {
  67. return ranges::begin(__base_);
  68. } else {
  69. using _DifferenceT = range_difference_t<const _View>;
  70. auto __size = size();
  71. return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size));
  72. }
  73. } else {
  74. return counted_iterator(ranges::begin(__base_), __count_);
  75. }
  76. }
  77. _LIBCPP_HIDE_FROM_ABI
  78. constexpr auto end() requires (!__simple_view<_View>) {
  79. if constexpr (sized_range<_View>) {
  80. if constexpr (random_access_range<_View>) {
  81. return ranges::begin(__base_) + size();
  82. } else {
  83. return default_sentinel;
  84. }
  85. } else {
  86. return __sentinel<false>{ranges::end(__base_)};
  87. }
  88. }
  89. _LIBCPP_HIDE_FROM_ABI
  90. constexpr auto end() const requires range<const _View> {
  91. if constexpr (sized_range<const _View>) {
  92. if constexpr (random_access_range<const _View>) {
  93. return ranges::begin(__base_) + size();
  94. } else {
  95. return default_sentinel;
  96. }
  97. } else {
  98. return __sentinel<true>{ranges::end(__base_)};
  99. }
  100. }
  101. _LIBCPP_HIDE_FROM_ABI
  102. constexpr auto size() requires sized_range<_View> {
  103. auto __n = ranges::size(__base_);
  104. // TODO: use ranges::min here.
  105. return std::min(__n, static_cast<decltype(__n)>(__count_));
  106. }
  107. _LIBCPP_HIDE_FROM_ABI
  108. constexpr auto size() const requires sized_range<const _View> {
  109. auto __n = ranges::size(__base_);
  110. // TODO: use ranges::min here.
  111. return std::min(__n, static_cast<decltype(__n)>(__count_));
  112. }
  113. };
  114. template<view _View>
  115. template<bool _Const>
  116. class take_view<_View>::__sentinel {
  117. using _Base = __maybe_const<_Const, _View>;
  118. template<bool _OtherConst>
  119. using _Iter = counted_iterator<iterator_t<__maybe_const<_OtherConst, _View>>>;
  120. _LIBCPP_NO_UNIQUE_ADDRESS sentinel_t<_Base> __end_ = sentinel_t<_Base>();
  121. template<bool>
  122. friend class take_view<_View>::__sentinel;
  123. public:
  124. _LIBCPP_HIDE_FROM_ABI
  125. __sentinel() = default;
  126. _LIBCPP_HIDE_FROM_ABI
  127. constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(std::move(__end)) {}
  128. _LIBCPP_HIDE_FROM_ABI
  129. constexpr __sentinel(__sentinel<!_Const> __s)
  130. requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
  131. : __end_(std::move(__s.__end_)) {}
  132. _LIBCPP_HIDE_FROM_ABI
  133. constexpr sentinel_t<_Base> base() const { return __end_; }
  134. _LIBCPP_HIDE_FROM_ABI
  135. friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) {
  136. return __lhs.count() == 0 || __lhs.base() == __rhs.__end_;
  137. }
  138. template<bool _OtherConst = !_Const>
  139. requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  140. _LIBCPP_HIDE_FROM_ABI
  141. friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) {
  142. return __lhs.count() == 0 || __lhs.base() == __rhs.__end_;
  143. }
  144. };
  145. template<class _Range>
  146. take_view(_Range&&, range_difference_t<_Range>) -> take_view<views::all_t<_Range>>;
  147. template<class _Tp>
  148. inline constexpr bool enable_borrowed_range<take_view<_Tp>> = enable_borrowed_range<_Tp>;
  149. } // namespace ranges
  150. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  151. _LIBCPP_END_NAMESPACE_STD
  152. _LIBCPP_POP_MACROS
  153. #endif // _LIBCPP___RANGES_TAKE_VIEW_H