September 5, 2017

C# HashSet

Best to demonstrate what some of the methods do with some unit tests. In particular what does 'SymmetricExceptWith' do?
[ TestFixture ]
public class HashSetTests
{
    private HashSet<int> integerSet1;
    private HashSet<int> integerSet1Copy;
    private HashSet<int> integerSet2;

    [ SetUp ]
    public void Setup()
    {
        integerSet1 = new HashSet<int> { 1, 2, 3 };
        integerSet2 = new HashSet<int> { 2, 3, 4 };
        integerSet1Copy = new HashSet<int> { 2, 3, 1 };
    }

    [ Test ]
    // The SetEquals method ignores duplicate entries and 
    // the order of elements in the other parameter.
    public void SetEqualsTest()
    {
        var integerList = new List<int> { 2, 3, 1, 3, 2 };

        Assert.That( !integerSet1.SetEquals( integerSet2 ) );
        Assert.That( integerSet1.SetEquals( integerSet1Copy ) );
        Assert.That( integerSet1.SetEquals( integerList ) );
    }

    [ Test ]
    public void IntersectWithTest() // All elements common to both sets
    {
        integerSet1.IntersectWith( integerSet2 );
        Assert.That( !integerSet1.Contains( 1 ) );
        Assert.That( integerSet1.Contains( 2 ) );
        Assert.That( integerSet1.Contains( 3 ) );
        Assert.That( !integerSet1.Contains( 4 ) );
    }

    [ Test ]
    public void UnionWithTest() // All elements in both sets
    {
        integerSet1.UnionWith( integerSet2 );
        Assert.That( integerSet1.Contains( 1 ) );
        Assert.That( integerSet1.Contains( 2 ) );
        Assert.That( integerSet1.Contains( 3 ) );
        Assert.That( integerSet1.Contains( 4 ) );
    }

    [ Test ]
    public void SymmetricExceptWithTest() // All elements not common to both sets
    {
        integerSet1.SymmetricExceptWith( integerSet2 );
        Assert.That( integerSet1.Contains( 1 ) );
        Assert.That( !integerSet1.Contains( 2 ) );
        Assert.That( !integerSet1.Contains( 3 ) );
        Assert.That( integerSet1.Contains( 4 ) );
    }
}

Here are some extension methods that maintain the original values of the sets (the existing methods modify the calling set)
public static class HashSetExtensions
{
	public static HashSet<T> Union<T>(
        this HashSet<T> hashSet1, IEnumerable<T> hashSet2)
	{
		var res = new HashSet<T>(hashSet1, hashSet1.Comparer);
		res.UnionWith(hashSet2);
		return res;
	}

	public static HashSet<T> Intersecting<T>(
        this HashSet<T> hashSet1, IEnumerable<T> hashSet2)
	{
		var res = new HashSet<T>(hashSet1, hashSet1.Comparer);
		res.IntersectWith(hashSet2);
		return res;
	}

	public static HashSet<T> RemoveAnyFrom<T>(
        this HashSet<T> hashSet1, IEnumerable<T> hashSet2)
	{
		var res = new HashSet<T>(hashSet1, hashSet1.Comparer);
		res.ExceptWith(hashSet2);
		return res;
	}

	public static HashSet<T> NotIntersecting<T>(
        this HashSet<T> hashSet1, IEnumerable<T> hashSet2)
	{
		var res = new HashSet<T>(hashSet1, hashSet1.Comparer);
		res.SymmetricExceptWith(hashSet2);
		return res;
	}
}
and some tests for them
string[] names1 = new string[] {
	"banana","apple","pear","naranja","grapes","mango"
};

string[] names2 = new string[] {
	"advocado","lemon","Pear","lime","banana","passion fruit"
};

HashSet<string> hSetN1 = new HashSet<string>(names1, StringComparer.OrdinalIgnoreCase);
HashSet<string> hSetN2 = new HashSet<string>(names2, StringComparer.OrdinalIgnoreCase);

var res = hSetN1.Union(hSetN2);
var res2 = hSetN1.Intersecting(hSetN2);
var res3 = hSetN1.RemoveAnyFrom(hSetN2);
var res4 = hSetN1.NotIntersecting(hSetN2);

No comments: