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.

partial_sum.hpp 7.0KB

5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_PARTIAL_SUM_HPP
  14. #define RANGES_V3_VIEW_PARTIAL_SUM_HPP
  15. #include <iterator>
  16. #include <type_traits>
  17. #include <utility>
  18. #include <meta/meta.hpp>
  19. #include <range/v3/range_fwd.hpp>
  20. #include <range/v3/functional/arithmetic.hpp>
  21. #include <range/v3/functional/bind_back.hpp>
  22. #include <range/v3/functional/invoke.hpp>
  23. #include <range/v3/range/access.hpp>
  24. #include <range/v3/range/primitives.hpp>
  25. #include <range/v3/range/traits.hpp>
  26. #include <range/v3/utility/addressof.hpp>
  27. #include <range/v3/utility/semiregular_box.hpp>
  28. #include <range/v3/utility/static_const.hpp>
  29. #include <range/v3/view/all.hpp>
  30. #include <range/v3/view/facade.hpp>
  31. #include <range/v3/view/view.hpp>
  32. namespace ranges
  33. {
  34. /// \cond
  35. namespace detail
  36. {
  37. // clang-format off
  38. CPP_def
  39. (
  40. template(typename Rng, typename Fun)
  41. concept partial_sum_view_constraints,
  42. input_range<Rng> &&
  43. constructible_from<range_value_t<Rng>, range_reference_t<Rng>> &&
  44. assignable_from<range_value_t<Rng> &, range_reference_t<Rng>> &&
  45. indirectly_binary_invocable_<Fun &, iterator_t<Rng>, iterator_t<Rng>> &&
  46. assignable_from<
  47. range_value_t<Rng> &,
  48. indirect_result_t<Fun &, iterator_t<Rng>, iterator_t<Rng>>>
  49. );
  50. // clang-format on
  51. } // namespace detail
  52. /// \endcond
  53. /// \addtogroup group-views
  54. /// @{
  55. template<typename Rng, typename Fun>
  56. struct partial_sum_view
  57. : view_facade<partial_sum_view<Rng, Fun>, range_cardinality<Rng>::value>
  58. {
  59. private:
  60. friend range_access;
  61. CPP_assert(view_<Rng>);
  62. CPP_assert(detail::partial_sum_view_constraints<Rng, Fun>);
  63. RANGES_NO_UNIQUE_ADDRESS Rng base_{};
  64. RANGES_NO_UNIQUE_ADDRESS semiregular_box_t<Fun> fun_;
  65. template<bool IsConst>
  66. struct cursor
  67. {
  68. private:
  69. friend cursor<true>;
  70. using Parent = meta::const_if_c<IsConst, partial_sum_view>;
  71. using Base = meta::const_if_c<IsConst, Rng>;
  72. Parent * parent_ = nullptr;
  73. RANGES_NO_UNIQUE_ADDRESS iterator_t<Base> current_{};
  74. RANGES_NO_UNIQUE_ADDRESS semiregular_box_t<range_value_t<Rng>> sum_;
  75. public:
  76. using single_pass = meta::bool_<single_pass_iterator_<iterator_t<Base>>>;
  77. cursor() = default;
  78. constexpr explicit cursor(Parent * rng)
  79. : parent_{rng}
  80. , current_(ranges::begin(rng->base_))
  81. {
  82. if(current_ != ranges::end(rng->base_))
  83. sum_ = *current_;
  84. }
  85. CPP_template(bool Other)( //
  86. requires IsConst && (!Other) &&
  87. convertible_to<iterator_t<Rng> const &,
  88. iterator_t<Base>>) //
  89. constexpr cursor(cursor<Other> const & that)
  90. : parent_{that.parent_}
  91. , current_(that.current_)
  92. , sum_(that.sum_)
  93. {}
  94. constexpr range_value_t<Rng> read() const
  95. {
  96. RANGES_EXPECT(current_ != ranges::end(parent_->base_));
  97. return sum_;
  98. }
  99. constexpr void next()
  100. {
  101. auto last = ranges::end(parent_->base_);
  102. RANGES_EXPECT(current_ != last);
  103. if(++current_ != last)
  104. {
  105. auto & sum = static_cast<range_value_t<Rng> &>(sum_);
  106. using F = meta::const_if_c<IsConst, Fun>;
  107. auto & f = static_cast<F &>(parent_->fun_);
  108. sum = invoke(f, sum, *current_);
  109. }
  110. }
  111. constexpr bool equal(default_sentinel_t) const
  112. {
  113. return current_ == ranges::end(parent_->base_);
  114. }
  115. CPP_member
  116. constexpr bool CPP_fun(equal)(cursor const & that)(
  117. const requires equality_comparable<iterator_t<Base>>)
  118. {
  119. RANGES_EXPECT(parent_ == that.parent_);
  120. return current_ == that.current_;
  121. }
  122. };
  123. constexpr cursor<false> begin_cursor()
  124. {
  125. return cursor<false>{this};
  126. }
  127. template<typename CRng = Rng const>
  128. constexpr auto begin_cursor() const -> CPP_ret(cursor<true>)( //
  129. requires detail::partial_sum_view_constraints<CRng, Fun const>)
  130. {
  131. return cursor<true>{this};
  132. }
  133. public:
  134. partial_sum_view() = default;
  135. constexpr partial_sum_view(Rng rng, Fun fun) noexcept(
  136. std::is_nothrow_move_constructible<Rng>::value &&
  137. std::is_nothrow_move_constructible<Fun>::value)
  138. : base_(std::move(rng))
  139. , fun_(std::move(fun))
  140. {}
  141. CPP_member
  142. constexpr auto CPP_fun(size)()(requires sized_range<Rng>)
  143. {
  144. return ranges::size(base_);
  145. }
  146. CPP_member
  147. constexpr auto CPP_fun(size)()(const requires sized_range<Rng const>)
  148. {
  149. return ranges::size(base_);
  150. }
  151. };
  152. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  153. CPP_template(typename Rng, typename Fun)(requires copy_constructible<Fun>)
  154. partial_sum_view(Rng &&, Fun)
  155. ->partial_sum_view<views::all_t<Rng>, Fun>;
  156. #endif
  157. namespace views
  158. {
  159. struct partial_sum_fn
  160. {
  161. private:
  162. friend view_access;
  163. template<typename Fun>
  164. static constexpr auto bind(partial_sum_fn partial_sum, Fun fun)
  165. {
  166. return make_pipeable(bind_back(partial_sum, std::move(fun)));
  167. }
  168. template<typename Fun = plus>
  169. RANGES_DEPRECATED(
  170. "Use \"ranges::views::partial_sum\" instead of "
  171. "\"ranges::views::partial_sum()\".")
  172. static constexpr auto bind(partial_sum_fn partial_sum)
  173. {
  174. return make_pipeable(bind_back(partial_sum, Fun{}));
  175. }
  176. public:
  177. template<typename Rng, typename Fun = plus>
  178. constexpr auto operator()(Rng && rng, Fun fun = {}) const
  179. -> CPP_ret(partial_sum_view<all_t<Rng>, Fun>)( //
  180. requires detail::partial_sum_view_constraints<all_t<Rng>, Fun>)
  181. {
  182. return {all(static_cast<Rng &&>(rng)), std::move(fun)};
  183. }
  184. };
  185. /// \relates partial_sum_fn
  186. /// \ingroup group-views
  187. RANGES_INLINE_VARIABLE(view<partial_sum_fn>, partial_sum)
  188. } // namespace views
  189. /// @}
  190. } // namespace ranges
  191. #include <range/v3/detail/satisfy_boost_range.hpp>
  192. RANGES_SATISFY_BOOST_RANGE(::ranges::partial_sum_view)
  193. #endif