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