drop_view.h 12 KB

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