|
| 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_CHUNK_BY_VIEW_H |
| 11 | +#define _LIBCPP___RANGES_CHUNK_BY_VIEW_H |
| 12 | + |
| 13 | +#include <__algorithm/ranges_adjacent_find.h> |
| 14 | +#include <__assert> |
| 15 | +#include <__concepts/constructible.h> |
| 16 | +#include <__config> |
| 17 | +#include <__functional/bind_back.h> |
| 18 | +#include <__functional/invoke.h> |
| 19 | +#include <__functional/not_fn.h> |
| 20 | +#include <__functional/reference_wrapper.h> |
| 21 | +#include <__iterator/concepts.h> |
| 22 | +#include <__iterator/default_sentinel.h> |
| 23 | +#include <__iterator/iterator_traits.h> |
| 24 | +#include <__iterator/next.h> |
| 25 | +#include <__iterator/prev.h> |
| 26 | +#include <__memory/addressof.h> |
| 27 | +#include <__ranges/access.h> |
| 28 | +#include <__ranges/all.h> |
| 29 | +#include <__ranges/concepts.h> |
| 30 | +#include <__ranges/movable_box.h> |
| 31 | +#include <__ranges/non_propagating_cache.h> |
| 32 | +#include <__ranges/range_adaptor.h> |
| 33 | +#include <__ranges/reverse_view.h> |
| 34 | +#include <__ranges/subrange.h> |
| 35 | +#include <__ranges/view_interface.h> |
| 36 | +#include <__type_traits/conditional.h> |
| 37 | +#include <__type_traits/decay.h> |
| 38 | +#include <__type_traits/is_nothrow_constructible.h> |
| 39 | +#include <__type_traits/is_object.h> |
| 40 | +#include <__utility/forward.h> |
| 41 | +#include <__utility/in_place.h> |
| 42 | +#include <__utility/move.h> |
| 43 | + |
| 44 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 45 | +# pragma GCC system_header |
| 46 | +#endif |
| 47 | + |
| 48 | +_LIBCPP_PUSH_MACROS |
| 49 | +#include <__undef_macros> |
| 50 | + |
| 51 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 52 | + |
| 53 | +#if _LIBCPP_STD_VER >= 23 |
| 54 | + |
| 55 | +namespace ranges { |
| 56 | + |
| 57 | +template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred> |
| 58 | + requires view<_View> && is_object_v<_Pred> |
| 59 | +class chunk_by_view : public view_interface<chunk_by_view<_View, _Pred>> { |
| 60 | + _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View(); |
| 61 | + _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_; |
| 62 | + |
| 63 | + // We cache the result of begin() to allow providing an amortized O(1). |
| 64 | + using _Cache = __non_propagating_cache<iterator_t<_View>>; |
| 65 | + _Cache __cached_begin_; |
| 66 | + |
| 67 | + class __iterator; |
| 68 | + |
| 69 | + _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_next(iterator_t<_View> __current) { |
| 70 | + _LIBCPP_ASSERT_UNCATEGORIZED( |
| 71 | + __pred_.__has_value(), "Trying to call __find_next() on a chunk_by_view that does not have a valid predicate."); |
| 72 | + |
| 73 | + return ranges::next(ranges::adjacent_find(__current, ranges::end(__base_), std::not_fn(std::ref(*__pred_))), |
| 74 | + 1, |
| 75 | + ranges::end(__base_)); |
| 76 | + } |
| 77 | + |
| 78 | + _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_prev(iterator_t<_View> __current) |
| 79 | + requires bidirectional_range<_View> |
| 80 | + { |
| 81 | + _LIBCPP_ASSERT_UNCATEGORIZED( |
| 82 | + __current != ranges::begin(__base_), "Trying to call __find_prev() on a begin iterator."); |
| 83 | + _LIBCPP_ASSERT_UNCATEGORIZED( |
| 84 | + __pred_.__has_value(), "Trying to call __find_prev() on a chunk_by_view that does not have a valid predicate."); |
| 85 | + |
| 86 | + auto __first = ranges::begin(__base_); |
| 87 | + reverse_view __reversed{subrange{__first, __current}}; |
| 88 | + auto __reversed_pred = [this]<class _Tp, class _Up>(_Tp&& __x, _Up&& __y) { |
| 89 | + return !std::invoke(*__pred_, std::forward<_Up>(__y), std::forward<_Tp>(__x)); |
| 90 | + }; |
| 91 | + return ranges::prev(ranges::adjacent_find(__reversed, __reversed_pred).base(), 1, std::move(__first)); |
| 92 | + } |
| 93 | + |
| 94 | +public: |
| 95 | + _LIBCPP_HIDE_FROM_ABI chunk_by_view() |
| 96 | + requires default_initializable<_View> && default_initializable<_Pred> |
| 97 | + = default; |
| 98 | + |
| 99 | + _LIBCPP_HIDE_FROM_ABI constexpr explicit chunk_by_view(_View __base, _Pred __pred) |
| 100 | + : __base_(std::move(__base)), __pred_(in_place, std::move(__pred)) {} |
| 101 | + |
| 102 | + _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& |
| 103 | + requires copy_constructible<_View> |
| 104 | + { |
| 105 | + return __base_; |
| 106 | + } |
| 107 | + |
| 108 | + _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); } |
| 109 | + |
| 110 | + _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; } |
| 111 | + |
| 112 | + _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() { |
| 113 | + _LIBCPP_ASSERT_UNCATEGORIZED( |
| 114 | + __pred_.__has_value(), "Trying to call begin() on a chunk_by_view that does not have a valid predicate."); |
| 115 | + |
| 116 | + auto __first = ranges::begin(__base_); |
| 117 | + if (!__cached_begin_.__has_value()) { |
| 118 | + __cached_begin_.__emplace(__find_next(__first)); |
| 119 | + } |
| 120 | + return {*this, std::move(__first), *__cached_begin_}; |
| 121 | + } |
| 122 | + |
| 123 | + _LIBCPP_HIDE_FROM_ABI constexpr auto end() { |
| 124 | + if constexpr (common_range<_View>) { |
| 125 | + return __iterator{*this, ranges::end(__base_), ranges::end(__base_)}; |
| 126 | + } else { |
| 127 | + return default_sentinel; |
| 128 | + } |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +template <class _Range, class _Pred> |
| 133 | +chunk_by_view(_Range&&, _Pred) -> chunk_by_view<views::all_t<_Range>, _Pred>; |
| 134 | + |
| 135 | +template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred> |
| 136 | + requires view<_View> && is_object_v<_Pred> |
| 137 | +class chunk_by_view<_View, _Pred>::__iterator { |
| 138 | + friend chunk_by_view; |
| 139 | + |
| 140 | + chunk_by_view* __parent_ = nullptr; |
| 141 | + _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>(); |
| 142 | + _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __next_ = iterator_t<_View>(); |
| 143 | + |
| 144 | + _LIBCPP_HIDE_FROM_ABI constexpr __iterator( |
| 145 | + chunk_by_view& __parent, iterator_t<_View> __current, iterator_t<_View> __next) |
| 146 | + : __parent_(std::addressof(__parent)), __current_(__current), __next_(__next) {} |
| 147 | + |
| 148 | +public: |
| 149 | + using value_type = subrange<iterator_t<_View>>; |
| 150 | + using difference_type = range_difference_t<_View>; |
| 151 | + using iterator_category = input_iterator_tag; |
| 152 | + using iterator_concept = conditional_t<bidirectional_range<_View>, bidirectional_iterator_tag, forward_iterator_tag>; |
| 153 | + |
| 154 | + _LIBCPP_HIDE_FROM_ABI __iterator() = default; |
| 155 | + |
| 156 | + _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { |
| 157 | + _LIBCPP_ASSERT_UNCATEGORIZED(__current_ != __next_, "Trying to dereference past-the-end chunk_by_view iterator."); |
| 158 | + return {__current_, __next_}; |
| 159 | + } |
| 160 | + |
| 161 | + _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() { |
| 162 | + _LIBCPP_ASSERT_UNCATEGORIZED(__current_ != __next_, "Trying to increment past end chunk_by_view iterator."); |
| 163 | + __current_ = __next_; |
| 164 | + __next_ = __parent_->__find_next(__current_); |
| 165 | + return *this; |
| 166 | + } |
| 167 | + |
| 168 | + _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) { |
| 169 | + auto __tmp = *this; |
| 170 | + ++*this; |
| 171 | + return __tmp; |
| 172 | + } |
| 173 | + |
| 174 | + _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--() |
| 175 | + requires bidirectional_range<_View> |
| 176 | + { |
| 177 | + __next_ = __current_; |
| 178 | + __current_ = __parent_->__find_prev(__next_); |
| 179 | + return *this; |
| 180 | + } |
| 181 | + |
| 182 | + _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int) |
| 183 | + requires bidirectional_range<_View> |
| 184 | + { |
| 185 | + auto __tmp = *this; |
| 186 | + --*this; |
| 187 | + return __tmp; |
| 188 | + } |
| 189 | + |
| 190 | + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) { |
| 191 | + return __x.__current_ == __y.__current_; |
| 192 | + } |
| 193 | + |
| 194 | + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, default_sentinel_t) { |
| 195 | + return __x.__current_ == __x.__next_; |
| 196 | + } |
| 197 | +}; |
| 198 | + |
| 199 | +namespace views { |
| 200 | +namespace __chunk_by { |
| 201 | +struct __fn { |
| 202 | + template <class _Range, class _Pred> |
| 203 | + _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const |
| 204 | + noexcept(noexcept(/**/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)))) |
| 205 | + -> decltype(/*--*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) { |
| 206 | + return /*-------------*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)); |
| 207 | + } |
| 208 | + |
| 209 | + template <class _Pred> |
| 210 | + requires constructible_from<decay_t<_Pred>, _Pred> |
| 211 | + _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const |
| 212 | + noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) { |
| 213 | + return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred))); |
| 214 | + } |
| 215 | +}; |
| 216 | +} // namespace __chunk_by |
| 217 | + |
| 218 | +inline namespace __cpo { |
| 219 | +inline constexpr auto chunk_by = __chunk_by::__fn{}; |
| 220 | +} // namespace __cpo |
| 221 | +} // namespace views |
| 222 | +} // namespace ranges |
| 223 | + |
| 224 | +#endif // _LIBCPP_STD_VER >= 23 |
| 225 | + |
| 226 | +_LIBCPP_END_NAMESPACE_STD |
| 227 | + |
| 228 | +_LIBCPP_POP_MACROS |
| 229 | + |
| 230 | +#endif // _LIBCPP___RANGES_CHUNK_BY_VIEW_H |
0 commit comments