Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
KeyedCollection.cs
Go to the documentation of this file.
5
7
10[DebuggerDisplay("Count = {Count}")]
11[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
12public abstract class KeyedCollection<TKey, TItem> : Collection<TItem> where TKey : notnull
13{
15
17
18 private int keyCount;
19
20 private readonly int threshold;
21
22 private new List<TItem> Items => (List<TItem>)base.Items;
23
25
26 public TItem this[TKey key]
27 {
28 get
29 {
31 {
32 return item;
33 }
35 }
36 }
37
39
40 protected KeyedCollection()
41 : this((IEqualityComparer<TKey>?)null, 0)
42 {
43 }
44
49
60
61 public bool Contains(TKey key)
62 {
63 if (key == null)
64 {
65 throw new ArgumentNullException("key");
66 }
67 if (dict != null)
68 {
69 return dict.ContainsKey(key);
70 }
71 foreach (TItem item in Items)
72 {
74 {
75 return true;
76 }
77 }
78 return false;
79 }
80
81 public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TItem item)
82 {
83 if (key == null)
84 {
85 throw new ArgumentNullException("key");
86 }
87 if (dict != null)
88 {
89 return dict.TryGetValue(key, out item);
90 }
91 foreach (TItem item2 in Items)
92 {
94 if (keyForItem != null && comparer.Equals(key, keyForItem))
95 {
96 item = item2;
97 return true;
98 }
99 }
100 item = default(TItem);
101 return false;
102 }
103
104 private bool ContainsItem(TItem item)
105 {
106 TKey keyForItem;
107 if (dict == null || (keyForItem = GetKeyForItem(item)) == null)
108 {
109 return Items.Contains(item);
110 }
112 {
113 return EqualityComparer<TItem>.Default.Equals(value, item);
114 }
115 return false;
116 }
117
118 public bool Remove(TKey key)
119 {
120 if (key == null)
121 {
122 throw new ArgumentNullException("key");
123 }
124 if (dict != null)
125 {
127 {
128 return Remove(value);
129 }
130 return false;
131 }
132 for (int i = 0; i < Items.Count; i++)
133 {
134 if (comparer.Equals(GetKeyForItem(Items[i]), key))
135 {
136 RemoveItem(i);
137 return true;
138 }
139 }
140 return false;
141 }
142
143 protected void ChangeItemKey(TItem item, TKey newKey)
144 {
145 if (!ContainsItem(item))
146 {
148 }
151 {
152 if (newKey != null)
153 {
155 }
156 if (keyForItem != null)
157 {
159 }
160 }
161 }
162
163 protected override void ClearItems()
164 {
165 base.ClearItems();
166 dict?.Clear();
167 keyCount = 0;
168 }
169
170 protected abstract TKey GetKeyForItem(TItem item);
171
172 protected override void InsertItem(int index, TItem item)
173 {
175 if (keyForItem != null)
176 {
178 }
179 base.InsertItem(index, item);
180 }
181
182 protected override void RemoveItem(int index)
183 {
184 TKey keyForItem = GetKeyForItem(Items[index]);
185 if (keyForItem != null)
186 {
188 }
189 base.RemoveItem(index);
190 }
191
192 protected override void SetItem(int index, TItem item)
193 {
195 TKey keyForItem2 = GetKeyForItem(Items[index]);
197 {
198 if (keyForItem != null && dict != null)
199 {
201 }
202 }
203 else
204 {
205 if (keyForItem != null)
206 {
208 }
209 if (keyForItem2 != null)
210 {
212 }
213 }
214 base.SetItem(index, item);
215 }
216
217 private void AddKey(TKey key, TItem item)
218 {
219 if (dict != null)
220 {
221 dict.Add(key, item);
222 return;
223 }
224 if (keyCount == threshold)
225 {
227 dict.Add(key, item);
228 return;
229 }
230 if (Contains(key))
231 {
233 }
234 keyCount++;
235 }
236
237 private void CreateDictionary()
238 {
240 foreach (TItem item in Items)
241 {
243 if (keyForItem != null)
244 {
246 }
247 }
248 }
249
250 private void RemoveKey(TKey key)
251 {
252 if (dict != null)
253 {
254 dict.Remove(key);
255 }
256 else
257 {
258 keyCount--;
259 }
260 }
261}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
readonly IEqualityComparer< TKey > comparer
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TItem item)
KeyedCollection(IEqualityComparer< TKey >? comparer)
override void SetItem(int index, TItem item)
KeyedCollection(IEqualityComparer< TKey >? comparer, int dictionaryCreationThreshold)
override void InsertItem(int index, TItem item)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_AddingDuplicate
Definition SR.cs:14
static string ArgumentOutOfRange_InvalidThreshold
Definition SR.cs:14
static string Argument_ItemNotExist
Definition SR.cs:16
static string Arg_KeyNotFoundWithKey
Definition SR.cs:94
Definition SR.cs:7
new bool Equals(object? x, object? y)