From fddc3480a9c6e7c2cd8c1cc2edee9ab7f8b74642 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 21 Oct 2022 23:11:25 +0200 Subject: [PATCH] feat: `copy` algorithm added --- src/core/include/units/bits/algorithm.h | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/core/include/units/bits/algorithm.h b/src/core/include/units/bits/algorithm.h index 1ac80362..ca3877e4 100644 --- a/src/core/include/units/bits/algorithm.h +++ b/src/core/include/units/bits/algorithm.h @@ -25,6 +25,7 @@ #include // IWYU pragma: keep #include #include +#include namespace units::detail { @@ -97,4 +98,45 @@ constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2) return ::units::detail::lexicographical_compare_three_way(f1, l1, f2, l2, std::compare_three_way()); } +template +struct in_out_result { + [[no_unique_address]] I in; + [[no_unique_address]] O out; + + template + requires std::convertible_to && std::convertible_to + constexpr operator in_out_result() const& + { + return {in, out}; + } + + template + requires std::convertible_to && std::convertible_to + constexpr operator in_out_result() && + { + return {std::move(in), std::move(out)}; + } +}; + +template +using copy_result = in_out_result; + +template S, std::weakly_incrementable O> + requires std::indirectly_copyable +constexpr copy_result copy(I first, S last, O result) +{ + for (; first != last; ++first, (void)++result) { + *result = *first; + } + return {std::move(first), std::move(result)}; +} + +template + requires std::indirectly_copyable, O> +constexpr copy_result, O> copy(R&& r, O result) +{ + return ::units::detail::copy(std::ranges::begin(r), std::ranges::end(r), std::move(result)); +} + + } // namespace units::detail