| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | namespace NanoCLang.Entities { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a class that compares two arrays of heap elements by building a heap from them. |
| | | 7 | | /// </summary> |
| | | 8 | | public class HeapElementComparer : IEqualityComparer<IHeapElement[]> { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Returns a default equality comparer for heap elements. |
| | | 11 | | /// </summary> |
| | 45 | 12 | | public static HeapElementComparer Default { get; } = new HeapElementComparer(); |
| | 668 | 13 | | private static IEnumerable<T> CastWhere<T>(IEnumerable<IHeapElement> items) where T : class => items.Where(i => |
| | 91 | 14 | | private static Heap GetDirectBindings(IHeapElement[] x) => new Heap(CastWhere<LocationBinding>(x).Concat(CastWhe |
| | 116 | 15 | | private static IEnumerable<IHeapElement> GetOthers(IHeapElement[] x) => CastWhere<FunctionCallExpression>(x).Ord |
| | | 16 | | /// <summary> |
| | | 17 | | /// Compares the heap element lists by creating a heap from the locations and comparing that, after that compari |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="x">Left operand</param> |
| | | 20 | | /// <param name="y">Right operand</param> |
| | | 21 | | /// <returns>True if the heap elements refer to the same heap after construction with the global environment.</r |
| | 44 | 22 | | public bool Equals(IHeapElement[] x, IHeapElement[] y) { |
| | 44 | 23 | | var l = GetDirectBindings(x); |
| | 44 | 24 | | var r = GetDirectBindings(y); |
| | 44 | 25 | | if (!EqualityComparer<Heap>.Default.Equals(l, r)) return false; |
| | 44 | 26 | | return GetOthers(x).SequenceEqual(GetOthers(y)); |
| | 44 | 27 | | } |
| | | 28 | | /// <inheritdoc cref="IEqualityComparer{T}.GetHashCode(T)"/> |
| | 0 | 29 | | public int GetHashCode(IHeapElement[] obj) => throw new System.NotImplementedException(); |
| | | 30 | | } |
| | | 31 | | } |