Skip to content

Commit

Permalink
(chocolateyGH-1612) Added extension method and struct
Browse files Browse the repository at this point in the history
- To allow for correctly generating hash code of objects
  • Loading branch information
gep13 committed Oct 9, 2018
1 parent 517b3ad commit 1dd642c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/chocolatey/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,28 @@ public static IEnumerable<T> distinct_last<T>(this IEnumerable<T> source, IEqual
yield return maxElement;
}
}

/// <summary>
/// Generates a unique hash code for a source of objects
/// </summary>
/// <typeparam name="T">Generic type.</typeparam>
/// <param name="source">The source.</param>
/// <returns>
/// Integer value representing hash code for input collection.
/// </returns>
/// <remarks>
/// Taken from here: https://stackoverflow.com/a/30758270/671491
/// </remarks>
public static int get_sequence_hash_code<T>(this IEnumerable<T> source)
{
const int seed = 487;
const int modifier = 31;

unchecked
{
return source.Aggregate(seed, (current, item) =>
(current*modifier) + item.GetHashCode());
}
}
}
}
3 changes: 2 additions & 1 deletion src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<Compile Include="infrastructure.app\events\HandlePackageResultCompletedMessage.cs" />
<Compile Include="infrastructure.app\templates\ChocolateyTodoTemplate.cs" />
<Compile Include="infrastructure.app\utility\ArgumentsUtility.cs" />
<Compile Include="infrastructure.app\utility\HashCode.cs" />
<Compile Include="infrastructure.app\utility\PackageUtility.cs" />
<Compile Include="infrastructure\logging\AggregateLog.cs" />
<Compile Include="infrastructure\logging\LogLevelType.cs" />
Expand Down Expand Up @@ -368,4 +369,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
75 changes: 75 additions & 0 deletions src/chocolatey/infrastructure.app/utility/HashCode.cs
Original file line number Diff line number Diff line change
@@ -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>(T item)
{
return new HashCode(GetHashCode(item));
}

public HashCode And<T>(T item)
{
return new HashCode(CombineHashCodes(this.value, GetHashCode(item)));
}

public HashCode AndEach<T>(IEnumerable<T> 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>(T item)
{
return item == null ? 0 : item.GetHashCode();
}
}

0 comments on commit 1dd642c

Please sign in to comment.