]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/libcxx/include/__ranges/filter_view.h
Merge llvm-project main llvmorg-15-init-17485-ga3e38b4a206b
[FreeBSD/FreeBSD.git] / contrib / llvm-project / libcxx / include / __ranges / filter_view.h
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 #ifndef _LIBCPP___RANGES_FILTER_VIEW_H
10 #define _LIBCPP___RANGES_FILTER_VIEW_H
11
12 #include <__algorithm/ranges_find_if.h>
13 #include <__config>
14 #include <__debug>
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>
33 #include <concepts>
34 #include <type_traits>
35
36 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
37 #  pragma GCC system_header
38 #endif
39
40 _LIBCPP_BEGIN_NAMESPACE_STD
41
42 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
43
44 namespace 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_;
50
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();
56
57     class __iterator;
58     class __sentinel;
59
60   public:
61     _LIBCPP_HIDE_FROM_ABI
62     filter_view() requires default_initializable<_View> && default_initializable<_Pred> = default;
63
64     _LIBCPP_HIDE_FROM_ABI
65     constexpr filter_view(_View __base, _Pred __pred)
66       : __base_(std::move(__base)), __pred_(in_place, std::move(__pred))
67     { }
68
69     template<class _Vp = _View>
70     _LIBCPP_HIDE_FROM_ABI
71     constexpr _View base() const& requires copy_constructible<_Vp> { return __base_; }
72     _LIBCPP_HIDE_FROM_ABI
73     constexpr _View base() && { return std::move(__base_); }
74
75     _LIBCPP_HIDE_FROM_ABI
76     constexpr _Pred const& pred() const { return *__pred_; }
77
78     _LIBCPP_HIDE_FROM_ABI
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_)));
84         }
85         return {*this, *__cached_begin_};
86       } else {
87         return {*this, ranges::find_if(__base_, std::ref(*__pred_))};
88       }
89     }
90
91     _LIBCPP_HIDE_FROM_ABI
92     constexpr auto end() {
93       if constexpr (common_range<_View>)
94         return __iterator{*this, ranges::end(__base_)};
95       else
96         return __sentinel{*this};
97     }
98   };
99
100   template<class _Range, class _Pred>
101   filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
102
103   template<class _View>
104   struct __filter_iterator_category { };
105
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,
112       /* else */                                          _Cat
113     >>;
114   };
115
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> {
119   public:
120     _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>();
121     _LIBCPP_NO_UNIQUE_ADDRESS filter_view* __parent_ = nullptr;
122
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
127     >>;
128     // using iterator_category = inherited;
129     using value_type = range_value_t<_View>;
130     using difference_type = range_difference_t<_View>;
131
132     _LIBCPP_HIDE_FROM_ABI
133     __iterator() requires default_initializable<iterator_t<_View>> = default;
134
135     _LIBCPP_HIDE_FROM_ABI
136     constexpr __iterator(filter_view& __parent, iterator_t<_View> __current)
137       : __current_(std::move(__current)), __parent_(std::addressof(__parent))
138     { }
139
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_); }
144
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>>
150     {
151       return __current_;
152     }
153
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_));
158       return *this;
159     }
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> {
164       auto __tmp = *this;
165       ++*this;
166       return __tmp;
167     }
168
169     _LIBCPP_HIDE_FROM_ABI
170     constexpr __iterator& operator--() requires bidirectional_range<_View> {
171       do {
172         --__current_;
173       } while (!std::invoke(*__parent_->__pred_, *__current_));
174       return *this;
175     }
176     _LIBCPP_HIDE_FROM_ABI
177     constexpr __iterator operator--(int) requires bidirectional_range<_View> {
178       auto tmp = *this;
179       --*this;
180       return tmp;
181     }
182
183     _LIBCPP_HIDE_FROM_ABI
184     friend constexpr bool operator==(__iterator const& __x, __iterator const& __y)
185       requires equality_comparable<iterator_t<_View>>
186     {
187       return __x.__current_ == __y.__current_;
188     }
189
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_)))
193     {
194       return ranges::iter_move(__it.__current_);
195     }
196
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>>
201     {
202       return ranges::iter_swap(__x.__current_, __y.__current_);
203     }
204   };
205
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 {
209   public:
210     sentinel_t<_View> __end_ = sentinel_t<_View>();
211
212     _LIBCPP_HIDE_FROM_ABI
213     __sentinel() = default;
214
215     _LIBCPP_HIDE_FROM_ABI
216     constexpr explicit __sentinel(filter_view& __parent)
217       : __end_(ranges::end(__parent.__base_))
218     { }
219
220     _LIBCPP_HIDE_FROM_ABI
221     constexpr sentinel_t<_View> base() const { return __end_; }
222
223     _LIBCPP_HIDE_FROM_ABI
224     friend constexpr bool operator==(__iterator const& __x, __sentinel const& __y) {
225       return __x.__current_ == __y.__end_;
226     }
227   };
228
229 namespace views {
230 namespace __filter {
231   struct __fn {
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)); }
238
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))); }
245   };
246 } // namespace __filter
247
248 inline namespace __cpo {
249   inline constexpr auto filter = __filter::__fn{};
250 } // namespace __cpo
251 } // namespace views
252
253 } // namespace ranges
254
255 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
256
257 _LIBCPP_END_NAMESPACE_STD
258
259 #endif // _LIBCPP___RANGES_FILTER_VIEW_H