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.

189 lines
6.1KB

  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_HPP
  14. #define RANGES_V3_VIEW_DROP_HPP
  15. #include <type_traits>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/algorithm/min.hpp>
  19. #include <range/v3/functional/bind_back.hpp>
  20. #include <range/v3/iterator/operations.hpp>
  21. #include <range/v3/iterator/traits.hpp>
  22. #include <range/v3/range/concepts.hpp>
  23. #include <range/v3/range/traits.hpp>
  24. #include <range/v3/utility/box.hpp>
  25. #include <range/v3/utility/optional.hpp>
  26. #include <range/v3/utility/static_const.hpp>
  27. #include <range/v3/view/all.hpp>
  28. #include <range/v3/view/interface.hpp>
  29. #include <range/v3/view/subrange.hpp>
  30. #include <range/v3/view/view.hpp>
  31. namespace ranges
  32. {
  33. /// \addtogroup group-views
  34. /// @{
  35. template<typename Rng>
  36. struct RANGES_EMPTY_BASES drop_view
  37. : view_interface<drop_view<Rng>,
  38. is_finite<Rng>::value ? finite : range_cardinality<Rng>::value>
  39. , private detail::non_propagating_cache<iterator_t<Rng>, drop_view<Rng>,
  40. !random_access_range<Rng>>
  41. {
  42. private:
  43. using difference_type_ = range_difference_t<Rng>;
  44. Rng rng_;
  45. difference_type_ n_;
  46. template<bool Const = true>
  47. auto get_begin_(std::true_type, std::true_type) const
  48. -> CPP_ret(iterator_t<meta::const_if_c<Const, Rng>>)( //
  49. requires Const && range<meta::const_if_c<Const, Rng>>)
  50. {
  51. CPP_assert(random_access_range<meta::const_if_c<Const, Rng>>);
  52. return next(ranges::begin(rng_), n_, ranges::end(rng_));
  53. }
  54. iterator_t<Rng> get_begin_(std::true_type, std::false_type)
  55. {
  56. CPP_assert(random_access_range<Rng>);
  57. return next(ranges::begin(rng_), n_, ranges::end(rng_));
  58. }
  59. iterator_t<Rng> get_begin_(std::false_type, detail::ignore_t)
  60. {
  61. CPP_assert(!random_access_range<Rng>);
  62. using cache_t =
  63. detail::non_propagating_cache<iterator_t<Rng>, drop_view<Rng>>;
  64. auto & begin_ = static_cast<cache_t &>(*this);
  65. if(!begin_)
  66. begin_ = next(ranges::begin(rng_), n_, ranges::end(rng_));
  67. return *begin_;
  68. }
  69. public:
  70. drop_view() = default;
  71. drop_view(Rng rng, difference_type_ n)
  72. : rng_(std::move(rng))
  73. , n_(n)
  74. {
  75. RANGES_EXPECT(n >= 0);
  76. }
  77. iterator_t<Rng> begin()
  78. {
  79. return this->get_begin_(meta::bool_<random_access_range<Rng>>{},
  80. std::false_type{});
  81. }
  82. sentinel_t<Rng> end()
  83. {
  84. return ranges::end(rng_);
  85. }
  86. template<bool Const = true>
  87. auto begin() const -> CPP_ret(iterator_t<meta::const_if_c<Const, Rng>>)( //
  88. requires Const && random_access_range<meta::const_if_c<Const, Rng>>)
  89. {
  90. return this->get_begin_(std::true_type{}, std::true_type{});
  91. }
  92. template<bool Const = true>
  93. auto end() const -> CPP_ret(sentinel_t<meta::const_if_c<Const, Rng>>)( //
  94. requires Const && random_access_range<meta::const_if_c<Const, Rng>>)
  95. {
  96. return ranges::end(rng_);
  97. }
  98. CPP_member
  99. auto CPP_fun(size)()(const requires sized_range<Rng const>)
  100. {
  101. auto const s = ranges::size(rng_);
  102. auto const n = static_cast<range_size_t<Rng const>>(n_);
  103. return s < n ? 0 : s - n;
  104. }
  105. CPP_member
  106. auto CPP_fun(size)()(requires sized_range<Rng>)
  107. {
  108. auto const s = ranges::size(rng_);
  109. auto const n = static_cast<range_size_t<Rng>>(n_);
  110. return s < n ? 0 : s - n;
  111. }
  112. Rng base() const
  113. {
  114. return rng_;
  115. }
  116. };
  117. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  118. template<typename Rng>
  119. drop_view(Rng &&, range_difference_t<Rng>)->drop_view<views::all_t<Rng>>;
  120. #endif
  121. namespace views
  122. {
  123. struct drop_fn
  124. {
  125. private:
  126. friend view_access;
  127. template<typename Int>
  128. static constexpr auto CPP_fun(bind)(drop_fn drop, Int n)( //
  129. requires integral<Int>)
  130. {
  131. return make_pipeable(bind_back(drop, n));
  132. }
  133. template<typename Rng>
  134. static auto impl_(Rng && rng, range_difference_t<Rng> n, input_range_tag)
  135. -> drop_view<all_t<Rng>>
  136. {
  137. return {all(static_cast<Rng &&>(rng)), n};
  138. }
  139. template<typename Rng>
  140. static auto impl_(Rng && rng, range_difference_t<Rng> n,
  141. random_access_range_tag)
  142. -> CPP_ret(subrange<iterator_t<Rng>, sentinel_t<Rng>>)( //
  143. requires forwarding_range_<Rng> && sized_range<Rng>)
  144. {
  145. return {begin(rng) + ranges::min(n, distance(rng)), end(rng)};
  146. }
  147. public:
  148. template<typename Rng>
  149. auto CPP_fun(operator())(Rng && rng, range_difference_t<Rng> n)(
  150. const requires viewable_range<Rng> && input_range<Rng>)
  151. {
  152. return drop_fn::impl_(static_cast<Rng &&>(rng), n, range_tag_of<Rng>{});
  153. }
  154. };
  155. /// \relates drop_fn
  156. /// \ingroup group-views
  157. RANGES_INLINE_VARIABLE(view<drop_fn>, drop)
  158. } // namespace views
  159. namespace cpp20
  160. {
  161. namespace views
  162. {
  163. using ranges::views::drop;
  164. }
  165. CPP_template(typename Rng)( //
  166. requires view_<Rng>) //
  167. using drop_view = ranges::drop_view<Rng>;
  168. } // namespace cpp20
  169. /// @}
  170. } // namespace ranges
  171. #include <range/v3/detail/satisfy_boost_range.hpp>
  172. RANGES_SATISFY_BOOST_RANGE(::ranges::drop_view)
  173. #endif