Create a storage class that displays the Add (a, b) functions and similar functions. The internal memory may be a HashSet<T> , where T is a suitable row key. The only thing that is important with respect to this key and the comparator is to use hash functions and equality functions that are symmetric, i.e. That (a, b) is equal to (b, a) and therefore hash (a, b) == hash (b, a).
As stated earlier, many hash functions have this property, such as sums and xor hash values. I decided not to use xor, because this means that all pairs of the same lines will have a zero hash, which can probably lead to an inefficient search if pairs of equal lines are likely.
The implementation below assumes that all lines are not null but do not have error checking.
public class Storage { private HashSet<Key> set; public Storage() { set = new HashSet<Key>(new Key.Comparer()); } public void Add(string a, string b) { set.Add(new Key{A=a, B=b}); } public bool Contains(string a, string b) { return set.Contains(new Key{A=a, B=b}); } internal class Key { internal String A { get; set; } internal String B { get; set; } internal class Comparer : IEqualityComparer<Key> { public bool Equals(Key x, Key y) { return (xA == yA && xB == yB) || (xA == yB && xB == yA); } public int GetHashCode(Key k) { int aHash = kAGetHashCode(); int bHash = kBGetHashCode();
source share