drop_view.h 13 KB

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