-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeferredHashSet.cs
More file actions
50 lines (43 loc) · 1.54 KB
/
Copy pathDeferredHashSet.cs
File metadata and controls
50 lines (43 loc) · 1.54 KB
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
using System;
using System.Collections.Generic;
namespace Open.RandomizationExtensions;
/// <summary>
/// A <see cref="HashSet{T}"/> that fills itself lazily from an enumerator as
/// <see cref="Contains(T)"/> is called.
/// </summary>
/// <remarks>
/// IMPORTANT: <see cref="Contains(T)"/> HIDES (<see langword="new"/>) rather than
/// overrides the base method, so it only executes when called through a
/// <see cref="DeferredHashSet{T}"/>-typed reference. A call through
/// <see cref="ISet{T}"/> or <see cref="HashSet{T}"/> bypasses the lazy pump and
/// consults only what has already been materialized. Likewise <see cref="HashSet{T}.Count"/>
/// reflects only what has been pumped so far. Callers must not use Count-based
/// fast paths against an instance of this type.
/// </remarks>
sealed class DeferredHashSet<T>(IEnumerator<T> source) : HashSet<T>, IDisposable
{
public DeferredHashSet(IEnumerable<T> source)
: this((source ?? throw new ArgumentNullException(nameof(source))).GetEnumerator()) { }
private readonly IEnumerator<T> Source = source ?? throw new ArgumentNullException(nameof(source));
public new bool Contains(T item)
{
if (base.Contains(item))
return true;
// Use the same comparer as the base set so pump-time matches agree with
// what base.Contains would later report.
var comparer = EqualityComparer<T>.Default;
while (Source.MoveNext())
{
var i = Source.Current;
_ = Add(i);
if (comparer.Equals(item, i))
return true;
}
return false;
}
public void Dispose()
{
Source.Dispose();
Clear();
}
}