Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

A generic Set type for .NET

Rate me:
Please Sign up or sign in to vote.
4.79/5 (14 votes)
31 Oct 20047 min read 95.6K   565   42  
Provides a generic set collection.
using System;
using System.Collections;
using System.Collections.Generic;
using Lambda.Collections.Generic;
using System.Threading;
using System.Diagnostics;

public class Tests
{
    private const int N = 1000000;
    private static int[] values;
    static Tests()
    {
        //create a list of integers with lots of duplicate values;
        Random random = new Random();
        values = new int[N];
        for (int i = 0; i < N; i++)
            values[i] = random.Next(N / 10);
    }

    public delegate object TestDelegate();
    public static void Benchmark(TestDelegate testMethod)
    {
#if(DEBUG)
        Console.WriteLine("Warning: benchmarking in debug mode will not produce meaningful results!");
#endif
        ThreadPriority p = Thread.CurrentThread.Priority;
        try
        {
#if(!DEBUG)
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
#endif
            DateTime time0, time1, endtime;
            //let the JIT do its thing for at least one second
            time0 = DateTime.Now;
            time1 = time0 + new TimeSpan(0, 0, 1);
            while ((endtime = DateTime.Now) < time1)
                testMethod();

            //benchmark for at least 10 seconds.
            long mem0 = System.GC.GetTotalMemory(true);
            time0 = DateTime.Now;
            time1 = time0 + new TimeSpan(0, 0, 10);
            int i = 0; object o = null;
            for (i = 0; (endtime = DateTime.Now) < time1; i++)
                o = testMethod();
            TimeSpan deltat = new TimeSpan((endtime.Ticks - time0.Ticks) / i);
            long mem1 = System.GC.GetTotalMemory(true);
            Console.WriteLine(
                "Method {0} took {1}",
                testMethod.Method.Name,
                deltat, o);
            Console.WriteLine(
                "The object returned consumes approx. {0} bytes after a full GC", mem1 - mem0);
        }
        finally
        {
            Thread.CurrentThread.Priority = p;
        }
    }
    public static object NonGenericHashSet()
    {
        Hashtable temp = new Hashtable();
        foreach (int i in values)
            temp[i] = null;
        return temp;
    }
    public static object GenericHashSet1()
    {
        Dictionary<int, object> temp = new Dictionary<int, object>();
        foreach (int i in values)
            temp[i] = null;
        return temp;
    }
    struct Dummy { };
    public static object GenericHashSet2()
    {
        Dummy dummy = new Dummy();
        Dictionary<int, int> temp = new Dictionary<int, int>();
        foreach (int i in values)
            temp[i] = 0;
        return temp;
    }
    public static object LambdaSet()
    {
        Set<int> s = new Set<int>();
        foreach (int i in values)
            s.Add(i);
        return s;
    }
    private static Set<int> Create(params int[] values)
    {
        return new Set<int>(values);
    }
    public static void Main(string[] args)
    {
        #region correctness tests
        Set<int> a = Create(1, 2, 3);
        Set<int> b = Create(3, 4, 5);
        Set<int> c = Create(1, 2, 3, 4);
        Trace.Assert((a & b) == Create(3));
        Trace.Assert((a | b) == Create(1, 2, 3, 4, 5));
        Trace.Assert((a ^ b) == Create(1, 2, 4, 5));
        Trace.Assert(a - b == Create(1, 2));
        Trace.Assert(b - a == Create(4, 5));
        Trace.Assert(a != c);
        Trace.Assert(a <= c);
        Trace.Assert(c >= a);
        Trace.Assert(a < c);
        Trace.Assert(c > a);
        #endregion
        #region speed tests
        Benchmark(NonGenericHashSet);
        Benchmark(GenericHashSet1);
        Benchmark(GenericHashSet2);
        Benchmark(LambdaSet);
        #endregion
        Console.ReadLine();
    }
}     

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
Rüdiger Klaehn works as freelance developer in the space industry. He is very interested in functional programming languages and hopes that .NET will lead to a more widespread adoption in the industry.

Comments and Discussions