-
Notifications
You must be signed in to change notification settings - Fork 936
/
Copy pathSetSnapShot.cs
180 lines (149 loc) · 3.33 KB
/
SetSnapShot.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections;
using System.Collections.Generic;
#if NETCOREAPP2_0_OR_GREATER
using System.Runtime.Serialization;
using System.Threading;
#endif
namespace NHibernate.Collection.Generic.SetHelpers
{
#if NETFX || NETSTANDARD2_0
// TODO 6.0: Consider removing this class in case we upgrade to .NET 4.7.2 and NET Standard 2.1,
// which have HashSet<T>.TryGetValue
[Serializable]
internal class SetSnapShot<T> : ICollection<T>, IReadOnlyCollection<T>, ICollection
{
private readonly Dictionary<T, T> _values;
private bool _hasNull;
public SetSnapShot(int capacity)
{
_values = new Dictionary<T, T>(capacity);
}
public SetSnapShot(IEnumerable<T> collection)
{
_values = new Dictionary<T, T>();
foreach (var item in collection)
{
if (item == null)
{
_hasNull = true;
}
else
{
_values[item] = item;
}
}
}
public bool TryGetValue(T equalValue, out T actualValue)
{
if (equalValue != null)
{
return _values.TryGetValue(equalValue, out actualValue);
}
actualValue = default(T);
return _hasNull;
}
public IEnumerator<T> GetEnumerator()
{
if (_hasNull)
{
yield return default(T);
}
foreach (var item in _values.Keys)
{
yield return item;
}
}
public void Add(T item)
{
if (item == null)
{
_hasNull = true;
return;
}
_values[item] = item;
}
public void Clear()
{
_values.Clear();
_hasNull = false;
}
public bool Contains(T item)
{
return item == null ? _hasNull : _values.ContainsKey(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
if (_hasNull)
array[arrayIndex] = default(T);
_values.Keys.CopyTo(array, arrayIndex + (_hasNull ? 1 : 0));
}
public bool Remove(T item)
{
if (item != null)
{
return _values.Remove(item);
}
if (!_hasNull)
{
return false;
}
_hasNull = false;
return true;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
void ICollection.CopyTo(Array array, int index)
{
if (!(array is T[] typedArray))
{
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array));
}
CopyTo(typedArray, index);
}
public int Count => _values.Count + (_hasNull ? 1 : 0);
public bool IsReadOnly => ((ICollection<KeyValuePair<T, T>>) _values).IsReadOnly;
public object SyncRoot => ((ICollection) _values).SyncRoot;
public bool IsSynchronized => ((ICollection) _values).IsSynchronized;
}
#endif
#if NETCOREAPP2_0_OR_GREATER
[Serializable]
internal class SetSnapShot<T> : HashSet<T>, ICollection
{
[NonSerialized]
private object _syncRoot;
public SetSnapShot(int capacity) : base(capacity)
{
}
public SetSnapShot(IEnumerable<T> collection) : base(collection)
{
}
protected SetSnapShot(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
void ICollection.CopyTo(Array array, int index)
{
if (!(array is T[] typedArray))
{
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array));
}
CopyTo(typedArray, index);
}
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot
{
get
{
if (_syncRoot == null)
{
Interlocked.CompareExchange<object>(ref _syncRoot, new object(), null);
}
return _syncRoot;
}
}
}
#endif
}