// SPDX-License-Identifier: BSD-2-Clause // author: Max Kellermann #ifndef TRANSFORM_N_HXX #define TRANSFORM_N_HXX #include /** * The std::transform_n() function that is missing in the C++ standard * library. */ template OutputIt transform_n(InputIt input, size_t n, OutputIt output, UnaryOperation unary_op) { while (n-- > 0) *output++ = unary_op(*input++); return output; } /** * Optimized overload for the above transform_n() implementation for * the special case that both input and output are regular pointers. * Turns out that most compilers generate better code this way. */ template OutputType * transform_n(const InputType *input, size_t n, OutputType *output, UnaryOperation unary_op) { for (size_t i = 0; i < n; ++i) output[i] = unary_op(input[i]); return output + n; } #endif