You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

remove_if.hpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_REMOVE_IF_HPP
  14. #define RANGES_V3_VIEW_REMOVE_IF_HPP
  15. #include <type_traits>
  16. #include <utility>
  17. #include <meta/meta.hpp>
  18. #include <range/v3/range_fwd.hpp>
  19. #include <range/v3/functional/bind_back.hpp>
  20. #include <range/v3/functional/compose.hpp>
  21. #include <range/v3/functional/invoke.hpp>
  22. #include <range/v3/range/access.hpp>
  23. #include <range/v3/range/concepts.hpp>
  24. #include <range/v3/range/traits.hpp>
  25. #include <range/v3/utility/box.hpp>
  26. #include <range/v3/utility/optional.hpp>
  27. #include <range/v3/utility/semiregular_box.hpp>
  28. #include <range/v3/utility/static_const.hpp>
  29. #include <range/v3/view/adaptor.hpp>
  30. #include <range/v3/view/view.hpp>
  31. RANGES_DISABLE_WARNINGS
  32. namespace ranges
  33. {
  34. /// \addtogroup group-views
  35. /// @{
  36. template<typename Rng, typename Pred>
  37. struct RANGES_EMPTY_BASES remove_if_view
  38. : view_adaptor<remove_if_view<Rng, Pred>, Rng,
  39. is_finite<Rng>::value ? finite : range_cardinality<Rng>::value>
  40. , private box<semiregular_box_t<Pred>>
  41. {
  42. remove_if_view() = default;
  43. constexpr remove_if_view(Rng rng, Pred pred)
  44. : remove_if_view::view_adaptor{detail::move(rng)}
  45. , remove_if_view::box(detail::move(pred))
  46. {}
  47. private:
  48. friend range_access;
  49. struct adaptor : adaptor_base
  50. {
  51. adaptor() = default;
  52. constexpr adaptor(remove_if_view * rng) noexcept
  53. : rng_(rng)
  54. {}
  55. static constexpr iterator_t<Rng> begin(remove_if_view & rng)
  56. {
  57. return *rng.begin_;
  58. }
  59. constexpr void next(iterator_t<Rng> & it) const
  60. {
  61. RANGES_ASSERT(it != ranges::end(rng_->base()));
  62. rng_->satisfy_forward(++it);
  63. }
  64. CPP_member
  65. constexpr auto prev(iterator_t<Rng> & it) const -> CPP_ret(void)( //
  66. requires bidirectional_range<Rng>)
  67. {
  68. rng_->satisfy_reverse(it);
  69. }
  70. void advance() = delete;
  71. void distance_to() = delete;
  72. private:
  73. remove_if_view * rng_;
  74. };
  75. constexpr adaptor begin_adaptor()
  76. {
  77. cache_begin();
  78. return {this};
  79. }
  80. CPP_member
  81. constexpr auto end_adaptor() const noexcept -> CPP_ret(adaptor_base)( //
  82. requires(!common_range<Rng>))
  83. {
  84. return {};
  85. }
  86. CPP_member
  87. constexpr auto end_adaptor() -> CPP_ret(adaptor)( //
  88. requires common_range<Rng>)
  89. {
  90. if(bidirectional_range<Rng>)
  91. cache_begin();
  92. return {this};
  93. }
  94. constexpr void satisfy_forward(iterator_t<Rng> & it)
  95. {
  96. auto const last = ranges::end(this->base());
  97. auto & pred = this->remove_if_view::box::get();
  98. while(it != last && invoke(pred, *it))
  99. ++it;
  100. }
  101. constexpr void satisfy_reverse(iterator_t<Rng> & it)
  102. {
  103. RANGES_ASSERT(begin_);
  104. auto const & first = *begin_;
  105. auto & pred = this->remove_if_view::box::get();
  106. do
  107. {
  108. RANGES_ASSERT(it != first);
  109. (void)first;
  110. --it;
  111. } while(invoke(pred, *it));
  112. }
  113. constexpr void cache_begin()
  114. {
  115. if(begin_)
  116. return;
  117. auto it = ranges::begin(this->base());
  118. satisfy_forward(it);
  119. begin_.emplace(std::move(it));
  120. }
  121. detail::non_propagating_cache<iterator_t<Rng>> begin_;
  122. };
  123. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  124. CPP_template(typename Rng, typename Pred)(requires copy_constructible<Pred>)
  125. remove_if_view(Rng &&, Pred)
  126. ->remove_if_view<views::all_t<Rng>, Pred>;
  127. #endif
  128. namespace views
  129. {
  130. /// Given a source range, unary predicate, and optional projection,
  131. /// present a view of the elements that do not satisfy the predicate.
  132. struct remove_if_fn
  133. {
  134. private:
  135. friend view_access;
  136. template<typename Pred>
  137. static constexpr auto bind(remove_if_fn remove_if, Pred pred)
  138. {
  139. return make_pipeable(bind_back(remove_if, std::move(pred)));
  140. }
  141. template<typename Pred, typename Proj>
  142. static constexpr auto bind(remove_if_fn remove_if, Pred pred, Proj proj)
  143. {
  144. return make_pipeable(
  145. bind_back(remove_if, std::move(pred), std::move(proj)));
  146. }
  147. public:
  148. template<typename Rng, typename Pred>
  149. constexpr auto operator()(Rng && rng, Pred pred) const
  150. -> CPP_ret(remove_if_view<all_t<Rng>, Pred>)( //
  151. requires viewable_range<Rng> && input_range<Rng> &&
  152. indirect_unary_predicate<Pred, iterator_t<Rng>>)
  153. {
  154. return remove_if_view<all_t<Rng>, Pred>{all(static_cast<Rng &&>(rng)),
  155. std::move(pred)};
  156. }
  157. template<typename Rng, typename Pred, typename Proj>
  158. constexpr auto operator()(Rng && rng, Pred pred, Proj proj) const
  159. -> CPP_ret(remove_if_view<all_t<Rng>, composed<Pred, Proj>>)( //
  160. requires viewable_range<Rng> && input_range<Rng> &&
  161. indirect_unary_predicate<Pred, projected<iterator_t<Rng>, Proj>>)
  162. {
  163. return remove_if_view<all_t<Rng>, composed<Pred, Proj>>{
  164. all(static_cast<Rng &&>(rng)),
  165. compose(std::move(pred), std::move(proj))};
  166. }
  167. };
  168. /// \relates remove_if_fn
  169. /// \ingroup group-views
  170. RANGES_INLINE_VARIABLE(view<remove_if_fn>, remove_if)
  171. } // namespace views
  172. /// @}
  173. } // namespace ranges
  174. RANGES_RE_ENABLE_WARNINGS
  175. #include <range/v3/detail/satisfy_boost_range.hpp>
  176. RANGES_SATISFY_BOOST_RANGE(::ranges::remove_if_view)
  177. #endif