Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ GetValueRefOrAddDefault()

static ref TValue System.Collections.Generic.Dictionary< TKey, TValue >.CollectionsMarshalHelper.GetValueRefOrAddDefault ( Dictionary< TKey, TValue > dictionary,
TKey key,
out bool exists )
inlinestatic

Definition at line 17 of file Dictionary.cs.

18 {
19 if (key == null)
20 {
21 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
22 }
23 if (dictionary._buckets == null)
24 {
25 dictionary.Initialize(0);
26 }
27 Entry[] entries = dictionary._entries;
28 IEqualityComparer<TKey> comparer = dictionary._comparer;
29 uint num = (uint)(comparer?.GetHashCode(key) ?? key.GetHashCode());
30 uint num2 = 0u;
31 ref int bucket = ref dictionary.GetBucket(num);
32 int num3 = bucket - 1;
33 if (comparer == null)
34 {
35 if (typeof(TKey).IsValueType)
36 {
37 while ((uint)num3 < (uint)entries.Length)
38 {
39 if (entries[num3].hashCode == num && EqualityComparer<TKey>.Default.Equals(entries[num3].key, key))
40 {
41 exists = true;
42 return ref entries[num3].value;
43 }
44 num3 = entries[num3].next;
45 num2++;
46 if (num2 > (uint)entries.Length)
47 {
48 ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported();
49 }
50 }
51 }
52 else
53 {
54 EqualityComparer<TKey> @default = EqualityComparer<TKey>.Default;
55 while ((uint)num3 < (uint)entries.Length)
56 {
57 if (entries[num3].hashCode == num && @default.Equals(entries[num3].key, key))
58 {
59 exists = true;
60 return ref entries[num3].value;
61 }
62 num3 = entries[num3].next;
63 num2++;
64 if (num2 > (uint)entries.Length)
65 {
66 ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported();
67 }
68 }
69 }
70 }
71 else
72 {
73 while ((uint)num3 < (uint)entries.Length)
74 {
75 if (entries[num3].hashCode == num && comparer.Equals(entries[num3].key, key))
76 {
77 exists = true;
78 return ref entries[num3].value;
79 }
80 num3 = entries[num3].next;
81 num2++;
82 if (num2 > (uint)entries.Length)
83 {
84 ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported();
85 }
86 }
87 }
88 int num4;
89 if (dictionary._freeCount > 0)
90 {
91 num4 = dictionary._freeList;
92 dictionary._freeList = -3 - entries[dictionary._freeList].next;
93 dictionary._freeCount--;
94 }
95 else
96 {
97 int count = dictionary._count;
98 if (count == entries.Length)
99 {
100 dictionary.Resize();
101 bucket = ref dictionary.GetBucket(num);
102 }
103 num4 = count;
105 entries = dictionary._entries;
106 }
108 reference.hashCode = num;
109 reference.next = bucket - 1;
111 reference.value = default(TValue);
112 bucket = num4 + 1;
113 dictionary._version++;
114 if (!typeof(TKey).IsValueType && num2 > 100 && comparer is NonRandomizedStringEqualityComparer)
115 {
116 dictionary.Resize(entries.Length, forceNewHashCodes: true);
117 exists = false;
118 return ref dictionary.FindValue(key);
119 }
120 exists = false;
121 return ref reference.value;
122 }

References System.Collections.Generic.Dictionary< TKey, TValue >.Dictionary(), System.Collections.Generic.Dictionary< TKey, TValue >._comparer, System.Collections.Generic.Dictionary< TKey, TValue >._entries, System.Collections.Generic.Dictionary< TKey, TValue >._freeCount, System.Collections.Generic.Dictionary< TKey, TValue >._freeList, System.Collections.Generic.Dictionary< TKey, TValue >._version, System.comparer, System.count, System.Default, System.dictionary, System.Collections.Generic.Dictionary< TKey, TValue >.FindValue(), System.Collections.Generic.Dictionary< TKey, TValue >.GetBucket(), System.key, System.ThrowHelper.ThrowArgumentNullException(), and System.ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported().

Referenced by System.Runtime.InteropServices.CollectionsMarshal.GetValueRefOrAddDefault< TKey, TValue >().