Skip to content

Commit

Permalink
Make more classes hashable and comparable
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
  • Loading branch information
jjerphan committed Jul 26, 2024
1 parent 4b8cc14 commit d09b5fb
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 0 deletions.
23 changes: 23 additions & 0 deletions libmamba/include/mamba/specs/build_number_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fmt/format.h>

#include "mamba/specs/error.hpp"
#include "mamba/util/tuple_hash.hpp"

namespace mamba::specs
{
Expand Down Expand Up @@ -124,6 +125,16 @@ namespace mamba::specs
*/
[[nodiscard]] auto contains(BuildNumber point) const -> bool;

[[nodiscard]] auto operator==(const BuildNumberSpec& other) const -> bool
{
return m_predicate == other.m_predicate;
}

[[nodiscard]] auto operator!=(const BuildNumberSpec& other) const -> bool
{
return !(*this == other);
}

private:

BuildNumberPredicate m_predicate;
Expand Down Expand Up @@ -155,4 +166,16 @@ struct fmt::formatter<mamba::specs::BuildNumberSpec>
format(const ::mamba::specs::BuildNumberSpec& spec, format_context& ctx) -> decltype(ctx.out());
};

template <>
struct std::hash<mamba::specs::BuildNumberSpec>
{
auto operator()(const mamba::specs::BuildNumberSpec& spec) const -> std::size_t
{
auto seed = std::size_t{ 0 };
seed = mamba::util::hash_combine_val(seed, spec.str());

return seed;
}
};

#endif
22 changes: 22 additions & 0 deletions libmamba/include/mamba/specs/chimera_string_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mamba/specs/error.hpp"
#include "mamba/specs/glob_spec.hpp"
#include "mamba/specs/regex_spec.hpp"
#include "mamba/util/tuple_hash.hpp"

namespace mamba::specs
{
Expand Down Expand Up @@ -51,6 +52,16 @@ namespace mamba::specs

[[nodiscard]] auto str() const -> const std::string&;

[[nodiscard]] auto operator==(const ChimeraStringSpec& other) const -> bool
{
return m_spec == other.m_spec;
}

[[nodiscard]] auto operator!=(const ChimeraStringSpec& other) const -> bool
{
return !(*this == other);
}

private:

Chimera m_spec;
Expand All @@ -66,4 +77,15 @@ struct fmt::formatter<mamba::specs::ChimeraStringSpec>
format(const ::mamba::specs::ChimeraStringSpec& spec, format_context& ctx) -> decltype(ctx.out());
};

template <>
struct std::hash<mamba::specs::ChimeraStringSpec>
{
auto operator()(const mamba::specs::ChimeraStringSpec& spec) const -> std::size_t
{
auto seed = std::size_t(0);
seed = mamba::util::hash_combine_val(seed, spec.str());
return seed;
}
};

#endif
19 changes: 19 additions & 0 deletions libmamba/include/mamba/specs/glob_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ namespace mamba::specs

[[nodiscard]] auto str() const -> const std::string&;

[[nodiscard]] auto operator==(const GlobSpec& other) const -> bool
{
return m_pattern == other.m_pattern;
}

[[nodiscard]] auto operator!=(const GlobSpec& other) const -> bool
{
return !(*this == other);
}

private:

std::string m_pattern = std::string(free_pattern);
Expand All @@ -57,4 +67,13 @@ struct fmt::formatter<mamba::specs::GlobSpec>
auto format(const ::mamba::specs::GlobSpec& spec, format_context& ctx) -> decltype(ctx.out());
};

template <>
struct std::hash<mamba::specs::GlobSpec>
{
auto operator()(const mamba::specs::GlobSpec& spec) const -> std::size_t
{
return std::hash<std::string>{}(spec.str());
}
};

#endif
72 changes: 72 additions & 0 deletions libmamba/include/mamba/specs/match_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "mamba/specs/version_spec.hpp"
#include "mamba/util/flat_set.hpp"
#include "mamba/util/heap_optional.hpp"
#include "mamba/util/tuple_hash.hpp"

namespace mamba::specs
{
Expand Down Expand Up @@ -135,6 +136,24 @@ namespace mamba::specs
*/
[[nodiscard]] auto contains_except_channel(const PackageInfo& pkg) const -> bool;

auto operator==(const MatchSpec& other) const -> bool
{
return m_channel == other.m_channel && m_version == other.m_version
&& m_name == other.m_name && m_build_string == other.m_build_string
&& m_name_space == other.m_name_space && m_build_number == other.m_build_number
&& m_extra == other.m_extra;
}

auto operator!=(const MatchSpec& other) const -> bool
{
return !(*this == other);
}

auto extra_members_hash(std::size_t seed) const -> std::size_t
{
return mamba::util::hash_combine_val(seed, m_extra);
}

private:

struct ExtraMembers
Expand All @@ -150,7 +169,23 @@ namespace mamba::specs
std::string features = {};
string_set track_features = {};
bool optional = false;

[[nodiscard]] auto operator==(const ExtraMembers& other) const -> bool
{
return filename == other.filename && subdirs == other.subdirs && md5 == other.md5
&& sha256 == other.sha256 && license == other.license
&& license_family == other.license_family && features == other.features
&& track_features == other.track_features && optional == other.optional;
}

[[nodiscard]] auto operator!=(const ExtraMembers& other) const -> bool
{
return !(*this == other);
}

friend struct std::hash<ExtraMembers>;
};
friend struct std::hash<MatchSpec::ExtraMembers>;

std::optional<UnresolvedChannel> m_channel;
VersionSpec m_version;
Expand Down Expand Up @@ -255,4 +290,41 @@ namespace mamba::specs
return true;
}
}

template <>
struct std::hash<mamba::specs::MatchSpec>
{
auto operator()(const mamba::specs::MatchSpec& spec) const -> std::size_t
{
auto seed = std::size_t(0);
seed = mamba::util::hash_combine_val(seed, spec.channel());
seed = mamba::util::hash_combine_val(seed, spec.version());
seed = mamba::util::hash_combine_val(seed, spec.name());
seed = mamba::util::hash_combine_val(seed, spec.build_string());
seed = mamba::util::hash_combine_val(seed, spec.name_space());
seed = mamba::util::hash_combine_val(seed, spec.build_number());
seed = spec.extra_members_hash(seed);
return seed;
}
};

template <>
struct std::hash<mamba::specs::MatchSpec::ExtraMembers>
{
auto operator()(const mamba::specs::MatchSpec::ExtraMembers& extra) const -> std::size_t
{
auto seed = std::size_t(0);
seed = mamba::util::hash_combine_val(seed, extra.filename);
seed = mamba::util::hash_combine_val(seed, extra.subdirs);
seed = mamba::util::hash_combine_val(seed, extra.md5);
seed = mamba::util::hash_combine_val(seed, extra.sha256);
seed = mamba::util::hash_combine_val(seed, extra.license);
seed = mamba::util::hash_combine_val(seed, extra.license_family);
seed = mamba::util::hash_combine_val(seed, extra.features);
seed = mamba::util::hash_combine_val(seed, extra.track_features);
seed = mamba::util::hash_combine_val(seed, extra.optional);
return seed;
}
};

#endif
44 changes: 44 additions & 0 deletions libmamba/include/mamba/specs/package_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "mamba/specs/error.hpp"
#include "mamba/specs/platform.hpp"
#include "mamba/util/tuple_hash.hpp"

