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_LAZY_SPLIT_VIEW_H
11#define _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H
12
13#include <__algorithm/ranges_find.h>
14#include <__algorithm/ranges_mismatch.h>
15#include <__assert>
16#include <__concepts/constructible.h>
17#include <__concepts/convertible_to.h>
18#include <__concepts/derived_from.h>
19#include <__config>
20#include <__functional/bind_back.h>
21#include <__functional/ranges_operations.h>
22#include <__iterator/concepts.h>
23#include <__iterator/default_sentinel.h>
24#include <__iterator/incrementable_traits.h>
25#include <__iterator/indirectly_comparable.h>
26#include <__iterator/iter_move.h>
27#include <__iterator/iter_swap.h>
28#include <__iterator/iterator_traits.h>
29#include <__memory/addressof.h>
30#include <__ranges/access.h>
31#include <__ranges/all.h>
32#include <__ranges/concepts.h>
33#include <__ranges/non_propagating_cache.h>
34#include <__ranges/range_adaptor.h>
35#include <__ranges/single_view.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/maybe_const.h>
42#include <__type_traits/remove_reference.h>
43#include <__utility/forward.h>
44#include <__utility/move.h>
45
46#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
47# pragma GCC system_header
48#endif
49
50_LIBCPP_PUSH_MACROS
51#include <__undef_macros>
52
53_LIBCPP_BEGIN_NAMESPACE_STD
54
55#if _LIBCPP_STD_VER >= 20
56
57namespace ranges {
58
59template <auto>
60struct __require_constant;
61
62template <class _Range>
63concept __tiny_range = sized_range<_Range> && requires {
64 typename __require_constant<remove_reference_t<_Range>::size()>;
65} && (remove_reference_t<_Range>::size() <= 1);
66
67template <input_range _View, forward_range _Pattern>
68 requires view<_View> && view<_Pattern> &&
69 indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to> &&
70 (forward_range<_View> || __tiny_range<_Pattern>)
71class lazy_split_view : public view_interface<lazy_split_view<_View, _Pattern>> {
72 _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
73 _LIBCPP_NO_UNIQUE_ADDRESS _Pattern __pattern_ = _Pattern();
74
75 using _MaybeCurrent _LIBCPP_NODEBUG =
76 _If<!forward_range<_View>, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
77 _LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();
78
79 template <bool>
80 struct __outer_iterator;
81 template <bool>
82 struct __inner_iterator;
83
84public:
85 _LIBCPP_HIDE_FROM_ABI lazy_split_view()
86 requires default_initializable<_View> && default_initializable<_Pattern>
87 = default;
88
89 _LIBCPP_HIDE_FROM_ABI constexpr explicit lazy_split_view(_View __base, _Pattern __pattern)
90 : __base_(std::move(__base)), __pattern_(std::move(__pattern)) {}
91
92 template <input_range _Range>
93 requires constructible_from<_View, views::all_t<_Range>> &&
94 constructible_from<_Pattern, single_view<range_value_t<_Range>>>
95 _LIBCPP_HIDE_FROM_ABI constexpr explicit lazy_split_view(_Range&& __r, range_value_t<_Range> __e)
96 : __base_(views::all(std::forward<_Range>(__r))), __pattern_(views::single(std::move(__e))) {}
97
98 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
99 requires copy_constructible<_View>
100 {
101 return __base_;
102 }
103 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
104
105 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
106 if constexpr (forward_range<_View>) {
107 return __outer_iterator<__simple_view<_View> && __simple_view<_Pattern>>{*this, ranges::begin(__base_)};
108 } else {
109 __current_.__emplace(ranges::begin(__base_));
110 return __outer_iterator<false>{*this};
111 }
112 }
113
114 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
115 requires forward_range<_View> && forward_range<const _View>
116 {
117 return __outer_iterator<true>{*this, ranges::begin(__base_)};
118 }
119
120 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end()
121 requires forward_range<_View> && common_range<_View>
122 {
123 return __outer_iterator<__simple_view<_View> && __simple_view<_Pattern>>{*this, ranges::end(__base_)};
124 }
125
126 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const {
127 if constexpr (forward_range<_View> && forward_range<const _View> && common_range<const _View>) {
128 return __outer_iterator<true>{*this, ranges::end(__base_)};
129 } else {
130 return default_sentinel;
131 }
132 }
133
134private:
135 template <class>
136 struct __outer_iterator_category {};
137
138 template <forward_range _Tp>
139 struct __outer_iterator_category<_Tp> {
140 using iterator_category = input_iterator_tag;
141 };
142
143 template <bool _Const>
144 struct __outer_iterator : __outer_iterator_category<__maybe_const<_Const, _View>> {
145 private:
146 template <bool>
147 friend struct __inner_iterator;
148 friend __outer_iterator<true>;
149
150 using _Parent _LIBCPP_NODEBUG = __maybe_const<_Const, lazy_split_view>;
151 using _Base _LIBCPP_NODEBUG = __maybe_const<_Const, _View>;
152
153 _Parent* __parent_ = nullptr;
154 using _MaybeCurrent _LIBCPP_NODEBUG = _If<forward_range<_View>, iterator_t<_Base>, __empty_cache>;
155 _LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();
156 bool __trailing_empty_ = false;
157
158 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __current() noexcept {
159 if constexpr (forward_range<_View>) {
160 return __current_;
161 } else {
162 return *__parent_->__current_;
163 }
164 }
165
166 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __current() const noexcept {
167 if constexpr (forward_range<_View>) {
168 return __current_;
169 } else {
170 return *__parent_->__current_;
171 }
172 }
173
174 // Workaround for the GCC issue that doesn't allow calling `__parent_->__base_` from friend functions (because
175 // `__base_` is private).
176 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __parent_base() const noexcept { return __parent_->__base_; }
177
178 public:
179 // using iterator_category = inherited;
180 using iterator_concept = conditional_t<forward_range<_Base>, forward_iterator_tag, input_iterator_tag>;
181 using difference_type = range_difference_t<_Base>;
182
183 struct value_type : view_interface<value_type> {
184 private:
185 __outer_iterator __i_ = __outer_iterator();
186
187 public:
188 _LIBCPP_HIDE_FROM_ABI value_type() = default;
189 _LIBCPP_HIDE_FROM_ABI constexpr explicit value_type(__outer_iterator __i) : __i_(std::move(__i)) {}
190
191 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator<_Const> begin() const {
192 return __inner_iterator<_Const>{__i_};
193 }
194 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr default_sentinel_t end() const noexcept { return default_sentinel; }
195 };
196
197 _LIBCPP_HIDE_FROM_ABI __outer_iterator() = default;
198
199 _LIBCPP_HIDE_FROM_ABI constexpr explicit __outer_iterator(_Parent& __parent)
200 requires(!forward_range<_Base>)
201 : __parent_(std::addressof(__parent)) {}
202
203 _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(_Parent& __parent, iterator_t<_Base> __current)
204 requires forward_range<_Base>
205 : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}
206
207 _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(__outer_iterator<!_Const> __i)
208 requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
209 : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
210
211 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return value_type{*this}; }
212
213 _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator& operator++() {
214 const auto __end = ranges::end(__parent_->__base_);
215 if (__current() == __end) {
216 __trailing_empty_ = false;
217 return *this;
218 }
219
220 const auto [__pbegin, __pend] = ranges::subrange{__parent_->__pattern_};
221 if (__pbegin == __pend) {
222 // Empty pattern: split on every element in the input range
223 ++__current();
224
225 } else if constexpr (__tiny_range<_Pattern>) {
226 // One-element pattern: we can use `ranges::find`.
227 __current() = ranges::find(std::move(__current()), __end, *__pbegin);
228 if (__current() != __end) {
229 // Make sure we point to after the separator we just found.
230 ++__current();
231 if (__current() == __end)
232 __trailing_empty_ = true;
233 }
234
235 } else {
236 // General case for n-element pattern.
237 do {
238 const auto [__b, __p] = ranges::mismatch(__current(), __end, __pbegin, __pend);
239 if (__p == __pend) {
240 __current() = __b;
241 if (__current() == __end) {
242 __trailing_empty_ = true;
243 }
244 break; // The pattern matched; skip it.
245 }
246 } while (++__current() != __end);
247 }
248
249 return *this;
250 }
251
252 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {
253 if constexpr (forward_range<_Base>) {
254 auto __tmp = *this;
255 ++*this;
256 return __tmp;
257
258 } else {
259 ++*this;
260 }
261 }
262
263 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, const __outer_iterator& __y)
264 requires forward_range<_Base>
265 {
266 return __x.__current_ == __y.__current_ && __x.__trailing_empty_ == __y.__trailing_empty_;
267 }
268
269 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, default_sentinel_t) {
270 _LIBCPP_ASSERT_NON_NULL(__x.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");
271 return __x.__current() == ranges::end(__x.__parent_base()) && !__x.__trailing_empty_;
272 }
273 };
274
275 template <class>
276 struct __inner_iterator_category {};
277
278 template <forward_range _Tp>
279 struct __inner_iterator_category<_Tp> {
280 using iterator_category =
281 _If<derived_from<typename iterator_traits<iterator_t<_Tp>>::iterator_category, forward_iterator_tag>,
282 forward_iterator_tag,
283 typename iterator_traits<iterator_t<_Tp>>::iterator_category>;
284 };
285
286 template <bool _Const>
287 struct __inner_iterator : __inner_iterator_category<__maybe_const<_Const, _View>> {
288 private:
289 using _Base _LIBCPP_NODEBUG = __maybe_const<_Const, _View>;
290 // Workaround for a GCC issue.
291 static constexpr bool _OuterConst = _Const;
292 __outer_iterator<_Const> __i_ = __outer_iterator<_OuterConst>();
293 bool __incremented_ = false;
294
295 // Note: these private functions are necessary because GCC doesn't allow calls to private members of `__i_` from
296 // free functions that are friends of `inner-iterator`.
297
298 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_done() const {
299 _LIBCPP_ASSERT_NON_NULL(__i_.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");
300
301 auto [__pcur, __pend] = ranges::subrange{__i_.__parent_->__pattern_};
302 auto __end = ranges::end(__i_.__parent_->__base_);
303
304 if constexpr (__tiny_range<_Pattern>) {
305 const auto& __cur = __i_.__current();
306 if (__cur == __end)
307 return true;
308 if (__pcur == __pend)
309 return __incremented_;
310
311 return *__cur == *__pcur;
312
313 } else {
314 auto __cur = __i_.__current();
315 if (__cur == __end)
316 return true;
317 if (__pcur == __pend)
318 return __incremented_;
319
320 do {
321 if (*__cur != *__pcur)
322 return false;
323 if (++__pcur == __pend)
324 return true;
325 } while (++__cur != __end);
326
327 return false;
328 }
329 }
330
331 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __outer_current() noexcept { return __i_.__current(); }
332
333 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __outer_current() const noexcept {
334 return __i_.__current();
335 }
336
337 public:
338 // using iterator_category = inherited;
339 using iterator_concept = typename __outer_iterator<_Const>::iterator_concept;
340 using value_type = range_value_t<_Base>;
341 using difference_type = range_difference_t<_Base>;
342
343 _LIBCPP_HIDE_FROM_ABI __inner_iterator() = default;
344
345 _LIBCPP_HIDE_FROM_ABI constexpr explicit __inner_iterator(__outer_iterator<_Const> __i) : __i_(std::move(__i)) {}
346
347 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept {
348 return __i_.__current();
349 }
350 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() &&
351 requires forward_range<_View>
352 {
353 return std::move(__i_.__current());
354 }
355
356 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const { return *__i_.__current(); }
357
358 _LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator& operator++() {
359 __incremented_ = true;
360
361 if constexpr (!forward_range<_Base>) {
362 if constexpr (_Pattern::size() == 0) {
363 return *this;
364 }
365 }
366
367 ++__i_.__current();
368 return *this;
369 }
370
371 _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {
372 if constexpr (forward_range<_Base>) {
373 auto __tmp = *this;
374 ++*this;
375 return __tmp;
376
377 } else {
378 ++*this;
379 }
380 }
381
382 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, const __inner_iterator& __y)
383 requires forward_range<_Base>
384 {
385 return __x.__outer_current() == __y.__outer_current();
386 }
387
388 _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, default_sentinel_t) {
389 return __x.__is_done();
390 }
391
392 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr decltype(auto)
393 iter_move(const __inner_iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__outer_current()))) {
394 return ranges::iter_move(__i.__outer_current());
395 }
396
397 _LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(
398 const __inner_iterator& __x,
399 const __inner_iterator& __y) noexcept(noexcept(ranges::iter_swap(__x.__outer_current(), __y.__outer_current())))
400 requires indirectly_swappable<iterator_t<_Base>>
401 {
402 ranges::iter_swap(__x.__outer_current(), __y.__outer_current());
403 }
404 };
405};
406
407template <class _Range, class _Pattern>
408lazy_split_view(_Range&&, _Pattern&&) -> lazy_split_view<views::all_t<_Range>, views::all_t<_Pattern>>;
409
410template <input_range _Range>
411lazy_split_view(_Range&&, range_value_t<_Range>)
412 -> lazy_split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
413
414namespace views {
415namespace __lazy_split_view {
416struct __fn {
417 template <class _Range, class _Pattern>
418 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pattern&& __pattern) const
419 noexcept(noexcept(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))))
420 -> decltype(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))) {
421 return lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern));
422 }
423
424 template <class _Pattern>
425 requires constructible_from<decay_t<_Pattern>, _Pattern>
426 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pattern&& __pattern) const
427 noexcept(is_nothrow_constructible_v<decay_t<_Pattern>, _Pattern>) {
428 return __pipeable(std::__bind_back(*this, std::forward<_Pattern>(__pattern)));
429 }
430};
431} // namespace __lazy_split_view
432
433inline namespace __cpo {
434inline constexpr auto lazy_split = __lazy_split_view::__fn{};
435} // namespace __cpo
436} // namespace views
437
438} // namespace ranges
439
440#endif // _LIBCPP_STD_VER >= 20
441
442_LIBCPP_END_NAMESPACE_STD
443
444_LIBCPP_POP_MACROS
445
446#endif // _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H
447