Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/lazy_split_view.h
35236 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H10#define _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H1112#include <__algorithm/ranges_find.h>13#include <__algorithm/ranges_mismatch.h>14#include <__assert>15#include <__concepts/constructible.h>16#include <__concepts/convertible_to.h>17#include <__concepts/derived_from.h>18#include <__config>19#include <__functional/bind_back.h>20#include <__functional/ranges_operations.h>21#include <__iterator/concepts.h>22#include <__iterator/default_sentinel.h>23#include <__iterator/incrementable_traits.h>24#include <__iterator/indirectly_comparable.h>25#include <__iterator/iter_move.h>26#include <__iterator/iter_swap.h>27#include <__iterator/iterator_traits.h>28#include <__memory/addressof.h>29#include <__ranges/access.h>30#include <__ranges/all.h>31#include <__ranges/concepts.h>32#include <__ranges/non_propagating_cache.h>33#include <__ranges/range_adaptor.h>34#include <__ranges/single_view.h>35#include <__ranges/subrange.h>36#include <__ranges/view_interface.h>37#include <__type_traits/conditional.h>38#include <__type_traits/decay.h>39#include <__type_traits/is_nothrow_constructible.h>40#include <__type_traits/maybe_const.h>41#include <__type_traits/remove_reference.h>42#include <__utility/forward.h>43#include <__utility/move.h>4445#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)46# pragma GCC system_header47#endif4849_LIBCPP_PUSH_MACROS50#include <__undef_macros>5152_LIBCPP_BEGIN_NAMESPACE_STD5354#if _LIBCPP_STD_VER >= 205556namespace ranges {5758template <auto>59struct __require_constant;6061template <class _Range>62concept __tiny_range = sized_range<_Range> && requires {63typename __require_constant<remove_reference_t<_Range>::size()>;64} && (remove_reference_t<_Range>::size() <= 1);6566template <input_range _View, forward_range _Pattern>67requires view<_View> && view<_Pattern> &&68indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to> &&69(forward_range<_View> || __tiny_range<_Pattern>)70class lazy_split_view : public view_interface<lazy_split_view<_View, _Pattern>> {71_LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();72_LIBCPP_NO_UNIQUE_ADDRESS _Pattern __pattern_ = _Pattern();7374using _MaybeCurrent = _If<!forward_range<_View>, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;75_LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();7677template <bool>78struct __outer_iterator;79template <bool>80struct __inner_iterator;8182public:83_LIBCPP_HIDE_FROM_ABI lazy_split_view()84requires default_initializable<_View> && default_initializable<_Pattern>85= default;8687_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 lazy_split_view(_View __base, _Pattern __pattern)88: __base_(std::move(__base)), __pattern_(std::move(__pattern)) {}8990template <input_range _Range>91requires constructible_from<_View, views::all_t<_Range>> &&92constructible_from<_Pattern, single_view<range_value_t<_Range>>>93_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 lazy_split_view(_Range&& __r, range_value_t<_Range> __e)94: __base_(views::all(std::forward<_Range>(__r))), __pattern_(views::single(std::move(__e))) {}9596_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&97requires copy_constructible<_View>98{99return __base_;100}101_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }102103_LIBCPP_HIDE_FROM_ABI constexpr auto begin() {104if constexpr (forward_range<_View>) {105return __outer_iterator < __simple_view<_View> && __simple_view < _Pattern >> {*this, ranges::begin(__base_)};106} else {107__current_.__emplace(ranges::begin(__base_));108return __outer_iterator<false>{*this};109}110}111112_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const113requires forward_range<_View> && forward_range<const _View>114{115return __outer_iterator<true>{*this, ranges::begin(__base_)};116}117118_LIBCPP_HIDE_FROM_ABI constexpr auto end()119requires forward_range<_View> && common_range<_View>120{121return __outer_iterator < __simple_view<_View> && __simple_view < _Pattern >> {*this, ranges::end(__base_)};122}123124_LIBCPP_HIDE_FROM_ABI constexpr auto end() const {125if constexpr (forward_range<_View> && forward_range<const _View> && common_range<const _View>) {126return __outer_iterator<true>{*this, ranges::end(__base_)};127} else {128return default_sentinel;129}130}131132private:133template <class>134struct __outer_iterator_category {};135136template <forward_range _Tp>137struct __outer_iterator_category<_Tp> {138using iterator_category = input_iterator_tag;139};140141template <bool _Const>142struct __outer_iterator : __outer_iterator_category<__maybe_const<_Const, _View>> {143private:144template <bool>145friend struct __inner_iterator;146friend __outer_iterator<true>;147148using _Parent = __maybe_const<_Const, lazy_split_view>;149using _Base = __maybe_const<_Const, _View>;150151_Parent* __parent_ = nullptr;152using _MaybeCurrent = _If<forward_range<_View>, iterator_t<_Base>, __empty_cache>;153_LIBCPP_NO_UNIQUE_ADDRESS _MaybeCurrent __current_ = _MaybeCurrent();154bool __trailing_empty_ = false;155156[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __current() noexcept {157if constexpr (forward_range<_View>) {158return __current_;159} else {160return *__parent_->__current_;161}162}163164[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __current() const noexcept {165if constexpr (forward_range<_View>) {166return __current_;167} else {168return *__parent_->__current_;169}170}171172// Workaround for the GCC issue that doesn't allow calling `__parent_->__base_` from friend functions (because173// `__base_` is private).174[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __parent_base() const noexcept { return __parent_->__base_; }175176public:177// using iterator_category = inherited;178using iterator_concept = conditional_t<forward_range<_Base>, forward_iterator_tag, input_iterator_tag>;179using difference_type = range_difference_t<_Base>;180181struct value_type : view_interface<value_type> {182private:183__outer_iterator __i_ = __outer_iterator();184185public:186_LIBCPP_HIDE_FROM_ABI value_type() = default;187_LIBCPP_HIDE_FROM_ABI constexpr explicit value_type(__outer_iterator __i) : __i_(std::move(__i)) {}188189_LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator<_Const> begin() const { return __inner_iterator<_Const>{__i_}; }190_LIBCPP_HIDE_FROM_ABI constexpr default_sentinel_t end() const noexcept { return default_sentinel; }191};192193_LIBCPP_HIDE_FROM_ABI __outer_iterator() = default;194195_LIBCPP_HIDE_FROM_ABI constexpr explicit __outer_iterator(_Parent& __parent)196requires(!forward_range<_Base>)197: __parent_(std::addressof(__parent)) {}198199_LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(_Parent& __parent, iterator_t<_Base> __current)200requires forward_range<_Base>201: __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}202203_LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator(__outer_iterator<!_Const> __i)204requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>205: __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}206207_LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return value_type{*this}; }208209_LIBCPP_HIDE_FROM_ABI constexpr __outer_iterator& operator++() {210const auto __end = ranges::end(__parent_->__base_);211if (__current() == __end) {212__trailing_empty_ = false;213return *this;214}215216const auto [__pbegin, __pend] = ranges::subrange{__parent_->__pattern_};217if (__pbegin == __pend) {218// Empty pattern: split on every element in the input range219++__current();220221} else if constexpr (__tiny_range<_Pattern>) {222// One-element pattern: we can use `ranges::find`.223__current() = ranges::find(std::move(__current()), __end, *__pbegin);224if (__current() != __end) {225// Make sure we point to after the separator we just found.226++__current();227if (__current() == __end)228__trailing_empty_ = true;229}230231} else {232// General case for n-element pattern.233do {234const auto [__b, __p] = ranges::mismatch(__current(), __end, __pbegin, __pend);235if (__p == __pend) {236__current() = __b;237if (__current() == __end) {238__trailing_empty_ = true;239}240break; // The pattern matched; skip it.241}242} while (++__current() != __end);243}244245return *this;246}247248_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {249if constexpr (forward_range<_Base>) {250auto __tmp = *this;251++*this;252return __tmp;253254} else {255++*this;256}257}258259_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, const __outer_iterator& __y)260requires forward_range<_Base>261{262return __x.__current_ == __y.__current_ && __x.__trailing_empty_ == __y.__trailing_empty_;263}264265_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __outer_iterator& __x, default_sentinel_t) {266_LIBCPP_ASSERT_NON_NULL(__x.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");267return __x.__current() == ranges::end(__x.__parent_base()) && !__x.__trailing_empty_;268}269};270271template <class>272struct __inner_iterator_category {};273274template <forward_range _Tp>275struct __inner_iterator_category<_Tp> {276using iterator_category =277_If< derived_from<typename iterator_traits<iterator_t<_Tp>>::iterator_category, forward_iterator_tag>,278forward_iterator_tag,279typename iterator_traits<iterator_t<_Tp>>::iterator_category >;280};281282template <bool _Const>283struct __inner_iterator : __inner_iterator_category<__maybe_const<_Const, _View>> {284private:285using _Base = __maybe_const<_Const, _View>;286// Workaround for a GCC issue.287static constexpr bool _OuterConst = _Const;288__outer_iterator<_Const> __i_ = __outer_iterator<_OuterConst>();289bool __incremented_ = false;290291// Note: these private functions are necessary because GCC doesn't allow calls to private members of `__i_` from292// free functions that are friends of `inner-iterator`.293294_LIBCPP_HIDE_FROM_ABI constexpr bool __is_done() const {295_LIBCPP_ASSERT_NON_NULL(__i_.__parent_ != nullptr, "Cannot call comparison on a default-constructed iterator.");296297auto [__pcur, __pend] = ranges::subrange{__i_.__parent_->__pattern_};298auto __end = ranges::end(__i_.__parent_->__base_);299300if constexpr (__tiny_range<_Pattern>) {301const auto& __cur = __i_.__current();302if (__cur == __end)303return true;304if (__pcur == __pend)305return __incremented_;306307return *__cur == *__pcur;308309} else {310auto __cur = __i_.__current();311if (__cur == __end)312return true;313if (__pcur == __pend)314return __incremented_;315316do {317if (*__cur != *__pcur)318return false;319if (++__pcur == __pend)320return true;321} while (++__cur != __end);322323return false;324}325}326327[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto& __outer_current() noexcept { return __i_.__current(); }328329[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const auto& __outer_current() const noexcept {330return __i_.__current();331}332333public:334// using iterator_category = inherited;335using iterator_concept = typename __outer_iterator<_Const>::iterator_concept;336using value_type = range_value_t<_Base>;337using difference_type = range_difference_t<_Base>;338339_LIBCPP_HIDE_FROM_ABI __inner_iterator() = default;340341_LIBCPP_HIDE_FROM_ABI constexpr explicit __inner_iterator(__outer_iterator<_Const> __i) : __i_(std::move(__i)) {}342343_LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __i_.__current(); }344_LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() &&345requires forward_range<_View>346{347return std::move(__i_.__current());348}349350_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const { return *__i_.__current(); }351352_LIBCPP_HIDE_FROM_ABI constexpr __inner_iterator& operator++() {353__incremented_ = true;354355if constexpr (!forward_range<_Base>) {356if constexpr (_Pattern::size() == 0) {357return *this;358}359}360361++__i_.__current();362return *this;363}364365_LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator++(int) {366if constexpr (forward_range<_Base>) {367auto __tmp = *this;368++*this;369return __tmp;370371} else {372++*this;373}374}375376_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, const __inner_iterator& __y)377requires forward_range<_Base>378{379return __x.__outer_current() == __y.__outer_current();380}381382_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __inner_iterator& __x, default_sentinel_t) {383return __x.__is_done();384}385386_LIBCPP_HIDE_FROM_ABI friend constexpr decltype(auto)387iter_move(const __inner_iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__outer_current()))) {388return ranges::iter_move(__i.__outer_current());389}390391_LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(392const __inner_iterator& __x,393const __inner_iterator& __y) noexcept(noexcept(ranges::iter_swap(__x.__outer_current(), __y.__outer_current())))394requires indirectly_swappable<iterator_t<_Base>>395{396ranges::iter_swap(__x.__outer_current(), __y.__outer_current());397}398};399};400401template <class _Range, class _Pattern>402lazy_split_view(_Range&&, _Pattern&&) -> lazy_split_view<views::all_t<_Range>, views::all_t<_Pattern>>;403404template <input_range _Range>405lazy_split_view(_Range&&,406range_value_t<_Range>) -> lazy_split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;407408namespace views {409namespace __lazy_split_view {410struct __fn {411template <class _Range, class _Pattern>412[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pattern&& __pattern) const413noexcept(noexcept(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))))414-> decltype(lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))) {415return lazy_split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern));416}417418template <class _Pattern>419requires constructible_from<decay_t<_Pattern>, _Pattern>420[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pattern&& __pattern) const421noexcept(is_nothrow_constructible_v<decay_t<_Pattern>, _Pattern>) {422return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pattern>(__pattern)));423}424};425} // namespace __lazy_split_view426427inline namespace __cpo {428inline constexpr auto lazy_split = __lazy_split_view::__fn{};429} // namespace __cpo430} // namespace views431432} // namespace ranges433434#endif // _LIBCPP_STD_VER >= 20435436_LIBCPP_END_NAMESPACE_STD437438_LIBCPP_POP_MACROS439440#endif // _LIBCPP___RANGES_LAZY_SPLIT_VIEW_H441442443