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#ifndef TETL_NUMERIC_TRANSFORM_REDUCE_HPP
3#define TETL_NUMERIC_TRANSFORM_REDUCE_HPP
4
7
8namespace etl {
9
12template <typename InputIt1, typename InputIt2, typename T, typename BinaryReductionOp, typename BinaryTransformOp>
13[[nodiscard]] constexpr auto transform_reduce(
14 InputIt1 first1,
15 InputIt1 last1,
16 InputIt2 first2,
17 T init,
18 BinaryReductionOp reduce,
19 BinaryTransformOp transform
20) -> T
21{
22 for (; first1 != last1; ++first1, (void)++first2) {
23 init = reduce(init, transform(*first1, *first2));
24 }
25 return init;
26}
27
30template <typename InputIt1, typename InputIt2, typename T>
31[[nodiscard]] constexpr auto transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init) -> T
32{
33 return etl::transform_reduce(first1, last1, first2, init, etl::plus(), etl::multiplies());
34}
35
38template <typename InputIt, typename T, typename BinaryReductionOp, typename UnaryTransformOp>
39[[nodiscard]] constexpr auto
40transform_reduce(InputIt first, InputIt last, T init, BinaryReductionOp reduce, UnaryTransformOp transform) -> T
41{
42 for (; first != last; ++first) {
43 init = reduce(init, transform(*first));
44 }
45 return init;
46}
47
48} // namespace etl
49
50#endif // TETL_NUMERIC_TRANSFORM_REDUCE_HPP
constexpr auto transform(InputIt first, InputIt last, OutputIt dest, UnaryOp op) -> OutputIt
Applies the given function to a range and stores the result in another range, beginning at dest....
Definition transform.hpp:24
constexpr auto reduce(InputIter first, InputIter last, T init, BinaryOp op) -> T
Similar to etl::accumulate.
Definition reduce.hpp:16
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:13
Definition adjacent_find.hpp:8
Function object for performing multiplication. Effectively calls operator* on two instances of type T...
Definition multiplies.hpp:14
Function object for performing addition. Effectively calls operator+ on two instances of type T....
Definition plus.hpp:14