2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___RANGES_CHUNK_BY_VIEW_H
11 #define _LIBCPP___RANGES_CHUNK_BY_VIEW_H
13 #include <__algorithm/ranges_adjacent_find.h>
15 #include <__concepts/constructible.h>
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>
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 # pragma GCC system_header
49 #include <__undef_macros>
51 _LIBCPP_BEGIN_NAMESPACE_STD
53 #if _LIBCPP_STD_VER >= 23
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_
;
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_
;
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.");
73 return ranges::next(ranges::adjacent_find(__current
, ranges::end(__base_
), std::not_fn(std::ref(*__pred_
))),
75 ranges::end(__base_
));
78 _LIBCPP_HIDE_FROM_ABI
constexpr iterator_t
<_View
> __find_prev(iterator_t
<_View
> __current
)
79 requires bidirectional_range
<_View
>
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.");
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
));
91 return ranges::prev(ranges::adjacent_find(__reversed
, __reversed_pred
).base(), 1, std::move(__first
));
95 _LIBCPP_HIDE_FROM_ABI
chunk_by_view()
96 requires default_initializable
<_View
> && default_initializable
<_Pred
>
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
)) {}
102 _LIBCPP_HIDE_FROM_ABI
constexpr _View
base() const&
103 requires copy_constructible
<_View
>
108 _LIBCPP_HIDE_FROM_ABI
constexpr _View
base() && { return std::move(__base_
); }
110 _LIBCPP_HIDE_FROM_ABI
constexpr const _Pred
& pred() const { return *__pred_
; }
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.");
116 auto __first
= ranges::begin(__base_
);
117 if (!__cached_begin_
.__has_value()) {
118 __cached_begin_
.__emplace(__find_next(__first
));
120 return {*this, std::move(__first
), *__cached_begin_
};
123 _LIBCPP_HIDE_FROM_ABI
constexpr auto end() {
124 if constexpr (common_range
<_View
>) {
125 return __iterator
{*this, ranges::end(__base_
), ranges::end(__base_
)};
127 return default_sentinel
;
132 template <class _Range
, class _Pred
>
133 chunk_by_view(_Range
&&, _Pred
) -> chunk_by_view
<views::all_t
<_Range
>, _Pred
>;
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
;
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
>();
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
) {}
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
>;
154 _LIBCPP_HIDE_FROM_ABI
__iterator() = default;
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_
};
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_
);
168 _LIBCPP_HIDE_FROM_ABI
constexpr __iterator
operator++(int) {
174 _LIBCPP_HIDE_FROM_ABI
constexpr __iterator
& operator--()
175 requires bidirectional_range
<_View
>
177 __next_
= __current_
;
178 __current_
= __parent_
->__find_prev(__next_
);
182 _LIBCPP_HIDE_FROM_ABI
constexpr __iterator
operator--(int)
183 requires bidirectional_range
<_View
>
190 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const __iterator
& __x
, const __iterator
& __y
) {
191 return __x
.__current_
== __y
.__current_
;
194 _LIBCPP_HIDE_FROM_ABI
friend constexpr bool operator==(const __iterator
& __x
, default_sentinel_t
) {
195 return __x
.__current_
== __x
.__next_
;
200 namespace __chunk_by
{
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
));
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
)));
216 } // namespace __chunk_by
218 inline namespace __cpo
{
219 inline constexpr auto chunk_by
= __chunk_by::__fn
{};
222 } // namespace ranges
224 #endif // _LIBCPP_STD_VER >= 23
226 _LIBCPP_END_NAMESPACE_STD
230 #endif // _LIBCPP___RANGES_CHUNK_BY_VIEW_H