Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:02

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H
0011 #define _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H
0012 
0013 #include <__algorithm/ranges_find.h>
0014 #include <__algorithm/ranges_mismatch.h>
0015 #include <__assert>
0016 #include <__concepts/constructible.h>
0017 #include <__concepts/convertible_to.h>
0018 #include <__concepts/derived_from.h>
0019 #include <__config>
0020 #include <__functional/bind_back.h>
0021 #include <__functional/ranges_operations.h>
0022 #include <__iterator/concepts.h>
0023 #include <__iterator/default_sentinel.h>
0024 #include <__iterator/incrementable_traits.h>
0025 #include <__iterator/indirectly_comparable.h>
0026 #include <__iterator/iter_move.h>
0027 #include <__iterator/iter_swap.h>
0028 #include <__iterator/iterator_traits.h>
0029 #include <__memory/addressof.h>
0030 #include <__ranges/access.h>
0031 #include <__ranges/all.h>
0032 #include <__ranges/concepts.h>
0033 #include <__ranges/non_propagating_cache.h>
0034 #include <__ranges/range_adaptor.h>
0035 #include <__ranges/single_view.h>
0036 #include <__ranges/subrange.h>
0037 #include <__ranges/view_interface.h>
0038 #include <__type_traits/conditional.h>
0039 #include <__type_traits/decay.h>
0040 #include <__type_traits/is_nothrow_constructible.h>
0041 #include <__type_traits/maybe_const.h>
0042 #include <__type_traits/remove_reference.h>
0043 #include <__utility/forward.h>
0044 #include <__utility/move.h>
0045 
0046 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0047 #  pragma GCC system_header
0048 #endif
0049 
0050 _LIBCPP_PUSH_MACROS
0051 #include <__undef_macros>
0052 
0053 _LIBCPP_BEGIN_NAMESPACE_STD
0054 
0055 #if _LIBCPP_STD_VER >= 20
0056 
0057 namespace ranges {
0058 
0059 template <auto>
0060 struct __require_constant;
0061 
0062 template <class _Range>
0063 concept __tiny_range = sized_range<_Range> && requires {
0064   typename __require_constant<remove_reference_t<_Range>::size()>;
0065 } && (remove_reference_t<_Range>::size() <= 1);
0066 
0067 template <input_range _View, forward_range _Pattern>
0068   requires view<_View> && view<_Pattern> &&
0069            indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to> &&
0070            (forward_range<_View> || __tiny_range<_Pattern>)
0071 class lazy_split_view : public view_interface<lazy_split_view<_View, _Pattern>> {
0072   _LIBCPP_NO_UNIQUE_ADDRESS _View __base_       = _View();
0073   _LIBCPP_NO_UNIQUE_ADDRESS _Pattern __pattern_ = _Pattern();
0074 
0075   using _MaybeCurrent _LIBCPP_NODEBUG =
0076       _If<!forward_range<_View>, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
0077   _LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();
0078 
0079   template <bool>
0080   struct __outer_iterator;
0081   template <bool>
0082   struct __inner_iterator;
0083 
0084 public:
0085   _LIBCPP_HIDE_FROM_ABI lazy_split_view()
0086     requires default_initializable<_View> && default_initializable<_Pattern>
0087   = default;
0088 
0089   _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 lazy_split_view(_View __base, _Pattern __pattern)
0090       : __base_(std::move(__base)), __pattern_(std::move(__pattern)) {}
0091 
0092   template <input_range _Range>
0093     requires constructible_from<_View, views::all_t<_Range>> &&
0094                  constructible_from<_Pattern, single_view<range_value_t<_Range>>>
0095   _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 lazy_split_view(_Range&& __r, range_value_t<_Range> __e)
0096       : __base_(views::all(std::forward<_Range>(__r))), __pattern_(views::single(std::move(__e))) {}
0097 
0098   _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
0099     requires copy_constructible<_View>
0100   {
0101     return __base_;
0102   }
0103   _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
0104 
0105   _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
0106     if constexpr (forward_range<_View>) {
0107       return __outer_iterator < __simple_view<_View> && __simple_view < _Pattern >> {*this, ranges::begin(__base_)};
0108     } else {
0109       __current_.__emplace(ranges::begin(__base_));
0110       return __outer_iterator<false>{*this};
0111     }
0112   }
0113 
0114   _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
0115     requires forward_range<_View> && forward_range<const _View>
0116   {
0117     return __outer_iterator<true>{*this, ranges::begin(__base_)};
0118   }
0119 
0120   _LIBCPP_HIDE_FROM_ABI constexpr auto end()
0121     requires forward_range<_View> && common_range<_View>
0122   {
0123     return __outer_iterator < __simple_view<_View> && __simple_view < _Pattern >> {*this, ranges::end(__base_)};
0124   }
0125 
0126   _LIBCPP_HIDE_FROM_ABI constexpr auto end() const {
0127     if constexpr (forward_range<_View> && forward_range<const _View> && common_range<const _View>) {
0128       return __outer_iterator<true>{*this, ranges::end(__base_)};
0129     } else {
0130       return default_sentinel;
0131     }
0132   }
0133 
0134 private:
0135   template <class>
0136   struct __outer_iterator_category {};
0137 
0138   template <forward_range _Tp>
0139   struct __outer_iterator_category<_Tp> {
0140     using iterator_category = input_iterator_tag;
0141   };
0142 
0143   template <bool _Const>
0144   struct __outer_iterator : __outer_iterator_category<__maybe_const<_Const, _View>> {
0145   private:
0146     template <bool>
0147     friend struct __inner_iterator;
0148     friend __outer_iterator<true>;
0149 
0150     using _Parent _LIBCPP_NODEBUG = __maybe_const<_Const, lazy_split_view>;
0151     using _Base _LIBCPP_NODEBUG   = __maybe_const<_Const, _View>;
0152 
0153     _Parent* __parent_                                 = nullptr;
0154     using _MaybeCurrent _LIBCPP_NODEBUG                = _If<forward_range<_View>, iterator_t<_Base>, __empty_cache>;
0155     _LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();
0156     bool __trailing_empty_                             = false;
0157 
0158     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __current() noexcept {
0159       if constexpr (forward_range<_View>) {
0160         return __current_;
0161       } else {
0162         return *__parent_->__current_;
0163       }
0164     }
0165 
0166     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __current() const noexcept {
0167       if constexpr (forward_range<_View>) {
0168         return __current_;
0169       } else {
0170         return *__parent_->__current_;
0171       }
0172     }
0173 
0174     // Workaround for the GCC issue that doesn't allow calling `__parent_->__base_` from friend functions (because
0175     // `__base_` is private).
0176     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __parent_base() const noexcept { return __parent_->__base_; }
0177 
0178   public:
0179     // using iterator_category = inherited;
0180     using iterator_concept = conditional_t<forward_range<_Base>, forward_iterator_tag, input_iterator_tag>;
0181     using difference_type  = range_difference_t<_Base>;
0182 
0183     struct value_type : view_interface<value_type> {
0184     private:
0185       __outer_iterator __i_ = __outer_iterator();
0186 
0187     public:
0188       _LIBCPP_HIDE_FROM_ABI value_type() = default;
0189       _LIBCPP_HIDE_FROM_ABI constexpr explicit value_type(__outer_iterator __i) : __i_(std::move(__i)) {}
0190 
0191       _LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator<_Const> begin() const { return __inner_iterator<_Const>{__i_}; }
0192       _LIBCPP_HIDE_FROM_ABI constexpr default_sentinel_t end() const noexcept { return default_sentinel; }
0193     };
0194 
0195     _LIBCPP_HIDE_FROM_ABI __outer_iterator() = default;
0196 
0197     _LIBCPP_HIDE_FROM_ABI constexpr explicit __outer_iterator(_Parent& __parent)
0198       requires(!forward_range<_Base>)
0199         : __parent_(std::addressof(__parent)) {}
0200 
0201     _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(_Parent& __parent, iterator_t<_Base> __current)
0202       requires forward_range<_Base>
0203         : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}
0204 
0205     _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(__outer_iterator<!_Const> __i)
0206       requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
0207         : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
0208 
0209     _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return value_type{*this}; }
0210 
0211     _LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator& operator++() {
0212       const auto __end = ranges::end(__parent_->__base_);
0213       if (__current() == __end) {
0214         __trailing_empty_ = false;
0215         return *this;
0216       }
0217 
0218       const auto [__pbegin, __pend] = ranges::subrange{__parent_->__pattern_};
0219       if (__pbegin == __pend) {
0220         // Empty pattern: split on every element in the input range
0221         ++__current();
0222 
0223       } else if constexpr (__tiny_range<_Pattern>) {
0224         // One-element pattern: we can use `ranges::find`.
0225         __current() = ranges::find(std::move(__current()), __end, *__pbegin);
0226         if (__current() != __end) {
0227           // Make sure we point to after the separator we just found.
0228           ++__current();
0229           if (__current() == __end)
0230             __trailing_empty_ = true;
0231         }
0232 
0233       } else {
0234         // General case for n-element pattern.
0235         do {
0236           const auto [__b, __p] = ranges::mismatch(__current(), __end, __pbegin, __pend);
0237           if (__p == __pend) {
0238             __current() = __b;
0239             if (__current() == __end) {
0240               __trailing_empty_ = true;
0241             }
0242             break; // The pattern matched; skip it.
0243           }
0244         } while (++__current() != __end);
0245       }
0246 
0247       return *this;
0248     }
0249 
0250     _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {
0251       if constexpr (forward_range<_Base>) {
0252         auto __tmp = *this;
0253         ++*this;
0254         return __tmp;
0255 
0256       } else {
0257         ++*this;
0258       }
0259     }
0260 
0261     _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, const __outer_iterator& __y)
0262       requires forward_range<_Base>
0263     {
0264       return __x.__current_ == __y.__current_ && __x.__trailing_empty_ == __y.__trailing_empty_;
0265     }
0266 
0267     _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, default_sentinel_t) {
0268       _LIBCPP_ASSERT_NON_NULL(__x.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");
0269       return __x.__current() == ranges::end(__x.__parent_base()) && !__x.__trailing_empty_;
0270     }
0271   };
0272 
0273   template <class>
0274   struct __inner_iterator_category {};
0275 
0276   template <forward_range _Tp>
0277   struct __inner_iterator_category<_Tp> {
0278     using iterator_category =
0279         _If< derived_from<typename iterator_traits<iterator_t<_Tp>>::iterator_category, forward_iterator_tag>,
0280              forward_iterator_tag,
0281              typename iterator_traits<iterator_t<_Tp>>::iterator_category >;
0282   };
0283 
0284   template <bool _Const>
0285   struct __inner_iterator : __inner_iterator_category<__maybe_const<_Const, _View>> {
0286   private:
0287     using _Base _LIBCPP_NODEBUG = __maybe_const<_Const, _View>;
0288     // Workaround for a GCC issue.
0289     static constexpr bool _OuterConst = _Const;
0290     __outer_iterator<_Const> __i_     = __outer_iterator<_OuterConst>();
0291     bool __incremented_               = false;
0292 
0293     // Note: these private functions are necessary because GCC doesn't allow calls to private members of `__i_` from
0294     // free functions that are friends of `inner-iterator`.
0295 
0296     _LIBCPP_HIDE_FROM_ABI constexpr bool __is_done() const {
0297       _LIBCPP_ASSERT_NON_NULL(__i_.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");
0298 
0299       auto [__pcur, __pend] = ranges::subrange{__i_.__parent_->__pattern_};
0300       auto __end            = ranges::end(__i_.__parent_->__base_);
0301 
0302       if constexpr (__tiny_range<_Pattern>) {
0303         const auto& __cur = __i_.__current();
0304         if (__cur == __end)
0305           return true;
0306         if (__pcur == __pend)
0307           return __incremented_;
0308 
0309         return *__cur == *__pcur;
0310 
0311       } else {
0312         auto __cur = __i_.__current();
0313         if (__cur == __end)
0314           return true;
0315         if (__pcur == __pend)
0316           return __incremented_;
0317 
0318         do {
0319           if (*__cur != *__pcur)
0320             return false;
0321           if (++__pcur == __pend)
0322             return true;
0323         } while (++__cur != __end);
0324 
0325         return false;
0326       }
0327     }
0328 
0329     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __outer_current() noexcept { return __i_.__current(); }
0330 
0331     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __outer_current() const noexcept {
0332       return __i_.__current();
0333     }
0334 
0335   public:
0336     // using iterator_category = inherited;
0337     using iterator_concept = typename __outer_iterator<_Const>::iterator_concept;
0338     using value_type       = range_value_t<_Base>;
0339     using difference_type  = range_difference_t<_Base>;
0340 
0341     _LIBCPP_HIDE_FROM_ABI __inner_iterator() = default;
0342 
0343     _LIBCPP_HIDE_FROM_ABI constexpr explicit __inner_iterator(__outer_iterator<_Const> __i) : __i_(std::move(__i)) {}
0344 
0345     _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __i_.__current(); }
0346     _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() &&
0347       requires forward_range<_View>
0348     {
0349       return std::move(__i_.__current());
0350     }
0351 
0352     _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const { return *__i_.__current(); }
0353 
0354     _LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator& operator++() {
0355       __incremented_ = true;
0356 
0357       if constexpr (!forward_range<_Base>) {
0358         if constexpr (_Pattern::size() == 0) {
0359           return *this;
0360         }
0361       }
0362 
0363       ++__i_.__current();
0364       return *this;
0365     }
0366 
0367     _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {
0368       if constexpr (forward_range<_Base>) {
0369         auto __tmp = *this;
0370         ++*this;
0371         return __tmp;
0372 
0373       } else {
0374         ++*this;
0375       }
0376     }
0377 
0378     _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, const __inner_iterator& __y)
0379       requires forward_range<_Base>
0380     {
0381       return __x.__outer_current() == __y.__outer_current();
0382     }
0383 
0384     _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, default_sentinel_t) {
0385       return __x.__is_done();
0386     }
0387 
0388     _LIBCPP_HIDE_FROM_ABI friend constexpr decltype(auto)
0389     iter_move(const __inner_iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__outer_current()))) {
0390       return ranges::iter_move(__i.__outer_current());
0391     }
0392 
0393     _LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(
0394         const __inner_iterator& __x,
0395         const __inner_iterator& __y) noexcept(noexcept(ranges::iter_swap(__x.__outer_current(), __y.__outer_current())))
0396       requires indirectly_swappable<iterator_t<_Base>>
0397     {
0398       ranges::iter_swap(__x.__outer_current(), __y.__outer_current());
0399     }
0400   };
0401 };
0402 
0403 template <class _Range, class _Pattern>
0404 lazy_split_view(_Range&&, _Pattern&&) -> lazy_split_view<views::all_t<_Range>, views::all_t<_Pattern>>;
0405 
0406 template <input_range _Range>
0407 lazy_split_view(_Range&&,
0408                 range_value_t<_Range>) -> lazy_split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;
0409 
0410 namespace views {
0411 namespace __lazy_split_view {
0412 struct __fn {
0413   template <class _Range, class _Pattern>
0414   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pattern&& __pattern) const
0415       noexcept(noexcept(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))))
0416           -> decltype(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))) {
0417     return lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern));
0418   }
0419 
0420   template <class _Pattern>
0421     requires constructible_from<decay_t<_Pattern>, _Pattern>
0422   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pattern&& __pattern) const
0423       noexcept(is_nothrow_constructible_v<decay_t<_Pattern>, _Pattern>) {
0424     return __pipeable(std::__bind_back(*this, std::forward<_Pattern>(__pattern)));
0425   }
0426 };
0427 } // namespace __lazy_split_view
0428 
0429 inline namespace __cpo {
0430 inline constexpr auto lazy_split = __lazy_split_view::__fn{};
0431 } // namespace __cpo
0432 } // namespace views
0433 
0434 } // namespace ranges
0435 
0436 #endif // _LIBCPP_STD_VER >= 20
0437 
0438 _LIBCPP_END_NAMESPACE_STD
0439 
0440 _LIBCPP_POP_MACROS
0441 
0442 #endif // _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H