|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace GitCore.Common; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Extension methods for <see cref="IEnumerable{T}"/>. |
| 10 | +/// </summary> |
| 11 | +public static class EnumerableExtensions |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Creates an <see cref="IEqualityComparer{T}"/> for sequences of <see cref="IComparable"/> elements. |
| 15 | + /// </summary> |
| 16 | + /// <typeparam name="T">The type of the enumerable, which must implement <see cref="IEnumerable{IComparable}"/>.</typeparam> |
| 17 | + /// <returns>An <see cref="IEqualityComparer{T}"/> that compares sequences element by element.</returns> |
| 18 | + public static IEqualityComparer<T> EqualityComparer<T>() where T : IEnumerable<IComparable> => |
| 19 | + new IEnumerableEqualityComparer<T>(); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Creates an <see cref="IComparer{T}"/> for sequences of <see cref="IComparable"/> elements. |
| 23 | + /// </summary> |
| 24 | + /// <typeparam name="T">The type of the enumerable, which must implement <see cref="IEnumerable{IComparable}"/>.</typeparam> |
| 25 | + /// <returns>An <see cref="IComparer{T}"/> that compares sequences element by element.</returns> |
| 26 | + public static IComparer<T> Comparer<T>() where T : IEnumerable<IComparable> => new IEnumerableComparer<T>(); |
| 27 | + |
| 28 | + private class IEnumerableEqualityComparer<T> : IEqualityComparer<T> where T : IEnumerable<IComparable> |
| 29 | + { |
| 30 | + public bool Equals(T? x, T? y) |
| 31 | + { |
| 32 | + if (ReferenceEquals(x, y)) |
| 33 | + return true; |
| 34 | + |
| 35 | + if (x is null || y is null) |
| 36 | + { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + return x.SequenceEqual(y); |
| 41 | + } |
| 42 | + |
| 43 | + public int GetHashCode(T obj) => 0; |
| 44 | + } |
| 45 | + |
| 46 | + private class IEnumerableComparer<T> : IComparer<T> where T : IEnumerable<IComparable> |
| 47 | + { |
| 48 | + public static int Compare([AllowNull] IEnumerable<IComparable> x, [AllowNull] IEnumerable<IComparable> y) |
| 49 | + { |
| 50 | + if (ReferenceEquals(x, y)) |
| 51 | + return 0; |
| 52 | + |
| 53 | + if (x is null) |
| 54 | + return -1; |
| 55 | + |
| 56 | + if (y is null) |
| 57 | + return 1; |
| 58 | + |
| 59 | + if (x.Equals(y)) |
| 60 | + return 0; |
| 61 | + |
| 62 | + var xEnumerator = x.GetEnumerator(); |
| 63 | + var yEnumerator = y.GetEnumerator(); |
| 64 | + |
| 65 | + while (true) |
| 66 | + { |
| 67 | + var xHasCurrent = xEnumerator.MoveNext(); |
| 68 | + var yHasCurrent = yEnumerator.MoveNext(); |
| 69 | + |
| 70 | + if (!xHasCurrent && !yHasCurrent) |
| 71 | + return 0; |
| 72 | + |
| 73 | + if (!xHasCurrent) |
| 74 | + return -1; |
| 75 | + |
| 76 | + if (!yHasCurrent) |
| 77 | + return 1; |
| 78 | + |
| 79 | + var currentComparison = xEnumerator.Current.CompareTo(yEnumerator.Current); |
| 80 | + |
| 81 | + if (currentComparison is not 0) |
| 82 | + return currentComparison; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public int Compare(T? x, T? y) |
| 87 | + { |
| 88 | + return IEnumerableComparer<T>.Compare(x, y); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments