tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
partial_sum.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3#ifndef TETL_NUMERIC_PARTIAL_SUM_HPP
4#define TETL_NUMERIC_PARTIAL_SUM_HPP
5
6#include <etl/_functional/plus.hpp>
7#include <etl/_utility/move.hpp>
8
9namespace etl {
10
11/// \brief Computes the partial sums of the elements in the subranges of the
12/// range [first, last) and writes them to the range beginning at destination.
13/// This version uses the given binary function op, both applying etl::move to
14/// their operands on the left hand side.
15///
16/// \details BinaryFunction must not invalidate any iterators, including the end
17/// iterators, or modify any elements of the range involved.
18///
19/// https://en.cppreference.com/w/cpp/algorithm/partial_sum
20///
21/// \returns Iterator to the element past the last element written.
22///
23/// \ingroup numeric
24template <typename InputIt, typename OutputIt, typename BinaryOperation>
25constexpr auto partial_sum(InputIt first, InputIt last, OutputIt destination, BinaryOperation op) -> OutputIt
26{
27 if (first == last) {
28 return destination;
29 }
30
31 auto sum = *first;
32 *destination = sum;
33
34 while (++first != last) {
35 sum = op(etl::move(sum), *first);
36 *++destination = sum;
37 }
38
39 return ++destination;
40}
41
42/// \ingroup numeric
43template <typename InputIt, typename OutputIt>
44constexpr auto partial_sum(InputIt first, InputIt last, OutputIt destination) -> OutputIt
45{
46 return etl::partial_sum(first, last, destination, etl::plus<>());
47}
48
49} // namespace etl
50
51#endif // TETL_NUMERIC_PARTIAL_SUM_HPP
constexpr auto partial_sum(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Definition partial_sum.hpp:44
constexpr auto partial_sum(InputIt first, InputIt last, OutputIt destination, BinaryOperation op) -> OutputIt
Computes the partial sums of the elements in the subranges of the range [first, last) and writes them...
Definition partial_sum.hpp:25
Definition adjacent_find.hpp:9
Function object for performing addition. Effectively calls operator+ on two instances of type T....
Definition plus.hpp:15