xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/take_while_view.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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/movable_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 template <view _View, class _Pred>
45   requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>>
46 class _LIBCPP_ABI_2023_OVERLAPPING_SUBOBJECT_FIX_TAG take_while_view
47     : public view_interface<take_while_view<_View, _Pred>> {
48   template <bool>
49   class __sentinel;
50 
51   _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
52   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_;
53 
54 public:
55   _LIBCPP_HIDE_FROM_ABI take_while_view()
56     requires default_initializable<_View> && default_initializable<_Pred>
57   = default;
58 
59   _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 take_while_view(_View __base, _Pred __pred)
60       : __base_(std::move(__base)), __pred_(std::in_place, std::move(__pred)) {}
61 
62   _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
63     requires copy_constructible<_View>
64   {
65     return __base_;
66   }
67 
68   _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
69 
70   _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; }
71 
72   _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
73     requires(!__simple_view<_View>)
74   {
75     return ranges::begin(__base_);
76   }
77 
78   _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
79     requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>
80   {
81     return ranges::begin(__base_);
82   }
83 
84   _LIBCPP_HIDE_FROM_ABI constexpr auto end()
85     requires(!__simple_view<_View>)
86   {
87     return __sentinel</*_Const=*/false>(ranges::end(__base_), std::addressof(*__pred_));
88   }
89 
90   _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
91     requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>
92   {
93     return __sentinel</*_Const=*/true>(ranges::end(__base_), std::addressof(*__pred_));
94   }
95 };
96 
97 template <class _Range, class _Pred>
98 take_while_view(_Range&&, _Pred) -> take_while_view<views::all_t<_Range>, _Pred>;
99 
100 template <view _View, class _Pred>
101   requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>>
102 template <bool _Const>
103 class take_while_view<_View, _Pred>::__sentinel {
104   using _Base = __maybe_const<_Const, _View>;
105 
106   sentinel_t<_Base> __end_ = sentinel_t<_Base>();
107   const _Pred* __pred_     = nullptr;
108 
109   friend class __sentinel<!_Const>;
110 
111 public:
112   _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
113 
114   _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end, const _Pred* __pred)
115       : __end_(std::move(__end)), __pred_(__pred) {}
116 
117   _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __s)
118     requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
119       : __end_(std::move(__s.__end_)), __pred_(__s.__pred_) {}
120 
121   _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
122 
123   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const iterator_t<_Base>& __x, const __sentinel& __y) {
124     return __x == __y.__end_ || !std::invoke(*__y.__pred_, *__x);
125   }
126 
127   template <bool _OtherConst = !_Const>
128     requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
129   _LIBCPP_HIDE_FROM_ABI friend constexpr bool
130   operator==(const iterator_t<__maybe_const<_OtherConst, _View>>& __x, const __sentinel& __y) {
131     return __x == __y.__end_ || !std::invoke(*__y.__pred_, *__x);
132   }
133 };
134 
135 namespace views {
136 namespace __take_while {
137 
138 struct __fn {
139   template <class _Range, class _Pred>
140   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
141       noexcept(noexcept(/**/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
142           -> decltype(/*--*/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {
143     return /*-------------*/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));
144   }
145 
146   template <class _Pred>
147     requires constructible_from<decay_t<_Pred>, _Pred>
148   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const
149       noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {
150     return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred)));
151   }
152 };
153 
154 } // namespace __take_while
155 
156 inline namespace __cpo {
157 inline constexpr auto take_while = __take_while::__fn{};
158 } // namespace __cpo
159 } // namespace views
160 } // namespace ranges
161 
162 #endif // _LIBCPP_STD_VER >= 20
163 
164 _LIBCPP_END_NAMESPACE_STD
165 
166 #endif // _LIBCPP___RANGES_TAKE_WHILE_VIEW_H
167