take_view.h 14 KB

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