source: OniSplit/Collections/Set.cs

Last change on this file was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace 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}
Note: See TracBrowser for help on using the repository browser.