tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
accumulate.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_ACCUMULATE_HPP
4#define TETL_NUMERIC_ACCUMULATE_HPP
5
6#include <etl/_utility/move.hpp>
7
8namespace etl {
9
10/// \brief Computes the sum of the given value init and the elements in the range `[first, last)`.
11/// \details https://en.cppreference.com/w/cpp/algorithm/accumulate
12/// \ingroup numeric
13template <typename InputIt, typename Type>
14[[nodiscard]] constexpr auto accumulate(InputIt first, InputIt last, Type init) noexcept -> Type
15{
16 for (; first != last; ++first) {
17 init = etl::move(init) + *first;
18 }
19 return init;
20}
21
22/// \brief Computes the sum of the given value init and the elements in the range `[first, last)`.
23/// \details https://en.cppreference.com/w/cpp/algorithm/accumulate
24/// \ingroup numeric
25template <typename InputIt, typename Type, typename BinaryOperation>
26[[nodiscard]] constexpr auto accumulate(InputIt first, InputIt last, Type init, BinaryOperation op) noexcept -> Type
27{
28 for (; first != last; ++first) {
29 init = op(etl::move(init), *first);
30 }
31 return init;
32}
33
34} // namespace etl
35
36#endif // TETL_NUMERIC_ACCUMULATE_HPP
constexpr auto accumulate(InputIt first, InputIt last, Type init, BinaryOperation op) noexcept -> Type
Computes the sum of the given value init and the elements in the range [first, last).
Definition accumulate.hpp:26
constexpr auto accumulate(InputIt first, InputIt last, Type init) noexcept -> Type
Computes the sum of the given value init and the elements in the range [first, last).
Definition accumulate.hpp:14
Definition adjacent_find.hpp:9