namespace mamba::specs
{
Expand Down Expand Up @@ -81,4 +82,47 @@ namespace mamba::specs

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

template <>
struct std::hash<mamba::specs::PackageInfo>
{
auto operator()(const mamba::specs::PackageInfo& pkg) const -> std::size_t
{
auto seed = std::size_t(0);
seed = mamba::util::hash_combine_val(seed, pkg.name);
seed = mamba::util::hash_combine_val(seed, pkg.version);
seed = mamba::util::hash_combine_val(seed, pkg.build_string);
seed = mamba::util::hash_combine_val(seed, pkg.build_number);
seed = mamba::util::hash_combine_val(seed, pkg.channel);
seed = mamba::util::hash_combine_val(seed, pkg.package_url);
seed = mamba::util::hash_combine_val(seed, pkg.platform);
seed = mamba::util::hash_combine_val(seed, pkg.filename);
seed = mamba::util::hash_combine_val(seed, pkg.license);
seed = mamba::util::hash_combine_val(seed, pkg.md5);
seed = mamba::util::hash_combine_val(seed, pkg.sha256);
seed = mamba::util::hash_combine_val(seed, pkg.signatures);
seed = mamba::util::hash_combine_val_range(
seed,
pkg.track_features.begin(),
pkg.track_features.end()
);
seed = mamba::util::hash_combine_val_range(
seed,
pkg.dependencies.begin(),
pkg.dependencies.end()
);
seed = mamba::util::hash_combine_val_range(seed, pkg.constrains.begin(), pkg.constrains.end());
seed = mamba::util::hash_combine_val_range(
seed,
pkg.defaulted_keys.begin(),
pkg.defaulted_keys.end()
);
seed = mamba::util::hash_combine_val(seed, pkg.noarch);
seed = mamba::util::hash_combine_val(seed, pkg.size);
seed = mamba::util::hash_combine_val(seed, pkg.timestamp);
seed = mamba::util::hash_combine_val(seed, pkg.package_type);
return seed;
}
};

#endif
12 changes: 12 additions & 0 deletions libmamba/include/mamba/specs/regex_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ namespace mamba::specs

[[nodiscard]] auto str() const -> const std::string&;

[[nodiscard]] auto operator==(const RegexSpec& other) const -> bool
{
return (
m_raw_pattern == other.m_raw_pattern && m_pattern.flags() == other.m_pattern.flags()
);
}

[[nodiscard]] auto operator!=(const RegexSpec& other) const -> bool
{
return !(*this == other);
}

private:

std::regex m_pattern;
Expand Down
25 changes: 25 additions & 0 deletions libmamba/include/mamba/specs/unresolved_channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mamba/specs/error.hpp"
#include "mamba/specs/platform.hpp"
#include "mamba/util/flat_set.hpp"
#include "mamba/util/tuple_hash.hpp"

namespace mamba::specs
{
Expand Down Expand Up @@ -111,6 +112,17 @@ namespace mamba::specs

[[nodiscard]] auto str() const -> std::string;

[[nodiscard]] auto operator==(const UnresolvedChannel& other) const -> bool
{
return m_location == other.m_location && m_platform_filters == other.m_platform_filters
&& m_type == other.m_type;
}

[[nodiscard]] auto operator!=(const UnresolvedChannel& other) const -> bool
{
return !(*this == other);
}

private:

std::string m_location = std::string(unknown_channel);
Expand All @@ -137,4 +149,17 @@ struct fmt::formatter<mamba::specs::UnresolvedChannel>
auto format(const UnresolvedChannel& uc, format_context& ctx) const -> format_context::iterator;
};

template <>
struct std::hash<mamba::specs::UnresolvedChannel>
{
auto operator()(const mamba::specs::UnresolvedChannel& uc) const -> std::size_t
{
auto seed = std::size_t{ 0 };
seed = mamba::util::hash_combine_val(seed, uc.location());
seed = mamba::util::hash_combine_val(seed, uc.platform_filters());
seed = mamba::util::hash_combine_val(seed, static_cast<int>(uc.type()));
return seed;
}
};

#endif
34 changes: 34 additions & 0 deletions libmamba/include/mamba/specs/version_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mamba/specs/error.hpp"
#include "mamba/specs/version.hpp"
#include "mamba/util/flat_bool_expr_tree.hpp"
#include "mamba/util/tuple_hash.hpp"

namespace mamba::specs
{
Expand Down Expand Up @@ -195,6 +196,16 @@ namespace mamba::specs
*/
[[nodiscard]] auto expression_size() const -> std::size_t;

[[nodiscard]] auto operator==(const VersionSpec& other) const -> bool
{
return m_tree == other.m_tree;
}

[[nodiscard]] auto operator!=(const VersionSpec& other) const -> bool
{
return !(*this == other);
}

private:

tree_type m_tree;
Expand Down Expand Up @@ -234,4 +245,27 @@ struct fmt::formatter<mamba::specs::VersionSpec>

auto format(const ::mamba::specs::VersionSpec& spec, format_context& ctx) -> decltype(ctx.out());
};

template <>
struct std::hash<mamba::specs::VersionPredicate>
{
auto operator()(const mamba::specs::VersionPredicate& pred) const -> std::size_t
{
auto seed = std::size_t{ 0 };
seed = mamba::util::hash_combine_val(seed, pred.str());
return seed;
}
};

template <>
struct std::hash<mamba::specs::VersionSpec>
{
auto operator()(const mamba::specs::VersionSpec& spec) const -> std::size_t
{
auto seed = std::size_t{ 0 };
seed = mamba::util::hash_combine_val(seed, spec.str());
return seed;
}
};

#endif
Loading

0 comments on commit d09b5fb

Please sign in to comment.