Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XHashtable.cs
Go to the documentation of this file.
3
4namespace System.Xml.Linq;
5
6internal sealed class XHashtable<TValue>
7{
8 public delegate string ExtractKeyDelegate(TValue value);
9
10 private sealed class XHashtableState
11 {
12 private struct Entry
13 {
14 public TValue Value;
15
16 public int HashCode;
17
18 public int Next;
19 }
20
21 private readonly int[] _buckets;
22
23 private readonly Entry[] _entries;
24
25 private int _numEntries;
26
28
35
37 {
38 if (_numEntries < _buckets.Length)
39 {
40 return this;
41 }
42 int num = 0;
43 for (int i = 0; i < _buckets.Length; i++)
44 {
45 int num2 = _buckets[i];
46 if (num2 == 0)
47 {
49 }
50 while (num2 > 0)
51 {
52 if (_extractKey(_entries[num2].Value) != null)
53 {
54 num++;
55 }
56 num2 = ((_entries[num2].Next != 0) ? _entries[num2].Next : Interlocked.CompareExchange(ref _entries[num2].Next, -1, 0));
57 }
58 }
59 if (num < _buckets.Length / 2)
60 {
61 num = _buckets.Length;
62 }
63 else
64 {
65 num = _buckets.Length * 2;
66 if (num < 0)
67 {
68 throw new OverflowException();
69 }
70 }
72 for (int j = 0; j < _buckets.Length; j++)
73 {
74 for (int num3 = _buckets[j]; num3 > 0; num3 = _entries[num3].Next)
75 {
77 }
78 }
79 return xHashtableState;
80 }
81
82 public bool TryGetValue(string key, int index, int count, [MaybeNullWhen(false)] out TValue value)
83 {
84 int hashCode = ComputeHashCode(key, index, count);
85 int entryIndex = 0;
86 if (FindEntry(hashCode, key, index, count, ref entryIndex))
87 {
89 return true;
90 }
91 value = default(TValue);
92 return false;
93 }
94
95 public bool TryAdd(TValue value, out TValue newValue)
96 {
98 string text = _extractKey(value);
99 if (text == null)
100 {
101 return true;
102 }
103 int num = ComputeHashCode(text, 0, text.Length);
105 if (num2 < 0 || num2 >= _buckets.Length)
106 {
107 return false;
108 }
110 _entries[num2].HashCode = num;
112 int entryIndex = 0;
113 while (!FindEntry(num, text, 0, text.Length, ref entryIndex))
114 {
116 if (entryIndex <= 0)
117 {
118 return entryIndex == 0;
119 }
120 }
122 return true;
123 }
124
125 private bool FindEntry(int hashCode, string key, int index, int count, ref int entryIndex)
126 {
127 int num = entryIndex;
128 int num2 = ((num != 0) ? num : _buckets[hashCode & (_buckets.Length - 1)]);
129 while (num2 > 0)
130 {
131 if (_entries[num2].HashCode == hashCode)
132 {
134 if (text == null)
135 {
136 if (_entries[num2].Next > 0)
137 {
138 _entries[num2].Value = default(TValue);
140 if (num == 0)
141 {
142 _buckets[hashCode & (_buckets.Length - 1)] = num2;
143 }
144 else
145 {
146 _entries[num].Next = num2;
147 }
148 continue;
149 }
150 }
151 else if (count == text.Length && string.CompareOrdinal(key, index, text, 0, count) == 0)
152 {
154 return true;
155 }
156 }
157 num = num2;
159 }
160 entryIndex = num;
161 return false;
162 }
163
164 private static int ComputeHashCode(string key, int index, int count)
165 {
166 return string.GetHashCode(key.AsSpan(index, count));
167 }
168 }
169
171
176
177 public bool TryGetValue(string key, int index, int count, [MaybeNullWhen(false)] out TValue value)
178 {
180 }
181
182 public TValue Add(TValue value)
183 {
184 TValue newValue;
185 while (!_state.TryAdd(value, out newValue))
186 {
187 lock (this)
188 {
191 _state = state;
192 }
193 }
194 return newValue;
195 }
196}
static int CompareExchange(ref int location1, int value, int comparand)
static int Increment(ref int location)
static void MemoryBarrier()
Definition Thread.cs:827
readonly ExtractKeyDelegate _extractKey
Definition XHashtable.cs:27
static int ComputeHashCode(string key, int index, int count)
bool TryGetValue(string key, int index, int count, [MaybeNullWhen(false)] out TValue value)
Definition XHashtable.cs:82
bool TryAdd(TValue value, out TValue newValue)
Definition XHashtable.cs:95
XHashtableState(ExtractKeyDelegate extractKey, int capacity)
Definition XHashtable.cs:29
bool FindEntry(int hashCode, string key, int index, int count, ref int entryIndex)
bool TryGetValue(string key, int index, int count, [MaybeNullWhen(false)] out TValue value)
delegate string ExtractKeyDelegate(TValue value)
TValue Add(TValue value)
XHashtable(ExtractKeyDelegate extractKey, int capacity)