-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDictionaryToHashSetWrapper.cs
140 lines (114 loc) · 3.24 KB
/
DictionaryToHashSetWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Open.Collections;
/// <summary>
/// A wrapper for a <see cref="IDictionary{TKey, TValue}"/> to implement <see cref="ISet{T}"/>.
/// </summary>
[method: ExcludeFromCodeCoverage]
public class DictionaryToHashSetWrapper<T>(
IDictionary<T, bool> source)
: ISet<T>
{
/// <summary>
/// The internal source dictionary.
/// </summary>
protected readonly IDictionary<T, bool> InternalSource = source;
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public int Count
=> InternalSource.Count;
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public bool IsReadOnly
=> InternalSource.IsReadOnly;
/// <inheritdoc />
public virtual bool Add(T item)
{
var source = InternalSource;
if (source.ContainsKey(item))
return false;
try
{
source.Add(item, true);
}
catch
{
return false;
}
return true;
}
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public bool Remove(T item) => InternalSource.Remove(item);
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public void Clear() => InternalSource.Clear();
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public bool Contains(T item) => InternalSource.ContainsKey(item);
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public void CopyTo(T[] array, int arrayIndex) => InternalSource.Keys.CopyTo(array, arrayIndex);
/// <inheritdoc cref="ReadOnlyCollectionWrapper{T, TCollection}.CopyTo(Span{T})"/>
[ExcludeFromCodeCoverage]
public virtual Span<T> CopyTo(Span<T> span) => InternalSource.Keys.CopyToSpan(span);
/// <summary>
/// Returns a copy of the underlying keys.
/// </summary>
[ExcludeFromCodeCoverage]
public HashSet<T> ToHashSet() => new(InternalSource.Keys);
/// <inheritdoc />
[ExcludeFromCodeCoverage]
public IEnumerator<T> GetEnumerator()
=> InternalSource.Keys.GetEnumerator();
/// <inheritdoc />
public void ExceptWith(IEnumerable<T> other)
{
foreach (T? e in other) Remove(e);
}
/// <inheritdoc />
public void IntersectWith(IEnumerable<T> other)
{
var source = InternalSource;
foreach (T? e in other)
{
if (!source.ContainsKey(e))
Remove(e);
}
}
/// <inheritdoc />
public bool IsProperSubsetOf(IEnumerable<T> other) => ToHashSet().IsProperSubsetOf(other);
/// <inheritdoc />
public bool IsProperSupersetOf(IEnumerable<T> other) => ToHashSet().IsProperSupersetOf(other);
/// <inheritdoc />
public bool IsSubsetOf(IEnumerable<T> other) => ToHashSet().IsSubsetOf(other);
/// <inheritdoc />
public bool IsSupersetOf(IEnumerable<T> other) => ToHashSet().IsSupersetOf(other);
/// <inheritdoc />
public bool Overlaps(IEnumerable<T> other) => ToHashSet().Overlaps(other);
/// <inheritdoc />
public bool SetEquals(IEnumerable<T> other) => ToHashSet().SetEquals(other);
/// <inheritdoc />
public void SymmetricExceptWith(IEnumerable<T> other)
{
var source = InternalSource;
foreach (T? e in other)
{
if (source.ContainsKey(e))
Add(e);
else
Remove(e);
}
}
/// <inheritdoc />
public void UnionWith(IEnumerable<T> other)
{
foreach (T? e in other) Add(e);
}
[ExcludeFromCodeCoverage]
void ICollection<T>.Add(T item) => Add(item);
[ExcludeFromCodeCoverage]
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}