Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/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_SPLIT_VIEW_H10#define _LIBCPP___RANGES_SPLIT_VIEW_H1112#include <__algorithm/ranges_search.h>13#include <__concepts/constructible.h>14#include <__config>15#include <__functional/bind_back.h>16#include <__functional/ranges_operations.h>17#include <__iterator/indirectly_comparable.h>18#include <__iterator/iterator_traits.h>19#include <__memory/addressof.h>20#include <__ranges/access.h>21#include <__ranges/all.h>22#include <__ranges/concepts.h>23#include <__ranges/empty.h>24#include <__ranges/non_propagating_cache.h>25#include <__ranges/range_adaptor.h>26#include <__ranges/single_view.h>27#include <__ranges/subrange.h>28#include <__ranges/view_interface.h>29#include <__type_traits/decay.h>30#include <__type_traits/is_nothrow_constructible.h>31#include <__utility/forward.h>32#include <__utility/move.h>3334#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)35# pragma GCC system_header36#endif3738_LIBCPP_PUSH_MACROS39#include <__undef_macros>4041_LIBCPP_BEGIN_NAMESPACE_STD4243#if _LIBCPP_STD_VER >= 204445namespace ranges {4647template <forward_range _View, forward_range _Pattern>48requires view<_View> && view<_Pattern> &&49indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to>50class split_view : public view_interface<split_view<_View, _Pattern>> {51private:52_LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();53_LIBCPP_NO_UNIQUE_ADDRESS _Pattern __pattern_ = _Pattern();54using _Cache = __non_propagating_cache<subrange<iterator_t<_View>>>;55_Cache __cached_begin_ = _Cache();5657template <class, class>58friend struct __iterator;5960template <class, class>61friend struct __sentinel;6263struct __iterator;64struct __sentinel;6566_LIBCPP_HIDE_FROM_ABI constexpr subrange<iterator_t<_View>> __find_next(iterator_t<_View> __it) {67auto [__begin, __end] = ranges::search(subrange(__it, ranges::end(__base_)), __pattern_);68if (__begin != ranges::end(__base_) && ranges::empty(__pattern_)) {69++__begin;70++__end;71}72return {__begin, __end};73}7475public:76_LIBCPP_HIDE_FROM_ABI split_view()77requires default_initializable<_View> && default_initializable<_Pattern>78= default;7980_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 split_view(_View __base, _Pattern __pattern)81: __base_(std::move(__base)), __pattern_(std::move((__pattern))) {}8283template <forward_range _Range>84requires constructible_from<_View, views::all_t<_Range>> &&85constructible_from<_Pattern, single_view<range_value_t<_Range>>>86_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX2387split_view(_Range&& __range, range_value_t<_Range> __elem)88: __base_(views::all(std::forward<_Range>(__range))), __pattern_(views::single(std::move(__elem))) {}8990_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&91requires copy_constructible<_View>92{93return __base_;94}9596_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }9798_LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() {99if (!__cached_begin_.__has_value()) {100__cached_begin_.__emplace(__find_next(ranges::begin(__base_)));101}102return {*this, ranges::begin(__base_), *__cached_begin_};103}104105_LIBCPP_HIDE_FROM_ABI constexpr auto end() {106if constexpr (common_range<_View>) {107return __iterator{*this, ranges::end(__base_), {}};108} else {109return __sentinel{*this};110}111}112};113114template <class _Range, class _Pattern>115split_view(_Range&&, _Pattern&&) -> split_view<views::all_t<_Range>, views::all_t<_Pattern>>;116117template <forward_range _Range>118split_view(_Range&&, range_value_t<_Range>) -> split_view<views::all_t<_Range>, single_view<range_value_t<_Range>>>;119120template <forward_range _View, forward_range _Pattern>121requires view<_View> && view<_Pattern> &&122indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to>123struct split_view<_View, _Pattern>::__iterator {124private:125split_view* __parent_ = nullptr;126_LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __cur_ = iterator_t<_View>();127_LIBCPP_NO_UNIQUE_ADDRESS subrange<iterator_t<_View>> __next_ = subrange<iterator_t<_View>>();128bool __trailing_empty_ = false;129130friend struct __sentinel;131132public:133using iterator_concept = forward_iterator_tag;134using iterator_category = input_iterator_tag;135using value_type = subrange<iterator_t<_View>>;136using difference_type = range_difference_t<_View>;137138_LIBCPP_HIDE_FROM_ABI __iterator() = default;139140_LIBCPP_HIDE_FROM_ABI constexpr __iterator(141split_view<_View, _Pattern>& __parent, iterator_t<_View> __current, subrange<iterator_t<_View>> __next)142: __parent_(std::addressof(__parent)), __cur_(std::move(__current)), __next_(std::move(__next)) {}143144_LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> base() const { return __cur_; }145146_LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { return {__cur_, __next_.begin()}; }147148_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {149__cur_ = __next_.begin();150if (__cur_ != ranges::end(__parent_->__base_)) {151__cur_ = __next_.end();152if (__cur_ == ranges::end(__parent_->__base_)) {153__trailing_empty_ = true;154__next_ = {__cur_, __cur_};155} else {156__next_ = __parent_->__find_next(__cur_);157}158} else {159__trailing_empty_ = false;160}161return *this;162}163164_LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {165auto __tmp = *this;166++*this;167return __tmp;168}169170_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {171return __x.__cur_ == __y.__cur_ && __x.__trailing_empty_ == __y.__trailing_empty_;172}173};174175template <forward_range _View, forward_range _Pattern>176requires view<_View> && view<_Pattern> &&177indirectly_comparable<iterator_t<_View>, iterator_t<_Pattern>, ranges::equal_to>178struct split_view<_View, _Pattern>::__sentinel {179private:180_LIBCPP_NO_UNIQUE_ADDRESS sentinel_t<_View> __end_ = sentinel_t<_View>();181182_LIBCPP_HIDE_FROM_ABI static constexpr bool __equals(const __iterator& __x, const __sentinel& __y) {183return __x.__cur_ == __y.__end_ && !__x.__trailing_empty_;184}185186public:187_LIBCPP_HIDE_FROM_ABI __sentinel() = default;188189_LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(split_view<_View, _Pattern>& __parent)190: __end_(ranges::end(__parent.__base_)) {}191192_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __sentinel& __y) {193return __equals(__x, __y);194}195};196197namespace views {198namespace __split_view {199struct __fn {200// clang-format off201template <class _Range, class _Pattern>202[[nodiscard]] _LIBCPP_HIDE_FROM_ABI203constexpr auto operator()(_Range&& __range, _Pattern&& __pattern) const204noexcept(noexcept(split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern))))205-> decltype( split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern)))206{ return split_view(std::forward<_Range>(__range), std::forward<_Pattern>(__pattern)); }207// clang-format on208209template <class _Pattern>210requires constructible_from<decay_t<_Pattern>, _Pattern>211[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pattern&& __pattern) const212noexcept(is_nothrow_constructible_v<decay_t<_Pattern>, _Pattern>) {213return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pattern>(__pattern)));214}215};216} // namespace __split_view217218inline namespace __cpo {219inline constexpr auto split = __split_view::__fn{};220} // namespace __cpo221} // namespace views222223} // namespace ranges224225#endif // _LIBCPP_STD_VER >= 20226227_LIBCPP_END_NAMESPACE_STD228229_LIBCPP_POP_MACROS230231#endif // _LIBCPP___RANGES_SPLIT_VIEW_H232233234