Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make more classes hashable and comparable #3363

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions libmamba/include/mamba/specs/build_number_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ namespace mamba::specs
*/
[[nodiscard]] auto contains(BuildNumber point) const -> bool;

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
jjerphan marked this conversation as resolved.
Show resolved Hide resolved
[[nodiscard]] auto operator==(const BuildNumberSpec& other) const -> bool
jjerphan marked this conversation as resolved.
Show resolved Hide resolved
{
return m_predicate == other.m_predicate;
Expand Down Expand Up @@ -171,10 +172,7 @@ 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;
return mamba::util::hash_vals(spec.str());
}
};

Expand Down
5 changes: 2 additions & 3 deletions libmamba/include/mamba/specs/chimera_string_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace mamba::specs

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

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[nodiscard]] auto operator==(const ChimeraStringSpec& other) const -> bool
{
return m_spec == other.m_spec;
Expand Down Expand Up @@ -82,9 +83,7 @@ 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;
return mamba::util::hash_vals(spec.str());
}
};

Expand Down
1 change: 1 addition & 0 deletions libmamba/include/mamba/specs/glob_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace mamba::specs

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

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[nodiscard]] auto operator==(const GlobSpec& other) const -> bool
{
return m_pattern == other.m_pattern;
Expand Down
72 changes: 41 additions & 31 deletions libmamba/include/mamba/specs/match_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,26 @@ namespace mamba::specs
*/
[[nodiscard]] auto contains_except_channel(const PackageInfo& pkg) const -> bool;

auto operator==(const MatchSpec& other) const -> bool
// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[nodiscard]] 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
return m_channel == other.m_channel //
Klaim marked this conversation as resolved.
Show resolved Hide resolved
&& 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
[[nodiscard]] auto operator!=(const MatchSpec& other) const -> bool
{
return !(*this == other);
}

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

private:
Expand All @@ -170,12 +174,18 @@ namespace mamba::specs
string_set track_features = {};
bool optional = false;

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[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;
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
Expand Down Expand Up @@ -296,15 +306,15 @@ 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;
return mamba::util::hash_vals(
spec.channel(),
spec.version(),
spec.name(),
spec.build_string(),
spec.name_space(),
spec.build_number(),
spec.extra_members_hash()
);
}
};

Expand All @@ -313,17 +323,17 @@ 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;
return mamba::util::hash_vals(
extra.filename,
extra.subdirs,
extra.md5,
extra.sha256,
extra.license,
extra.license_family,
extra.features,
extra.track_features,
extra.optional
);
}
};

Expand Down
32 changes: 16 additions & 16 deletions libmamba/include/mamba/specs/package_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,21 @@ 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_vals(
seed,
pkg.name,
pkg.version,
pkg.build_string,
pkg.build_number,
pkg.channel,
pkg.package_url,
pkg.platform,
pkg.filename,
pkg.license,
pkg.md5,
pkg.sha256,
pkg.signatures
);
seed = mamba::util::hash_combine_val_range(
seed,
pkg.track_features.begin(),
Expand All @@ -117,10 +120,7 @@ struct std::hash<mamba::specs::PackageInfo>
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);
seed = mamba::util::hash_vals(seed, pkg.noarch, pkg.size, pkg.timestamp, pkg.package_type);
return seed;
}
};
Expand Down
6 changes: 3 additions & 3 deletions libmamba/include/mamba/specs/regex_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ namespace mamba::specs

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

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[nodiscard]] auto operator==(const RegexSpec& other) const -> bool
{
return (
m_raw_pattern == other.m_raw_pattern && m_pattern.flags() == other.m_pattern.flags()
);
return m_raw_pattern == other.m_raw_pattern
&& m_pattern.flags() == other.m_pattern.flags();
}

[[nodiscard]] auto operator!=(const RegexSpec& other) const -> bool
Expand Down
9 changes: 3 additions & 6 deletions libmamba/include/mamba/specs/unresolved_channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ namespace mamba::specs

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

Expand Down Expand Up @@ -154,11 +155,7 @@ 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;
return mamba::util::hash_vals(uc.location(), uc.platform_filters(), static_cast<int>(uc.type()));
}
};

Expand Down
8 changes: 2 additions & 6 deletions libmamba/include/mamba/specs/version_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ 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;
return mamba::util::hash_vals(pred.str());
}
};

Expand All @@ -262,9 +260,7 @@ 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;
return mamba::util::hash_vals(spec.str());
}
};

Expand Down
5 changes: 4 additions & 1 deletion libmamba/include/mamba/util/flat_binary_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ namespace mamba::util
std::size_t left_child = 0;
std::size_t right_child = 0;

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[nodiscard]] auto operator==(const branch_node& other) const -> bool
{
return data == other.data && left_child == other.left_child
return data == other.data //
&& left_child == other.left_child //
&& right_child == other.right_child;
}

Expand Down Expand Up @@ -101,6 +103,7 @@ namespace mamba::util
[[nodiscard]] auto right(idx_type idx) const -> idx_type;
[[nodiscard]] auto root() const -> idx_type;

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[nodiscard]] auto operator==(const flat_binary_tree& other) const -> bool
{
return m_nodes == other.m_nodes && m_root == other.m_root;
Expand Down
1 change: 1 addition & 0 deletions libmamba/include/mamba/util/flat_bool_expr_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ namespace mamba::util
template <typename UnaryFunc>
void infix_for_each(UnaryFunc&& func) const;

// TODO: only use the `= default` implementation of `operator==` when we will use C++20.
[[nodiscard]] auto operator==(const self_type& other) const -> bool
{
return m_tree == other.m_tree;
Expand Down
7 changes: 1 addition & 6 deletions libmamba/include/mamba/util/flat_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,7 @@ struct std::hash<mamba::util::flat_set<Key, Compare, Allocator>>
{
auto operator()(const mamba::util::flat_set<Key, Compare, Allocator>& set) const -> std::size_t
{
auto seed = std::size_t{ 0 };
for (const auto& key : set)
{
seed = mamba::util::hash_combine_val(seed, key);
}
return seed;
return mamba::util::hash_vals(set);
}
};

Expand Down
Loading