diff --git a/src/chocolatey/EnumerableExtensions.cs b/src/chocolatey/EnumerableExtensions.cs index 086dc8ee76..96dcfecfb3 100644 --- a/src/chocolatey/EnumerableExtensions.cs +++ b/src/chocolatey/EnumerableExtensions.cs @@ -97,5 +97,28 @@ public static IEnumerable distinct_last(this IEnumerable source, IEqual yield return maxElement; } } + + /// + /// Generates a unique hash code for a source of objects + /// + /// Generic type. + /// The source. + /// + /// Integer value representing hash code for input collection. + /// + /// + /// Taken from here: https://stackoverflow.com/a/30758270/671491 + /// + public static int get_sequence_hash_code(this IEnumerable source) + { + const int seed = 487; + const int modifier = 31; + + unchecked + { + return source.Aggregate(seed, (current, item) => + (current*modifier) + item.GetHashCode()); + } + } } } diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index 1e2dd0664a..8b4976c53e 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -122,6 +122,7 @@ + @@ -368,4 +369,4 @@ --> - + \ No newline at end of file diff --git a/src/chocolatey/infrastructure.app/utility/HashCode.cs b/src/chocolatey/infrastructure.app/utility/HashCode.cs new file mode 100644 index 0000000000..cac40983e2 --- /dev/null +++ b/src/chocolatey/infrastructure.app/utility/HashCode.cs @@ -0,0 +1,75 @@ +// The MIT License (MIT) +// +// Copyright (c) Muhammad Rehan Saeed +// +// Taken from this blog post: https://rehansaeed.com/gethashcode-made-easy/ +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of +// this software and associated documentation files (the "Software"), to deal in +// the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +using System.Collections.Generic; +using System.Linq; + +public struct HashCode +{ + private readonly int value; + + private HashCode(int value) + { + this.value = value; + } + + public static implicit operator int(HashCode hashCode) + { + return hashCode.value; + } + + public static HashCode Of(T item) + { + return new HashCode(GetHashCode(item)); + } + + public HashCode And(T item) + { + return new HashCode(CombineHashCodes(this.value, GetHashCode(item))); + } + + public HashCode AndEach(IEnumerable items) + { + if (items == null) + { + return new HashCode(this.value); + } + + var hashCode = items.Any() ? items.Select(GetHashCode).Aggregate(CombineHashCodes) : 0; + return new HashCode(CombineHashCodes(this.value, hashCode)); + } + + private static int CombineHashCodes(int h1, int h2) + { + unchecked + { + // Code copied from System.Tuple so it must be the best way to combine hash codes or at least a good one. + return ((h1 << 5) + h1) ^ h2; + } + } + + private static int GetHashCode(T item) + { + return item == null ? 0 : item.GetHashCode(); + } +} \ No newline at end of file