I’m building a graph and verify uniqueness of a vertex to add using HashSet<T>. Its method Add() returns true if an item was not added already.
What technique can I use for edges? I need to verify that either pair A-B or pair B-A pair were added once.
Here’s a lightweight version of the data I’m using:
interface INode
{
INode[] Previous { get; }
Node Next { get; }
}
Then I have:
INode current;
INode[] allPrevious = ExternalMethod1(current);
INode[] allNext = ExternalMethod2(current);
So I can’t build a graph while go round recursively, I have all the data stored up to iterate over.
Create your own equality comparer implementation for edges and set
HashSet<T>.Comparerto an instance of it. For example:Of course you would also have to check for nullity. All the standard caveats apply to the
GetHashCodeimplementation as well.