Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SortedDictionary.cs
Go to the documentation of this file.
4
6
8[DebuggerTypeProxy(typeof(System.Collections.Generic.IDictionaryDebugView<, >))]
9[DebuggerDisplay("Count = {Count}")]
10[TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
11public class SortedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>> where TKey : notnull
12{
13 public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDisposable, IEnumerator, IDictionaryEnumerator
14 {
16
17 private readonly int _getEnumeratorRetType;
18
20
21 internal bool NotStartedOrEnded => _treeEnum.NotStartedOrEnded;
22
23 object? IEnumerator.Current
24 {
25 get
26 {
28 {
30 }
31 if (_getEnumeratorRetType == 2)
32 {
33 return new DictionaryEntry(Current.Key, Current.Value);
34 }
35 return new KeyValuePair<TKey, TValue>(Current.Key, Current.Value);
36 }
37 }
38
39 object IDictionaryEnumerator.Key
40 {
41 get
42 {
44 {
46 }
47 return Current.Key;
48 }
49 }
50
51 object? IDictionaryEnumerator.Value
52 {
53 get
54 {
56 {
58 }
59 return Current.Value;
60 }
61 }
62
63 DictionaryEntry IDictionaryEnumerator.Entry
64 {
65 get
66 {
68 {
70 }
71 return new DictionaryEntry(Current.Key, Current.Value);
72 }
73 }
74
80
81 public bool MoveNext()
82 {
83 return _treeEnum.MoveNext();
84 }
85
86 public void Dispose()
87 {
88 _treeEnum.Dispose();
89 }
90
91 internal void Reset()
92 {
93 _treeEnum.Reset();
94 }
95
97 {
98 _treeEnum.Reset();
99 }
100 }
101
103 [DebuggerDisplay("Count = {Count}")]
105 {
107 {
109
110 public TKey Current => _dictEnum.Current.Key;
111
112 object? IEnumerator.Current
113 {
114 get
115 {
116 if (_dictEnum.NotStartedOrEnded)
117 {
119 }
120 return Current;
121 }
122 }
123
128
129 public void Dispose()
130 {
131 _dictEnum.Dispose();
132 }
133
134 public bool MoveNext()
135 {
136 return _dictEnum.MoveNext();
137 }
138
140 {
141 _dictEnum.Reset();
142 }
143 }
144
146
147 public int Count => _dictionary.Count;
148
150
151 bool ICollection.IsSynchronized => false;
152
153 object ICollection.SyncRoot => ((ICollection)_dictionary).SyncRoot;
154
156 {
157 if (dictionary == null)
158 {
159 throw new ArgumentNullException("dictionary");
160 }
162 }
163
165 {
166 return new Enumerator(_dictionary);
167 }
168
173
178
179 public void CopyTo(TKey[] array, int index)
180 {
181 TKey[] array2 = array;
182 if (array2 == null)
183 {
184 throw new ArgumentNullException("array");
185 }
186 if (index < 0)
187 {
189 }
190 if (array2.Length - index < Count)
191 {
193 }
195 {
196 array2[index++] = node.Item.Key;
197 return true;
198 });
199 }
200
202 {
203 if (array == null)
204 {
205 throw new ArgumentNullException("array");
206 }
207 if (array.Rank != 1)
208 {
210 }
211 if (array.GetLowerBound(0) != 0)
212 {
214 }
215 if (index < 0)
216 {
218 }
219 if (array.Length - index < _dictionary.Count)
220 {
222 }
223 if (array is TKey[] array2)
224 {
226 return;
227 }
228 try
229 {
230 object[] objects = (object[])array;
232 {
233 objects[index++] = node.Item.Key;
234 return true;
235 });
236 }
238 {
240 }
241 }
242
247
252
254 {
256 }
257
262 }
263
265 [DebuggerDisplay("Count = {Count}")]
266 public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, IEnumerable, ICollection, IReadOnlyCollection<TValue>
267 {
269 {
271
272 public TValue Current => _dictEnum.Current.Value;
273
274 object? IEnumerator.Current
275 {
276 get
277 {
278 if (_dictEnum.NotStartedOrEnded)
279 {
281 }
282 return Current;
283 }
284 }
285
290
291 public void Dispose()
292 {
293 _dictEnum.Dispose();
294 }
295
296 public bool MoveNext()
297 {
298 return _dictEnum.MoveNext();
299 }
300
302 {
303 _dictEnum.Reset();
304 }
305 }
306
308
309 public int Count => _dictionary.Count;
310
312
313 bool ICollection.IsSynchronized => false;
314
315 object ICollection.SyncRoot => ((ICollection)_dictionary).SyncRoot;
316
318 {
319 if (dictionary == null)
320 {
321 throw new ArgumentNullException("dictionary");
322 }
324 }
325
327 {
328 return new Enumerator(_dictionary);
329 }
330
335
340
341 public void CopyTo(TValue[] array, int index)
342 {
343 TValue[] array2 = array;
344 if (array2 == null)
345 {
346 throw new ArgumentNullException("array");
347 }
348 if (index < 0)
349 {
351 }
352 if (array2.Length - index < Count)
353 {
355 }
357 {
358 array2[index++] = node.Item.Value;
359 return true;
360 });
361 }
362
364 {
365 if (array == null)
366 {
367 throw new ArgumentNullException("array");
368 }
369 if (array.Rank != 1)
370 {
372 }
373 if (array.GetLowerBound(0) != 0)
374 {
376 }
377 if (index < 0)
378 {
380 }
381 if (array.Length - index < _dictionary.Count)
382 {
384 }
385 if (array is TValue[] array2)
386 {
388 return;
389 }
390 try
391 {
392 object[] objects = (object[])array;
394 {
395 objects[index++] = node.Item.Value;
396 return true;
397 });
398 }
400 {
402 }
403 }
404
409
414
416 {
418 }
419
424 }
425
426 [Serializable]
427 public sealed class KeyValuePairComparer : Comparer<KeyValuePair<TKey, TValue>>
428 {
430
432 {
433 if (keyComparer == null)
434 {
435 this.keyComparer = Comparer<TKey>.Default;
436 }
437 else
438 {
439 this.keyComparer = keyComparer;
440 }
441 }
442
444 {
445 return keyComparer.Compare(x.Key, y.Key);
446 }
447
448 public override bool Equals(object? obj)
449 {
451 {
452 if (keyComparer != keyValuePairComparer.keyComparer)
453 {
454 return keyComparer.Equals(keyValuePairComparer.keyComparer);
455 }
456 return true;
457 }
458 return false;
459 }
460
461 public override int GetHashCode()
462 {
463 return keyComparer.GetHashCode();
464 }
465 }
466
469
472
474
476
477 public TValue this[TKey key]
478 {
479 get
480 {
481 if (key == null)
482 {
483 throw new ArgumentNullException("key");
484 }
485 SortedSet<KeyValuePair<TKey, TValue>>.Node node = _set.FindNode(new KeyValuePair<TKey, TValue>(key, default(TValue)));
486 if (node == null)
487 {
489 }
490 return node.Item.Value;
491 }
492 set
493 {
494 if (key == null)
495 {
496 throw new ArgumentNullException("key");
497 }
498 SortedSet<KeyValuePair<TKey, TValue>>.Node node = _set.FindNode(new KeyValuePair<TKey, TValue>(key, default(TValue)));
499 if (node == null)
500 {
502 return;
503 }
504 node.Item = new KeyValuePair<TKey, TValue>(node.Item.Key, value);
505 _set.UpdateVersion();
506 }
507 }
508
509 public int Count => _set.Count;
510
512
514 {
515 get
516 {
517 if (_keys == null)
518 {
519 _keys = new KeyCollection(this);
520 }
521 return _keys;
522 }
523 }
524
526
528
530 {
531 get
532 {
533 if (_values == null)
534 {
535 _values = new ValueCollection(this);
536 }
537 return _values;
538 }
539 }
540
542
544
545 bool IDictionary.IsFixedSize => false;
546
547 bool IDictionary.IsReadOnly => false;
548
549 ICollection IDictionary.Keys => Keys;
550
551 ICollection IDictionary.Values => Values;
552
553 object? IDictionary.this[object key]
554 {
555 get
556 {
558 {
559 return value;
560 }
561 return null;
562 }
563 set
564 {
565 if (key == null)
566 {
567 throw new ArgumentNullException("key");
568 }
569 if (value == null && default(TValue) != null)
570 {
571 throw new ArgumentNullException("value");
572 }
573 try
574 {
575 TKey key2 = (TKey)key;
576 try
577 {
578 this[key2] = (TValue)value;
579 }
581 {
582 throw new ArgumentException(System.SR.Format(System.SR.Arg_WrongType, value, typeof(TValue)), "value");
583 }
584 }
586 {
587 throw new ArgumentException(System.SR.Format(System.SR.Arg_WrongType, key, typeof(TKey)), "key");
588 }
589 }
590 }
591
592 bool ICollection.IsSynchronized => false;
593
594 object ICollection.SyncRoot => ((ICollection)_set).SyncRoot;
595
597 : this((IComparer<TKey>?)null)
598 {
599 }
600
605
624
629
634
636 {
638 if (node == null)
639 {
640 return false;
641 }
642 if (keyValuePair.Value == null)
643 {
644 return node.Item.Value == null;
645 }
646 return EqualityComparer<TValue>.Default.Equals(node.Item.Value, keyValuePair.Value);
647 }
648
650 {
652 if (node == null)
653 {
654 return false;
655 }
656 if (EqualityComparer<TValue>.Default.Equals(node.Item.Value, keyValuePair.Value))
657 {
659 return true;
660 }
661 return false;
662 }
663
664 public void Add(TKey key, TValue value)
665 {
666 if (key == null)
667 {
668 throw new ArgumentNullException("key");
669 }
671 }
672
673 public void Clear()
674 {
675 _set.Clear();
676 }
677
678 public bool ContainsKey(TKey key)
679 {
680 if (key == null)
681 {
682 throw new ArgumentNullException("key");
683 }
684 return _set.Contains(new KeyValuePair<TKey, TValue>(key, default(TValue)));
685 }
686
687 public bool ContainsValue(TValue value)
688 {
689 TValue value2 = value;
690 bool found = false;
691 if (value2 == null)
692 {
694 {
695 if (node.Item.Value == null)
696 {
697 found = true;
698 return false;
699 }
700 return true;
701 });
702 }
703 else
704 {
707 {
708 if (valueComparer.Equals(node.Item.Value, value2))
709 {
710 found = true;
711 return false;
712 }
713 return true;
714 });
715 }
716 return found;
717 }
718
720 {
722 }
723
725 {
726 return new Enumerator(this, 1);
727 }
728
733
734 public bool Remove(TKey key)
735 {
736 if (key == null)
737 {
738 throw new ArgumentNullException("key");
739 }
740 return _set.Remove(new KeyValuePair<TKey, TValue>(key, default(TValue)));
741 }
742
743 public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
744 {
745 if (key == null)
746 {
747 throw new ArgumentNullException("key");
748 }
749 SortedSet<KeyValuePair<TKey, TValue>>.Node node = _set.FindNode(new KeyValuePair<TKey, TValue>(key, default(TValue)));
750 if (node == null)
751 {
752 value = default(TValue);
753 return false;
754 }
755 value = node.Item.Value;
756 return true;
757 }
758
763
764 void IDictionary.Add(object key, object value)
765 {
766 if (key == null)
767 {
768 throw new ArgumentNullException("key");
769 }
770 if (value == null && default(TValue) != null)
771 {
772 throw new ArgumentNullException("value");
773 }
774 try
775 {
776 TKey key2 = (TKey)key;
777 try
778 {
779 Add(key2, (TValue)value);
780 }
782 {
783 throw new ArgumentException(System.SR.Format(System.SR.Arg_WrongType, value, typeof(TValue)), "value");
784 }
785 }
787 {
788 throw new ArgumentException(System.SR.Format(System.SR.Arg_WrongType, key, typeof(TKey)), "key");
789 }
790 }
791
793 {
794 if (IsCompatibleKey(key))
795 {
796 return ContainsKey((TKey)key);
797 }
798 return false;
799 }
800
801 private static bool IsCompatibleKey(object key)
802 {
803 if (key == null)
804 {
805 throw new ArgumentNullException("key");
806 }
807 return key is TKey;
808 }
809
811 {
812 return new Enumerator(this, 2);
813 }
814
815 void IDictionary.Remove(object key)
816 {
817 if (IsCompatibleKey(key))
818 {
819 Remove((TKey)key);
820 }
821 }
822
824 {
825 return new Enumerator(this, 1);
826 }
827}
IEqualityComparer< TKey > Comparer
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
bool ICollection< KeyValuePair< TKey, TValue > >. IsReadOnly
void Add(TKey key, TValue value)
readonly SortedDictionary< TKey, TValue > _dictionary
KeyCollection(SortedDictionary< TKey, TValue > dictionary)
override int Compare(KeyValuePair< TKey, TValue > x, KeyValuePair< TKey, TValue > y)
ValueCollection(SortedDictionary< TKey, TValue > dictionary)
readonly SortedDictionary< TKey, TValue > _dictionary
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
SortedDictionary(IComparer< TKey >? comparer)
IEnumerable< TValue > IReadOnlyDictionary< TKey, TValue >. Values
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
ICollection< TKey > IDictionary< TKey, TValue >. Keys
ICollection< TValue > IDictionary< TKey, TValue >. Values
IEnumerable< TKey > IReadOnlyDictionary< TKey, TValue >. Keys
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
SortedDictionary(IDictionary< TKey, TValue > dictionary, IComparer< TKey >? comparer)
readonly TreeSet< KeyValuePair< TKey, TValue > > _set
SortedDictionary(IDictionary< TKey, TValue > dictionary)
static string Arg_WrongType
Definition SR.cs:16
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Arg_ArrayPlusOffTooSmall
Definition SR.cs:16
static string Argument_InvalidArrayType
Definition SR.cs:54
static string Arg_NonZeroLowerBound
Definition SR.cs:14
static string Arg_RankMultiDimNotSupported
Definition SR.cs:18
static string NotSupported_ValueCollectionSet
Definition SR.cs:42
static string InvalidOperation_EnumOpCantHappen
Definition SR.cs:48
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string Arg_KeyNotFoundWithKey
Definition SR.cs:94
static string NotSupported_KeyCollectionSet
Definition SR.cs:50
Definition SR.cs:7
void CopyTo(T[] array, int arrayIndex)
void Add(TKey key, TValue value)
new IEnumerator< T > GetEnumerator()
Enumerator(SortedDictionary< TKey, TValue > dictionary, int getEnumeratorRetType)
SortedSet< KeyValuePair< TKey, TValue > >.Enumerator _treeEnum