Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- collection1 = {1, 2, 3, 4};
- collection2 = {2, 4, 1, 3};
- collection1 == collection2; // true
- if (collection1.Count != collection2.Count)
- return false; // the collections are not equal
- foreach (Item item in collection1)
- {
- if (!collection2.Contains(item))
- return false; // the collections are not equal
- }
- foreach (Item item in collection2)
- {
- if (!collection1.Contains(item))
- return false; // the collections are not equal
- }
- return true; // the collections are equal
- collection1 = {1, 2, 3, 3, 4}
- collection2 = {1, 2, 2, 3, 4}
- public class CollectionComparer<T> : IEqualityComparer<IEnumerable<T>>
- {
- public bool Equals(IEnumerable<T> first, IEnumerable<T> second)
- {
- if ((first == null) != (second == null))
- return false;
- if (!object.ReferenceEquals(first, second) && (first != null))
- {
- if (first.Count() != second.Count())
- return false;
- if ((first.Count() != 0) && HaveMismatchedElement(first, second))
- return false;
- }
- return true;
- }
- private static bool HaveMismatchedElement(IEnumerable<T> first,
- IEnumerable<T> second)
- {
- int firstCount;
- int secondCount;
- var firstElementCounts = GetElementCounts(first, out firstCount);
- var secondElementCounts = GetElementCounts(second, out secondCount);
- if (firstCount != secondCount)
- return true;
- foreach (var kvp in firstElementCounts)
- {
- firstCount = kvp.Value;
- secondElementCounts.TryGetValue(kvp.Key, out secondCount);
- if (firstCount != secondCount)
- return true;
- }
- return false;
- }
- private static Dictionary<T, int> GetElementCounts(IEnumerable<T> enumerable,
- out int nullCount)
- {
- var dictionary = new Dictionary<T, int>();
- nullCount = 0;
- foreach (T element in enumerable)
- {
- if (element == null)
- {
- nullCount++;
- }
- else
- {
- int num;
- dictionary.TryGetValue(element, out num);
- num++;
- dictionary[element] = num;
- }
- }
- return dictionary;
- }
- public int GetHashCode(IEnumerable<T> enumerable)
- {
- int hash = 17;
- foreach (T val in enumerable.OrderBy(x => x))
- hash = hash * 23 + val.GetHashCode();
- return hash;
- }
- }
- private bool SetEqual (List<int> left, List<int> right) {
- if (left.Count != right.Count)
- return false;
- Dictionary<int, int> dict = new Dictionary<int, int>();
- foreach (int member in left) {
- if (dict.ContainsKey(member) == false)
- dict[member] = 1;
- else
- dict[member]++;
- }
- foreach (int member in right) {
- if (dict.ContainsKey(member) == false)
- return false;
- else
- dict[member]--;
- }
- foreach (KeyValuePair<int, int> kvp in dict) {
- if (kvp.Value != 0)
- return false;
- }
- return true;
- }
- /// <summary>
- /// Represents a service used to compare two collections for equality.
- /// </summary>
- /// <typeparam name="T">The type of the items in the collections.</typeparam>
- public class CollectionComparer<T>
- {
- /// <summary>
- /// Compares the content of two collections for equality.
- /// </summary>
- /// <param name="foo">The first collection.</param>
- /// <param name="bar">The second collection.</param>
- /// <returns>True if both collections have the same content, false otherwise.</returns>
- public bool Execute(ICollection<T> foo, ICollection<T> bar)
- {
- // Declare a dictionary to count the occurence of the items in the collection
- Dictionary<T, int> itemCounts = new Dictionary<T,int>();
- // Increase the count for each occurence of the item in the first collection
- foreach (T item in foo)
- {
- if (itemCounts.ContainsKey(item))
- {
- itemCounts[item]++;
- }
- else
- {
- itemCounts[item] = 1;
- }
- }
- // Wrap the keys in a searchable list
- List<T> keys = new List<T>(itemCounts.Keys);
- // Decrease the count for each occurence of the item in the second collection
- foreach (T item in bar)
- {
- // Try to find a key for the item
- // The keys of a dictionary are compared by reference, so we have to
- // find the original key that is equivalent to the "item"
- // You may want to override ".Equals" to define what it means for
- // two "T" objects to be equal
- T key = keys.Find(
- delegate(T listKey)
- {
- return listKey.Equals(item);
- });
- // Check if a key was found
- if(key != null)
- {
- itemCounts[key]--;
- }
- else
- {
- // There was no occurence of this item in the first collection, thus the collections are not equal
- return false;
- }
- }
- // The count of each item should be 0 if the contents of the collections are equal
- foreach (int value in itemCounts.Values)
- {
- if (value != 0)
- {
- return false;
- }
- }
- // The collections are equal
- return true;
- }
- }
- bool equal = collection1.OrderBy(i => i).SequenceEqual(
- collection2.OrderBy(i => i);
- return c1.Count == c2.Count && c1.Intersect(c2).Count() == c1.Count;
- __Test Latency(ms)__
- N, SetEquals, OrderBy, Intersect
- 1024, 0, 0, 0
- 2048, 0, 0, 0
- 4096, 31.2468, 0, 0
- 8192, 62.4936, 0, 0
- 16384, 156.234, 15.6234, 0
- 32768, 312.468, 15.6234, 46.8702
- 65536, 640.5594, 46.8702, 31.2468
- 131072, 1312.3656, 93.7404, 203.1042
- 262144, 3765.2394, 187.4808, 187.4808
- 524288, 5718.1644, 374.9616, 406.2084
- 1048576, 11420.7054, 734.2998, 718.6764
- 2097152, 35090.1564, 1515.4698, 1484.223
- (new HashBag(collection1)).equals(new HashBag(collection2))
- // Create the IEnumerable data sources.
- string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt");
- string[] names2 = System.IO.File.ReadAllLines(@"../../../names2.txt");
- // Create the query. Note that method syntax must be used here.
- IEnumerable<string> differenceQuery = names1.Except(names2);
- // Execute the query.
- Console.WriteLine("The following lines are in names1.txt but not names2.txt");
- foreach (string s in differenceQuery)
- Console.WriteLine(s);
- [Serializable]
- public class SetComparer<T> : IEqualityComparer<IEnumerable<T>>
- where T:IComparable<T>
- {
- public bool Equals(IEnumerable<T> first, IEnumerable<T> second)
- {
- if (first == second)
- return true;
- if ((first == null) || (second == null))
- return false;
- return first.ToHashSet().SetEquals(second);
- }
- public int GetHashCode(IEnumerable<T> enumerable)
- {
- int hash = 17;
- foreach (T val in enumerable.OrderBy(x => x))
- hash = hash * 23 + val.GetHashCode();
- return hash;
- }
- }
- var list1 = new[] { "Bill", "Bob", "Sally" };
- var list2 = new[] { "Bob", "Bill", "Sally" };
- bool isequal = list1.Compare(list2).IsSame;
- var list1 = new[] { "Billy", "Bob" };
- var list2 = new[] { "Bob", "Sally" };
- var diff = list1.Compare(list2);
- var onlyinlist1 = diff.Removed; //Billy
- var onlyinlist2 = diff.Added; //Sally
- var inbothlists = diff.Equal; //Bob
- var original = new Dictionary<int, string>() { { 1, "a" }, { 2, "b" } };
- var changed = new Dictionary<int, string>() { { 1, "aaa" }, { 2, "b" } };
- var diff = original.Compare(changed, (x, y) => x.Value == y.Value, (x, y) => x.Value == y.Value);
- foreach (var item in diff.Different)
- Console.Write("{0} changed to {1}", item.Key.Value, item.Value.Value);
- //Will output: a changed to aaa
Add Comment
Please, Sign In to add comment