ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/OniSplit/Collections/Set.cs
Revision: 1114
Committed: Wed Jan 22 14:08:57 2020 UTC (5 years, 8 months ago) by iritscen
File size: 1094 byte(s)
Log Message:
Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File Contents

# Content
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 }