Skip to content

Commit 8c461e0

Browse files
committed
Make PackageInfo comparable
Signed-off-by: Julien Jerphanion <[email protected]>
1 parent 2dd5482 commit 8c461e0

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

libmamba/include/mamba/specs/package_info.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ namespace mamba::specs
8585

8686
auto operator!=(const PackageInfo& lhs, const PackageInfo& rhs) -> bool;
8787

88+
auto operator<(const PackageInfo& lhs, const PackageInfo& rhs) -> bool;
89+
8890
void to_json(nlohmann::json& j, const PackageInfo& pkg);
8991

9092
void from_json(const nlohmann::json& j, PackageInfo& pkg);

libmamba/src/core/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "mamba/core/thread_utils.hpp"
2020
#include "mamba/core/util.hpp"
2121
#include "mamba/core/util_os.hpp"
22+
#include "mamba/specs/package_info.hpp"
2223
#include "mamba/util/encoding.hpp"
2324
#include "mamba/util/environment.hpp"
2425
#include "mamba/util/path_manip.hpp"

libmamba/src/specs/package_info.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,51 @@ namespace mamba::specs
466466
return !(lhs == rhs);
467467
}
468468

469+
auto operator<(const PackageInfo& lhs, const PackageInfo& rhs) -> bool
470+
{
471+
// Compare name first
472+
if (lhs.name != rhs.name)
473+
{
474+
return lhs.name < rhs.name;
475+
}
476+
477+
// If track features are present, prefer the package having the least of them
478+
if (lhs.track_features.size() != rhs.track_features.size())
479+
{
480+
return lhs.track_features.size() > rhs.track_features.size();
481+
}
482+
483+
// Then version
484+
if (lhs.version != rhs.version)
485+
{
486+
return lhs.version < rhs.version;
487+
}
488+
489+
// Then build string
490+
if (lhs.build_string != rhs.build_string)
491+
{
492+
return lhs.build_string < rhs.build_string;
493+
}
494+
495+
// Then build number
496+
if (lhs.build_number != rhs.build_number)
497+
{
498+
return lhs.build_number < rhs.build_number;
499+
}
500+
501+
// We could add the `dependencies` comparison here, but it's costly
502+
// and not needed.
503+
504+
// Then channel
505+
if (lhs.channel != rhs.channel)
506+
{
507+
return lhs.channel < rhs.channel;
508+
}
509+
510+
// If all else is equal, compare package URL
511+
return lhs.package_url < rhs.package_url;
512+
}
513+
469514
void to_json(nlohmann::json& j, const PackageInfo& pkg)
470515
{
471516
j["name"] = pkg.name;

0 commit comments

Comments
 (0)