A simple Set collection

Published 26 May 05 12:39 PM

In a Microsoft internal discussion, someone asked for Set functionality in a collection.  I threw this together. 

What do you think?  Useful or no?

    using System.Collections.Generic;

    using System.Diagnostics;

 

    class Set<TItem>

    {

        Dictionary<TItem, bool> _list = new Dictionary<TItem, bool>();

 

        public bool Contains(TItem item) { return _list.ContainsKey(item); }

 

        public void Add(TItem item)

        {

            if (!Contains(item))

            {

                this._list.Add(item, false);

            }

        }

 

        public void Remove(TItem item)

        {

            if (!Contains(item))

            {

                // TODO: pick a better exception type

                throw new System.Exception();

            }

 

            this._list.Remove(item);

        }

 

        public int Count { get { return this._list.Count; } }

    }

 

    class Test

    {

        static void Main(string[] args)

        {

            Set<int> set = new Set<int>();

 

            Debug.Assert(set.Count == 0);

            Debug.Assert(set.Contains(1) == false);

 

            set.Add(1);

            Debug.Assert(set.Count == 1);

            Debug.Assert(set.Contains(1) == true);

            Debug.Assert(set.Contains(2) == false);

 

            set.Add(1);

            Debug.Assert(set.Count == 1);

            Debug.Assert(set.Contains(1) == true);

            Debug.Assert(set.Contains(2) == false);

 

            set.Add(2);

            Debug.Assert(set.Count == 2);

            Debug.Assert(set.Contains(1) == true);

            Debug.Assert(set.Contains(2) == true);

 

            set.Remove(1);

            Debug.Assert(set.Count == 1);

            Debug.Assert(set.Contains(1) == false);

            Debug.Assert(set.Contains(2) == true);

        }

    }

Comments

# Paul D. Murphy said on May 26, 2005 1:11 PM:
google power collections, that's useful. This is interesting.
# Keith J. Farmer said on May 26, 2005 3:37 PM:
Built-in support for sets would be useful, but only if there were Set operations to go along with it (which is where Power Collections has provided value). Perhaps adding set operations to the framework, useful on ArrayList, List<T>, etc would be the more direct approach.

This would preserve multiple copies of an item in a List, but you could then provide a specialization of List with more traditional Set logic on that count, as you've written here.

Since we're on the topic of nice things to have, baking the Singleton pattern into .NET would be another useful idea, I think.

public static interface ISingleton<T> where T: new
{
public static T Instance
{
get;
}
}

... or an actual generic static class to handle singleton instantiation (delegating a singleton constructor).
# RonO said on May 27, 2005 7:57 AM:
I agree with Keith. Baking in a formal Singleton pattern would have been/be a useful addition to the Framework.

As was pointed out already, for Set operations I'd direct the internal discussion to the Power Collections site:

http://www.wintellect.com/powercollections/

As far as I can tell it is considered a quasi-official extention to the Framework.
# Job Search said on June 5, 2005 2:57 PM:
Very good. Great staff.
# jaybaz MS WebLog A simple Set collection | Cast Iron Cookware said on May 26, 2009 2:23 PM:

PingBack from http://castironbakeware.info/story.php?title=jaybaz-ms-weblog-a-simple-set-collection

# jaybaz MS WebLog A simple Set collection | Indoor Grills said on June 1, 2009 3:55 AM:

PingBack from http://indoorgrillsrecipes.info/story.php?id=285

New Comments to this post are disabled

This Blog

Syndication

Page view tracker