Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

adjacent_remove_if.hpp 5.5KB

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