| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Internal |
| | | 7 | | { |
| | | 8 | | internal static class Hash |
| | | 9 | | { |
| | | 10 | | internal static int Combine(int newKey, int currentKey) |
| | 0 | 11 | | { |
| | 0 | 12 | | return unchecked((currentKey * (int)0xA5555529) + newKey); |
| | 0 | 13 | | } |
| | | 14 | | |
| | | 15 | | internal static int Combine(uint newKey, int currentKey) |
| | 0 | 16 | | { |
| | 0 | 17 | | return unchecked((currentKey * (int)0xA5555529) + (int)newKey); |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The offset bias value used in the FNV-1a algorithm |
| | | 22 | | /// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function |
| | | 23 | | /// </summary> |
| | | 24 | | internal const int FnvOffsetBias = unchecked((int)2166136261); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The generative factor used in the FNV-1a algorithm |
| | | 28 | | /// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function |
| | | 29 | | /// </summary> |
| | | 30 | | internal const int FnvPrime = 16777619; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Compute the FNV-1a hash of a sequence of bytes |
| | | 34 | | /// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="data">The sequence of bytes</param> |
| | | 37 | | /// <returns>The FNV-1a hash of <paramref name="data"/></returns> |
| | | 38 | | internal static int GetFNVHashCode(ReadOnlySpan<byte> data) |
| | 0 | 39 | | { |
| | 0 | 40 | | int hashCode = Hash.FnvOffsetBias; |
| | | 41 | | |
| | 0 | 42 | | for (int i = 0; i < data.Length; i++) |
| | 0 | 43 | | { |
| | 0 | 44 | | hashCode = unchecked((hashCode ^ data[i]) * Hash.FnvPrime); |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | return hashCode; |
| | 0 | 48 | | } |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |