Skip to content

Commit

Permalink
Fix and simplify combined hashcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Dec 7, 2023
1 parent f135f8f commit 4cea065
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 97 deletions.
9 changes: 4 additions & 5 deletions Core/Relationships/RelationshipResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,24 +851,23 @@ public bool Equals(SelectionReason rsn)

public override int GetHashCode()
{
var typeCode = GetType().GetHashCode();
var type = GetType();
// Parent throws in some derived classes
try
{
#if NET5_0_OR_GREATER
return HashCode.Combine(typeCode,
Parent.GetHashCode());
return HashCode.Combine(type, Parent);
#else
unchecked
{
return (typeCode * 397) ^ Parent.GetHashCode();
return (type, Parent).GetHashCode();
}
#endif
}
catch
{
// If thrown, then we're type-only
return typeCode;
return type.GetHashCode();
}
}

Expand Down
7 changes: 1 addition & 6 deletions Core/Types/CkanModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,7 @@ private static bool RelationshipsAreEquivalent(List<RelationshipDescriptor> a, L
}

public override int GetHashCode()
{
unchecked
{
return (identifier.GetHashCode() * 397) ^ version.GetHashCode();
}
}
=> (identifier, version).GetHashCode();

bool IEquatable<CkanModule>.Equals(CkanModule other)
=> Equals(other);
Expand Down
59 changes: 13 additions & 46 deletions Core/Versioning/GameVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,19 +589,12 @@ public sealed partial class GameVersion : IEquatable<GameVersion>
/// of the <see cref="obj"/> parameter; otherwise, <c>false</c>.
/// </returns>
public bool Equals(GameVersion obj)
{
if (obj is null)
{
return false;
}

if (ReferenceEquals(obj, this))
{
return true;
}

return _major == obj._major && _minor == obj._minor && _patch == obj._patch && _build == obj._build;
}
=> !(obj is null)
&& (ReferenceEquals(obj, this)
|| (_major == obj._major
&& _minor == obj._minor
&& _patch == obj._patch
&& _build == obj._build));

/// <summary>
/// Returns a value indicating whether the current <see cref="GameVersion"/> object is equal to a specified
Expand All @@ -616,41 +609,19 @@ public bool Equals(GameVersion obj)
/// matches the corresponding component of <see cref="obj"/>; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
if (obj is null)
{
return false;
}

if (ReferenceEquals(obj, this))
{
return true;
}

return obj is GameVersion gv && Equals(gv);
}
=> !(obj is null)
&& (ReferenceEquals(obj, this)
|| (obj is GameVersion gv && Equals(gv)));

/// <summary>
/// Returns a hash code for the current <see cref="GameVersion"/> object.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
#if NET5_0_OR_GREATER
=> HashCode.Combine(_major.GetHashCode(),
_minor.GetHashCode(),
_patch.GetHashCode(),
_build.GetHashCode());
=> HashCode.Combine(_major, _minor, _patch, _build);
#else
{
unchecked
{
var hashCode = _major.GetHashCode();
hashCode = (hashCode*397) ^ _minor.GetHashCode();
hashCode = (hashCode*397) ^ _patch.GetHashCode();
hashCode = (hashCode*397) ^ _build.GetHashCode();
return hashCode;
}
}
=> (_major, _minor, _patch, _build).GetHashCode();
#endif

/// <summary>
Expand All @@ -660,9 +631,7 @@ public override int GetHashCode()
/// <param name="v2">The second <see cref="GameVersion"/> object.</param>
/// <returns><c>true</c> if <see cref="v1"/> equals <see cref="v2"/>; otherwise, <c>false</c>.</returns>
public static bool operator ==(GameVersion v1, GameVersion v2)
{
return Equals(v1, v2);
}
=> Equals(v1, v2);

/// <summary>
/// Determines whether two specified <see cref="GameVersion"/> objects are not equal.
Expand All @@ -673,9 +642,7 @@ public override int GetHashCode()
/// <c>true</c> if <see cref="v1"/> does not equal <see cref="v2"/>; otherwise, <c>false</c>.
/// </returns>
public static bool operator !=(GameVersion v1, GameVersion v2)
{
return !Equals(v1, v2);
}
=> !Equals(v1, v2);
}

public sealed partial class GameVersion : IComparable, IComparable<GameVersion>
Expand Down
7 changes: 1 addition & 6 deletions Core/Versioning/GameVersionBound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,7 @@ public override bool Equals(object obj)
}

public override int GetHashCode()
{
unchecked
{
return ((Value != null ? Value.GetHashCode() : 0)*397) ^ Inclusive.GetHashCode();
}
}
=> (Value, Inclusive).GetHashCode();

public static bool operator ==(GameVersionBound left, GameVersionBound right) => Equals(left, right);
public static bool operator !=(GameVersionBound left, GameVersionBound right) => !Equals(left, right);
Expand Down
7 changes: 1 addition & 6 deletions Core/Versioning/GameVersionRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,7 @@ public override bool Equals(object obj)
}

public override int GetHashCode()
{
unchecked
{
return ((Lower != null ? Lower.GetHashCode() : 0)*397) ^ (Upper != null ? Upper.GetHashCode() : 0);
}
}
=> (Lower, Upper).GetHashCode();

public static bool operator ==(GameVersionRange left, GameVersionRange right) => Equals(left, right);
public static bool operator !=(GameVersionRange left, GameVersionRange right) => !Equals(left, right);
Expand Down
35 changes: 7 additions & 28 deletions Core/Versioning/ModuleVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,15 @@ public string ToString(bool hideEpoch, bool hideV)
public partial class ModuleVersion : IEquatable<ModuleVersion>
{
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}

return obj is ModuleVersion version && Equals(version);
}
=> ReferenceEquals(this, obj)
|| (obj is ModuleVersion version && Equals(version));

public bool Equals(ModuleVersion other)
{
if (ReferenceEquals(this, other))
{
return true;
}

return CompareTo(other) == 0;
}
=> ReferenceEquals(this, other)
|| CompareTo(other) == 0;

public override int GetHashCode()
{
unchecked
{
return (_epoch * 397) ^ _version.GetHashCode();
}
}
=> (_epoch, _version).GetHashCode();

/// <summary>
/// Compares two <see cref="ModuleVersion"/> objects to determine if the first is equal to the second.
Expand All @@ -152,9 +135,7 @@ public override int GetHashCode()
/// </list>
/// </returns>
public static bool operator ==(ModuleVersion left, ModuleVersion right)
{
return Equals(left, right);
}
=> Equals(left, right);

/// <summary>
/// Compares two <see cref="ModuleVersion"/> objects to determine if the first is not equal to the second.
Expand All @@ -178,9 +159,7 @@ public override int GetHashCode()
/// </list>
/// </returns>
public static bool operator !=(ModuleVersion left, ModuleVersion right)
{
return !Equals(left, right);
}
=> !Equals(left, right);
}

public partial class ModuleVersion : IComparable<ModuleVersion>
Expand Down

0 comments on commit 4cea065

Please sign in to comment.