tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
reduce.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_REDUCE_HPP
4#define TETL_NUMERIC_REDUCE_HPP
5
6#include <etl/_functional/plus.hpp>
7#include <etl/_iterator/iterator_traits.hpp>
8#include <etl/_numeric/accumulate.hpp>
9#include <etl/_utility/move.hpp>
10
11namespace etl {
12
13/// \brief Similar to etl::accumulate.
14/// \details https://en.cppreference.com/w/cpp/algorithm/reduce
15/// \ingroup numeric
16template <typename InputIter, typename T, typename BinaryOp>
17[[nodiscard]] constexpr auto reduce(InputIter first, InputIter last, T init, BinaryOp op) -> T
18{
19 return accumulate(first, last, init, op);
20}
21
22/// \brief Similar to etl::accumulate.
23/// \details https://en.cppreference.com/w/cpp/algorithm/reduce
24/// \ingroup numeric
25template <typename InputIter, typename T>
26[[nodiscard]] constexpr auto reduce(InputIter first, InputIter last, T init) -> T
27{
28 return reduce(first, last, init, etl::plus<>());
29}
30
31/// \brief Similar to etl::accumulate.
32/// \details https://en.cppreference.com/w/cpp/algorithm/reduce
33/// \ingroup numeric
34template <typename InputIter>
35[[nodiscard]] constexpr auto reduce(InputIter first, InputIter last) ->
36 typename etl::iterator_traits<InputIter>::value_type
37{
38 auto init = typename etl::iterator_traits<InputIter>::value_type{};
39 return reduce(first, last, init);
40}
41
42} // namespace etl
43
44#endif // TETL_NUMERIC_REDUCE_HPP
constexpr auto reduce(InputIter first, InputIter last, T init) -> T
Similar to etl::accumulate.
Definition reduce.hpp:26
constexpr auto reduce(InputIter first, InputIter last) -> typename etl::iterator_traits< InputIter >::value_type
Similar to etl::accumulate.
Definition reduce.hpp:35
constexpr auto reduce(InputIter first, InputIter last, T init, BinaryOp op) -> T
Similar to etl::accumulate.
Definition reduce.hpp:17
Definition adjacent_find.hpp:9
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:48
Function object for performing addition. Effectively calls operator+ on two instances of type T....
Definition plus.hpp:15