| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace Trailblazer.Pathing; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Deterministic allocation-free hash combiner for path request cache keys. |
| | | 7 | | /// </summary> |
| | | 8 | | internal struct PathRequestHashBuilder |
| | | 9 | | { |
| | | 10 | | private const int Seed = 5381; |
| | | 11 | | private const int Shift1 = 16; |
| | | 12 | | private const int Shift2 = 5; |
| | | 13 | | private const int Shift3 = 27; |
| | | 14 | | private const int Factor3 = 1566083941; |
| | | 15 | | |
| | | 16 | | private int _hash1; |
| | | 17 | | private int _hash2; |
| | | 18 | | private int _count; |
| | | 19 | | |
| | | 20 | | private PathRequestHashBuilder(int hash1, int hash2) |
| | | 21 | | { |
| | 8191 | 22 | | _hash1 = hash1; |
| | 8191 | 23 | | _hash2 = hash2; |
| | 8191 | 24 | | _count = 0; |
| | 8191 | 25 | | } |
| | | 26 | | |
| | | 27 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 28 | | public static PathRequestHashBuilder Create() |
| | | 29 | | { |
| | 8191 | 30 | | int hash = (Seed << Shift1) + Seed; |
| | 8191 | 31 | | return new PathRequestHashBuilder(hash, hash); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 35 | | public void Add(int value) |
| | | 36 | | { |
| | 68381 | 37 | | if ((_count & 1) == 0) |
| | 35635 | 38 | | _hash1 = Mix(_hash1, value); |
| | | 39 | | else |
| | 32746 | 40 | | _hash2 = Mix(_hash2, value); |
| | | 41 | | |
| | 68381 | 42 | | _count++; |
| | 68381 | 43 | | } |
| | | 44 | | |
| | | 45 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 46 | | public void Add(bool value) |
| | | 47 | | { |
| | 13847 | 48 | | Add(value ? 1 : 0); |
| | 13847 | 49 | | } |
| | | 50 | | |
| | | 51 | | public void AddOrdinalString(string? value) |
| | | 52 | | { |
| | 4 | 53 | | if (string.IsNullOrEmpty(value)) |
| | | 54 | | { |
| | 0 | 55 | | Add(0); |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | unchecked |
| | | 60 | | { |
| | 4 | 61 | | int hash = 17; |
| | 88 | 62 | | for (int i = 0; i < value.Length; i++) |
| | 40 | 63 | | hash = (hash * 31) + value[i]; |
| | | 64 | | |
| | 4 | 65 | | Add(hash); |
| | | 66 | | } |
| | 4 | 67 | | } |
| | | 68 | | |
| | | 69 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 70 | | public int ToHashCode() |
| | | 71 | | { |
| | | 72 | | unchecked |
| | | 73 | | { |
| | 8191 | 74 | | return _hash1 + (_hash2 * Factor3); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 79 | | private static int Mix(int hash, int itemHash) |
| | | 80 | | { |
| | | 81 | | unchecked |
| | | 82 | | { |
| | 68381 | 83 | | return ((hash << Shift2) + hash + (hash >> Shift3)) ^ itemHash; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |