drop_while_view.h 4.7 KB

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