xref: /freebsd-src/contrib/llvm-project/libcxx/include/__ranges/drop_while_view.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
1bdd1243dSDimitry Andric // -*- C++ -*-
2bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
3bdd1243dSDimitry Andric //
4bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7bdd1243dSDimitry Andric //
8bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
9bdd1243dSDimitry Andric 
10bdd1243dSDimitry Andric #ifndef _LIBCPP___RANGES_DROP_WHILE_VIEW_H
11bdd1243dSDimitry Andric #define _LIBCPP___RANGES_DROP_WHILE_VIEW_H
12bdd1243dSDimitry Andric 
13bdd1243dSDimitry Andric #include <__algorithm/ranges_find_if_not.h>
14bdd1243dSDimitry Andric #include <__assert>
15bdd1243dSDimitry Andric #include <__concepts/constructible.h>
16bdd1243dSDimitry Andric #include <__config>
17bdd1243dSDimitry Andric #include <__functional/bind_back.h>
18bdd1243dSDimitry Andric #include <__functional/reference_wrapper.h>
19bdd1243dSDimitry Andric #include <__iterator/concepts.h>
20bdd1243dSDimitry Andric #include <__ranges/access.h>
21bdd1243dSDimitry Andric #include <__ranges/all.h>
22bdd1243dSDimitry Andric #include <__ranges/concepts.h>
23bdd1243dSDimitry Andric #include <__ranges/enable_borrowed_range.h>
2406c3fb27SDimitry Andric #include <__ranges/movable_box.h>
25bdd1243dSDimitry Andric #include <__ranges/non_propagating_cache.h>
26bdd1243dSDimitry Andric #include <__ranges/range_adaptor.h>
27bdd1243dSDimitry Andric #include <__ranges/view_interface.h>
28bdd1243dSDimitry Andric #include <__type_traits/conditional.h>
29bdd1243dSDimitry Andric #include <__type_traits/decay.h>
30bdd1243dSDimitry Andric #include <__type_traits/is_nothrow_constructible.h>
31bdd1243dSDimitry Andric #include <__type_traits/is_object.h>
32bdd1243dSDimitry Andric #include <__utility/forward.h>
33bdd1243dSDimitry Andric #include <__utility/in_place.h>
34bdd1243dSDimitry Andric #include <__utility/move.h>
35bdd1243dSDimitry Andric 
36bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
37bdd1243dSDimitry Andric #  pragma GCC system_header
38bdd1243dSDimitry Andric #endif
39bdd1243dSDimitry Andric 
40*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
41*b3edf446SDimitry Andric #include <__undef_macros>
42*b3edf446SDimitry Andric 
43bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
44bdd1243dSDimitry Andric 
45bdd1243dSDimitry Andric #if _LIBCPP_STD_VER >= 20
46bdd1243dSDimitry Andric 
47bdd1243dSDimitry Andric namespace ranges {
48bdd1243dSDimitry Andric 
49bdd1243dSDimitry Andric template <view _View, class _Pred>
50bdd1243dSDimitry Andric   requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>>
517a6dacacSDimitry Andric class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS drop_while_view : public view_interface<drop_while_view<_View, _Pred>> {
52bdd1243dSDimitry Andric public:
53bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI drop_while_view()
54bdd1243dSDimitry Andric     requires default_initializable<_View> && default_initializable<_Pred>
55bdd1243dSDimitry Andric   = default;
56bdd1243dSDimitry Andric 
drop_while_view(_View __base,_Pred __pred)5706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 drop_while_view(_View __base, _Pred __pred)
58bdd1243dSDimitry Andric       : __base_(std::move(__base)), __pred_(std::in_place, std::move(__pred)) {}
59bdd1243dSDimitry Andric 
base()60bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
61bdd1243dSDimitry Andric     requires copy_constructible<_View>
62bdd1243dSDimitry Andric   {
63bdd1243dSDimitry Andric     return __base_;
64bdd1243dSDimitry Andric   }
65bdd1243dSDimitry Andric 
base()66bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
67bdd1243dSDimitry Andric 
pred()68bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; }
69bdd1243dSDimitry Andric 
begin()70bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
711db9f3b2SDimitry Andric     // Note: this duplicates a check in `optional` but provides a better error message.
721db9f3b2SDimitry Andric     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
7306c3fb27SDimitry Andric         __pred_.__has_value(),
74bdd1243dSDimitry Andric         "drop_while_view needs to have a non-empty predicate before calling begin() -- did a previous "
75bdd1243dSDimitry Andric         "assignment to this drop_while_view fail?");
76bdd1243dSDimitry Andric     if constexpr (_UseCache) {
77bdd1243dSDimitry Andric       if (!__cached_begin_.__has_value()) {
78bdd1243dSDimitry Andric         __cached_begin_.__emplace(ranges::find_if_not(__base_, std::cref(*__pred_)));
79bdd1243dSDimitry Andric       }
80bdd1243dSDimitry Andric       return *__cached_begin_;
81bdd1243dSDimitry Andric     } else {
82bdd1243dSDimitry Andric       return ranges::find_if_not(__base_, std::cref(*__pred_));
83bdd1243dSDimitry Andric     }
84bdd1243dSDimitry Andric   }
85bdd1243dSDimitry Andric 
end()86bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto end() { return ranges::end(__base_); }
87bdd1243dSDimitry Andric 
88bdd1243dSDimitry Andric private:
89bdd1243dSDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
9006c3fb27SDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_;
91bdd1243dSDimitry Andric 
92bdd1243dSDimitry Andric   static constexpr bool _UseCache = forward_range<_View>;
93bdd1243dSDimitry Andric   using _Cache                    = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
94bdd1243dSDimitry Andric   _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();
95bdd1243dSDimitry Andric };
96bdd1243dSDimitry Andric 
97bdd1243dSDimitry Andric template <class _View, class _Pred>
98bdd1243dSDimitry Andric inline constexpr bool enable_borrowed_range<drop_while_view<_View, _Pred>> = enable_borrowed_range<_View>;
99bdd1243dSDimitry Andric 
100bdd1243dSDimitry Andric template <class _Range, class _Pred>
101bdd1243dSDimitry Andric drop_while_view(_Range&&, _Pred) -> drop_while_view<views::all_t<_Range>, _Pred>;
102bdd1243dSDimitry Andric 
103bdd1243dSDimitry Andric namespace views {
104bdd1243dSDimitry Andric namespace __drop_while {
105bdd1243dSDimitry Andric 
106bdd1243dSDimitry Andric struct __fn {
107bdd1243dSDimitry Andric   template <class _Range, class _Pred>
108bdd1243dSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
109bdd1243dSDimitry Andric       noexcept(noexcept(/**/ drop_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
110bdd1243dSDimitry Andric           -> decltype(/*--*/ drop_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {
111bdd1243dSDimitry Andric     return /*-------------*/ drop_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));
112bdd1243dSDimitry Andric   }
113bdd1243dSDimitry Andric 
114bdd1243dSDimitry Andric   template <class _Pred>
115bdd1243dSDimitry Andric     requires constructible_from<decay_t<_Pred>, _Pred>
operator__fn116bdd1243dSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const
117bdd1243dSDimitry Andric       noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {
118bdd1243dSDimitry Andric     return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred)));
119bdd1243dSDimitry Andric   }
120bdd1243dSDimitry Andric };
121bdd1243dSDimitry Andric 
122bdd1243dSDimitry Andric } // namespace __drop_while
123bdd1243dSDimitry Andric 
124bdd1243dSDimitry Andric inline namespace __cpo {
125bdd1243dSDimitry Andric inline constexpr auto drop_while = __drop_while::__fn{};
126bdd1243dSDimitry Andric } // namespace __cpo
127bdd1243dSDimitry Andric } // namespace views
128bdd1243dSDimitry Andric } // namespace ranges
129bdd1243dSDimitry Andric 
130bdd1243dSDimitry Andric #endif // _LIBCPP_STD_VER >= 20
131bdd1243dSDimitry Andric 
132bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
133bdd1243dSDimitry Andric 
134*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
135*b3edf446SDimitry Andric 
136bdd1243dSDimitry Andric #endif // _LIBCPP___RANGES_DROP_WHILE_VIEW_H
137