Skip to content

Commit

Permalink
Implement IEquatable<T> (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Dec 21, 2024
1 parent 4cfbf69 commit ab0b94f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.0",
"version": "9.0.0",
"allowPrerelease": false,
"rollForward": "latestFeature"
}
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ContinuousIntegrationBuild Condition="'$(CI)' == 'true'">true</ContinuousIntegrationBuild>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<PropertyGroup Label="ProductInfo">
Expand Down
5 changes: 4 additions & 1 deletion src/Nethermind.Int256/Int256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Nethermind.Int256
{
public readonly struct Int256 : IComparable, IComparable<Int256>, IInteger<Int256>, IConvertible
public readonly struct Int256 : IEquatable<Int256>, IComparable, IComparable<Int256>, IInteger<Int256>, IConvertible
{
public static readonly Int256 Zero = (Int256)0UL;
public static readonly Int256 One = (Int256)1UL;
Expand Down Expand Up @@ -530,8 +530,11 @@ public override string ToString()
return ToString(null);
}

[OverloadResolutionPriority(1)]
private bool Equals(in Int256 other) => _value.Equals(other._value);

public bool Equals(Int256 other) => _value.Equals(other._value);

public override bool Equals(object? obj) => obj is Int256 other && Equals(other);

public override int GetHashCode() => _value.GetHashCode();
Expand Down
19 changes: 10 additions & 9 deletions src/Nethermind.Int256/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Nethermind.Int256
{
[StructLayout(LayoutKind.Explicit)]
public readonly struct UInt256 : IComparable, IComparable<UInt256>, IInteger<UInt256>, IConvertible
public readonly struct UInt256 : IEquatable<UInt256>, IComparable, IComparable<UInt256>, IInteger<UInt256>, IConvertible
{
public static readonly UInt256 Zero = 0ul;
public static readonly UInt256 One = 1ul;
Expand Down Expand Up @@ -1803,13 +1803,6 @@ public string ToString(string format)

public bool IsUint64 => (u1 | u2 | u3) == 0;

public bool Equals(UInt256 other)
{
var v1 = Unsafe.As<ulong, Vector256<ulong>>(ref Unsafe.AsRef(in u0));
var v2 = Unsafe.As<UInt256, Vector256<ulong>>(ref Unsafe.AsRef(in other));
return v1 == v2;
}

public bool Equals(int other)
{
return other >= 0 && Equals((uint)other);
Expand Down Expand Up @@ -1843,8 +1836,16 @@ public bool Equals(ulong other)
}
}

[OverloadResolutionPriority(1)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool Equals(in UInt256 other)
public bool Equals(in UInt256 other)
{
var v1 = Unsafe.As<ulong, Vector256<ulong>>(ref Unsafe.AsRef(in u0));
var v2 = Unsafe.As<UInt256, Vector256<ulong>>(ref Unsafe.AsRef(in other));
return v1 == v2;
}

public bool Equals(UInt256 other)
{
var v1 = Unsafe.As<ulong, Vector256<ulong>>(ref Unsafe.AsRef(in u0));
var v2 = Unsafe.As<UInt256, Vector256<ulong>>(ref Unsafe.AsRef(in other));
Expand Down

0 comments on commit ab0b94f

Please sign in to comment.