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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANGES_FILTER_VIEW_H
10 #define _LIBCPP___RANGES_FILTER_VIEW_H
12 #include <__algorithm/ranges_find_if.h>
15 #include <__functional/bind_back.h>
16 #include <__functional/invoke.h>
17 #include <__functional/reference_wrapper.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/iter_move.h>
20 #include <__iterator/iter_swap.h>
21 #include <__iterator/iterator_traits.h>
22 #include <__memory/addressof.h>
23 #include <__ranges/access.h>
24 #include <__ranges/all.h>
25 #include <__ranges/concepts.h>
26 #include <__ranges/copyable_box.h>
27 #include <__ranges/non_propagating_cache.h>
28 #include <__ranges/range_adaptor.h>
29 #include <__ranges/view_interface.h>
30 #include <__utility/forward.h>
31 #include <__utility/in_place.h>
32 #include <__utility/move.h>
34 #include <type_traits>
36 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
37 # pragma GCC system_header
40 _LIBCPP_BEGIN_NAMESPACE_STD
42 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
45 template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
46 requires view<_View> && is_object_v<_Pred>
47 class filter_view : public view_interface<filter_view<_View, _Pred>> {
48 _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
49 _LIBCPP_NO_UNIQUE_ADDRESS __copyable_box<_Pred> __pred_;
51 // We cache the result of begin() to allow providing an amortized O(1) begin() whenever
52 // the underlying range is at least a forward_range.
53 static constexpr bool _UseCache = forward_range<_View>;
54 using _Cache = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
55 _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();
62 filter_view() requires default_initializable<_View> && default_initializable<_Pred> = default;
65 constexpr filter_view(_View __base, _Pred __pred)
66 : __base_(std::move(__base)), __pred_(in_place, std::move(__pred))
69 template<class _Vp = _View>
71 constexpr _View base() const& requires copy_constructible<_Vp> { return __base_; }
73 constexpr _View base() && { return std::move(__base_); }
76 constexpr _Pred const& pred() const { return *__pred_; }
79 constexpr __iterator begin() {
80 _LIBCPP_ASSERT(__pred_.__has_value(), "Trying to call begin() on a filter_view that does not have a valid predicate.");
81 if constexpr (_UseCache) {
82 if (!__cached_begin_.__has_value()) {
83 __cached_begin_.__emplace(ranges::find_if(__base_, std::ref(*__pred_)));
85 return {*this, *__cached_begin_};
87 return {*this, ranges::find_if(__base_, std::ref(*__pred_))};
92 constexpr auto end() {
93 if constexpr (common_range<_View>)
94 return __iterator{*this, ranges::end(__base_)};
96 return __sentinel{*this};
100 template<class _Range, class _Pred>
101 filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
103 template<class _View>
104 struct __filter_iterator_category { };
106 template<forward_range _View>
107 struct __filter_iterator_category<_View> {
108 using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category;
109 using iterator_category =
110 _If<derived_from<_Cat, bidirectional_iterator_tag>, bidirectional_iterator_tag,
111 _If<derived_from<_Cat, forward_iterator_tag>, forward_iterator_tag,
116 template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
117 requires view<_View> && is_object_v<_Pred>
118 class filter_view<_View, _Pred>::__iterator : public __filter_iterator_category<_View> {
120 _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>();
121 _LIBCPP_NO_UNIQUE_ADDRESS filter_view* __parent_ = nullptr;
123 using iterator_concept =
124 _If<bidirectional_range<_View>, bidirectional_iterator_tag,
125 _If<forward_range<_View>, forward_iterator_tag,
126 /* else */ input_iterator_tag
128 // using iterator_category = inherited;
129 using value_type = range_value_t<_View>;
130 using difference_type = range_difference_t<_View>;
132 _LIBCPP_HIDE_FROM_ABI
133 __iterator() requires default_initializable<iterator_t<_View>> = default;
135 _LIBCPP_HIDE_FROM_ABI
136 constexpr __iterator(filter_view& __parent, iterator_t<_View> __current)
137 : __current_(std::move(__current)), __parent_(std::addressof(__parent))
140 _LIBCPP_HIDE_FROM_ABI
141 constexpr iterator_t<_View> const& base() const& noexcept { return __current_; }
142 _LIBCPP_HIDE_FROM_ABI
143 constexpr iterator_t<_View> base() && { return std::move(__current_); }
145 _LIBCPP_HIDE_FROM_ABI
146 constexpr range_reference_t<_View> operator*() const { return *__current_; }
147 _LIBCPP_HIDE_FROM_ABI
148 constexpr iterator_t<_View> operator->() const
149 requires __has_arrow<iterator_t<_View>> && copyable<iterator_t<_View>>
154 _LIBCPP_HIDE_FROM_ABI
155 constexpr __iterator& operator++() {
156 __current_ = ranges::find_if(std::move(++__current_), ranges::end(__parent_->__base_),
157 std::ref(*__parent_->__pred_));
160 _LIBCPP_HIDE_FROM_ABI
161 constexpr void operator++(int) { ++*this; }
162 _LIBCPP_HIDE_FROM_ABI
163 constexpr __iterator operator++(int) requires forward_range<_View> {
169 _LIBCPP_HIDE_FROM_ABI
170 constexpr __iterator& operator--() requires bidirectional_range<_View> {
173 } while (!std::invoke(*__parent_->__pred_, *__current_));
176 _LIBCPP_HIDE_FROM_ABI
177 constexpr __iterator operator--(int) requires bidirectional_range<_View> {
183 _LIBCPP_HIDE_FROM_ABI
184 friend constexpr bool operator==(__iterator const& __x, __iterator const& __y)
185 requires equality_comparable<iterator_t<_View>>
187 return __x.__current_ == __y.__current_;
190 _LIBCPP_HIDE_FROM_ABI
191 friend constexpr range_rvalue_reference_t<_View> iter_move(__iterator const& __it)
192 noexcept(noexcept(ranges::iter_move(__it.__current_)))
194 return ranges::iter_move(__it.__current_);
197 _LIBCPP_HIDE_FROM_ABI
198 friend constexpr void iter_swap(__iterator const& __x, __iterator const& __y)
199 noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
200 requires indirectly_swappable<iterator_t<_View>>
202 return ranges::iter_swap(__x.__current_, __y.__current_);
206 template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
207 requires view<_View> && is_object_v<_Pred>
208 class filter_view<_View, _Pred>::__sentinel {
210 sentinel_t<_View> __end_ = sentinel_t<_View>();
212 _LIBCPP_HIDE_FROM_ABI
213 __sentinel() = default;
215 _LIBCPP_HIDE_FROM_ABI
216 constexpr explicit __sentinel(filter_view& __parent)
217 : __end_(ranges::end(__parent.__base_))
220 _LIBCPP_HIDE_FROM_ABI
221 constexpr sentinel_t<_View> base() const { return __end_; }
223 _LIBCPP_HIDE_FROM_ABI
224 friend constexpr bool operator==(__iterator const& __x, __sentinel const& __y) {
225 return __x.__current_ == __y.__end_;
232 template<class _Range, class _Pred>
233 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
234 constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
235 noexcept(noexcept(filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
236 -> decltype( filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)))
237 { return filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)); }
239 template<class _Pred>
240 requires constructible_from<decay_t<_Pred>, _Pred>
241 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
242 constexpr auto operator()(_Pred&& __pred) const
243 noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>)
244 { return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred))); }
246 } // namespace __filter
248 inline namespace __cpo {
249 inline constexpr auto filter = __filter::__fn{};
253 } // namespace ranges
255 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
257 _LIBCPP_END_NAMESPACE_STD
259 #endif // _LIBCPP___RANGES_FILTER_VIEW_H