-
Notifications
You must be signed in to change notification settings - Fork 0
/
CcHashSet.cs
341 lines (276 loc) · 10.1 KB
/
CcHashSet.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Copyright <2021> <Thorben Linneweber>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
namespace HashSetDemo
{
/// <summary>
/// Concurrent HashSet implementation.
/// </summary>
public class CcHashSet<T> : IEnumerable<T> where T : IEquatable<T>
{
private class HashSet
{
public struct Node
{
public int Hash;
public int Next;
public T Data;
public void Set(int hash, int next, ref T data)
{
this.Data = data;
this.Hash = hash;
this.Next = next;
}
}
private static readonly int[] bucketsSizeArray = {
1367, 2741, 5471, 10_937, 19_841, 40_241, 84_463, 174_767,
349_529, 699_053, 1_398_107, 2_796_221, 5_592_407,
11_184_829, 22_369_661, 44_739_259, 89_478_503,
178_956_983, 357_913_951, 715_827_947,
1_431_655_777, 2_147_483_629 };
private int[] slots;
public Node[] nodes;
public const int NullNode = 0;
private IEqualityComparer<T> comparer;
private int bucketSize = 0;
private int freeNodes;
public int nodePointer;
public HashSet()
{
slots = new int[bucketsSizeArray[bucketSize]];
nodes = new Node[bucketsSizeArray[bucketSize]];
comparer = EqualityComparer<T>.Default;
nodePointer = 1; freeNodes = 0;
}
public void Clear()
{
Array.Clear(slots, 0, slots.Length);
Array.Clear(nodes, 0, nodes.Length);
nodePointer = 1; freeNodes = 0;
}
public bool Add(T item, int hash)
{
if (8 * (nodePointer - freeNodes) > slots.Length * 6)
Resize();
int hashms = hash % slots.Length;
for(int nidx = slots[hashms]; nidx != NullNode; nidx = nodes[nidx].Next)
{
if (nodes[nidx].Hash == hash && comparer.Equals(nodes[nidx].Data, item))
{
return false; // object already in set
}
}
int nn = AllocateNode();
nodes[nn].Set(hash, slots[hashms], ref item);
slots[hashms] = nn;
return true;
}
public bool Contains(T item, int hash)
{
int hashms = hash % slots.Length;
for(int nidx = slots[hashms]; nidx != NullNode; nidx = nodes[nidx].Next)
{
if (nodes[nidx].Hash == hash && comparer.Equals(nodes[nidx].Data, item))
{
return true; // object already in set
}
}
return false;
}
public bool Remove(T item, int hash)
{
int hashms = hash % slots.Length;
int current = slots[hashms];
if (current == NullNode) return false;
if (nodes[current].Hash == hash && comparer.Equals(nodes[current].Data, item))
{
slots[hashms] = nodes[current].Next;
FreeNode(current);
return true;
}
int next;
while ((next = nodes[current].Next) != NullNode)
{
if (nodes[next].Hash == hash && comparer.Equals(nodes[next].Data, item))
{
nodes[current].Next = nodes[next].Next;
FreeNode(next);
return true;
}
current = next;
}
return false;
}
private void Resize()
{
// A resize event does only take place when
// there are no freenodes in the freenode buffer.
System.Diagnostics.Debug.Assert(freeNodes == 0);
slots = new int[bucketsSizeArray[++bucketSize]];
Array.Resize<Node>(ref nodes, bucketsSizeArray[bucketSize]);
for (int i = 1; i < nodePointer; i++)
{
int modhash = nodes[i].Hash % slots.Length;
nodes[i].Next = slots[modhash];
slots[modhash] = i;
}
}
public int Count
{
get => nodePointer - freeNodes - 1;
}
private int AllocateNode()
{
int next = nodes[0].Next;
if (next == NullNode)
{
return nodePointer++;
}
else
{
freeNodes--;
nodes[0].Next = nodes[next].Next;
return next;
}
}
private void FreeNode(int node)
{
freeNodes++;
int oldnext = nodes[0].Next;
nodes[0].Next = node;
nodes[node].Next = oldnext;
nodes[node].Hash = 0;
}
}
private HashSet[] sets = new HashSet[NumSets];
private const int NumSets = 17;
private IEqualityComparer<T> hasher;
public CcHashSet()
{
hasher = EqualityComparer<T>.Default;
for (int i = 0; i < NumSets; i++)
sets[i] = new HashSet();
}
/// <summary>
/// Adds an item to the set.
/// </summary>
/// <param name="item">Item to add.</param>
/// <returns>Return false, if the set already contains the item. Otherwise true.</returns>
public bool Add(T item)
{
int hc = hasher.GetHashCode(item) & 0x7FFFFFFF;
int idx = hc % NumSets;
lock (sets[idx]) { return sets[idx].Add(item, hc); }
}
/// <summary>
/// Removes an item from the set.
/// </summary>
/// <param name="item">Item to remove.</param>
/// <returns>Return false, if the set does not contain the item. Otherwise true.</returns>
public bool Remove(T item)
{
int hc = hasher.GetHashCode(item) & 0x7FFFFFFF;
int idx = hc % NumSets;
lock (sets[idx]) { return sets[idx].Remove(item, hc); }
}
/// <summary>
/// Checks wether an item is in the hashset. This method is not thread-safe.
/// </summary>
/// <param name="item">Item to check.</param>
/// <returns>True, if the item is in the set. Otherwise false.</returns>
public bool Contains(T item)
{
int hc = hasher.GetHashCode(item) & 0x7FFFFFFF;
int idx = hc % NumSets;
return sets[idx].Contains(item, hc);
}
/// <summary>
/// Returns the current number of elements in the set.
/// </summary>
/// <value>Number of elements.</value>
public int Count
{
get
{
int count = 0;
foreach(var hs in sets) count += hs.Count;
return count;
}
}
/// <summary>
/// Clears the hashset. Internal data structures keep their sizes.
/// This method is not thread-safe.
/// </summary>
public void Clear()
{
foreach(var hs in sets) hs.Clear();
}
public IEnumerator<T> GetEnumerator()
{
return new CcHashSetEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new CcHashSetEnumerator(this);
}
/// <summary>
/// Enumerates a CcHashSet.
/// </summary>
public class CcHashSetEnumerator : IEnumerator<T>
{
CcHashSet<T> hashSet;
int index1;
int index2;
public CcHashSetEnumerator(CcHashSet<T> hashSet)
{
this.hashSet = hashSet;
}
object IEnumerator.Current => hashSet.sets[index1].nodes[index2].Data;
T IEnumerator<T>.Current => hashSet.sets[index1].nodes[index2].Data;
public void Dispose() { }
internal bool MoveNext()
{
while (index1 < hashSet.sets.Length)
{
while (index2++ < hashSet.sets[index1].nodePointer)
{
if (hashSet.sets[index1].nodes[index2].Hash != 0) return true;
}
index1++;
index2 = 0;
}
return false;
}
public void Reset()
{
index1 = 0;
index2 = 0;
}
bool IEnumerator.MoveNext()
{
return this.MoveNext();
}
}
}
}