Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/chunk_by_view.h
35235 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_CHUNK_BY_VIEW_H10#define _LIBCPP___RANGES_CHUNK_BY_VIEW_H1112#include <__algorithm/ranges_adjacent_find.h>13#include <__assert>14#include <__concepts/constructible.h>15#include <__config>16#include <__functional/bind_back.h>17#include <__functional/invoke.h>18#include <__iterator/concepts.h>19#include <__iterator/default_sentinel.h>20#include <__iterator/iterator_traits.h>21#include <__iterator/next.h>22#include <__iterator/prev.h>23#include <__memory/addressof.h>24#include <__ranges/access.h>25#include <__ranges/all.h>26#include <__ranges/concepts.h>27#include <__ranges/movable_box.h>28#include <__ranges/non_propagating_cache.h>29#include <__ranges/range_adaptor.h>30#include <__ranges/reverse_view.h>31#include <__ranges/subrange.h>32#include <__ranges/view_interface.h>33#include <__type_traits/conditional.h>34#include <__type_traits/decay.h>35#include <__type_traits/is_nothrow_constructible.h>36#include <__type_traits/is_object.h>37#include <__utility/forward.h>38#include <__utility/in_place.h>39#include <__utility/move.h>4041#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)42# pragma GCC system_header43#endif4445_LIBCPP_PUSH_MACROS46#include <__undef_macros>4748_LIBCPP_BEGIN_NAMESPACE_STD4950#if _LIBCPP_STD_VER >= 235152namespace ranges {5354template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred>55requires view<_View> && is_object_v<_Pred>56class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS chunk_by_view : public view_interface<chunk_by_view<_View, _Pred>> {57_LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();58_LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_;5960// We cache the result of begin() to allow providing an amortized O(1).61using _Cache = __non_propagating_cache<iterator_t<_View>>;62_Cache __cached_begin_;6364class __iterator;6566_LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_next(iterator_t<_View> __current) {67// Note: this duplicates a check in `optional` but provides a better error message.68_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(69__pred_.__has_value(), "Trying to call __find_next() on a chunk_by_view that does not have a valid predicate.");70auto __reversed_pred = [this]<class _Tp, class _Up>(_Tp&& __x, _Up&& __y) -> bool {71return !std::invoke(*__pred_, std::forward<_Tp>(__x), std::forward<_Up>(__y));72};73return ranges::next(74ranges::adjacent_find(__current, ranges::end(__base_), __reversed_pred), 1, ranges::end(__base_));75}7677_LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_prev(iterator_t<_View> __current)78requires bidirectional_range<_View>79{80// Attempting to decrement a begin iterator is a no-op (`__find_prev` would return the same argument given to it).81_LIBCPP_ASSERT_PEDANTIC(__current != ranges::begin(__base_), "Trying to call __find_prev() on a begin iterator.");82// Note: this duplicates a check in `optional` but provides a better error message.83_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(84__pred_.__has_value(), "Trying to call __find_prev() on a chunk_by_view that does not have a valid predicate.");8586auto __first = ranges::begin(__base_);87reverse_view __reversed{subrange{__first, __current}};88auto __reversed_pred = [this]<class _Tp, class _Up>(_Tp&& __x, _Up&& __y) -> bool {89return !std::invoke(*__pred_, std::forward<_Up>(__y), std::forward<_Tp>(__x));90};91return ranges::prev(ranges::adjacent_find(__reversed, __reversed_pred).base(), 1, std::move(__first));92}9394public:95_LIBCPP_HIDE_FROM_ABI chunk_by_view()96requires default_initializable<_View> && default_initializable<_Pred>97= default;9899_LIBCPP_HIDE_FROM_ABI constexpr explicit chunk_by_view(_View __base, _Pred __pred)100: __base_(std::move(__base)), __pred_(in_place, std::move(__pred)) {}101102_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&103requires copy_constructible<_View>104{105return __base_;106}107108_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }109110_LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; }111112_LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() {113// Note: this duplicates a check in `optional` but provides a better error message.114_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(115__pred_.__has_value(), "Trying to call begin() on a chunk_by_view that does not have a valid predicate.");116117auto __first = ranges::begin(__base_);118if (!__cached_begin_.__has_value()) {119__cached_begin_.__emplace(__find_next(__first));120}121return {*this, std::move(__first), *__cached_begin_};122}123124_LIBCPP_HIDE_FROM_ABI constexpr auto end() {125if constexpr (common_range<_View>) {126return __iterator{*this, ranges::end(__base_), ranges::end(__base_)};127} else {128return default_sentinel;129}130}131};132133template <class _Range, class _Pred>134chunk_by_view(_Range&&, _Pred) -> chunk_by_view<views::all_t<_Range>, _Pred>;135136template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred>137requires view<_View> && is_object_v<_Pred>138class chunk_by_view<_View, _Pred>::__iterator {139friend chunk_by_view;140141chunk_by_view* __parent_ = nullptr;142_LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>();143_LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __next_ = iterator_t<_View>();144145_LIBCPP_HIDE_FROM_ABI constexpr __iterator(146chunk_by_view& __parent, iterator_t<_View> __current, iterator_t<_View> __next)147: __parent_(std::addressof(__parent)), __current_(__current), __next_(__next) {}148149public:150using value_type = subrange<iterator_t<_View>>;151using difference_type = range_difference_t<_View>;152using iterator_category = input_iterator_tag;153using iterator_concept = conditional_t<bidirectional_range<_View>, bidirectional_iterator_tag, forward_iterator_tag>;154155_LIBCPP_HIDE_FROM_ABI __iterator() = default;156157_LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const {158// If the iterator is at end, this would return an empty range which can be checked by the calling code and doesn't159// necessarily lead to a bad access.160_LIBCPP_ASSERT_PEDANTIC(__current_ != __next_, "Trying to dereference past-the-end chunk_by_view iterator.");161return {__current_, __next_};162}163164_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {165// Attempting to increment an end iterator is a no-op (`__find_next` would return the same argument given to it).166_LIBCPP_ASSERT_PEDANTIC(__current_ != __next_, "Trying to increment past end chunk_by_view iterator.");167__current_ = __next_;168__next_ = __parent_->__find_next(__current_);169return *this;170}171172_LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {173auto __tmp = *this;174++*this;175return __tmp;176}177178_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()179requires bidirectional_range<_View>180{181__next_ = __current_;182__current_ = __parent_->__find_prev(__next_);183return *this;184}185186_LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)187requires bidirectional_range<_View>188{189auto __tmp = *this;190--*this;191return __tmp;192}193194_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {195return __x.__current_ == __y.__current_;196}197198_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, default_sentinel_t) {199return __x.__current_ == __x.__next_;200}201};202203namespace views {204namespace __chunk_by {205struct __fn {206template <class _Range, class _Pred>207[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const208noexcept(noexcept(/**/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))209-> decltype(/*--*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {210return /*-------------*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));211}212213template <class _Pred>214requires constructible_from<decay_t<_Pred>, _Pred>215[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const216noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {217return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred)));218}219};220} // namespace __chunk_by221222inline namespace __cpo {223inline constexpr auto chunk_by = __chunk_by::__fn{};224} // namespace __cpo225} // namespace views226} // namespace ranges227228#endif // _LIBCPP_STD_VER >= 23229230_LIBCPP_END_NAMESPACE_STD231232_LIBCPP_POP_MACROS233234#endif // _LIBCPP___RANGES_CHUNK_BY_VIEW_H235236237