Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

143 linhas
4.4KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_VIEW_DROP_WHILE_HPP
  14. #define RANGES_V3_VIEW_DROP_WHILE_HPP
  15. #include <utility>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/algorithm/find_if_not.hpp>
  19. #include <range/v3/functional/bind_back.hpp>
  20. #include <range/v3/functional/compose.hpp>
  21. #include <range/v3/range/concepts.hpp>
  22. #include <range/v3/range/traits.hpp>
  23. #include <range/v3/utility/optional.hpp>
  24. #include <range/v3/utility/semiregular_box.hpp>
  25. #include <range/v3/utility/static_const.hpp>
  26. #include <range/v3/view/all.hpp>
  27. #include <range/v3/view/interface.hpp>
  28. #include <range/v3/view/view.hpp>
  29. namespace ranges
  30. {
  31. /// \addtogroup group-views
  32. /// @{
  33. template<typename Rng, typename Pred>
  34. struct drop_while_view
  35. : view_interface<drop_while_view<Rng, Pred>,
  36. is_finite<Rng>::value ? finite : unknown>
  37. {
  38. private:
  39. Rng rng_;
  40. RANGES_NO_UNIQUE_ADDRESS semiregular_box_t<Pred> pred_;
  41. detail::non_propagating_cache<iterator_t<Rng>> begin_;
  42. iterator_t<Rng> get_begin_()
  43. {
  44. if(!begin_)
  45. begin_ = find_if_not(rng_, std::ref(pred_));
  46. return *begin_;
  47. }
  48. public:
  49. drop_while_view() = default;
  50. drop_while_view(Rng rng, Pred pred)
  51. : rng_(std::move(rng))
  52. , pred_(std::move(pred))
  53. {}
  54. iterator_t<Rng> begin()
  55. {
  56. return get_begin_();
  57. }
  58. sentinel_t<Rng> end()
  59. {
  60. return ranges::end(rng_);
  61. }
  62. Rng base() const
  63. {
  64. return rng_;
  65. }
  66. };
  67. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  68. CPP_template(typename Rng, typename Fun)(requires copy_constructible<Fun>)
  69. drop_while_view(Rng &&, Fun)
  70. ->drop_while_view<views::all_t<Rng>, Fun>;
  71. #endif
  72. namespace views
  73. {
  74. struct drop_while_fn
  75. {
  76. private:
  77. friend view_access;
  78. template<typename Pred>
  79. static constexpr auto bind(drop_while_fn drop_while, Pred pred)
  80. {
  81. return make_pipeable(bind_back(drop_while, std::move(pred)));
  82. }
  83. template<typename Pred, typename Proj>
  84. static constexpr auto bind(drop_while_fn drop_while, Pred pred, Proj proj)
  85. {
  86. return make_pipeable(
  87. bind_back(drop_while, std::move(pred), std::move(proj)));
  88. }
  89. public:
  90. template<typename Rng, typename Pred>
  91. auto operator()(Rng && rng, Pred pred) const
  92. -> CPP_ret(drop_while_view<all_t<Rng>, Pred>)( //
  93. requires viewable_range<Rng> && input_range<Rng> &&
  94. indirect_unary_predicate<Pred, iterator_t<Rng>>)
  95. {
  96. return {all(static_cast<Rng &&>(rng)), std::move(pred)};
  97. }
  98. template<typename Rng, typename Pred, typename Proj>
  99. auto operator()(Rng && rng, Pred pred, Proj proj) const
  100. -> CPP_ret(drop_while_view<all_t<Rng>, composed<Pred, Proj>>)( //
  101. requires viewable_range<Rng> && input_range<Rng> &&
  102. indirect_unary_predicate<composed<Pred, Proj>, iterator_t<Rng>>)
  103. {
  104. return {all(static_cast<Rng &&>(rng)),
  105. compose(std::move(pred), std::move(proj))};
  106. }
  107. };
  108. /// \relates drop_while_fn
  109. /// \ingroup group-views
  110. RANGES_INLINE_VARIABLE(view<drop_while_fn>, drop_while)
  111. } // namespace views
  112. namespace cpp20
  113. {
  114. namespace views
  115. {
  116. using ranges::views::drop_while;
  117. }
  118. CPP_template(typename Rng, typename Pred)( //
  119. requires viewable_range<Rng> && input_range<Rng> &&
  120. indirect_unary_predicate<Pred, iterator_t<Rng>>) //
  121. using drop_while_view = ranges::drop_while_view<Rng, Pred>;
  122. } // namespace cpp20
  123. /// @}
  124. } // namespace ranges
  125. #include <range/v3/detail/satisfy_boost_range.hpp>
  126. RANGES_SATISFY_BOOST_RANGE(::ranges::drop_while_view)
  127. #endif