// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// This file is ported from ComputeSharp (Sergio0694/ComputeSharp),
// more info in ThirdPartyNotices.txt in the root of the project.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
///
/// Extension methods for .
///
internal static class IEqualityComparerExtensions
{
///
/// Gets an for an sequence.
///
/// The type of items to compare.
/// The compare for individual items.
public static IEqualityComparer> ForImmutableArray(this IEqualityComparer comparer)
{
return new Comparer(comparer);
}
///
/// An implementation for an value.
///
private sealed class Comparer : IEqualityComparer>
{
///
/// The comparer.
///
private readonly IEqualityComparer comparer;
///
/// Creates a new instance with the specified parameters.
///
/// The comparer.
public Comparer(IEqualityComparer comparer)
{
this.comparer = comparer;
}
///
public bool Equals(ImmutableArray x, ImmutableArray y)
{
return
x == y ||
x.SequenceEqual(y, this.comparer);
}
///
public int GetHashCode(ImmutableArray obj)
{
HashCode hashCode = default;
hashCode.AddRange(obj, this.comparer);
return hashCode.ToHashCode();
}
}
}