Advertisement
peter90

EquivalentWith

Oct 30th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CollectionEquality
  5. {
  6.     public static class CollectionExtensions
  7.     {
  8.         public static bool EquivalentWith<T>(this IEnumerable<T> source, IEnumerable<T> other)
  9.         {
  10.             return EquivalentWith(source, other, EqualityComparer<T>.Default);
  11.         }
  12.  
  13.         public static bool EquivalentWith<T>(this IEnumerable<T> source, IEnumerable<T> other, IEqualityComparer<T> comparer)
  14.         {
  15.             if (source == null) throw new ArgumentNullException("source");
  16.             if (other == null) throw new ArgumentNullException("other");
  17.             if (comparer == null) throw new ArgumentNullException("comparer");
  18.  
  19.             if (ReferenceEquals(source, other))
  20.             {
  21.                 return true;
  22.             }
  23.  
  24.             if (CollectionsButNotSameCount(source, other))
  25.             {
  26.                 return false;
  27.             }
  28.  
  29.             int sourceNullCount;
  30.             var sourceDict = GetElementCounts(source, comparer, out sourceNullCount);
  31.             int otherNullCount;
  32.             var otherDict = GetElementCounts(other, comparer, out otherNullCount);
  33.  
  34.             if (otherNullCount != sourceNullCount || sourceDict.Count != otherDict.Count)
  35.             {
  36.                 return false;
  37.             }
  38.  
  39.             foreach (var s in sourceDict)
  40.             {
  41.                 int otherCount;
  42.                 if (!otherDict.TryGetValue(s.Key, out otherCount) || s.Value != otherCount)
  43.                 {
  44.                     return false;
  45.                 }
  46.             }
  47.  
  48.             return true;
  49.         }
  50.  
  51.         private static bool CollectionsButNotSameCount<T>(IEnumerable<T> source, IEnumerable<T> other)
  52.         {
  53.             var sourceColl = source as ICollection<T>;
  54.             var otherColl = other as ICollection<T>;
  55.  
  56.             return sourceColl != null && otherColl != null && sourceColl.Count != otherColl.Count;
  57.         }
  58.  
  59.         private static Dictionary<T, int> GetElementCounts<T>(IEnumerable<T> source, IEqualityComparer<T> comparer, out int nullCount)
  60.         {
  61.             var asCollection = source as ICollection<T>;
  62.  
  63.             var dictionary = new Dictionary<T, int>(asCollection == null ? 0 : asCollection.Count, comparer);
  64.             nullCount = 0;
  65.             foreach (var key in source)
  66.             {
  67.                 if (key == null)
  68.                 {
  69.                     ++nullCount;
  70.                 }
  71.                 else
  72.                 {
  73.                     int num;
  74.                     dictionary.TryGetValue(key, out num);
  75.                     ++num;
  76.                     dictionary[key] = num;
  77.                 }
  78.             }
  79.             return dictionary;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement