Last active
January 23, 2019 04:05
-
-
Save NickStrupat/1bef18b51fc6a4da7870d9f271da2413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class ArrayEqualityComparer<TElement> : IEqualityComparer<TElement[]> | |
{ | |
public static readonly ArrayEqualityComparer<TElement> Default = | |
new ArrayEqualityComparer<TElement>(EqualityComparer<TElement>.Default); | |
private readonly IEqualityComparer<TElement> elementEqualityComparer; | |
public ArrayEqualityComparer(IEqualityComparer<TElement> elementEqualityComparer) => | |
this.elementEqualityComparer = elementEqualityComparer ?? throw new ArgumentNullException(nameof(elementEqualityComparer)); | |
public Boolean Equals(TElement[] x, TElement[] y) | |
{ | |
if (ReferenceEquals(x, y)) // both null or both referencing the same array object | |
return true; | |
if (x is null | y is null) | |
return false; | |
if (x.Length != y.Length) | |
return false; | |
for (var i = 0; i != x.Length; i++) | |
if (!elementEqualityComparer.Equals(x[i], y[i])) | |
return false; | |
return true; | |
} | |
public Int32 GetHashCode(TElement[] types) | |
{ | |
var hashCode = 0x51ed270b; | |
if (types == null) | |
return hashCode; | |
for (var i = 0; i != types.Length; i++) | |
hashCode = hashCode * -1521134295 + elementEqualityComparer.GetHashCode(types[i]); | |
return hashCode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment