Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
HashSet.cs
Go to the documentation of this file.
1 using System.Linq;
2 
3 namespace System.Collections.Generic {
4  /// <summary>
5  /// This is a minimal implementation of the missing HashSet from Silverlight BCL
6  /// It's nowhere near the real one, it's just enough to make Irony work with Silverlight
7  /// </summary>
8  public class HashSet<T> : Dictionary<T, bool>, IEnumerable<T> {
9  public HashSet() {
10 
11  }
12 
13  public HashSet(StringComparer comparer)
14  : base((IEqualityComparer<T>)comparer) {
15 
16  }
17 
18  public void UnionWith(IEnumerable<T> items) {
19  foreach (var item in items) {
20  Add(item);
21  }
22  }
23 
24  public void IntersectWith(HashSet<T> items) {
25  List<T> removal = new List<T>();
26  foreach (var item in this) {
27  if (!items.Contains(item)) {
28  removal.Add(item);
29  }
30  }
31  foreach (var item in removal) {
32  Remove(item);
33  }
34  }
35 
36  public bool Overlaps(HashSet<T> items) {
37  return this.Where<T>(x => items.Contains(x)).Count() > 0;
38  }
39 
40  public void ExceptWith(HashSet<T> items) {
41  List<T> removal = new List<T>();
42  foreach (var item in this) {
43  if (items.Contains(item)) {
44  removal.Add(item);
45  }
46  }
47  foreach (var item in removal) {
48  Remove(item);
49  }
50  }
51 
52  public bool Contains(T item) {
53  return ContainsKey(item);
54  }
55 
56  public T First() {
57  return Keys.First();
58  }
59 
60  public void RemoveWhere(Func<T, bool> predicate) {
61  var removal = this.Where<T>(predicate);
62  foreach (var item in removal) {
63  Remove(item);
64  }
65  }
66 
67  public bool Add(T item) {
68  if (Contains(item)) {
69  return false;
70  }
71  Add(item, true);
72  return true;
73  }
74 
75  public T this[int index] {
76  get { throw new NotImplementedException(); }
77  }
78 
79  public new IEnumerator<T> GetEnumerator() {
80  return this.Keys.GetEnumerator();
81  }
82 
83  public T[] ToArray() {
84  return this.Keys.ToArray();
85  }
86  }
87 }
void UnionWith(IEnumerable< T > items)
Definition: HashSet.cs:18
void ExceptWith(HashSet< T > items)
Definition: HashSet.cs:40
bool Overlaps(HashSet< T > items)
Definition: HashSet.cs:36
This is a minimal implementation of the missing HashSet from Silverlight BCL It's nowhere near the re...
Definition: HashSet.cs:8
void IntersectWith(HashSet< T > items)
Definition: HashSet.cs:24
new IEnumerator< T > GetEnumerator()
Definition: HashSet.cs:79
void RemoveWhere(Func< T, bool > predicate)
Definition: HashSet.cs:60
HashSet(StringComparer comparer)
Definition: HashSet.cs:13