Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 2.6 KB

File metadata and controls

71 lines (54 loc) · 2.6 KB

algorithm.hpp

algorithm.hpp provides an implementation of some algorithms.

for_each and for_each_n

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

template <typename InputIt, typename Operation, typename... InputItN>
constexpr auto for_each(InputIt first, InputIt last,
                        Operation op, InputItN... first_n) -> Operation;
Note
stdx::for_each is constexpr in C++20 and later, because it uses std::invoke.

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

template <typename InputIt, typename Size, typename Operation,
          typename... InputItN>
constexpr auto for_each_n(InputIt first, Size n,
                          Operation op, InputItN... first_n) -> Operation;

transform and transform_n

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

template <typename InputIt, typename OutputIt, typename Operation, typename... InputItN>
constexpr auto transform(InputIt first, InputIt last, OutputIt d_first,
                         Operation op, InputItN... first_n)
    -> transform_result<OutputIt, InputIt, InputItN...>;

Without the variadic pack InputItN…​ here, a call site looks like regular unary std::transform. But with the addition of the pack, stdx::transform becomes more like std::ranges::views::zip_transform. It can transform using an n-ary function and n sequences.

Note
The return value is equivalent to a tuple<OutputIt, InputIt, InputItN…​>. In C++20 and later this is a stdx::tuple, in C++17 a std::tuple.
Note
stdx::transform is constexpr in C++20 and later, because it uses std::invoke.

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

template <typename InputIt, typename Size, typename OutputIt, typename Operation,
          typename... InputItN>
constexpr auto transform(InputIt first, Size n, OutputIt d_first,
                         Operation op, InputItN... first_n)
    -> transform_result<OutputIt, InputIt, InputItN...>;