Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
OrderedHashSet.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;
5 using System.Collections.Generic;
6 
7 namespace SiliconStudio.Shaders.Utility
8 {
9  /// <summary>
10  /// An ordered set
11  /// </summary>
12  /// <typeparam name="T">Type of the element in the set</typeparam>
13  public class OrderedSet<T> : ISet<T>, IList<T>
14  {
15  #region Constants and Fields
16 
17  private HashSet<T> hashSet;
18 
19  private List<T> listSet;
20 
21  #endregion
22 
23  #region Constructors and Destructors
24 
25  /// <summary>
26  /// Initializes a new instance of the <see cref = "OrderedSet&lt;T&gt;" /> class.
27  /// </summary>
28  public OrderedSet()
29  {
30  hashSet = new HashSet<T>();
31  listSet = new List<T>();
32  }
33 
34  /// <summary>
35  /// Initializes a new instance of the <see cref="OrderedSet&lt;T&gt;"/> class.
36  /// </summary>
37  /// <param name="items">The items.</param>
38  public OrderedSet(IEnumerable<T> items)
39  {
40  hashSet = new HashSet<T>();
41  listSet = new List<T>();
42  UnionWith(items);
43  }
44 
45  #endregion
46 
47  #region Public Properties
48 
49  /// <inheritdoc />
50  public int Count
51  {
52  get
53  {
54  return hashSet.Count;
55  }
56  }
57 
58  /// <inheritdoc />
59  public bool IsReadOnly
60  {
61  get
62  {
63  return false;
64  }
65  }
66 
67  #endregion
68 
69  #region Public Methods
70 
71  /// <inheritdoc />
72  public bool Add(T item)
73  {
74  if (hashSet.Add(item))
75  {
76  listSet.Add(item);
77  return true;
78  }
79 
80  return false;
81  }
82 
83  public int IndexOf(T item)
84  {
85  return listSet.IndexOf(item);
86  }
87 
88  public void Insert(int index, T item)
89  {
90  if (hashSet.Add(item))
91  {
92  listSet.Insert(index, item);
93  }
94  }
95 
96  public void RemoveAll(Func<T,bool> filter)
97  {
98  var array = listSet.ToArray();
99  foreach (var element in array)
100  {
101  if (filter(element))
102  Remove(element);
103  }
104  }
105 
106  public void RemoveAt(int index)
107  {
108  var element = listSet[index];
109  hashSet.Remove(element);
110  listSet.RemoveAt(index);
111  }
112 
113  public T this[int index]
114  {
115  get
116  {
117  return listSet[index];
118  }
119  set
120  {
121  var element = listSet[index];
122  hashSet.Remove(element);
123  listSet[index] = value;
124  if (!hashSet.Add(value))
125  {
126  throw new InvalidOperationException("Value is already in the set");
127  }
128  }
129  }
130 
131  /// <inheritdoc />
132  public void Clear()
133  {
134  hashSet.Clear();
135  listSet.Clear();
136  }
137 
138  /// <inheritdoc />
139  public bool Contains(T item)
140  {
141  return hashSet.Contains(item);
142  }
143 
144  /// <inheritdoc />
145  public void CopyTo(T[] array, int arrayIndex)
146  {
147  listSet.CopyTo(array, arrayIndex);
148  }
149 
150  /// <inheritdoc />
151  public void ExceptWith(IEnumerable<T> other)
152  {
153  if (other == null)
154  {
155  throw new ArgumentNullException("other");
156  }
157 
158  if (Count != 0)
159  {
160  if (other == this)
161  {
162  Clear();
163  }
164  else
165  {
166  foreach (T local in other)
167  {
168  Remove(local);
169  }
170  }
171  }
172  }
173 
174  /// <inheritdoc />
175  public IEnumerator<T> GetEnumerator()
176  {
177  return listSet.GetEnumerator();
178  }
179 
180  /// <inheritdoc />
181  public void IntersectWith(IEnumerable<T> other)
182  {
183  if (other == null)
184  {
185  throw new ArgumentNullException("other");
186  }
187 
188  hashSet.IntersectWith(other);
189 
190  // Remove items from ordered list
191  for (int i = 0; i < listSet.Count && i != hashSet.Count; i++)
192  {
193  var item = listSet[i];
194  if (!hashSet.Contains(item))
195  {
196  listSet.RemoveAt(i);
197  i--;
198  }
199  }
200  }
201 
202  /// <inheritdoc />
203  public bool IsProperSubsetOf(IEnumerable<T> other)
204  {
205  return hashSet.IsProperSubsetOf(other);
206  }
207 
208  /// <inheritdoc />
210  {
211  return hashSet.IsProperSupersetOf(other);
212  }
213 
214  /// <inheritdoc />
215  public bool IsSubsetOf(IEnumerable<T> other)
216  {
217  return hashSet.IsSubsetOf(other);
218  }
219 
220  /// <inheritdoc />
221  public bool IsSupersetOf(IEnumerable<T> other)
222  {
223  return hashSet.IsSupersetOf(other);
224  }
225 
226  /// <inheritdoc />
227  public bool Overlaps(IEnumerable<T> other)
228  {
229  return hashSet.Overlaps(other);
230  }
231 
232  /// <inheritdoc />
233  public bool Remove(T item)
234  {
235  if (hashSet.Remove(item))
236  {
237  listSet.Remove(item);
238  return true;
239  }
240 
241  return false;
242  }
243 
244  /// <inheritdoc />
245  public bool SetEquals(IEnumerable<T> other)
246  {
247  return hashSet.SetEquals(other);
248  }
249 
250  /// <inheritdoc />
252  {
253  if (other == null)
254  {
255  throw new ArgumentNullException("other");
256  }
257 
258  var temp = new List<T>(other);
259  hashSet.SymmetricExceptWith(temp);
260 
261  // Remove items from ordered list
262  foreach (var item in temp)
263  {
264  int indexOf = listSet.IndexOf(item);
265  if (indexOf < 0)
266  {
267  listSet.Add(item);
268  }
269  }
270 
271  // Remove items from ordered list
272  for (int i = 0; i < listSet.Count && i != hashSet.Count; i++)
273  {
274  var item = listSet[i];
275  if (!hashSet.Contains(item))
276  {
277  listSet.RemoveAt(i);
278  i--;
279  }
280  }
281  }
282 
283  /// <inheritdoc />
284  public void UnionWith(IEnumerable<T> other)
285  {
286  if (other == null)
287  {
288  throw new ArgumentNullException("other");
289  }
290 
291  foreach (var item in other)
292  {
293  Add(item);
294  }
295  }
296 
297  #endregion
298 
299  #region Explicit Interface Methods
300 
301  /// <inheritdoc />
302  void ICollection<T>.Add(T item)
303  {
304  Add(item);
305  }
306 
307  /// <inheritdoc />
308  IEnumerator IEnumerable.GetEnumerator()
309  {
310  return GetEnumerator();
311  }
312 
313  #endregion
314  }
315 }
void IntersectWith(IEnumerable< T > other)
OrderedSet()
Initializes a new instance of the OrderedSet<T> class.
This is a minimal implementation of the missing HashSet from Silverlight BCL It's nowhere near the re...
Definition: HashSet.cs:8
bool IsSupersetOf(IEnumerable< T > other)
bool IsProperSubsetOf(IEnumerable< T > other)
OrderedSet(IEnumerable< T > items)
Initializes a new instance of the OrderedSet<T> class.
void CopyTo(T[] array, int arrayIndex)
void SymmetricExceptWith(IEnumerable< T > other)
bool IsProperSupersetOf(IEnumerable< T > other)
void RemoveAll(Func< T, bool > filter)