Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TrackingHashSet.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.Specialized;
6 using System.Linq;
7 
8 namespace SiliconStudio.Core.Collections
9 {
10  /// <summary>
11  /// Represents a hash set that generates events when items get added or removed.
12  /// </summary>
13  /// <remarks>
14  /// Underlying storage is done with a <see cref="HashSet{T}"/>.
15  /// </remarks>
16  /// <typeparam name="T">The type of elements in the hash set.</typeparam>
17  public class TrackingHashSet<T> : ISet<T>, IReadOnlySet<T>, ITrackingCollectionChanged
18  {
19  private HashSet<T> innerHashSet = new HashSet<T>();
20 
21  /// <inheritdoc/>
22  public event EventHandler<TrackingCollectionChangedEventArgs> CollectionChanged;
23 
24  /// <inheritdoc/>
25  public bool Add(T item)
26  {
27  if (innerHashSet.Add(item))
28  {
29  if (CollectionChanged != null)
30  CollectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, null, -1, true));
31  return true;
32  }
33 
34  return false;
35  }
36 
37  /// <inheritdoc/>
38  public void ExceptWith(IEnumerable<T> other)
39  {
40  throw new NotImplementedException();
41  }
42 
43  /// <inheritdoc/>
44  public void IntersectWith(IEnumerable<T> other)
45  {
46  throw new NotImplementedException();
47  }
48 
49  /// <inheritdoc/>
50  public bool IsProperSubsetOf(IEnumerable<T> other)
51  {
52  return innerHashSet.IsProperSubsetOf(other);
53  }
54 
55  /// <inheritdoc/>
57  {
58  return innerHashSet.IsProperSupersetOf(other);
59  }
60 
61  /// <inheritdoc/>
62  public bool IsSubsetOf(IEnumerable<T> other)
63  {
64  return innerHashSet.IsSubsetOf(other);
65  }
66 
67  /// <inheritdoc/>
68  public bool IsSupersetOf(IEnumerable<T> other)
69  {
70  return innerHashSet.IsSupersetOf(other);
71  }
72 
73  /// <inheritdoc/>
74  public bool Overlaps(IEnumerable<T> other)
75  {
76  return innerHashSet.Overlaps(other);
77  }
78 
79  /// <inheritdoc/>
80  public bool SetEquals(IEnumerable<T> other)
81  {
82  return innerHashSet.SetEquals(other);
83  }
84 
85  /// <inheritdoc/>
87  {
88  throw new NotImplementedException();
89  }
90 
91  /// <inheritdoc/>
92  public void UnionWith(IEnumerable<T> other)
93  {
94  throw new NotImplementedException();
95  }
96 
97  /// <inheritdoc/>
98  void ICollection<T>.Add(T item)
99  {
100  innerHashSet.Add(item);
101  }
102 
103  /// <inheritdoc/>
104  public void Clear()
105  {
106  if (CollectionChanged != null)
107  {
108  foreach (var item in innerHashSet.ToArray())
109  {
110  Remove(item);
111  }
112  }
113  else
114  {
115  innerHashSet.Clear();
116  }
117  }
118 
119  /// <inheritdoc/>
120  public bool Contains(T item)
121  {
122  return innerHashSet.Contains(item);
123  }
124 
125  /// <inheritdoc/>
126  public void CopyTo(T[] array, int arrayIndex)
127  {
128  innerHashSet.CopyTo(array, arrayIndex);
129  }
130 
131  /// <inheritdoc/>
132  public int Count
133  {
134  get { return innerHashSet.Count; }
135  }
136 
137  /// <inheritdoc/>
138  public bool IsReadOnly
139  {
140  get { return ((ICollection<T>)innerHashSet).IsReadOnly; }
141  }
142 
143  /// <inheritdoc/>
144  public bool Remove(T item)
145  {
146  var result = innerHashSet.Remove(item);
147 
148  if (CollectionChanged != null && result)
149  CollectionChanged(this, new TrackingCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, null, -1, true));
150 
151  return result;
152  }
153 
154  /// <inheritdoc/>
155  public HashSet<T>.Enumerator GetEnumerator()
156  {
157  return innerHashSet.GetEnumerator();
158  }
159 
160  /// <inheritdoc/>
161  IEnumerator<T> IEnumerable<T>.GetEnumerator()
162  {
163  return innerHashSet.GetEnumerator();
164  }
165 
166  /// <inheritdoc/>
167  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
168  {
169  return innerHashSet.GetEnumerator();
170  }
171  }
172 }
bool Contains(T item)
Determines whether the set [contains] [the specified item]. The item.true if the set [contains] [the ...
EventHandler< TrackingCollectionChangedEventArgs > CollectionChanged
This is a minimal implementation of the missing HashSet from Silverlight BCL It's nowhere near the re...
Definition: HashSet.cs:8