Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions src/Comparation/CollectionEquality.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
#if NET6_0_OR_GREATER
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -44,32 +43,10 @@ public bool Equals(IReadOnlyCollection<T?>? x, IReadOnlyCollection<T?>? y)

public int GetHashCode(IReadOnlyCollection<T?> collection)
{
var count = collection.Count;
#if NETCOREAPP2_1_OR_GREATER
var hashCodes = count <= 1024 / sizeof(int)
? stackalloc int[count]
: new int[count];
#else
var hashCodes = new int[count];
#endif
var i = 0;
foreach (var item in collection)
{
hashCodes[i++] = itemEquality.GetHashCode(new Box<T?>(item));
}

#if NETCOREAPP2_1_OR_GREATER
hashCodes.Sort();
#else
Array.Sort(hashCodes);
#endif

var resultHashCode = 0;
foreach (var hashCode in hashCodes)
foreach (var item in collection)
{
resultHashCode = unchecked(
(resultHashCode * 397) ^ hashCode
);
resultHashCode ^= itemEquality.GetHashCode(new Box<T?>(item));
}

return resultHashCode;
Expand Down
15 changes: 9 additions & 6 deletions src/Comparation/SequenceEquality.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
#if !NETSTANDARD2_0
using System;
#endif
using System.Collections.Generic;
using System.Linq;

namespace Comparation
Expand Down Expand Up @@ -29,22 +32,22 @@ public bool Equals(IEnumerable<T?>? x, IEnumerable<T?>? y)
return false;
}

return x!.SequenceEqual(y!, itemEquality!);
return x!.SequenceEqual(y!, itemEquality);
}

public int GetHashCode(IEnumerable<T?> obj)
{
var hashCode = 0;
var hash = new HashCode();
foreach (var item in obj)
{
var itemHashCode = item is { } value
var itemHash = item is { } value
? itemEquality.GetHashCode(value)
: 0;

hashCode = unchecked((hashCode * 397) ^ itemHashCode);
hash.Add(itemHash);
}

return hashCode;
return hash.ToHashCode();
}
}
}