Click here to Skip to main content
6,293,171 members and growing! (11,932 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Data Structures     Intermediate

A C# sets class

By Richard Bothne, Jim Showalter

A fairly decent sets class in C#.
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:13 Nov 2002
Views:69,035
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
14 votes for this article.
Popularity: 4.15 Rating: 3.63 out of 5

1
1 vote, 12.5%
2
1 vote, 12.5%
3
2 votes, 25.0%
4
4 votes, 50.0%
5

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

/// Refer to Hashtable constructor documentation.

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)

//  Adds an item to the set. Items are stored as keys, with no associated values.

public void Add(Object entry)

// Union of set1 and set2.

public static Set operator | (Set set1, Set set2)

// Union of this set and otherSet.

public Set Union(Set otherSet)

// Intersection of set1 and set2.

public static Set operator & (Set set1, Set set2) 

// Intersection of this set and otherSet.

public Set Intersection(Set otherSet) 

// Exclusive-OR of set1 and set2.

public static Set operator ^ (Set set1, Set set2) 

// Exclusive-OR of this set and otherSet.

public Set ExclusiveOr(Set otherSet) 

// The set1 minus set2. This is not associative.

public static Set operator - (Set set1, Set set2) 

// This set minus otherSet. This is not associative.

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
{
    // Unit test for Sets class.

    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));
        }
    }
}

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

About the Authors

Richard Bothne


Member

Occupation: Web Developer
Location: United States United States

Jim Showalter


Member
Over 20 years of software-engineering experience as manager and engineer, with responsibilities throughout product lifecycle.
Occupation: Web Developer
Location: United States United States

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralLicense? Pinmemberxiyukun23:47 12 Feb '08  
GeneralFundamental data type Pinsussedciv14:51 1 Dec '04  
GeneralModification to allow value retrieval PinmemberBTEProg8:20 8 Jun '04  
GeneralImplementation PinmemberDylan Spence5:21 17 Jun '03  
GeneralRe: Implementation PinmemberMichael Micco14:08 10 Dec '03  
GeneralA Delphi-Pascal Perspective PinmemberPeter Gummer16:26 19 Nov '02  
GeneralExplanation needed PinmemberTim Hodgson8:59 14 Nov '02  
GeneralRe: Explanation needed PinmemberJim Showalter10:46 14 Nov '02  
GeneralRe: Explanation needed PinmemberChristopher Lord16:57 14 Nov '02  
GeneralRe: Explanation needed PinmemberJim Showalter20:23 14 Nov '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Nov 2002
Editor: Chris Maunder
Copyright 2002 by Richard Bothne, Jim Showalter
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project