| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PredicateEqualityComparer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Primitives.Comparers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// An equality comparer that uses a predicate function to determine equality. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="T">Type of objects to compare.</typeparam> |
| | | 18 | | public class PredicateEqualityComparer<T>(Func<T, T, bool> predicate) : IEqualityComparer, IEqualityComparer<T> |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Returns a hash code for the specified object. |
| | | 22 | | /// This implementation uses <see cref="RuntimeHelpers.GetHashCode(object)"/> which is based on object identity. |
| | | 23 | | /// </summary> |
| | 3 | 24 | | public int GetHashCode(object obj) => RuntimeHelpers.GetHashCode(obj); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Determines whether the specified objects are equal using the provided predicate function. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="x">The first object to compare.</param> |
| | | 30 | | /// <param name="y">The second object to compare.</param> |
| | | 31 | | /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns> |
| | 3 | 32 | | bool IEqualityComparer.Equals(object? x, object? y) => x is not null && y is not null && !ReferenceEquals(x, y) && p |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Determines whether the specified objects of type <typeparamref name="T"/> are equal using the provided predicate |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="x">The first object to compare.</param> |
| | | 38 | | /// <param name="y">The second object to compare.</param> |
| | | 39 | | /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns> |
| | 15 | 40 | | bool IEqualityComparer<T>.Equals(T? x, T? y) => x is not null && y is not null && !ReferenceEquals(x, y) && predicat |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Returns a hash code for the specified object of type <typeparamref name="T"/>. |
| | | 44 | | /// This implementation uses <see cref="RuntimeHelpers.GetHashCode(object)"/> which is based on object identity. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="obj">The object for which to get the hash code.</param> |
| | | 47 | | /// <returns>A hash code for the specified object.</returns> |
| | 0 | 48 | | int IEqualityComparer<T>.GetHashCode(T? obj) => RuntimeHelpers.GetHashCode(obj); |
| | | 49 | | } |
| | | 50 | | |