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_DROP_WHILE_VIEW_H 11 #define _LIBCPP___RANGES_DROP_WHILE_VIEW_H 12 13 #include <__algorithm/ranges_find_if_not.h> 14 #include <__assert> 15 #include <__concepts/constructible.h> 16 #include <__config> 17 #include <__functional/bind_back.h> 18 #include <__functional/reference_wrapper.h> 19 #include <__iterator/concepts.h> 20 #include <__ranges/access.h> 21 #include <__ranges/all.h> 22 #include <__ranges/concepts.h> 23 #include <__ranges/enable_borrowed_range.h> 24 #include <__ranges/movable_box.h> 25 #include <__ranges/non_propagating_cache.h> 26 #include <__ranges/range_adaptor.h> 27 #include <__ranges/view_interface.h> 28 #include <__type_traits/conditional.h> 29 #include <__type_traits/decay.h> 30 #include <__type_traits/is_nothrow_constructible.h> 31 #include <__type_traits/is_object.h> 32 #include <__utility/forward.h> 33 #include <__utility/in_place.h> 34 #include <__utility/move.h> 35 36 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 37 # pragma GCC system_header 38 #endif 39 40 _LIBCPP_PUSH_MACROS 41 #include <__undef_macros> 42 43 _LIBCPP_BEGIN_NAMESPACE_STD 44 45 #if _LIBCPP_STD_VER >= 20 46 47 namespace ranges { 48 49 template <view _View, class _Pred> 50 requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>> 51 class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS drop_while_view : public view_interface<drop_while_view<_View, _Pred>> { 52 public: 53 _LIBCPP_HIDE_FROM_ABI drop_while_view() 54 requires default_initializable<_View> && default_initializable<_Pred> 55 = default; 56 57 _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 drop_while_view(_View __base, _Pred __pred) 58 : __base_(std::move(__base)), __pred_(std::in_place, std::move(__pred)) {} 59 60 _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& 61 requires copy_constructible<_View> 62 { 63 return __base_; 64 } 65 66 _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); } 67 68 _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; } 69 70 _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { 71 // Note: this duplicates a check in `optional` but provides a better error message. 72 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( 73 __pred_.__has_value(), 74 "drop_while_view needs to have a non-empty predicate before calling begin() -- did a previous " 75 "assignment to this drop_while_view fail?"); 76 if constexpr (_UseCache) { 77 if (!__cached_begin_.__has_value()) { 78 __cached_begin_.__emplace(ranges::find_if_not(__base_, std::cref(*__pred_))); 79 } 80 return *__cached_begin_; 81 } else { 82 return ranges::find_if_not(__base_, std::cref(*__pred_)); 83 } 84 } 85 86 _LIBCPP_HIDE_FROM_ABI constexpr auto end() { return ranges::end(__base_); } 87 88 private: 89 _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View(); 90 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_; 91 92 static constexpr bool _UseCache = forward_range<_View>; 93 using _Cache = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>; 94 _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache(); 95 }; 96 97 template <class _View, class _Pred> 98 inline constexpr bool enable_borrowed_range<drop_while_view<_View, _Pred>> = enable_borrowed_range<_View>; 99 100 template <class _Range, class _Pred> 101 drop_while_view(_Range&&, _Pred) -> drop_while_view<views::all_t<_Range>, _Pred>; 102 103 namespace views { 104 namespace __drop_while { 105 106 struct __fn { 107 template <class _Range, class _Pred> 108 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const 109 noexcept(noexcept(/**/ drop_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)))) 110 -> decltype(/*--*/ drop_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) { 111 return /*-------------*/ drop_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)); 112 } 113 114 template <class _Pred> 115 requires constructible_from<decay_t<_Pred>, _Pred> 116 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const 117 noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) { 118 return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred))); 119 } 120 }; 121 122 } // namespace __drop_while 123 124 inline namespace __cpo { 125 inline constexpr auto drop_while = __drop_while::__fn{}; 126 } // namespace __cpo 127 } // namespace views 128 } // namespace ranges 129 130 #endif // _LIBCPP_STD_VER >= 20 131 132 _LIBCPP_END_NAMESPACE_STD 133 134 _LIBCPP_POP_MACROS 135 136 #endif // _LIBCPP___RANGES_DROP_WHILE_VIEW_H 137