From 33cb83b585e618a9c7d7e643d5ee6ee57626c4dc Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Mon, 9 Sep 2024 11:17:59 +0200 Subject: Generalize GetMissingArtifacts with templated iterators --- src/buildtool/execution_api/common/common_api.hpp | 27 ++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/buildtool/execution_api/common/common_api.hpp') diff --git a/src/buildtool/execution_api/common/common_api.hpp b/src/buildtool/execution_api/common/common_api.hpp index c11180da..16931832 100644 --- a/src/buildtool/execution_api/common/common_api.hpp +++ b/src/buildtool/execution_api/common/common_api.hpp @@ -18,7 +18,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -59,21 +61,26 @@ struct MissingArtifactsInfo { /// be uploaded. /// \returns A struct storing the missing artifacts and a back-mapping to the /// original given type, or nullopt in case of exceptions. -template +template + requires(std::is_same_v< + TValue, + typename std::iterator_traits::value_type>) [[nodiscard]] auto GetMissingArtifactsInfo( IExecutionApi const& api, - typename std::vector::const_iterator const& begin, - typename std::vector::const_iterator const& end, - typename std::function const& converter) noexcept - -> std::optional> { + TIterator const& begin, + TIterator const& end, + typename std::function const& + converter) noexcept -> std::optional> { std::vector digests; - digests.reserve(end - begin); - MissingArtifactsInfo res{}; + digests.reserve(std::distance(begin, end)); + MissingArtifactsInfo res{}; for (auto it = begin; it != end; ++it) { try { - auto dgst = converter(*it); // can't enforce it to be noexcept - digests.emplace_back(dgst); - res.back_map.emplace(std::move(dgst), *it); + auto const inserted = + res.back_map.insert({std::invoke(converter, *it), *it}); + if (inserted.second) { + digests.emplace_back(inserted.first->first); + } } catch (...) { return std::nullopt; } -- cgit v1.2.3