Implement IEquitable for Cluster
MODIFY
WalletWasabi/BlockchainAnalysis/Cluster.cs
WalletWasabi/BlockchainAnalysis/Cluster.cs
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Linq;
using WalletWasabi.Bases;
@@ -6,12 +7,20 @@ using WalletWasabi.Coins;
namespace WalletWasabi.BlockchainAnalysis
{
- public class Cluster : NotifyPropertyChangedBase
+ public class Cluster : NotifyPropertyChangedBase, IEquatable<Cluster>
{
private List<SmartCoin> Coins { get; set; }
private SmartLabel _labels;
+ public SmartLabel Labels
+ {
+ get => _labels;
+ private set => RaiseAndSetIfChanged(ref _labels, value);
+ }
+
+ public int Size => Coins.Count;
+
public Cluster(params SmartCoin[] coins)
: this(coins as IEnumerable<SmartCoin>)
{
@@ -23,14 +32,6 @@ namespace WalletWasabi.BlockchainAnalysis
Labels = SmartLabel.Merge(Coins.Select(x => x.Label));
}
- public SmartLabel Labels
- {
- get => _labels;
- private set => RaiseAndSetIfChanged(ref _labels, value);
- }
-
- public int Size => Coins.Count;
-
public void Merge(Cluster clusters) => Merge(clusters.Coins);
public void Merge(IEnumerable<SmartCoin> coins)
@@ -49,5 +50,54 @@ namespace WalletWasabi.BlockchainAnalysis
Labels = SmartLabel.Merge(Coins.Select(x => x.Label));
}
}
+
+ #region EqualityAndComparison
+
+ public override bool Equals(object obj) => obj is Cluster clus && this == clus;
+
+ public bool Equals(Cluster other) => this == other;
+
+ public override int GetHashCode()
+ {
+ int hash = 0;
+ if (Coins != null)
+ {
+ foreach (var coin in Coins)
+ {
+ hash ^= coin.GetHashCode();
+ }
+ }
+ return hash;
+ }
+
+ public static bool operator ==(Cluster x, Cluster y)
+ {
+ if (x is null)
+ {
+ if (y is null)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ else
+ {
+ if (y is null)
+ {
+ return false;
+ }
+ else
+ {
+ return x.Coins.SequenceEqual(y.Coins);
+ }
+ }
+ }
+
+ public static bool operator !=(Cluster x, Cluster y) => !(x == y);
+
+ #endregion EqualityAndComparison
}
}

MODIFY
WalletWasabi/BlockchainAnalysis/SmartLabel.cs
WalletWasabi/BlockchainAnalysis/SmartLabel.cs
@@ -30,7 +30,6 @@ namespace WalletWasabi.BlockchainAnalysis
.OrderBy(x => x)
.ToArray();
- HashCode = ((IStructuralEquatable)Labels).GetHashCode(EqualityComparer<string>.Default);
IsEmpty = !Labels.Any();
LabelString = string.Join(", ", Labels);
@@ -63,9 +62,7 @@ namespace WalletWasabi.BlockchainAnalysis
public bool Equals(string other) => this == other;
- private int HashCode { get; }
-
- public override int GetHashCode() => HashCode;
+ public override int GetHashCode() => ((IStructuralEquatable)Labels).GetHashCode(EqualityComparer<string>.Default);
public static bool operator ==(SmartLabel x, SmartLabel y)
{

Implement IEquitable for Cluster