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.

65 lines
2.2KB

  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_NUMERIC_ACCUMULATE_HPP
  14. #define RANGES_V3_NUMERIC_ACCUMULATE_HPP
  15. #include <meta/meta.hpp>
  16. #include <range/v3/functional/arithmetic.hpp>
  17. #include <range/v3/functional/identity.hpp>
  18. #include <range/v3/functional/invoke.hpp>
  19. #include <range/v3/iterator/concepts.hpp>
  20. #include <range/v3/iterator/traits.hpp>
  21. #include <range/v3/range/access.hpp>
  22. #include <range/v3/range/concepts.hpp>
  23. #include <range/v3/range/traits.hpp>
  24. #include <range/v3/utility/static_const.hpp>
  25. namespace ranges
  26. {
  27. /// \addtogroup group-numerics
  28. /// @{
  29. struct accumulate_fn
  30. {
  31. template<typename I, typename S, typename T, typename Op = plus,
  32. typename P = identity>
  33. auto operator()(I first, S last, T init, Op op = Op{},
  34. P proj = P{}) const -> CPP_ret(T)( //
  35. requires sentinel_for<S, I> && input_iterator<I> &&
  36. indirectly_binary_invocable_<Op, T *, projected<I, P>> &&
  37. assignable_from<T &, indirect_result_t<Op &, T *, projected<I, P>>>)
  38. {
  39. for(; first != last; ++first)
  40. init = invoke(op, init, invoke(proj, *first));
  41. return init;
  42. }
  43. template<typename Rng, typename T, typename Op = plus, typename P = identity>
  44. auto operator()(Rng && rng, T init, Op op = Op{},
  45. P proj = P{}) const -> CPP_ret(T)( //
  46. requires input_range<Rng> &&
  47. indirectly_binary_invocable_<Op, T *, projected<iterator_t<Rng>, P>> &&
  48. assignable_from<
  49. T &, indirect_result_t<Op &, T *, projected<iterator_t<Rng>, P>>>)
  50. {
  51. return (*this)(
  52. begin(rng), end(rng), std::move(init), std::move(op), std::move(proj));
  53. }
  54. };
  55. RANGES_INLINE_VARIABLE(accumulate_fn, accumulate)
  56. /// @}
  57. } // namespace ranges
  58. #endif