tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
transform_reduce.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3#ifndef TETL_NUMERIC_TRANSFORM_REDUCE_HPP
4#define TETL_NUMERIC_TRANSFORM_REDUCE_HPP
5
6#include <etl/_functional/multiplies.hpp>
7#include <etl/_functional/plus.hpp>
8
9namespace etl {
10
11/// https://en.cppreference.com/w/cpp/algorithm/transform_reduce
12/// \ingroup numeric
13template <typename InputIt1, typename InputIt2, typename T, typename BinaryReductionOp, typename BinaryTransformOp>
14[[nodiscard]] constexpr auto transform_reduce(
15 InputIt1 first1,
16 InputIt1 last1,
17 InputIt2 first2,
18 T init,
19 BinaryReductionOp reduce,
20 BinaryTransformOp transform
21) -> T
22{
23 for (; first1 != last1; ++first1, (void)++first2) {
24 init = reduce(init, transform(*first1, *first2));
25 }
26 return init;
27}
28
29/// https://en.cppreference.com/w/cpp/algorithm/transform_reduce
30/// \ingroup numeric
31template <typename InputIt1, typename InputIt2, typename T>
32[[nodiscard]] constexpr auto transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init) -> T
33{
34 return etl::transform_reduce(first1, last1, first2, init, etl::plus(), etl::multiplies());
35}
36
37/// https://en.cppreference.com/w/cpp/algorithm/transform_reduce
38/// \ingroup numeric
39template <typename InputIt, typename T, typename BinaryReductionOp, typename UnaryTransformOp>
40[[nodiscard]] constexpr auto
41transform_reduce(InputIt first, InputIt last, T init, BinaryReductionOp reduce, UnaryTransformOp transform) -> T
42{
43 for (; first != last; ++first) {
44 init = reduce(init, transform(*first));
45 }
46 return init;
47}
48
49} // namespace etl
50
51#endif // TETL_NUMERIC_TRANSFORM_REDUCE_HPP
constexpr auto transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryReductionOp reduce, BinaryTransformOp transform) -> T
https://en.cppreference.com/w/cpp/algorithm/transform_reduce
Definition transform_reduce.hpp:14
constexpr auto transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init) -> T
https://en.cppreference.com/w/cpp/algorithm/transform_reduce
Definition transform_reduce.hpp:32
constexpr auto transform_reduce(InputIt first, InputIt last, T init, BinaryReductionOp reduce, UnaryTransformOp transform) -> T
https://en.cppreference.com/w/cpp/algorithm/transform_reduce
Definition transform_reduce.hpp:41
Definition adjacent_find.hpp:9
Function object for performing multiplication. Effectively calls operator* on two instances of type T...
Definition multiplies.hpp:15
Function object for performing addition. Effectively calls operator+ on two instances of type T....
Definition plus.hpp:15