take_view.h 12 KB

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