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