forked from CommunityToolkit/dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashCode.cs
More file actions
497 lines (423 loc) · 19.5 KB
/
Copy pathHashCode.cs
File metadata and controls
497 lines (423 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
#pragma warning disable CS0809
namespace System;
/// <summary>
/// A polyfill type that mirrors some methods from <see cref="HashCode"/> on .NET 6.
/// </summary>
internal struct HashCode
{
private const uint Prime1 = 2654435761U;
private const uint Prime2 = 2246822519U;
private const uint Prime3 = 3266489917U;
private const uint Prime4 = 668265263U;
private const uint Prime5 = 374761393U;
private static readonly uint seed = GenerateGlobalSeed();
private uint v1, v2, v3, v4;
private uint queue1, queue2, queue3;
private uint length;
/// <summary>
/// Initializes the default seed.
/// </summary>
/// <returns>A random seed.</returns>
private static unsafe uint GenerateGlobalSeed()
{
byte[] bytes = new byte[4];
RandomNumberGenerator.Create().GetBytes(bytes);
return BitConverter.ToUInt32(bytes, 0);
}
/// <summary>
/// Combines a value into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the value to combine into the hash code.</typeparam>
/// <param name="value">The value to combine into the hash code.</param>
/// <returns>The hash code that represents the value.</returns>
public static int Combine<T1>(T1 value)
{
uint hc1 = (uint)(value?.GetHashCode() ?? 0);
uint hash = MixEmptyState();
hash += 4;
hash = QueueRound(hash, hc1);
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Combines two values into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
/// <param name="value1">The first value to combine into the hash code.</param>
/// <param name="value2">The second value to combine into the hash code.</param>
/// <returns>The hash code that represents the values.</returns>
public static int Combine<T1, T2>(T1 value1, T2 value2)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hash = MixEmptyState();
hash += 8;
hash = QueueRound(hash, hc1);
hash = QueueRound(hash, hc2);
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Combines three values into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
/// <param name="value1">The first value to combine into the hash code.</param>
/// <param name="value2">The second value to combine into the hash code.</param>
/// <param name="value3">The third value to combine into the hash code.</param>
/// <returns>The hash code that represents the values.</returns>
public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hash = MixEmptyState();
hash += 12;
hash = QueueRound(hash, hc1);
hash = QueueRound(hash, hc2);
hash = QueueRound(hash, hc3);
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Combines four values into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
/// <param name="value1">The first value to combine into the hash code.</param>
/// <param name="value2">The second value to combine into the hash code.</param>
/// <param name="value3">The third value to combine into the hash code.</param>
/// <param name="value4">The fourth value to combine into the hash code.</param>
/// <returns>The hash code that represents the values.</returns>
public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 16;
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Combines five values into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
/// <param name="value1">The first value to combine into the hash code.</param>
/// <param name="value2">The second value to combine into the hash code.</param>
/// <param name="value3">The third value to combine into the hash code.</param>
/// <param name="value4">The fourth value to combine into the hash code.</param>
/// <param name="value5">The fifth value to combine into the hash code.</param>
/// <returns>The hash code that represents the values.</returns>
public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 20;
hash = QueueRound(hash, hc5);
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Combines six values into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
/// <typeparam name="T6">The type of the sixth value to combine into the hash code.</typeparam>
/// <param name="value1">The first value to combine into the hash code.</param>
/// <param name="value2">The second value to combine into the hash code.</param>
/// <param name="value3">The third value to combine into the hash code.</param>
/// <param name="value4">The fourth value to combine into the hash code.</param>
/// <param name="value5">The fifth value to combine into the hash code.</param>
/// <param name="value6">The sixth value to combine into the hash code.</param>
/// <returns>The hash code that represents the values.</returns>
public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 24;
hash = QueueRound(hash, hc5);
hash = QueueRound(hash, hc6);
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Combines seven values into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
/// <typeparam name="T6">The type of the sixth value to combine into the hash code.</typeparam>
/// <typeparam name="T7">The type of the seventh value to combine into the hash code.</typeparam>
/// <param name="value1">The first value to combine into the hash code.</param>
/// <param name="value2">The second value to combine into the hash code.</param>
/// <param name="value3">The third value to combine into the hash code.</param>
/// <param name="value4">The fourth value to combine into the hash code.</param>
/// <param name="value5">The fifth value to combine into the hash code.</param>
/// <param name="value6">The sixth value to combine into the hash code.</param>
/// <param name="value7">The seventh value to combine into the hash code.</param>
/// <returns>The hash code that represents the values.</returns>
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)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 28;
hash = QueueRound(hash, hc5);
hash = QueueRound(hash, hc6);
hash = QueueRound(hash, hc7);
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Combines eight values into a hash code.
/// </summary>
/// <typeparam name="T1">The type of the first value to combine into the hash code.</typeparam>
/// <typeparam name="T2">The type of the second value to combine into the hash code.</typeparam>
/// <typeparam name="T3">The type of the third value to combine into the hash code.</typeparam>
/// <typeparam name="T4">The type of the fourth value to combine into the hash code.</typeparam>
/// <typeparam name="T5">The type of the fifth value to combine into the hash code.</typeparam>
/// <typeparam name="T6">The type of the sixth value to combine into the hash code.</typeparam>
/// <typeparam name="T7">The type of the seventh value to combine into the hash code.</typeparam>
/// <typeparam name="T8">The type of the eighth value to combine into the hash code.</typeparam>
/// <param name="value1">The first value to combine into the hash code.</param>
/// <param name="value2">The second value to combine into the hash code.</param>
/// <param name="value3">The third value to combine into the hash code.</param>
/// <param name="value4">The fourth value to combine into the hash code.</param>
/// <param name="value5">The fifth value to combine into the hash code.</param>
/// <param name="value6">The sixth value to combine into the hash code.</param>
/// <param name="value7">The seventh value to combine into the hash code.</param>
/// <param name="value8">The eighth value to combine into the hash code.</param>
/// <returns>The hash code that represents the values.</returns>
public 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)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
uint hc8 = (uint)(value8?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
v1 = Round(v1, hc5);
v2 = Round(v2, hc6);
v3 = Round(v3, hc7);
v4 = Round(v4, hc8);
uint hash = MixState(v1, v2, v3, v4);
hash += 32;
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Adds a single value to the current hash.
/// </summary>
/// <typeparam name="T">The type of the value to add into the hash code.</typeparam>
/// <param name="value">The value to add into the hash code.</param>
public void Add<T>(T value)
{
Add(value?.GetHashCode() ?? 0);
}
/// <summary>
/// Adds a single value to the current hash.
/// </summary>
/// <typeparam name="T">The type of the value to add into the hash code.</typeparam>
/// <param name="value">The value to add into the hash code.</param>
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance to use.</param>
public void Add<T>(T value, IEqualityComparer<T>? comparer)
{
Add(value is null ? 0 : (comparer?.GetHashCode(value) ?? value.GetHashCode()));
}
/// <summary>
/// Adds a span of bytes to the hash code.
/// </summary>
/// <param name="value">The span.</param>
public void AddBytes(ReadOnlySpan<byte> value)
{
ref byte pos = ref MemoryMarshal.GetReference(value);
ref byte end = ref Unsafe.Add(ref pos, value.Length);
while ((nint)Unsafe.ByteOffset(ref pos, ref end) >= sizeof(int))
{
Add(Unsafe.ReadUnaligned<int>(ref pos));
pos = ref Unsafe.Add(ref pos, sizeof(int));
}
while (Unsafe.IsAddressLessThan(ref pos, ref end))
{
Add((int)pos);
pos = ref Unsafe.Add(ref pos, 1);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4)
{
v1 = seed + Prime1 + Prime2;
v2 = seed + Prime2;
v3 = seed;
v4 = seed - Prime1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Round(uint hash, uint input)
{
return RotateLeft(hash + input * Prime2, 13) * Prime1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint QueueRound(uint hash, uint queuedValue)
{
return RotateLeft(hash + queuedValue * Prime3, 17) * Prime4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MixState(uint v1, uint v2, uint v3, uint v4)
{
return RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MixEmptyState()
{
return seed + Prime5;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MixFinal(uint hash)
{
hash ^= hash >> 15;
hash *= Prime2;
hash ^= hash >> 13;
hash *= Prime3;
hash ^= hash >> 16;
return hash;
}
private void Add(int value)
{
uint val = (uint)value;
uint previousLength = length++;
uint position = previousLength % 4;
if (position == 0)
{
queue1 = val;
}
else if (position == 1)
{
queue2 = val;
}
else if (position == 2)
{
queue3 = val;
}
else
{
if (previousLength == 3)
{
Initialize(out v1, out v2, out v3, out v4);
}
v1 = Round(v1, queue1);
v2 = Round(v2, queue2);
v3 = Round(v3, queue3);
v4 = Round(v4, val);
}
}
/// <summary>
/// Gets the resulting hashcode from the current instance.
/// </summary>
/// <returns>The resulting hashcode from the current instance.</returns>
public int ToHashCode()
{
uint length = this.length;
uint position = length % 4;
uint hash = length < 4 ? MixEmptyState() : MixState(v1, v2, v3, v4);
hash += length * 4;
if (position > 0)
{
hash = QueueRound(hash, queue1);
if (position > 1)
{
hash = QueueRound(hash, queue2);
if (position > 2)
{
hash = QueueRound(hash, queue3);
}
}
}
hash = MixFinal(hash);
return (int)hash;
}
/// <inheritdoc/>
[Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes. Use ToHashCode to retrieve the computed hash code.", error: true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => throw new NotSupportedException();
/// <inheritdoc/>
[Obsolete("HashCode is a mutable struct and should not be compared with other HashCodes.", error: true)]
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object? obj) => throw new NotSupportedException();
/// <summary>
/// Rotates the specified value left by the specified number of bits.
/// Similar in behavior to the x86 instruction ROL.
/// </summary>
/// <param name="value">The value to rotate.</param>
/// <param name="offset">The number of bits to rotate by.
/// Any value outside the range [0..31] is treated as congruent mod 32.</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint RotateLeft(uint value, int offset)
{
return (value << offset) | (value >> (32 - offset));
}
}