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.

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