Introduction
A fairly decent sets class based on hashtables and iteration.
Background
Even Google couldn't locate a sets class in C#. So we wrote one.
Below is the Set
API
public Set()
public Set(Set otherSet)
public Set(int capacity)
public Set(Set otherSet, float loadFactor)
public Set(IHashCodeProvider iHashCodeProvider, IComparer iComparer)
public Set(int capacity, float loadFactor) : base(capacity, loadFactor)
public Set(Set otherSet, IHashCodeProvider iHashCodeProvider, IComparer iComparer)
public Set(int capacity, IHashCodeProvider iHashCodeProvider, IComparer iComparer)
public Set(Set otherSet, float loadFactor, IHashCodeProvider iHashCodeProvider,
IComparer iComparer)
public Set(int capacity, float loadFactor, IHashCodeProvider iHashCodeProvider,
IComparer iComparer)
public void Add(Object entry)
public static Set operator | (Set set1, Set set2)
public Set Union(Set otherSet)
public static Set operator & (Set set1, Set set2)
public Set Intersection(Set otherSet)
public static Set operator ^ (Set set1, Set set2)
public Set ExclusiveOr(Set otherSet)
public static Set operator - (Set set1, Set set2)
public Set Difference(Set otherSet)
Using the code
Below is some self explanatory code that demonstrates the use of this code:
using System;
using System.Collections;
using Extensions;
namespace Extensions.Test
{
class Test
{
static void DisplayItems(String name, Set theSet)
{
Console.WriteLine(name);
SortedList sortedItems = new SortedList(CaseInsensitiveComparer.Default);
foreach(object key in theSet.Keys)
{
sortedItems.Add(key, null);
}
Console.Write('\t');
int count = 1;
foreach(object key in sortedItems.Keys)
{
Console.Write(key);
if (count++ < sortedItems.Keys.Count)
{
Console.Write(", ");
}
}
Console.WriteLine();
}
[STAThread]
static void Main()
{
Set setA = new Set(CaseInsensitiveHashCodeProvider.Default,
CaseInsensitiveComparer.Default);
setA.Add("Green");
setA.Add("purple");
setA.Add("Red");
setA.Add("blue");
DisplayItems("set A:", setA);
Set setB = new Set(CaseInsensitiveHashCodeProvider.Default,
CaseInsensitiveComparer.Default);
setB.Add("black");
setB.Add("Blue");
setB.Add("green");
setB.Add("red");
setB.Add("orange");
DisplayItems("set B:", setB);
Set setC = new Set(CaseInsensitiveHashCodeProvider.Default,
CaseInsensitiveComparer.Default);
setC.Add("pink");
setC.Add("yellow");
setC.Add("Black");
setC.Add("turqoise");
setC.Add("red");
DisplayItems("set C:", setC);
Console.WriteLine("------------------------------------");
DisplayItems("A union B:", setA.Union(setB));
DisplayItems("B union C:", setB.Union(setC));
DisplayItems("A intersect B", setA.Intersection(setB));
DisplayItems("A xor B:", setA.ExclusiveOr(setB));
DisplayItems("A xor C:", setA.ExclusiveOr(setC));
DisplayItems("B xor C:", setB.ExclusiveOr(setC));
DisplayItems("A - B:", setA.Difference(setB));
DisplayItems("B - A:", setB.Difference(setA));
DisplayItems("A union B union C:", setA | setB | setC);
DisplayItems("B union C union A:", setB | setC | setA);
DisplayItems("C union B union A:", setC | setB | setA);
DisplayItems("A intersect B intersect C:", setA & setB & setC);
DisplayItems("B intersect A intersect C:", setB & setA & setC);
DisplayItems("(A xor B) xor C:", (setA ^ setB) ^ setC);
DisplayItems("A xor (B xor C):", setA ^ (setB ^ setC));
DisplayItems("(C xor A) xor B:", (setC ^ setA) ^ setB);
DisplayItems("C xor (A xor B):", setC ^ (setA ^ setB));
DisplayItems("(A - B) - C:", (setA - setB) - setC);
DisplayItems("A - (B - C):", setA - (setB - setC));
DisplayItems("(C - B) - A:", (setC - setB) - setA);
DisplayItems("C - (B - A):", setC - (setB - setA));
}
}
}