xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/take_while_view.h (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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_TAKE_WHILE_VIEW_H
11 #define _LIBCPP___RANGES_TAKE_WHILE_VIEW_H
12 
13 #include <__concepts/constructible.h>
14 #include <__concepts/convertible_to.h>
15 #include <__config>
16 #include <__functional/bind_back.h>
17 #include <__functional/invoke.h>
18 #include <__iterator/concepts.h>
19 #include <__memory/addressof.h>
20 #include <__ranges/access.h>
21 #include <__ranges/all.h>
22 #include <__ranges/concepts.h>
23 #include <__ranges/copyable_box.h>
24 #include <__ranges/range_adaptor.h>
25 #include <__ranges/view_interface.h>
26 #include <__type_traits/decay.h>
27 #include <__type_traits/is_nothrow_constructible.h>
28 #include <__type_traits/is_object.h>
29 #include <__type_traits/maybe_const.h>
30 #include <__utility/forward.h>
31 #include <__utility/in_place.h>
32 #include <__utility/move.h>
33 
34 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
35 #  pragma GCC system_header
36 #endif
37 
38 _LIBCPP_BEGIN_NAMESPACE_STD
39 
40 #if _LIBCPP_STD_VER >= 20
41 
42 namespace ranges {
43 
44 // The spec uses the unnamed requirement inside the `begin` and `end` member functions:
45 //     constexpr auto begin() const
46 //       requires range<const V> && indirect_unary_predicate<const Pred, iterator_t<const V>>
47 // However, due to a clang-14 and clang-15 bug, the above produces a hard error when `const V` is not a range.
48 // The workaround is to create a named concept and use the concept instead.
49 // As of take_while_view is implemented, the clang-trunk has already fixed the bug.
50 // It is OK to remove the workaround once our CI no longer uses clang-14, clang-15 based compilers,
51 // because we don't actually expect a lot of vendors to ship a new libc++ with an old clang.
52 template <class _View, class _Pred>
53 concept __take_while_const_is_range =
54     range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>;
55 
56 template <view _View, class _Pred>
57   requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>>
58 class take_while_view : public view_interface<take_while_view<_View, _Pred>> {
59   template <bool>
60   class __sentinel;
61 
62   _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
63   _LIBCPP_NO_UNIQUE_ADDRESS __copyable_box<_Pred> __pred_;
64 
65 public:
66   _LIBCPP_HIDE_FROM_ABI take_while_view()
67     requires default_initializable<_View> && default_initializable<_Pred>
68   = default;
69 
70   _LIBCPP_HIDE_FROM_ABI constexpr take_while_view(_View __base, _Pred __pred)
71       : __base_(std::move(__base)), __pred_(std::in_place, std::move(__pred)) {}
72 
73   _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
74     requires copy_constructible<_View>
75   {
76     return __base_;
77   }
78 
79   _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
80 
81   _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; }
82 
83   _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
84     requires(!__simple_view<_View>)
85   {
86     return ranges::begin(__base_);
87   }
88 
89   _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
90     requires __take_while_const_is_range<_View, _Pred>
91   {
92     return ranges::begin(__base_);
93   }
94 
95   _LIBCPP_HIDE_FROM_ABI constexpr auto end()
96     requires(!__simple_view<_View>)
97   {
98     return __sentinel</*_Const=*/false>(ranges::end(__base_), std::addressof(*__pred_));
99   }
100 
101   _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
102     requires __take_while_const_is_range<_View, _Pred>
103   {
104     return __sentinel</*_Const=*/true>(ranges::end(__base_), std::addressof(*__pred_));
105   }
106 };
107 
108 template <class _Range, class _Pred>
109 take_while_view(_Range&&, _Pred) -> take_while_view<views::all_t<_Range>, _Pred>;
110 
111 template <view _View, class _Pred>
112   requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>>
113 template <bool _Const>
114 class take_while_view<_View, _Pred>::__sentinel {
115   using _Base = __maybe_const<_Const, _View>;
116 
117   sentinel_t<_Base> __end_ = sentinel_t<_Base>();
118   const _Pred* __pred_     = nullptr;
119 
120   friend class __sentinel<!_Const>;
121 
122 public:
123   _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
124 
125   _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end, const _Pred* __pred)
126       : __end_(std::move(__end)), __pred_(__pred) {}
127 
128   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __s)
129     requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
130       : __end_(std::move(__s.__end_)), __pred_(__s.__pred_) {}
131 
132   _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
133 
134   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const iterator_t<_Base>& __x, const __sentinel& __y) {
135     return __x == __y.__end_ || !std::invoke(*__y.__pred_, *__x);
136   }
137 
138   template <bool _OtherConst = !_Const>
139     requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
140   _LIBCPP_HIDE_FROM_ABI friend constexpr bool
141   operator==(const iterator_t<__maybe_const<_OtherConst, _View>>& __x, const __sentinel& __y) {
142     return __x == __y.__end_ || !std::invoke(*__y.__pred_, *__x);
143   }
144 };
145 
146 namespace views {
147 namespace __take_while {
148 
149 struct __fn {
150   template <class _Range, class _Pred>
151   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
152       noexcept(noexcept(/**/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
153           -> decltype(/*--*/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {
154     return /*-------------*/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));
155   }
156 
157   template <class _Pred>
158     requires constructible_from<decay_t<_Pred>, _Pred>
159   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const
160       noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {
161     return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred)));
162   }
163 };
164 
165 } // namespace __take_while
166 
167 inline namespace __cpo {
168 inline constexpr auto take_while = __take_while::__fn{};
169 } // namespace __cpo
170 } // namespace views
171 } // namespace ranges
172 
173 #endif // _LIBCPP_STD_VER >= 20
174 
175 _LIBCPP_END_NAMESPACE_STD
176 
177 #endif // _LIBCPP___RANGES_TAKE_WHILE_VIEW_H
178