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.

191 lines
5.9KB

  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_TAKE_EXACTLY_HPP
  14. #define RANGES_V3_VIEW_TAKE_EXACTLY_HPP
  15. #include <type_traits>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/functional/bind_back.hpp>
  18. #include <range/v3/iterator/counted_iterator.hpp>
  19. #include <range/v3/iterator/default_sentinel.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/static_const.hpp>
  25. #include <range/v3/view/all.hpp>
  26. #include <range/v3/view/counted.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. /// \cond
  33. namespace detail
  34. {
  35. template<typename Rng>
  36. struct is_random_access_common_
  37. : meta::bool_<(bool)random_access_range<Rng> && (bool)common_range<Rng>>
  38. {};
  39. // BUGBUG Per the discussion in https://github.com/ericniebler/stl2/issues/63,
  40. // it's unclear if we can infer anything from random_access_range<Rng> &&
  41. // common_range<Rng>
  42. template<typename Rng,
  43. bool IsRandomAccessCommon /*= is_random_access_common_<Rng>::value*/>
  44. struct take_exactly_view_
  45. : view_interface<take_exactly_view_<Rng, IsRandomAccessCommon>, finite>
  46. {
  47. private:
  48. Rng rng_;
  49. range_difference_t<Rng> n_;
  50. public:
  51. take_exactly_view_() = default;
  52. take_exactly_view_(Rng rng, range_difference_t<Rng> n)
  53. : rng_(std::move(rng))
  54. , n_(n)
  55. {
  56. RANGES_EXPECT(n >= 0);
  57. }
  58. counted_iterator<iterator_t<Rng>> begin()
  59. {
  60. return {ranges::begin(rng_), n_};
  61. }
  62. template<typename BaseRng = Rng>
  63. auto begin() const -> CPP_ret(counted_iterator<iterator_t<BaseRng const>>)( //
  64. requires range<BaseRng const>)
  65. {
  66. return {ranges::begin(rng_), n_};
  67. }
  68. default_sentinel_t end() const
  69. {
  70. return {};
  71. }
  72. auto size() const
  73. {
  74. return static_cast<detail::iter_size_t<iterator_t<Rng>>>(n_);
  75. }
  76. Rng base() const
  77. {
  78. return rng_;
  79. }
  80. };
  81. template<typename Rng>
  82. struct take_exactly_view_<Rng, true>
  83. : view_interface<take_exactly_view_<Rng, true>, finite>
  84. {
  85. private:
  86. Rng rng_;
  87. range_difference_t<Rng> n_;
  88. public:
  89. take_exactly_view_() = default;
  90. take_exactly_view_(Rng rng, range_difference_t<Rng> n)
  91. : rng_(std::move(rng))
  92. , n_(n)
  93. {
  94. RANGES_EXPECT(n >= 0);
  95. RANGES_EXPECT(!(bool)sized_range<Rng> || n <= ranges::distance(rng_));
  96. }
  97. iterator_t<Rng> begin()
  98. {
  99. return ranges::begin(rng_);
  100. }
  101. iterator_t<Rng> end()
  102. {
  103. return ranges::begin(rng_) + n_;
  104. }
  105. CPP_member
  106. auto CPP_fun(begin)()(const requires range<Rng const>)
  107. {
  108. return ranges::begin(rng_);
  109. }
  110. CPP_member
  111. auto CPP_fun(end)()(const requires range<Rng const>)
  112. {
  113. return ranges::begin(rng_) + n_;
  114. }
  115. detail::iter_size_t<iterator_t<Rng>> size() const
  116. {
  117. return static_cast<detail::iter_size_t<iterator_t<Rng>>>(n_);
  118. }
  119. Rng base() const
  120. {
  121. return rng_;
  122. }
  123. };
  124. } // namespace detail
  125. /// \endcond
  126. /// \addtogroup group-views
  127. /// @{
  128. template<typename Rng>
  129. using take_exactly_view = detail::take_exactly_view_<Rng>;
  130. namespace views
  131. {
  132. struct take_exactly_fn
  133. {
  134. private:
  135. friend view_access;
  136. template<typename Int>
  137. static constexpr auto CPP_fun(bind)(take_exactly_fn take_exactly, Int n)( //
  138. requires integral<Int>)
  139. {
  140. return make_pipeable(bind_back(take_exactly, n));
  141. }
  142. template<typename Rng>
  143. static constexpr take_exactly_view<all_t<Rng>> impl_(
  144. Rng && rng, range_difference_t<Rng> n, input_range_tag)
  145. {
  146. return {all(static_cast<Rng &&>(rng)), n};
  147. }
  148. template<typename Rng>
  149. static constexpr auto impl_(Rng && rng, range_difference_t<Rng> n,
  150. random_access_range_tag)
  151. -> CPP_ret(subrange<iterator_t<Rng>>)( //
  152. requires forwarding_range_<Rng>)
  153. {
  154. return {begin(rng), next(begin(rng), n)};
  155. }
  156. public:
  157. template<typename Rng>
  158. constexpr auto CPP_fun(operator())(Rng && rng, range_difference_t<Rng> n)(
  159. const requires viewable_range<Rng> && input_range<Rng>)
  160. {
  161. return take_exactly_fn::impl_(
  162. static_cast<Rng &&>(rng), n, range_tag_of<Rng>{});
  163. }
  164. };
  165. /// \relates take_exactly_fn
  166. /// \ingroup group-views
  167. RANGES_INLINE_VARIABLE(view<take_exactly_fn>, take_exactly)
  168. } // namespace views
  169. /// @}
  170. } // namespace ranges
  171. #include <range/v3/detail/satisfy_boost_range.hpp>
  172. RANGES_SATISFY_BOOST_RANGE(::ranges::detail::take_exactly_view_)
  173. #endif