|
2 | 2 | using System.Collections; |
3 | 3 | using System.Collections.Generic; |
4 | 4 |
|
5 | | -namespace Open.Collections |
| 5 | +namespace Open.Collections; |
| 6 | + |
| 7 | +public class DictionaryToHashSetWrapper<T> : ISet<T> |
6 | 8 | { |
7 | | - public class DictionaryToHashSetWrapper<T> : ISet<T> |
8 | | - { |
9 | | - protected readonly IDictionary<T, bool> InternalSource; |
| 9 | + protected readonly IDictionary<T, bool> InternalSource; |
10 | 10 |
|
11 | | - // ReSharper disable once MemberCanBeProtected.Global |
12 | | - public DictionaryToHashSetWrapper(IDictionary<T, bool> source) => InternalSource = source; |
| 11 | + // ReSharper disable once MemberCanBeProtected.Global |
| 12 | + public DictionaryToHashSetWrapper(IDictionary<T, bool> source) => InternalSource = source; |
13 | 13 |
|
14 | | - /// <inheritdoc /> |
15 | | - public int Count |
16 | | - => InternalSource.Count; |
| 14 | + /// <inheritdoc /> |
| 15 | + public int Count |
| 16 | + => InternalSource.Count; |
17 | 17 |
|
18 | | - /// <inheritdoc /> |
19 | | - public bool IsReadOnly |
20 | | - => InternalSource.IsReadOnly; |
| 18 | + /// <inheritdoc /> |
| 19 | + public bool IsReadOnly |
| 20 | + => InternalSource.IsReadOnly; |
21 | 21 |
|
22 | | - /// <inheritdoc /> |
23 | | - public virtual bool Add(T item) |
| 22 | + /// <inheritdoc /> |
| 23 | + public virtual bool Add(T item) |
| 24 | + { |
| 25 | + if (InternalSource.ContainsKey(item)) |
| 26 | + return false; |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + InternalSource.Add(item, true); |
| 31 | + } |
| 32 | + catch |
24 | 33 | { |
25 | | - if (InternalSource.ContainsKey(item)) |
26 | | - return false; |
27 | | - |
28 | | - try |
29 | | - { |
30 | | - InternalSource.Add(item, true); |
31 | | - } |
32 | | - catch |
33 | | - { |
34 | | - return false; |
35 | | - } |
36 | | - return true; |
| 34 | + return false; |
37 | 35 | } |
| 36 | + return true; |
| 37 | + } |
38 | 38 |
|
39 | | - /// <inheritdoc /> |
40 | | - public bool Remove(T item) => InternalSource.Remove(item); |
| 39 | + /// <inheritdoc /> |
| 40 | + public bool Remove(T item) => InternalSource.Remove(item); |
41 | 41 |
|
42 | | - /// <inheritdoc /> |
43 | | - public void Clear() => InternalSource.Clear(); |
| 42 | + /// <inheritdoc /> |
| 43 | + public void Clear() => InternalSource.Clear(); |
44 | 44 |
|
45 | | - /// <inheritdoc /> |
46 | | - public bool Contains(T item) => InternalSource.ContainsKey(item); |
| 45 | + /// <inheritdoc /> |
| 46 | + public bool Contains(T item) => InternalSource.ContainsKey(item); |
47 | 47 |
|
48 | | - /// <inheritdoc /> |
49 | | - public void CopyTo(T[] array, int arrayIndex) => InternalSource.Keys.CopyTo(array, arrayIndex); |
| 48 | + /// <inheritdoc /> |
| 49 | + public void CopyTo(T[] array, int arrayIndex) => InternalSource.Keys.CopyTo(array, arrayIndex); |
50 | 50 |
|
51 | | - /// <inheritdoc cref="ReadOnlyCollectionWrapper{T, TCollection}.CopyTo(Span{T})"/> |
52 | | - public virtual Span<T> CopyTo(Span<T> span) => InternalSource.Keys.CopyToSpan(span); |
| 51 | + /// <inheritdoc cref="ReadOnlyCollectionWrapper{T, TCollection}.CopyTo(Span{T})"/> |
| 52 | + public virtual Span<T> CopyTo(Span<T> span) => InternalSource.Keys.CopyToSpan(span); |
53 | 53 |
|
54 | | - /// <summary> |
55 | | - /// Returns a copy of the underlying keys. |
56 | | - /// </summary> |
57 | | - public HashSet<T> ToHashSet() => new(InternalSource.Keys); |
| 54 | + /// <summary> |
| 55 | + /// Returns a copy of the underlying keys. |
| 56 | + /// </summary> |
| 57 | + public HashSet<T> ToHashSet() => new(InternalSource.Keys); |
58 | 58 |
|
59 | | - /// <inheritdoc /> |
60 | | - public IEnumerator<T> GetEnumerator() => InternalSource.Keys.GetEnumerator(); |
| 59 | + /// <inheritdoc /> |
| 60 | + public IEnumerator<T> GetEnumerator() => InternalSource.Keys.GetEnumerator(); |
61 | 61 |
|
62 | | - /// <inheritdoc /> |
63 | | - public void ExceptWith(IEnumerable<T> other) |
64 | | - { |
65 | | - foreach (var e in other) Remove(e); |
66 | | - } |
| 62 | + /// <inheritdoc /> |
| 63 | + public void ExceptWith(IEnumerable<T> other) |
| 64 | + { |
| 65 | + foreach (var e in other) Remove(e); |
| 66 | + } |
67 | 67 |
|
68 | | - /// <inheritdoc /> |
69 | | - public void IntersectWith(IEnumerable<T> other) |
| 68 | + /// <inheritdoc /> |
| 69 | + public void IntersectWith(IEnumerable<T> other) |
| 70 | + { |
| 71 | + foreach (var e in other) |
70 | 72 | { |
71 | | - foreach (var e in other) |
72 | | - { |
73 | | - if (!InternalSource.ContainsKey(e)) |
74 | | - Remove(e); |
75 | | - } |
| 73 | + if (!InternalSource.ContainsKey(e)) |
| 74 | + Remove(e); |
76 | 75 | } |
| 76 | + } |
77 | 77 |
|
78 | | - /// <inheritdoc /> |
79 | | - public bool IsProperSubsetOf(IEnumerable<T> other) => ToHashSet().IsProperSubsetOf(other); |
| 78 | + /// <inheritdoc /> |
| 79 | + public bool IsProperSubsetOf(IEnumerable<T> other) => ToHashSet().IsProperSubsetOf(other); |
80 | 80 |
|
81 | | - /// <inheritdoc /> |
82 | | - public bool IsProperSupersetOf(IEnumerable<T> other) => ToHashSet().IsProperSupersetOf(other); |
| 81 | + /// <inheritdoc /> |
| 82 | + public bool IsProperSupersetOf(IEnumerable<T> other) => ToHashSet().IsProperSupersetOf(other); |
83 | 83 |
|
84 | | - /// <inheritdoc /> |
85 | | - public bool IsSubsetOf(IEnumerable<T> other) => ToHashSet().IsSubsetOf(other); |
| 84 | + /// <inheritdoc /> |
| 85 | + public bool IsSubsetOf(IEnumerable<T> other) => ToHashSet().IsSubsetOf(other); |
86 | 86 |
|
87 | | - /// <inheritdoc /> |
88 | | - public bool IsSupersetOf(IEnumerable<T> other) => ToHashSet().IsSupersetOf(other); |
| 87 | + /// <inheritdoc /> |
| 88 | + public bool IsSupersetOf(IEnumerable<T> other) => ToHashSet().IsSupersetOf(other); |
89 | 89 |
|
90 | | - /// <inheritdoc /> |
91 | | - public bool Overlaps(IEnumerable<T> other) => ToHashSet().Overlaps(other); |
| 90 | + /// <inheritdoc /> |
| 91 | + public bool Overlaps(IEnumerable<T> other) => ToHashSet().Overlaps(other); |
92 | 92 |
|
93 | | - /// <inheritdoc /> |
94 | | - public bool SetEquals(IEnumerable<T> other) => ToHashSet().SetEquals(other); |
| 93 | + /// <inheritdoc /> |
| 94 | + public bool SetEquals(IEnumerable<T> other) => ToHashSet().SetEquals(other); |
95 | 95 |
|
96 | | - /// <inheritdoc /> |
97 | | - public void SymmetricExceptWith(IEnumerable<T> other) |
| 96 | + /// <inheritdoc /> |
| 97 | + public void SymmetricExceptWith(IEnumerable<T> other) |
| 98 | + { |
| 99 | + foreach (var e in other) |
98 | 100 | { |
99 | | - foreach (var e in other) |
100 | | - { |
101 | | - if (InternalSource.ContainsKey(e)) |
102 | | - Add(e); |
103 | | - else |
104 | | - Remove(e); |
105 | | - } |
| 101 | + if (InternalSource.ContainsKey(e)) |
| 102 | + Add(e); |
| 103 | + else |
| 104 | + Remove(e); |
106 | 105 | } |
| 106 | + } |
107 | 107 |
|
108 | | - /// <inheritdoc /> |
109 | | - public void UnionWith(IEnumerable<T> other) |
110 | | - { |
111 | | - foreach (var e in other) Add(e); |
112 | | - } |
| 108 | + /// <inheritdoc /> |
| 109 | + public void UnionWith(IEnumerable<T> other) |
| 110 | + { |
| 111 | + foreach (var e in other) Add(e); |
| 112 | + } |
113 | 113 |
|
114 | | - void ICollection<T>.Add(T item) => Add(item); |
| 114 | + void ICollection<T>.Add(T item) => Add(item); |
115 | 115 |
|
116 | | - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
117 | | - } |
| 116 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
118 | 117 | } |
0 commit comments