I think you should implement the IEquatable
interface for your Edge
class:
public class Edge : IEquatable<Edge> { ... public bool Equals(Edge other) { return ( (other.Source == this.Source && other.Target == this.Target) || (other.Target == this.Source && other.Source == this.Target)); } public override int GetHashCode() { return (Source.GetHashCode() ^ Target.GetHashCode()); } }
and add edges to the HashSet<Edge>
collection. You can then call its Contains
method to check if it contains an edge or not.
EDIT: as Henk said, you can also implement your own IEqualityComparer
class:
public sealed class EdgeComparer : IEqualityComparer<Edge> { public static EdgeComparer Default { get; private set; } static EdgeComparer() { Default = new EdgeComparer(); } private EdgeComparer() { } public bool Equals(Edge x, Edge y) { return ( (x.Source == y.Source && x.Target == y.Target) || (x.Target == y.Source && x.Source == y.Target)); } public int GetHashCode(Edge edge) { return (edge.Source.GetHashCode() ^ edge.Target.GetHashCode()); } }
and initialize your hashset with
_drawableEdges = new HashSet<Edge>(EdgeComparer.Default);
source share