-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultDictionary.cs
More file actions
143 lines (112 loc) · 4.35 KB
/
Copy pathDefaultDictionary.cs
File metadata and controls
143 lines (112 loc) · 4.35 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Celeste.Mod.BetterRefillGemsPlus
{
internal class DefaultDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDictionary<TKey, TValue>, IDeserializationCallback, ISerializable where TKey : notnull
{
private readonly Dictionary<TKey, TValue> _cache = [];
public readonly Func<TKey, TValue> DefaultValue;
public DefaultDictionary(Func<TKey, TValue> defaultValue)
{
DefaultValue = defaultValue;
}
public TValue this[TKey key]
{
get
{
if (!_cache.TryGetValue(key, out var value))
{
value = _cache[key] = DefaultValue(key);
}
return value;
}
set
{
((IDictionary<TKey, TValue>)_cache)[key] = value;
}
}
public int Count => ((ICollection<KeyValuePair<TKey, TValue>>)_cache).Count;
public bool IsReadOnly => ((ICollection<KeyValuePair<TKey, TValue>>)_cache).IsReadOnly;
public IEnumerable<TKey> Keys => ((IReadOnlyDictionary<TKey, TValue>)_cache).Keys;
public IEnumerable<TValue> Values => ((IReadOnlyDictionary<TKey, TValue>)_cache).Values;
public bool IsSynchronized => ((ICollection)_cache).IsSynchronized;
public object SyncRoot => ((ICollection)_cache).SyncRoot;
public bool IsFixedSize => ((IDictionary)_cache).IsFixedSize;
ICollection<TKey> IDictionary<TKey, TValue>.Keys => ((IDictionary<TKey, TValue>)_cache).Keys;
ICollection<TValue> IDictionary<TKey, TValue>.Values => ((IDictionary<TKey, TValue>)_cache).Values;
public void Add(KeyValuePair<TKey, TValue> item)
{
((ICollection<KeyValuePair<TKey, TValue>>)_cache).Add(item);
}
public void Add(TKey key, TValue value)
{
((IDictionary<TKey, TValue>)_cache).Add(key, value);
}
public void Add(object key, object value)
{
((IDictionary)_cache).Add(key, value);
}
public void Clear()
{
((ICollection<KeyValuePair<TKey, TValue>>)_cache).Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_cache).Contains(item);
}
public bool Contains(object key)
{
return ((IDictionary)_cache).Contains(key);
}
public bool ContainsKey(TKey key)
{
return ((IReadOnlyDictionary<TKey, TValue>)_cache).ContainsKey(key);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<TKey, TValue>>)_cache).CopyTo(array, arrayIndex);
}
public void CopyTo(Array array, int index)
{
((ICollection)_cache).CopyTo(array, index);
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return ((IEnumerable<KeyValuePair<TKey, TValue>>)_cache).GetEnumerator();
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
((ISerializable)_cache).GetObjectData(info, context);
}
public void OnDeserialization(object sender)
{
((IDeserializationCallback)_cache).OnDeserialization(sender);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_cache).Remove(item);
}
public bool Remove(TKey key)
{
return ((IDictionary<TKey, TValue>)_cache).Remove(key);
}
public void Remove(object key)
{
((IDictionary)_cache).Remove(key);
}
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{
return ((IReadOnlyDictionary<TKey, TValue>)_cache).TryGetValue(key, out value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_cache).GetEnumerator();
}
}
}