Skip to content

Commit

Permalink
Tweaks to array/list comparers (#1932)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jul 29, 2021
1 parent dc03707 commit 482c1ac
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,50 @@ private static bool Compare(TElem[]? a, TElem[]? b, ValueComparer<TElem> element
return false;
}

if (ReferenceEquals(a, b))
{
return true;
}

// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
for (var i = 0; i < a.Length; i++)
{
if (!elementComparer.Equals(a[i], b[i]))
{
return false;
}
}

return true;
}

private static int GetHashCode(TElem[] source, ValueComparer<TElem> elementComparer)
{
var hash = new HashCode();

foreach (var el in source)
{
hash.Add(el, elementComparer);
}

return hash.ToHashCode();
}

[return: NotNullIfNotNull("source")]
private static TElem[]? Snapshot(TElem[]? source, ValueComparer<TElem> elementComparer)
{
if (source == null)
if (source is null)
return null;

var snapshot = new TElem[source.Length];
// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
for (var i = 0; i < source.Length; i++)
{
snapshot[i] = elementComparer.Snapshot(source[i])!; // TODO: https://github.com/dotnet/efcore/pull/24410
}

return snapshot;
}
}
Expand All @@ -191,6 +207,11 @@ private static bool Compare(TElem?[]? a, TElem?[]? b, ValueComparer<TElem> eleme
return false;
}

if (ReferenceEquals(a, b))
{
return true;
}

// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
for (var i = 0; i < a.Length; i++)
Expand All @@ -199,11 +220,17 @@ private static bool Compare(TElem?[]? a, TElem?[]? b, ValueComparer<TElem> eleme
if (el1 is null)
{
if (el2 is null)
{
continue;
}

return false;
}

if (el2 is null || !elementComparer.Equals(el1, el2))
{
return false;
}
}

return true;
Expand All @@ -213,22 +240,31 @@ private static int GetHashCode(TElem?[] source, ValueComparer<TElem> elementComp
{
var nullableEqualityComparer = new NullableEqualityComparer<TElem>(elementComparer);
var hash = new HashCode();

foreach (var el in source)
{
hash.Add(el, nullableEqualityComparer);
}

return hash.ToHashCode();
}

[return: NotNullIfNotNull("source")]
private static TElem?[]? Snapshot(TElem?[]? source, ValueComparer<TElem> elementComparer)
{
if (source == null)
if (source is null)
{
return null;
}

var snapshot = new TElem?[source.Length];
// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
for (var i = 0; i < source.Length; i++)
snapshot[i] = source[i] is { } value ? elementComparer.Snapshot(value) : (TElem?)null;
{
snapshot[i] = source[i] is { } value ? elementComparer.Snapshot(value) : null;
}

return snapshot;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,51 @@ private static bool Compare(List<TElem>? a, List<TElem>? b, ValueComparer<TElem>
return false;
}

if (ReferenceEquals(a, b))
{
return true;
}

// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
for (var i = 0; i < a.Count; i++)
{
if (!elementComparer.Equals(a[i], b[i]))
{
return false;
}
}

return true;
}

private static int GetHashCode(List<TElem> source, ValueComparer<TElem> elementComparer)
{
var hash = new HashCode();

foreach (var el in source)
{
hash.Add(el, elementComparer);
}

return hash.ToHashCode();
}

private static List<TElem>? Snapshot(List<TElem>? source, ValueComparer<TElem> elementComparer)
{
if (source == null)
if (source is null)
{
return null;
}

var snapshot = new List<TElem>(source.Count);

// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
foreach (var e in source)
{
snapshot.Add(elementComparer.Snapshot(e)!); // TODO: https://github.com/dotnet/efcore/pull/24410
}

return snapshot;
}
Expand Down Expand Up @@ -191,6 +208,11 @@ private static bool Compare(List<TElem?>? a, List<TElem?>? b, ValueComparer<TEle
return false;
}

if (ReferenceEquals(a, b))
{
return true;
}

// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
for (var i = 0; i < a.Count; i++)
Expand All @@ -199,11 +221,17 @@ private static bool Compare(List<TElem?>? a, List<TElem?>? b, ValueComparer<TEle
if (el1 is null)
{
if (el2 is null)
{
continue;
}

return false;
}

if (el2 is null || !elementComparer.Equals(a[i], b[i]))
{
return false;
}
}

return true;
Expand All @@ -213,22 +241,30 @@ private static int GetHashCode(List<TElem?> source, ValueComparer<TElem> element
{
var nullableEqualityComparer = new NullableEqualityComparer<TElem>(elementComparer);
var hash = new HashCode();

foreach (var el in source)
{
hash.Add(el, nullableEqualityComparer);
}

return hash.ToHashCode();
}

private static List<TElem?>? Snapshot(List<TElem?>? source, ValueComparer<TElem> elementComparer)
{
if (source == null)
{
return null;
}

var snapshot = new List<TElem?>(source.Count);

// Note: the following currently boxes every element access because ValueComparer isn't really
// generic (see https://github.com/aspnet/EntityFrameworkCore/issues/11072)
foreach (var e in source)
snapshot.Add(e is { } value ? elementComparer.Snapshot(value) : (TElem?)null);
{
snapshot.Add(e is { } value ? elementComparer.Snapshot(value) : null);
}

return snapshot;
}
Expand Down

0 comments on commit 482c1ac

Please sign in to comment.