Created
January 5, 2013 05:32
-
-
Save gogsbread/4459947 to your computer and use it in GitHub Desktop.
UnorderedSet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace RandomMusings | |
{ | |
public class UnOrderedSet:IEnumerable<int> | |
{ | |
Dictionary<int,int> _set = null; | |
public UnOrderedSet () | |
{ | |
_set = new Dictionary<int, int> (); | |
} | |
public void Add (int value) | |
{ | |
if (!_set.ContainsKey (value)) | |
_set.Add (value, value); | |
} | |
public UnOrderedSet Union (UnOrderedSet B) | |
{ | |
UnOrderedSet union = new UnOrderedSet (); | |
foreach (KeyValuePair<int,int> a in _set) { | |
union.Add (a.Key); | |
} | |
foreach (int b in B) { | |
union.Add (b); | |
} | |
return union; | |
} | |
#region IEnumerable implementation | |
public IEnumerator<int> GetEnumerator () | |
{ | |
foreach (KeyValuePair<int,int> v in _set) { | |
yield return v.Key; | |
} | |
} | |
#endregion | |
#region IEnumerable implementation | |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () | |
{ | |
return GetEnumerator (); | |
} | |
#endregion | |
} | |
public class Client{ | |
public static void Main(){ | |
UnOrderedSet aset = new UnOrderedSet(); | |
aset.Add(12); | |
aset.Add(23); | |
aset.Add(12); | |
foreach(int a in aset){ | |
//Console.WriteLine(a); | |
} | |
UnOrderedSet bset = new UnOrderedSet(); | |
bset.Add(23); | |
bset.Add(14); | |
UnOrderedSet u = aset.Union(bset); | |
foreach(int a in u){ | |
Console.WriteLine(a); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment