drop_view.h 14 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_DROP_VIEW_H
  10. #define _LIBCPP___RANGES_DROP_VIEW_H
  11. #include <__algorithm/min.h>
  12. #include <__assert>
  13. #include <__concepts/constructible.h>
  14. #include <__concepts/convertible_to.h>
  15. #include <__config>
  16. #include <__functional/bind_back.h>
  17. #include <__fwd/span.h>
  18. #include <__fwd/string_view.h>
  19. #include <__iterator/concepts.h>
  20. #include <__iterator/distance.h>
  21. #include <__iterator/iterator_traits.h>
  22. #include <__iterator/next.h>
  23. #include <__ranges/access.h>
  24. #include <__ranges/all.h>
  25. #include <__ranges/concepts.h>
  26. #include <__ranges/empty_view.h>
  27. #include <__ranges/enable_borrowed_range.h>
  28. #include <__ranges/iota_view.h>
  29. #include <__ranges/non_propagating_cache.h>
  30. #include <__ranges/range_adaptor.h>
  31. #include <__ranges/repeat_view.h>
  32. #include <__ranges/size.h>
  33. #include <__ranges/subrange.h>
  34. #include <__ranges/view_interface.h>
  35. #include <__type_traits/conditional.h>
  36. #include <__type_traits/decay.h>
  37. #include <__type_traits/is_nothrow_constructible.h>
  38. #include <__type_traits/make_unsigned.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 drop_view
  54. : public view_interface<drop_view<_View>>
  55. {
  56. // We cache begin() whenever ranges::next is not guaranteed O(1) to provide an
  57. // amortized O(1) begin() method. If this is an input_range, then we cannot cache
  58. // begin because begin is not equality preserving.
  59. // Note: drop_view<input-range>::begin() is still trivially amortized O(1) because
  60. // one can't call begin() on it more than once.
  61. static constexpr bool _UseCache = forward_range<_View> && !(random_access_range<_View> && sized_range<_View>);
  62. using _Cache = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
  63. _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();
  64. range_difference_t<_View> __count_ = 0;
  65. _View __base_ = _View();
  66. public:
  67. _LIBCPP_HIDE_FROM_ABI drop_view() requires default_initializable<_View> = default;
  68. _LIBCPP_HIDE_FROM_ABI
  69. constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 drop_view(_View __base, range_difference_t<_View> __count)
  70. : __count_(__count)
  71. , __base_(std::move(__base))
  72. {
  73. _LIBCPP_ASSERT_UNCATEGORIZED(__count_ >= 0, "count must be greater than or equal to zero.");
  74. }
  75. _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
  76. _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
  77. _LIBCPP_HIDE_FROM_ABI
  78. constexpr auto begin()
  79. requires (!(__simple_view<_View> &&
  80. random_access_range<const _View> && sized_range<const _View>))
  81. {
  82. if constexpr (_UseCache)
  83. if (__cached_begin_.__has_value())
  84. return *__cached_begin_;
  85. auto __tmp = ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));
  86. if constexpr (_UseCache)
  87. __cached_begin_.__emplace(__tmp);
  88. return __tmp;
  89. }
  90. _LIBCPP_HIDE_FROM_ABI
  91. constexpr auto begin() const
  92. requires random_access_range<const _View> && sized_range<const _View>
  93. {
  94. return ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));
  95. }
  96. _LIBCPP_HIDE_FROM_ABI
  97. constexpr auto end()
  98. requires (!__simple_view<_View>)
  99. { return ranges::end(__base_); }
  100. _LIBCPP_HIDE_FROM_ABI
  101. constexpr auto end() const
  102. requires range<const _View>
  103. { return ranges::end(__base_); }
  104. _LIBCPP_HIDE_FROM_ABI
  105. static constexpr auto __size(auto& __self) {
  106. const auto __s = ranges::size(__self.__base_);
  107. const auto __c = static_cast<decltype(__s)>(__self.__count_);
  108. return __s < __c ? 0 : __s - __c;
  109. }
  110. _LIBCPP_HIDE_FROM_ABI
  111. constexpr auto size()
  112. requires sized_range<_View>
  113. { return __size(*this); }
  114. _LIBCPP_HIDE_FROM_ABI
  115. constexpr auto size() const
  116. requires sized_range<const _View>
  117. { return __size(*this); }
  118. };
  119. template<class _Range>
  120. drop_view(_Range&&, range_difference_t<_Range>) -> drop_view<views::all_t<_Range>>;
  121. template<class _Tp>
  122. inline constexpr bool enable_borrowed_range<drop_view<_Tp>> = enable_borrowed_range<_Tp>;
  123. namespace views {
  124. namespace __drop {
  125. template <class _Tp>
  126. inline constexpr bool __is_empty_view = false;
  127. template <class _Tp>
  128. inline constexpr bool __is_empty_view<empty_view<_Tp>> = true;
  129. template <class _Tp>
  130. inline constexpr bool __is_passthrough_specialization = false;
  131. template <class _Tp, size_t _Extent>
  132. inline constexpr bool __is_passthrough_specialization<span<_Tp, _Extent>> = true;
  133. template <class _CharT, class _Traits>
  134. inline constexpr bool __is_passthrough_specialization<basic_string_view<_CharT, _Traits>> = true;
  135. template <class _Np, class _Bound>
  136. inline constexpr bool __is_passthrough_specialization<iota_view<_Np, _Bound>> = true;
  137. template <class _Iter, class _Sent, subrange_kind _Kind>
  138. inline constexpr bool __is_passthrough_specialization<subrange<_Iter, _Sent, _Kind>> =
  139. !subrange<_Iter, _Sent, _Kind>::_StoreSize;
  140. template <class _Tp>
  141. inline constexpr bool __is_subrange_specialization_with_store_size = false;
  142. template <class _Iter, class _Sent, subrange_kind _Kind>
  143. inline constexpr bool __is_subrange_specialization_with_store_size<subrange<_Iter, _Sent, _Kind>> =
  144. subrange<_Iter, _Sent, _Kind>::_StoreSize;
  145. template <class _Tp>
  146. struct __passthrough_type;
  147. template <class _Tp, size_t _Extent>
  148. struct __passthrough_type<span<_Tp, _Extent>> {
  149. using type = span<_Tp>;
  150. };
  151. template <class _CharT, class _Traits>
  152. struct __passthrough_type<basic_string_view<_CharT, _Traits>> {
  153. using type = basic_string_view<_CharT, _Traits>;
  154. };
  155. template <class _Np, class _Bound>
  156. struct __passthrough_type<iota_view<_Np, _Bound>> {
  157. using type = iota_view<_Np, _Bound>;
  158. };
  159. template <class _Iter, class _Sent, subrange_kind _Kind>
  160. struct __passthrough_type<subrange<_Iter, _Sent, _Kind>> {
  161. using type = subrange<_Iter, _Sent, _Kind>;
  162. };
  163. template <class _Tp>
  164. using __passthrough_type_t = typename __passthrough_type<_Tp>::type;
  165. struct __fn {
  166. // [range.drop.overview]: the `empty_view` case.
  167. template <class _Range, convertible_to<range_difference_t<_Range>> _Np>
  168. requires __is_empty_view<remove_cvref_t<_Range>>
  169. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  170. constexpr auto operator()(_Range&& __range, _Np&&) const
  171. noexcept(noexcept(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))))
  172. -> decltype( _LIBCPP_AUTO_CAST(std::forward<_Range>(__range)))
  173. { return _LIBCPP_AUTO_CAST(std::forward<_Range>(__range)); }
  174. // [range.drop.overview]: the `span | basic_string_view | iota_view | subrange (StoreSize == false)` case.
  175. template <class _Range,
  176. convertible_to<range_difference_t<_Range>> _Np,
  177. class _RawRange = remove_cvref_t<_Range>,
  178. class _Dist = range_difference_t<_Range>>
  179. requires (!__is_empty_view<_RawRange> &&
  180. random_access_range<_RawRange> &&
  181. sized_range<_RawRange> &&
  182. __is_passthrough_specialization<_RawRange>)
  183. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  184. constexpr auto operator()(_Range&& __rng, _Np&& __n) const
  185. noexcept(noexcept(__passthrough_type_t<_RawRange>(
  186. ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),
  187. ranges::end(__rng)
  188. )))
  189. -> decltype( __passthrough_type_t<_RawRange>(
  190. // Note: deliberately not forwarding `__rng` to guard against double moves.
  191. ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),
  192. ranges::end(__rng)
  193. ))
  194. { return __passthrough_type_t<_RawRange>(
  195. ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),
  196. ranges::end(__rng)
  197. ); }
  198. // [range.drop.overview]: the `subrange (StoreSize == true)` case.
  199. template <class _Range,
  200. convertible_to<range_difference_t<_Range>> _Np,
  201. class _RawRange = remove_cvref_t<_Range>,
  202. class _Dist = range_difference_t<_Range>>
  203. requires (!__is_empty_view<_RawRange> &&
  204. random_access_range<_RawRange> &&
  205. sized_range<_RawRange> &&
  206. __is_subrange_specialization_with_store_size<_RawRange>)
  207. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  208. constexpr auto operator()(_Range&& __rng, _Np&& __n) const
  209. noexcept(noexcept(_RawRange(
  210. ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),
  211. ranges::end(__rng),
  212. std::__to_unsigned_like(ranges::distance(__rng) -
  213. std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
  214. )))
  215. -> decltype( _RawRange(
  216. // Note: deliberately not forwarding `__rng` to guard against double moves.
  217. ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),
  218. ranges::end(__rng),
  219. std::__to_unsigned_like(ranges::distance(__rng) -
  220. std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))
  221. ))
  222. {
  223. // Introducing local variables avoids calculating `min` and `distance` twice (at the cost of diverging from the
  224. // expression used in the `noexcept` clause and the return statement).
  225. auto __dist = ranges::distance(__rng);
  226. auto __clamped = std::min<_Dist>(__dist, std::forward<_Np>(__n));
  227. return _RawRange(
  228. ranges::begin(__rng) + __clamped,
  229. ranges::end(__rng),
  230. std::__to_unsigned_like(__dist - __clamped)
  231. );}
  232. // clang-format off
  233. #if _LIBCPP_STD_VER >= 23
  234. // [range.drop.overview]: the `repeat_view` "_RawRange models sized_range" case.
  235. template <class _Range,
  236. convertible_to<range_difference_t<_Range>> _Np,
  237. class _RawRange = remove_cvref_t<_Range>,
  238. class _Dist = range_difference_t<_Range>>
  239. requires (__is_repeat_specialization<_RawRange> && sized_range<_RawRange>)
  240. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const
  241. noexcept(noexcept(views::repeat(*__range.__value_, ranges::distance(__range) - std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n)))))
  242. -> decltype( views::repeat(*__range.__value_, ranges::distance(__range) - std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))))
  243. { return views::repeat(*__range.__value_, ranges::distance(__range) - std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))); }
  244. // [range.drop.overview]: the `repeat_view` "otherwise" case.
  245. template <class _Range,
  246. convertible_to<range_difference_t<_Range>> _Np,
  247. class _RawRange = remove_cvref_t<_Range>,
  248. class _Dist = range_difference_t<_Range>>
  249. requires (__is_repeat_specialization<_RawRange> && !sized_range<_RawRange>)
  250. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
  251. constexpr auto operator()(_Range&& __range, _Np&&) const
  252. noexcept(noexcept(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))))
  253. -> decltype( _LIBCPP_AUTO_CAST(std::forward<_Range>(__range)))
  254. { return _LIBCPP_AUTO_CAST(std::forward<_Range>(__range)); }
  255. #endif
  256. // clang-format on
  257. // [range.drop.overview]: the "otherwise" case.
  258. template <class _Range, convertible_to<range_difference_t<_Range>> _Np,
  259. class _RawRange = remove_cvref_t<_Range>>
  260. // Note: without specifically excluding the other cases, GCC sees this overload as ambiguous with the other
  261. // overloads.
  262. requires (!(__is_empty_view<_RawRange> ||
  263. #if _LIBCPP_STD_VER >= 23
  264. __is_repeat_specialization<_RawRange> ||
  265. #endif
  266. (__is_subrange_specialization_with_store_size<_RawRange> &&
  267. sized_range<_RawRange> &&
  268. random_access_range<_RawRange>) ||
  269. (__is_passthrough_specialization<_RawRange> &&
  270. sized_range<_RawRange> &&
  271. random_access_range<_RawRange>)
  272. ))
  273. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  274. constexpr auto operator()(_Range&& __range, _Np&& __n) const
  275. noexcept(noexcept(drop_view(std::forward<_Range>(__range), std::forward<_Np>(__n))))
  276. -> decltype( drop_view(std::forward<_Range>(__range), std::forward<_Np>(__n)))
  277. { return drop_view(std::forward<_Range>(__range), std::forward<_Np>(__n)); }
  278. template <class _Np>
  279. requires constructible_from<decay_t<_Np>, _Np>
  280. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  281. constexpr auto operator()(_Np&& __n) const
  282. noexcept(is_nothrow_constructible_v<decay_t<_Np>, _Np>)
  283. { return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Np>(__n))); }
  284. };
  285. } // namespace __drop
  286. inline namespace __cpo {
  287. inline constexpr auto drop = __drop::__fn{};
  288. } // namespace __cpo
  289. } // namespace views
  290. } // namespace ranges
  291. #endif // _LIBCPP_STD_VER >= 20
  292. _LIBCPP_END_NAMESPACE_STD
  293. _LIBCPP_POP_MACROS
  294. #endif // _LIBCPP___RANGES_DROP_VIEW_H