From b104891d015d0b8e152d8ac723027f824077948a Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 28 Aug 2021 13:24:06 +0200 Subject: Simplify selection of highest source version providers The old code is a bit longer and does a sort (N*log(N)) + find (at most N) It is replaced by max_element (N) + remove_if (N). The practical difference is minimal as the N we operate on is rather small, but the replacement is hopefully easier to understand at a glance as well. Gbp-Dch: Ignore --- apt-pkg/depcache.cc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'apt-pkg/depcache.cc') diff --git a/apt-pkg/depcache.cc b/apt-pkg/depcache.cc index 2c3c6d01d..dfd1e1084 100644 --- a/apt-pkg/depcache.cc +++ b/apt-pkg/depcache.cc @@ -2308,7 +2308,7 @@ void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &Pkg, auto const sort_by_source_version = [](pkgCache::VerIterator const &A, pkgCache::VerIterator const &B) { auto const verret = A.Cache()->VS->CmpVersion(A.SourceVerStr(), B.SourceVerStr()); if (verret != 0) - return verret > 0; + return verret < 0; return A->ID < B->ID; }; @@ -2361,11 +2361,8 @@ void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &Pkg, // only latest binary package of a source package is marked instead of all for (auto &providers : providers_by_source) { - std::sort(providers.second.begin(), providers.second.end(), sort_by_source_version); - auto const highestSrcVer = (*providers.second.begin()).SourceVerStr(); - auto notThisVer = std::find_if_not(providers.second.begin(), providers.second.end(), [&](auto const &V) { return strcmp(highestSrcVer, V.SourceVerStr()) == 0; }); - if (notThisVer != providers.second.end()) - providers.second.erase(notThisVer, providers.second.end()); + auto const highestSrcVer = (*std::max_element(providers.second.begin(), providers.second.end(), sort_by_source_version)).SourceVerStr(); + providers.second.erase(std::remove_if(providers.second.begin(), providers.second.end(), [&](auto const &V) { return strcmp(highestSrcVer, V.SourceVerStr()) != 0; }), providers.second.end()); // if the provider is a versioned kernel package mark them only for protected kernels if (providers.second.size() == 1) continue; -- cgit v1.2.3-70-g09d2