| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | // Copied from https://github.com/aspnet/Extensions/tree/master/src/Shared/src/HashCodeCombiner |
| | | 5 | | |
| | | 6 | | using System.Collections; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | namespace Azure.Core.Http.Multipart |
| | | 11 | | { |
| | | 12 | | internal struct HashCodeCombiner |
| | | 13 | | { |
| | | 14 | | private long _combinedHash64; |
| | | 15 | | |
| | | 16 | | public int CombinedHash |
| | | 17 | | { |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 19 | | get { return _combinedHash64.GetHashCode(); } |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 23 | | private HashCodeCombiner(long seed) |
| | | 24 | | { |
| | 0 | 25 | | _combinedHash64 = seed; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 29 | | public void Add(IEnumerable e) |
| | | 30 | | { |
| | 0 | 31 | | if (e == null) |
| | | 32 | | { |
| | 0 | 33 | | Add(0); |
| | | 34 | | } |
| | | 35 | | else |
| | | 36 | | { |
| | 0 | 37 | | var count = 0; |
| | 0 | 38 | | foreach (object o in e) |
| | | 39 | | { |
| | 0 | 40 | | Add(o); |
| | 0 | 41 | | count++; |
| | | 42 | | } |
| | 0 | 43 | | Add(count); |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 48 | | public static implicit operator int(HashCodeCombiner self) |
| | | 49 | | { |
| | 0 | 50 | | return self.CombinedHash; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 54 | | public void Add(int i) |
| | | 55 | | { |
| | 0 | 56 | | _combinedHash64 = ((_combinedHash64 << 5) + _combinedHash64) ^ i; |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 60 | | public void Add(string s) |
| | | 61 | | { |
| | 0 | 62 | | var hashCode = (s != null) ? s.GetHashCode() : 0; |
| | 0 | 63 | | Add(hashCode); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 67 | | public void Add(object o) |
| | | 68 | | { |
| | 0 | 69 | | var hashCode = (o != null) ? o.GetHashCode() : 0; |
| | 0 | 70 | | Add(hashCode); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 74 | | public void Add<TValue>(TValue value, IEqualityComparer<TValue> comparer) |
| | | 75 | | { |
| | 0 | 76 | | var hashCode = value != null ? comparer.GetHashCode(value) : 0; |
| | 0 | 77 | | Add(hashCode); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 81 | | public static HashCodeCombiner Start() |
| | | 82 | | { |
| | 0 | 83 | | return new HashCodeCombiner(0x1505L); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |