take_view.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <__algorithm/ranges_min.h>
  13. #include <__assert>
  14. #include <__concepts/constructible.h>
  15. #include <__concepts/convertible_to.h>
  16. #include <__config>
  17. #include <__functional/bind_back.h>
  18. #include <__fwd/span.h>
  19. #include <__fwd/string_view.h>
  20. #include <__iterator/concepts.h>
  21. #include <__iterator/counted_iterator.h>
  22. #include <__iterator/default_sentinel.h>
  23. #include <__iterator/distance.h>
  24. #include <__iterator/iterator_traits.h>
  25. #include <__ranges/access.h>
  26. #include <__ranges/all.h>
  27. #include <__ranges/concepts.h>
  28. #include <__ranges/empty_view.h>
  29. #include <__ranges/enable_borrowed_range.h>
  30. #include <__ranges/iota_view.h>
  31. #include <__ranges/range_adaptor.h>
  32. #include <__ranges/repeat_view.h>
  33. #include <__ranges/size.h>
  34. #include <__ranges/subrange.h>
  35. #include <__ranges/view_interface.h>
  36. #include <__type_traits/decay.h>
  37. #include <__type_traits/is_nothrow_constructible.h>
  38. #include <__type_traits/maybe_const.h>
  39. #include <__type_traits/remove_cvref.h>
  40. #include <__utility/auto_cast.h>
  41. #include <__utility/forward.h>
  42. #include <__utility/move.h>
  43. #include <cstddef>
  44. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  45. # pragma GCC system_header
  46. #endif
  47. _LIBCPP_PUSH_MACROS
  48. #include <__undef_macros>
  49. _LIBCPP_BEGIN_NAMESPACE_STD
  50. #if _LIBCPP_STD_VER >= 20
  51. namespace ranges {
  52. template <view _View>
  53. class take_view : public view_interface<take_view<_View>> {
  54. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  55. range_difference_t<_View> __count_ = 0;
  56. template <bool>
  57. class __sentinel;
  58. public:
  59. _LIBCPP_HIDE_FROM_ABI take_view()
  60. requires default_initializable<_View>
  61. = default;
  62. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23
  63. take_view(_View __base, range_difference_t<_View> __count)
  64. : __base_(std::move(__base)), __count_(__count) {
  65. _LIBCPP_ASSERT_UNCATEGORIZED(__count >= 0, "count has to be greater than or equal to zero");
  66. }
  67. _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
  68. requires copy_constructible<_View>
  69. {
  70. return __base_;
  71. }
  72. _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
  73. _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
  74. requires(!__simple_view<_View>)
  75. {
  76. if constexpr (sized_range<_View>) {
  77. if constexpr (random_access_range<_View>) {
  78. return ranges::begin(__base_);
  79. } else {
  80. using _DifferenceT = range_difference_t<_View>;
  81. auto __size = size();
  82. return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size));
  83. }
  84. } else {
  85. return counted_iterator(ranges::begin(__base_), __count_);
  86. }
  87. }
  88. _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
  89. requires range<const _View>
  90. {
  91. if constexpr (sized_range<const _View>) {
  92. if constexpr (random_access_range<const _View>) {
  93. return ranges::begin(__base_);
  94. } else {
  95. using _DifferenceT = range_difference_t<const _View>;
  96. auto __size = size();
  97. return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size));
  98. }
  99. } else {
  100. return counted_iterator(ranges::begin(__base_), __count_);
  101. }
  102. }
  103. _LIBCPP_HIDE_FROM_ABI constexpr auto end()
  104. requires(!__simple_view<_View>)
  105. {
  106. if constexpr (sized_range<_View>) {
  107. if constexpr (random_access_range<_View>) {
  108. return ranges::begin(__base_) + size();
  109. } else {
  110. return default_sentinel;
  111. }
  112. } else {
  113. return __sentinel<false>{ranges::end(__base_)};
  114. }
  115. }
  116. _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
  117. requires range<const _View>
  118. {
  119. if constexpr (sized_range<const _View>) {
  120. if constexpr (random_access_range<const _View>) {
  121. return ranges::begin(__base_) + size();
  122. } else {
  123. return default_sentinel;
  124. }
  125. } else {
  126. return __sentinel<true>{ranges::end(__base_)};
  127. }
  128. }
  129. _LIBCPP_HIDE_FROM_ABI constexpr auto size()
  130. requires sized_range<_View>
  131. {
  132. auto __n = ranges::size(__base_);
  133. return ranges::min(__n, static_cast<decltype(__n)>(__count_));
  134. }
  135. _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
  136. requires sized_range<const _View>
  137. {
  138. auto __n = ranges::size(__base_);
  139. return ranges::min(__n, static_cast<decltype(__n)>(__count_));
  140. }
  141. };
  142. template <view _View>
  143. template <bool _Const>
  144. class take_view<_View>::__sentinel {
  145. using _Base = __maybe_const<_Const, _View>;
  146. template <bool _OtherConst>
  147. using _Iter = counted_iterator<iterator_t<__maybe_const<_OtherConst, _View>>>;
  148. _LIBCPP_NO_UNIQUE_ADDRESS sentinel_t<_Base> __end_ = sentinel_t<_Base>();
  149. template <bool>
  150. friend class take_view<_View>::__sentinel;
  151. public:
  152. _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
  153. _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(std::move(__end)) {}
  154. _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __s)
  155. requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
  156. : __end_(std::move(__s.__end_)) {}
  157. _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
  158. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) {
  159. return __lhs.count() == 0 || __lhs.base() == __rhs.__end_;
  160. }
  161. template <bool _OtherConst = !_Const>
  162. requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  163. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const _Iter<_OtherConst>& __lhs, const __sentinel& __rhs) {
  164. return __lhs.count() == 0 || __lhs.base() == __rhs.__end_;
  165. }
  166. };
  167. template <class _Range>
  168. take_view(_Range&&, range_difference_t<_Range>) -> take_view<views::all_t<_Range>>;
  169. template <class _Tp>
  170. inline constexpr bool enable_borrowed_range<take_view<_Tp>> = enable_borrowed_range<_Tp>;
  171. namespace views {
  172. namespace __take {
  173. template <class _Tp>
  174. inline constexpr bool __is_empty_view = false;
  175. template <class _Tp>
  176. inline constexpr bool __is_empty_view<empty_view<_Tp>> = true;
  177. template <class _Tp>
  178. inline constexpr bool __is_passthrough_specialization = false;
  179. template <class _Tp, size_t _Extent>
  180. inline constexpr bool __is_passthrough_specialization<span<_Tp, _Extent>> = true;
  181. template <class _CharT, class _Traits>
  182. inline constexpr bool __is_passthrough_specialization<basic_string_view<_CharT, _Traits>> = true;
  183. template <class _Iter, class _Sent, subrange_kind _Kind>
  184. inline constexpr bool __is_passthrough_specialization<subrange<_Iter, _Sent, _Kind>> = true;
  185. template <class _Tp>
  186. inline constexpr bool __is_iota_specialization = false;
  187. template <class _Np, class _Bound>
  188. inline constexpr bool __is_iota_specialization<iota_view<_Np, _Bound>> = true;
  189. template <class _Tp>
  190. struct __passthrough_type;
  191. template <class _Tp, size_t _Extent>
  192. struct __passthrough_type<span<_Tp, _Extent>> {
  193. using type = span<_Tp>;
  194. };
  195. template <class _CharT, class _Traits>
  196. struct __passthrough_type<basic_string_view<_CharT, _Traits>> {
  197. using type = basic_string_view<_CharT, _Traits>;
  198. };
  199. template <class _Iter, class _Sent, subrange_kind _Kind>
  200. requires requires { typename subrange<_Iter>; }
  201. struct __passthrough_type<subrange<_Iter, _Sent, _Kind>> {
  202. using type = subrange<_Iter>;
  203. };
  204. template <class _Tp>
  205. using __passthrough_type_t = typename __passthrough_type<_Tp>::type;
  206. struct __fn {
  207. // [range.take.overview]: the `empty_view` case.
  208. template <class _Range, convertible_to<range_difference_t<_Range>> _Np>
  209. requires __is_empty_view<remove_cvref_t<_Range>>
  210. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&&) const
  211. noexcept(noexcept(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))))
  212. -> decltype(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))) {
  213. return _LIBCPP_AUTO_CAST(std::forward<_Range>(__range));
  214. }
  215. // [range.take.overview]: the `span | basic_string_view | subrange` case.
  216. template <class _Range,
  217. convertible_to<range_difference_t<_Range>> _Np,
  218. class _RawRange = remove_cvref_t<_Range>,
  219. class _Dist = range_difference_t<_Range>>
  220. requires(!__is_empty_view<_RawRange> && random_access_range<_RawRange> && sized_range<_RawRange> &&
  221. __is_passthrough_specialization<_RawRange>)
  222. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto
  223. operator()(_Range&& __rng, _Np&& __n) const noexcept(noexcept(__passthrough_type_t<_RawRange>(
  224. ranges::begin(__rng), ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))))
  225. -> decltype(__passthrough_type_t<_RawRange>(
  226. // Note: deliberately not forwarding `__rng` to guard against double moves.
  227. ranges::begin(__rng),
  228. ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))) {
  229. return __passthrough_type_t<_RawRange>(
  230. ranges::begin(__rng), ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)));
  231. }
  232. // [range.take.overview]: the `iota_view` case.
  233. // clang-format off
  234. template <class _Range,
  235. convertible_to<range_difference_t<_Range>> _Np,
  236. class _RawRange = remove_cvref_t<_Range>,
  237. class _Dist = range_difference_t<_Range>>
  238. requires (!__is_empty_view<_RawRange> &&
  239. random_access_range<_RawRange> &&
  240. sized_range<_RawRange> &&
  241. __is_iota_specialization<_RawRange>)
  242. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  243. constexpr auto operator()(_Range&& __rng, _Np&& __n) const
  244. noexcept(noexcept(ranges::iota_view(
  245. *ranges::begin(__rng),
  246. *(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
  247. )))
  248. -> decltype( ranges::iota_view(
  249. // Note: deliberately not forwarding `__rng` to guard against double moves.
  250. *ranges::begin(__rng),
  251. *(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
  252. ))
  253. { return ranges::iota_view(
  254. *ranges::begin(__rng),
  255. *(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
  256. ); }
  257. #if _LIBCPP_STD_VER >= 23
  258. // [range.take.overview]: the `repeat_view` "_RawRange models sized_range" case.
  259. template <class _Range,
  260. convertible_to<range_difference_t<_Range>> _Np,
  261. class _RawRange = remove_cvref_t<_Range>,
  262. class _Dist = range_difference_t<_Range>>
  263. requires(__is_repeat_specialization<_RawRange> && sized_range<_RawRange>)
  264. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const
  265. noexcept(noexcept(views::repeat(*__range.__value_, std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n)))))
  266. -> decltype( views::repeat(*__range.__value_, std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))))
  267. { return views::repeat(*__range.__value_, std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))); }
  268. // [range.take.overview]: the `repeat_view` "otherwise" case.
  269. template <class _Range,
  270. convertible_to<range_difference_t<_Range>> _Np,
  271. class _RawRange = remove_cvref_t<_Range>,
  272. class _Dist = range_difference_t<_Range>>
  273. requires(__is_repeat_specialization<_RawRange> && !sized_range<_RawRange>)
  274. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const
  275. noexcept(noexcept(views::repeat(*__range.__value_, static_cast<_Dist>(__n))))
  276. -> decltype( views::repeat(*__range.__value_, static_cast<_Dist>(__n)))
  277. { return views::repeat(*__range.__value_, static_cast<_Dist>(__n)); }
  278. #endif
  279. // clang-format on
  280. // [range.take.overview]: the "otherwise" case.
  281. template <class _Range, convertible_to<range_difference_t<_Range>> _Np, class _RawRange = remove_cvref_t<_Range>>
  282. // Note: without specifically excluding the other cases, GCC sees this overload as ambiguous with the other
  283. // overloads.
  284. requires(!(__is_empty_view<_RawRange> ||
  285. # if _LIBCPP_STD_VER >= 23
  286. __is_repeat_specialization<_RawRange> ||
  287. # endif
  288. (__is_iota_specialization<_RawRange> && sized_range<_RawRange> && random_access_range<_RawRange>) ||
  289. (__is_passthrough_specialization<_RawRange> && sized_range<_RawRange> &&
  290. random_access_range<_RawRange>)))
  291. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const
  292. noexcept(noexcept(take_view(std::forward<_Range>(__range), std::forward<_Np>(__n))))
  293. -> decltype(take_view(std::forward<_Range>(__range), std::forward<_Np>(__n))) {
  294. return take_view(std::forward<_Range>(__range), std::forward<_Np>(__n));
  295. }
  296. template <class _Np>
  297. requires constructible_from<decay_t<_Np>, _Np>
  298. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Np&& __n) const
  299. noexcept(is_nothrow_constructible_v<decay_t<_Np>, _Np>) {
  300. return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Np>(__n)));
  301. }
  302. };
  303. } // namespace __take
  304. inline namespace __cpo {
  305. inline constexpr auto take = __take::__fn{};
  306. } // namespace __cpo
  307. } // namespace views
  308. } // namespace ranges
  309. #endif // _LIBCPP_STD_VER >= 20
  310. _LIBCPP_END_NAMESPACE_STD
  311. _LIBCPP_POP_MACROS
  312. #endif // _LIBCPP___RANGES_TAKE_VIEW_H