-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReadCollection{T}.cs
71 lines (53 loc) · 2.28 KB
/
ReadCollection{T}.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
using System.Runtime.CompilerServices;
namespace System.Collections.Generic
{
public readonly struct ReadCollection<T> : IReadOnlyCollection<T>, IEquatableReadOnlyStruct<ReadCollection<T>>
{
private readonly ICollection<T> source;
private readonly bool hasSource;
public int Count => GetSource().Count;
public ReadCollection(ICollection<T> source)
{
this.source = source ?? _empty;
this.hasSource = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ICollection<T> GetSource()
=> this.hasSource ? (this.source ?? _empty) : _empty;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
=> GetSource().GetHashCode();
public override bool Equals(object obj)
=> obj is ReadCollection<T> other && Equals(in other);
public bool Equals(ReadCollection<T> other)
{
var source = GetSource();
var otherSource = other.GetSource();
return source == otherSource;
}
public bool Equals(in ReadCollection<T> other)
{
var source = GetSource();
var otherSource = other.GetSource();
return source == otherSource;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(T item)
=> GetSource().Contains(item);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(T[] array, int arrayIndex)
=> GetSource().CopyTo(array, arrayIndex);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IEnumerator<T> GetEnumerator()
=> GetSource().GetEnumerator();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
private static ICollection<T> _empty { get; } = new T[0];
public static ReadCollection<T> Empty { get; } = new ReadCollection<T>(_empty);
public static bool operator ==(in ReadCollection<T> a, in ReadCollection<T> b)
=> a.Equals(in b);
public static bool operator !=(in ReadCollection<T> a, in ReadCollection<T> b)
=> !a.Equals(in b);
}
}