forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadDictionary{TKey,TValue}.cs
131 lines (102 loc) · 4.67 KB
/
ReadDictionary{TKey,TValue}.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
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace System.Collections.Generic
{
public readonly struct ReadDictionary<TKey, TValue> :
IReadOnlyDictionary<TKey, TValue>,
IEquatableReadOnlyStruct<ReadDictionary<TKey, TValue>>,
IDeserializationCallback
{
private readonly Dictionary<TKey, TValue> source;
private readonly bool hasSource;
public TValue this[TKey key]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetSource()[key];
}
public ReadCollection<TKey> Keys
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new ReadCollection<TKey>(GetSource().Keys);
}
public ReadCollection<TValue> Values
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new ReadCollection<TValue>(GetSource().Values);
}
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetSource().Keys;
}
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetSource().Values;
}
public int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetSource().Count;
}
public ReadDictionary(Dictionary<TKey, TValue> source)
{
this.source = source ?? _empty;
this.hasSource = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Dictionary<TKey, TValue> GetSource()
=> this.hasSource ? (this.source ?? _empty) : _empty;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
=> GetSource().GetHashCode();
public override bool Equals(object obj)
=> obj is ReadDictionary<TKey, TValue> other && Equals(in other);
public bool Equals(ReadDictionary<TKey, TValue> other)
{
var source = GetSource();
var otherSource = other.GetSource();
return source == otherSource;
}
public bool Equals(in ReadDictionary<TKey, TValue> other)
{
var source = GetSource();
var otherSource = other.GetSource();
return source == otherSource;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ContainsKey(TKey key)
=> GetSource().ContainsKey(key);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ContainsValue(TValue value)
=> GetSource().ContainsValue(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetValue(TKey key, out TValue value)
=> GetSource().TryGetValue(key, out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
=> GetSource().GetObjectData(info, context);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void OnDeserialization(object sender)
=> GetSource().OnDeserialization(sender);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Dictionary<TKey, TValue>.Enumerator GetEnumerator()
=> GetSource().GetEnumerator();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
=> GetEnumerator();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
private static Dictionary<TKey, TValue> _empty { get; } = new Dictionary<TKey, TValue>(0);
public static ReadDictionary<TKey, TValue> Empty { get; } = new ReadDictionary<TKey, TValue>(_empty);
public static implicit operator ReadDictionary<TKey, TValue>(Dictionary<TKey, TValue> source)
=> source == null ? Empty : new ReadDictionary<TKey, TValue>(source);
public static implicit operator ReadCollection<KeyValuePair<TKey, TValue>>(in ReadDictionary<TKey, TValue> source)
=> new ReadCollection<KeyValuePair<TKey, TValue>>(source.GetSource());
public static bool operator ==(in ReadDictionary<TKey, TValue> a, in ReadDictionary<TKey, TValue> b)
=> a.Equals(in b);
public static bool operator !=(in ReadDictionary<TKey, TValue> a, in ReadDictionary<TKey, TValue> b)
=> !a.Equals(in b);
}
}