Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 2.01 KB

File metadata and controls

48 lines (37 loc) · 2.01 KB

numeric.hpp

numeric.hpp provides an implementation of some numeric algorithms.

transform_reduce and transform_reduce_n

stdx::transform_reduce is similar to std::transform_reduce, but variadic in its inputs.

template <typename T, typename InputIt, typename ROp, typename TOp,
          typename... InputItN>
constexpr auto transform_reduce(InputIt first, InputIt last, T init,
                                ROp rop, TOp top, InputItN... first_n) -> T;

stdx::transform_reduce is to std::transform_reduce as stdx::transform is to std::transform.

Note
The return type of stdx::transform_reduce is the first template parameter, which allows it to be manifestly fixed more conveniently to avoid a common pitfall with accumulation algorithms, viz. summing a range of double​s and passing 0 - an int - as the initial value.
Caution
Unlike stdx::transform, the return type here does not include the iterators: this means stdx::transform_reduce violates the Law of Useful Return (just like std::transform_reduce). However, it is probably true that stdx::transform_reduce is mostly used just for the accumulation result, where stdx::transform writes to an output iterator.
Note
Like stdx::transform, stdx::transform_reduce is constexpr in C++20 and later, because it uses std::invoke.

stdx::transform_reduce_n is just like stdx::transform_reduce, but instead of taking two iterators to delimit the input range, it takes an iterator and size.

template <typename T, typename InputIt, typename Size, typename ROp,
          typename TOp, typename... InputItN>
constexpr auto transform_reduce_n(InputIt first, Size n, T init, ROp rop,
                                  TOp top, InputItN... first_n) -> T;