1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
|
4 |
namespace Oni.Collections |
5 |
{ |
6 |
internal class Set<T> : IEnumerable<T> |
7 |
{ |
8 |
private readonly Dictionary<T, int> set; |
9 |
|
10 |
public Set() |
11 |
{ |
12 |
set = new Dictionary<T, int>(); |
13 |
} |
14 |
|
15 |
public Set(IEqualityComparer<T> comparer) |
16 |
{ |
17 |
set = new Dictionary<T, int>(comparer); |
18 |
} |
19 |
|
20 |
public bool Add(T t) |
21 |
{ |
22 |
if (set.ContainsKey(t)) |
23 |
return false; |
24 |
|
25 |
set.Add(t, 0); |
26 |
return true; |
27 |
} |
28 |
|
29 |
public bool Contains(T t) => set.ContainsKey(t); |
30 |
|
31 |
public int Count => set.Count; |
32 |
|
33 |
public void UnionWith(IEnumerable<T> with) |
34 |
{ |
35 |
foreach (T t in with) |
36 |
set[t] = 0; |
37 |
} |
38 |
|
39 |
public IEnumerator<T> GetEnumerator() |
40 |
{ |
41 |
foreach (var pair in set) |
42 |
yield return pair.Key; |
43 |
} |
44 |
|
45 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); |
46 |
} |
47 |
} |