Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HashCode.cs
Go to the documentation of this file.
7
8namespace System;
9
10public struct HashCode
11{
12 private static readonly uint s_seed = GenerateGlobalSeed();
13
14 private uint _v1;
15
16 private uint _v2;
17
18 private uint _v3;
19
20 private uint _v4;
21
22 private uint _queue1;
23
24 private uint _queue2;
25
26 private uint _queue3;
27
28 private uint _length;
29
30 private unsafe static uint GenerateGlobalSeed()
31 {
32 System.Runtime.CompilerServices.Unsafe.SkipInit(out uint result);
33 Interop.GetRandomBytes((byte*)(&result), 4);
34 return result;
35 }
36
37 public static int Combine<T1>(T1 value1)
38 {
39 uint queuedValue = (uint)(value1?.GetHashCode() ?? 0);
40 uint num = MixEmptyState();
41 num += 4;
42 num = QueueRound(num, queuedValue);
43 return (int)MixFinal(num);
44 }
45
46 public static int Combine<T1, T2>(T1 value1, T2 value2)
47 {
48 uint queuedValue = (uint)(value1?.GetHashCode() ?? 0);
49 uint queuedValue2 = (uint)(value2?.GetHashCode() ?? 0);
50 uint num = MixEmptyState();
51 num += 8;
52 num = QueueRound(num, queuedValue);
53 num = QueueRound(num, queuedValue2);
54 return (int)MixFinal(num);
55 }
56
57 public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
58 {
59 uint queuedValue = (uint)(value1?.GetHashCode() ?? 0);
60 uint queuedValue2 = (uint)(value2?.GetHashCode() ?? 0);
61 uint queuedValue3 = (uint)(value3?.GetHashCode() ?? 0);
62 uint num = MixEmptyState();
63 num += 12;
64 num = QueueRound(num, queuedValue);
65 num = QueueRound(num, queuedValue2);
66 num = QueueRound(num, queuedValue3);
67 return (int)MixFinal(num);
68 }
69
70 public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4)
71 {
72 uint input = (uint)(value1?.GetHashCode() ?? 0);
73 uint input2 = (uint)(value2?.GetHashCode() ?? 0);
74 uint input3 = (uint)(value3?.GetHashCode() ?? 0);
75 uint input4 = (uint)(value4?.GetHashCode() ?? 0);
77 v = Round(v, input);
78 v2 = Round(v2, input2);
79 v3 = Round(v3, input3);
80 v4 = Round(v4, input4);
81 uint num = MixState(v, v2, v3, v4);
82 num += 16;
83 return (int)MixFinal(num);
84 }
85
86 public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
87 {
88 uint input = (uint)(value1?.GetHashCode() ?? 0);
89 uint input2 = (uint)(value2?.GetHashCode() ?? 0);
90 uint input3 = (uint)(value3?.GetHashCode() ?? 0);
91 uint input4 = (uint)(value4?.GetHashCode() ?? 0);
92 uint queuedValue = (uint)(value5?.GetHashCode() ?? 0);
94 v = Round(v, input);
95 v2 = Round(v2, input2);
96 v3 = Round(v3, input3);
97 v4 = Round(v4, input4);
98 uint num = MixState(v, v2, v3, v4);
99 num += 20;
100 num = QueueRound(num, queuedValue);
101 return (int)MixFinal(num);
102 }
103
104 public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
105 {
106 uint input = (uint)(value1?.GetHashCode() ?? 0);
107 uint input2 = (uint)(value2?.GetHashCode() ?? 0);
108 uint input3 = (uint)(value3?.GetHashCode() ?? 0);
109 uint input4 = (uint)(value4?.GetHashCode() ?? 0);
110 uint queuedValue = (uint)(value5?.GetHashCode() ?? 0);
111 uint queuedValue2 = (uint)(value6?.GetHashCode() ?? 0);
113 v = Round(v, input);
114 v2 = Round(v2, input2);
115 v3 = Round(v3, input3);
116 v4 = Round(v4, input4);
117 uint num = MixState(v, v2, v3, v4);
118 num += 24;
119 num = QueueRound(num, queuedValue);
120 num = QueueRound(num, queuedValue2);
121 return (int)MixFinal(num);
122 }
123
124 public static int Combine<T1, T2, T3, T4, T5, T6, T7>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
125 {
126 uint input = (uint)(value1?.GetHashCode() ?? 0);
127 uint input2 = (uint)(value2?.GetHashCode() ?? 0);
128 uint input3 = (uint)(value3?.GetHashCode() ?? 0);
129 uint input4 = (uint)(value4?.GetHashCode() ?? 0);
130 uint queuedValue = (uint)(value5?.GetHashCode() ?? 0);
131 uint queuedValue2 = (uint)(value6?.GetHashCode() ?? 0);
132 uint queuedValue3 = (uint)(value7?.GetHashCode() ?? 0);
134 v = Round(v, input);
135 v2 = Round(v2, input2);
136 v3 = Round(v3, input3);
137 v4 = Round(v4, input4);
138 uint num = MixState(v, v2, v3, v4);
139 num += 28;
140 num = QueueRound(num, queuedValue);
141 num = QueueRound(num, queuedValue2);
142 num = QueueRound(num, queuedValue3);
143 return (int)MixFinal(num);
144 }
145
147 {
148 uint input = (uint)(value1?.GetHashCode() ?? 0);
149 uint input2 = (uint)(value2?.GetHashCode() ?? 0);
150 uint input3 = (uint)(value3?.GetHashCode() ?? 0);
151 uint input4 = (uint)(value4?.GetHashCode() ?? 0);
152 uint input5 = (uint)(value5?.GetHashCode() ?? 0);
153 uint input6 = (uint)(value6?.GetHashCode() ?? 0);
154 uint input7 = (uint)(value7?.GetHashCode() ?? 0);
155 uint input8 = (uint)(value8?.GetHashCode() ?? 0);
157 v = Round(v, input);
158 v2 = Round(v2, input2);
159 v3 = Round(v3, input3);
160 v4 = Round(v4, input4);
161 v = Round(v, input5);
162 v2 = Round(v2, input6);
163 v3 = Round(v3, input7);
164 v4 = Round(v4, input8);
165 uint num = MixState(v, v2, v3, v4);
166 num += 32;
167 return (int)MixFinal(num);
168 }
169
170 [MethodImpl(MethodImplOptions.AggressiveInlining)]
171 private static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4)
172 {
173 v1 = (uint)((int)s_seed + -1640531535 + -2048144777);
174 v2 = s_seed + 2246822519u;
175 v3 = s_seed;
176 v4 = s_seed - 2654435761u;
177 }
178
179 [MethodImpl(MethodImplOptions.AggressiveInlining)]
180 private static uint Round(uint hash, uint input)
181 {
182 return BitOperations.RotateLeft(hash + (uint)((int)input * -2048144777), 13) * 2654435761u;
183 }
184
185 [MethodImpl(MethodImplOptions.AggressiveInlining)]
186 private static uint QueueRound(uint hash, uint queuedValue)
187 {
188 return BitOperations.RotateLeft(hash + (uint)((int)queuedValue * -1028477379), 17) * 668265263;
189 }
190
191 [MethodImpl(MethodImplOptions.AggressiveInlining)]
192 private static uint MixState(uint v1, uint v2, uint v3, uint v4)
193 {
195 }
196
197 private static uint MixEmptyState()
198 {
199 return s_seed + 374761393;
200 }
201
202 [MethodImpl(MethodImplOptions.AggressiveInlining)]
203 private static uint MixFinal(uint hash)
204 {
205 hash ^= hash >> 15;
206 hash *= 2246822519u;
207 hash ^= hash >> 13;
208 hash *= 3266489917u;
209 hash ^= hash >> 16;
210 return hash;
211 }
212
213 public void Add<T>(T value)
214 {
215 Add(value?.GetHashCode() ?? 0);
216 }
217
219 {
220 Add((value != null) ? (comparer?.GetHashCode(value) ?? value.GetHashCode()) : 0);
221 }
222
224 {
225 ref byte reference = ref MemoryMarshal.GetReference(value);
226 ref byte reference2 = ref Unsafe.Add(ref reference, value.Length);
227 while ((nint)Unsafe.ByteOffset(ref reference, ref reference2) >= 4)
228 {
229 Add(Unsafe.ReadUnaligned<int>(ref reference));
230 reference = ref Unsafe.Add(ref reference, 4);
231 }
232 while (Unsafe.IsAddressLessThan(ref reference, ref reference2))
233 {
234 Add((int)reference);
235 reference = ref Unsafe.Add(ref reference, 1);
236 }
237 }
238
239 private void Add(int value)
240 {
241 uint num = _length++;
242 switch (num % 4)
243 {
244 case 0u:
245 _queue1 = (uint)value;
246 return;
247 case 1u:
248 _queue2 = (uint)value;
249 return;
250 case 2u:
251 _queue3 = (uint)value;
252 return;
253 }
254 if (num == 3)
255 {
257 }
258 _v1 = Round(_v1, _queue1);
259 _v2 = Round(_v2, _queue2);
260 _v3 = Round(_v3, _queue3);
261 _v4 = Round(_v4, (uint)value);
262 }
263
264 public int ToHashCode()
265 {
266 uint length = _length;
267 uint num = length % 4;
268 uint num2 = ((length < 4) ? MixEmptyState() : MixState(_v1, _v2, _v3, _v4));
269 num2 += length * 4;
270 if (num != 0)
271 {
273 if (num > 1)
274 {
276 if (num > 2)
277 {
279 }
280 }
281 }
282 return (int)MixFinal(num2);
283 }
284
285 [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes. Use ToHashCode to retrieve the computed hash code.", true)]
287 public override int GetHashCode()
288 {
290 }
291
292 [Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes.", true)]
294 public override bool Equals(object? obj)
295 {
297 }
298}
static unsafe void GetRandomBytes(byte *buffer, int length)
Definition Interop.cs:1871
static uint RotateLeft(uint value, int offset)
static string HashCode_HashCodeNotSupported
Definition SR.cs:2062
static string HashCode_EqualityNotSupported
Definition SR.cs:2064
Definition SR.cs:7
static int Combine< T1, T2, T3, T4, T5, T6 >(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
Definition HashCode.cs:104
static uint QueueRound(uint hash, uint queuedValue)
Definition HashCode.cs:186
static uint MixEmptyState()
Definition HashCode.cs:197
static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4)
Definition HashCode.cs:171
void AddBytes(ReadOnlySpan< byte > value)
Definition HashCode.cs:223
static unsafe uint GenerateGlobalSeed()
Definition HashCode.cs:30
static int Combine< T1, T2 >(T1 value1, T2 value2)
Definition HashCode.cs:46
static uint Round(uint hash, uint input)
Definition HashCode.cs:180
static int Combine< T1, T2, T3, T4, T5, T6, T7 >(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
Definition HashCode.cs:124
static readonly uint s_seed
Definition HashCode.cs:12
override bool Equals(object? obj)
Definition HashCode.cs:294
void Add< T >(T value)
Definition HashCode.cs:213
void Add(int value)
Definition HashCode.cs:239
static int Combine< T1, T2, T3, T4, T5 >(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
Definition HashCode.cs:86
override int GetHashCode()
Definition HashCode.cs:287
static uint MixState(uint v1, uint v2, uint v3, uint v4)
Definition HashCode.cs:192
static int Combine< T1, T2, T3, T4, T5, T6, T7, T8 >(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
Definition HashCode.cs:146
static int Combine< T1, T2, T3, T4 >(T1 value1, T2 value2, T3 value3, T4 value4)
Definition HashCode.cs:70
static int Combine< T1 >(T1 value1)
Definition HashCode.cs:37
static uint MixFinal(uint hash)
Definition HashCode.cs:203
static int Combine< T1, T2, T3 >(T1 value1, T2 value2, T3 value3)
Definition HashCode.cs:57